From 59a5e9ac33a4c7ab8ba1409677157880ebf33bfb Mon Sep 17 00:00:00 2001 From: jaindiv26 Date: Tue, 2 Jul 2019 13:01:52 +0530 Subject: [PATCH 01/27] WIP: Multiple Accounts --- app/src/main/AndroidManifest.xml | 2 + .../sync/activities/AccountListAdapter.kt | 42 +++++++++++++++++++ .../sync/activities/SelectAccountActivity.kt | 42 +++++++++++++++++++ .../sync/activities/SyncSettingsActivity.kt | 23 ++++++++-- .../sync/auth/AuthenticatorActivity.kt | 16 +++---- .../xwiki/android/sync/rest/BaseApiManager.kt | 4 ++ .../sync/utils/AccountClickListener.kt | 5 +++ .../main/res/layout/account_list_layout.xml | 15 +++++++ .../main/res/layout/act_select_account.xml | 41 ++++++++++++++++++ app/src/main/res/layout/list_item_group.xml | 18 ++++---- app/src/main/res/xml/prefs.xml | 2 +- 11 files changed, 186 insertions(+), 24 deletions(-) create mode 100644 app/src/main/java/org/xwiki/android/sync/activities/AccountListAdapter.kt create mode 100644 app/src/main/java/org/xwiki/android/sync/activities/SelectAccountActivity.kt create mode 100644 app/src/main/java/org/xwiki/android/sync/utils/AccountClickListener.kt create mode 100644 app/src/main/res/layout/account_list_layout.xml create mode 100644 app/src/main/res/layout/act_select_account.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 757bd568..40f4faaa 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -64,6 +64,8 @@ + + ?, + private val listener : AccountClickListener) + : RecyclerView.Adapter() { + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AccountListViewHolder { + return AccountListViewHolder(LayoutInflater.from(mContext).inflate(R.layout.account_list_layout, parent, false)) + } + + override fun getItemCount(): Int { + return availableAccounts?.size!! + } + + override fun onBindViewHolder(holder: AccountListViewHolder, position: Int) { + holder.tvAccountName.text = availableAccounts?.get(position)?.name + + holder.llAccountItem.setOnClickListener { + listener.onItemClicked(availableAccounts?.get(position)?.name) + } + } + +} + +class AccountListViewHolder (view: View) : RecyclerView.ViewHolder(view) { + + val tvAccountName = view.findViewById(R.id.tvAccountName) + val llAccountItem = view.findViewById(R.id.llAccountItem) +} \ No newline at end of file diff --git a/app/src/main/java/org/xwiki/android/sync/activities/SelectAccountActivity.kt b/app/src/main/java/org/xwiki/android/sync/activities/SelectAccountActivity.kt new file mode 100644 index 00000000..a04b5821 --- /dev/null +++ b/app/src/main/java/org/xwiki/android/sync/activities/SelectAccountActivity.kt @@ -0,0 +1,42 @@ +package org.xwiki.android.sync.activities + +import android.accounts.AccountManager +import android.content.Intent +import android.os.Bundle +import android.widget.Toast +import androidx.databinding.DataBindingUtil +import androidx.recyclerview.widget.LinearLayoutManager +import org.xwiki.android.sync.ACCOUNT_TYPE +import org.xwiki.android.sync.R +import org.xwiki.android.sync.activities.base.BaseActivity +import org.xwiki.android.sync.databinding.ActSelectAccountBinding +import org.xwiki.android.sync.utils.AccountClickListener + +class SelectAccountActivity : BaseActivity(), AccountClickListener { + + lateinit var binding : ActSelectAccountBinding + + + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + binding = DataBindingUtil.setContentView(this, R.layout.act_select_account) + + val mAccountManager = AccountManager.get(applicationContext) + val availableAccountsList = mAccountManager.getAccountsByType(ACCOUNT_TYPE) + + val adapter: AccountListAdapter = AccountListAdapter(this, availableAccountsList, this) + binding.rvAvailableAccounts.adapter = adapter + binding.rvAvailableAccounts.layoutManager = LinearLayoutManager(this) + + } + + override fun onItemClicked(accountName: String?) { + val syncActivityIntent = Intent(this, SyncSettingsActivity::class.java) + syncActivityIntent.putExtra(AccountManager.KEY_ACCOUNT_NAME, accountName) + startActivity( + syncActivityIntent + ) + finish() + } +} \ No newline at end of file diff --git a/app/src/main/java/org/xwiki/android/sync/activities/SyncSettingsActivity.kt b/app/src/main/java/org/xwiki/android/sync/activities/SyncSettingsActivity.kt index ed8da458..d22f5730 100644 --- a/app/src/main/java/org/xwiki/android/sync/activities/SyncSettingsActivity.kt +++ b/app/src/main/java/org/xwiki/android/sync/activities/SyncSettingsActivity.kt @@ -1,5 +1,6 @@ package org.xwiki.android.sync.activities +import android.accounts.Account import android.accounts.AccountManager import android.content.ComponentName import android.content.ContentResolver @@ -110,6 +111,8 @@ class SyncSettingsActivity : BaseActivity() { @Volatile private var allUsersAreLoading: Boolean = false + private lateinit var currentUserAccountName : String + /** * Init all views and other activity objects * @@ -119,8 +122,8 @@ class SyncSettingsActivity : BaseActivity() { */ override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - binding = DataBindingUtil.setContentView(this, R.layout.activity_sync_settings); - + binding = DataBindingUtil.setContentView(this, R.layout.activity_sync_settings) + currentUserAccountName = intent.getStringExtra(AccountManager.KEY_ACCOUNT_NAME) binding.versionCheck.text = String.format( getString(R.string.versionTemplate), getAppVersionName(this) @@ -296,7 +299,13 @@ class SyncSettingsActivity : BaseActivity() { //TODO:: fix when will separate to different accounts val mAccountManager = AccountManager.get(applicationContext) val availableAccounts = mAccountManager.getAccountsByType(ACCOUNT_TYPE) - val account = availableAccounts[0] +// var account : Account = availableAccounts[0] + var account : Account = availableAccounts[0] + for (acc in availableAccounts) { + if (acc.name.equals(currentUserAccountName)) { + account = acc + } + } clearOldAccountContacts( contentResolver, @@ -333,7 +342,13 @@ class SyncSettingsActivity : BaseActivity() { private fun setSync(syncEnabled: Boolean) { val mAccountManager = AccountManager.get(applicationContext) val availableAccounts = mAccountManager.getAccountsByType(ACCOUNT_TYPE) - val account = availableAccounts[0] +// var account : Account = availableAccounts[0] + var account : Account = availableAccounts[0] + for (acc in availableAccounts) { + if (acc.name.equals(currentUserAccountName)) { + account = acc + } + } if (syncEnabled) { mAccountManager.setUserData(account, SYNC_MARKER_KEY, null) ContentResolver.cancelSync(account, ContactsContract.AUTHORITY) diff --git a/app/src/main/java/org/xwiki/android/sync/auth/AuthenticatorActivity.kt b/app/src/main/java/org/xwiki/android/sync/auth/AuthenticatorActivity.kt index 5bf1e699..0c71cc2d 100644 --- a/app/src/main/java/org/xwiki/android/sync/auth/AuthenticatorActivity.kt +++ b/app/src/main/java/org/xwiki/android/sync/auth/AuthenticatorActivity.kt @@ -168,13 +168,6 @@ class AuthenticatorActivity : AccountAuthenticatorActivity() { isTestRunning = intent.getBooleanExtra("Test", false) - if (!isTestRunning) { - if (availableAccounts.size > 0) { - Toast.makeText(this, "The user already exists!", Toast.LENGTH_SHORT).show() - finish() - return - } - } showViewFlipper(position) } @@ -355,7 +348,7 @@ class AuthenticatorActivity : AccountAuthenticatorActivity() { Log.d(TAG, "> finishLogin") //before add new account, clear old account data. - clearOldAccount() +// clearOldAccount() //get values val accountName = intent.getStringExtra(AccountManager.KEY_ACCOUNT_NAME) @@ -392,10 +385,13 @@ class AuthenticatorActivity : AccountAuthenticatorActivity() { setAccountAuthenticatorResult(intentReturn.extras) setResult(Activity.RESULT_OK, intentReturn) Log.d(TAG, ">" + "finish return") - finish() + val syncActivityIntent = Intent(this, SyncSettingsActivity::class.java) + syncActivityIntent.putExtra(AccountManager.KEY_ACCOUNT_NAME, accountName) + syncActivityIntent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, ACCOUNT_TYPE) startActivity( - Intent(this, SyncSettingsActivity::class.java) + syncActivityIntent ) + finish() } /** diff --git a/app/src/main/java/org/xwiki/android/sync/rest/BaseApiManager.kt b/app/src/main/java/org/xwiki/android/sync/rest/BaseApiManager.kt index bf5cbcbb..205e87ce 100644 --- a/app/src/main/java/org/xwiki/android/sync/rest/BaseApiManager.kt +++ b/app/src/main/java/org/xwiki/android/sync/rest/BaseApiManager.kt @@ -28,6 +28,7 @@ import org.xwiki.android.sync.utils.getValue import retrofit2.Retrofit import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory import retrofit2.converter.gson.GsonConverterFactory +import java.util.concurrent.TimeUnit /** * Will help to contain and separate functionality of working with API @@ -76,6 +77,9 @@ class BaseApiManager { loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY val okHttpClient = OkHttpClient.Builder() + .connectTimeout(60, TimeUnit.SECONDS) + .writeTimeout(60, TimeUnit.SECONDS) + .readTimeout(60, TimeUnit.SECONDS) .addInterceptor(XWikiInterceptor()) .addInterceptor(loggingInterceptor) .build() diff --git a/app/src/main/java/org/xwiki/android/sync/utils/AccountClickListener.kt b/app/src/main/java/org/xwiki/android/sync/utils/AccountClickListener.kt new file mode 100644 index 00000000..b2ab6d34 --- /dev/null +++ b/app/src/main/java/org/xwiki/android/sync/utils/AccountClickListener.kt @@ -0,0 +1,5 @@ +package org.xwiki.android.sync.utils + +interface AccountClickListener { + fun onItemClicked (accountName: String?) +} \ No newline at end of file diff --git a/app/src/main/res/layout/account_list_layout.xml b/app/src/main/res/layout/account_list_layout.xml new file mode 100644 index 00000000..c0756cfe --- /dev/null +++ b/app/src/main/res/layout/account_list_layout.xml @@ -0,0 +1,15 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/act_select_account.xml b/app/src/main/res/layout/act_select_account.xml new file mode 100644 index 00000000..a9a77502 --- /dev/null +++ b/app/src/main/res/layout/act_select_account.xml @@ -0,0 +1,41 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/list_item_group.xml b/app/src/main/res/layout/list_item_group.xml index 7aed36f0..48a41531 100644 --- a/app/src/main/res/layout/list_item_group.xml +++ b/app/src/main/res/layout/list_item_group.xml @@ -7,15 +7,15 @@ android:descendantFocusability="blocksDescendants"> + android:id="@+id/groupName" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_marginLeft="16dp" + android:layout_marginTop="10dip" + android:ellipsize="end" + android:maxLines="1" + android:text="Admin" + android:textSize="20sp" android:layout_marginStart="16dp"/> + android:targetClass="org.xwiki.android.sync.activities.SelectAccountActivity" /> From 7688734a79f0330eb0e1830aea83bcb67de8d501 Mon Sep 17 00:00:00 2001 From: jaindiv26 Date: Wed, 3 Jul 2019 15:27:33 +0530 Subject: [PATCH 02/27] Added activity to select from multiple account. --- .../sync/activities/AccountListAdapter.kt | 55 ++++++-- .../sync/activities/SelectAccountActivity.kt | 19 ++- .../sync/activities/SyncSettingsActivity.kt | 36 ++++- .../sync/utils/AccountClickListener.kt | 4 +- .../main/res/drawable/ic_edit_black_24dp.xml | 9 ++ .../main/res/layout/account_list_layout.xml | 18 ++- .../main/res/layout/act_select_account.xml | 6 +- .../res/layout/activity_sync_settings.xml | 125 +++++++++++++++--- app/src/main/res/xml/prefs.xml | 2 +- 9 files changed, 225 insertions(+), 49 deletions(-) create mode 100644 app/src/main/res/drawable/ic_edit_black_24dp.xml diff --git a/app/src/main/java/org/xwiki/android/sync/activities/AccountListAdapter.kt b/app/src/main/java/org/xwiki/android/sync/activities/AccountListAdapter.kt index 3c099c56..2dc37134 100644 --- a/app/src/main/java/org/xwiki/android/sync/activities/AccountListAdapter.kt +++ b/app/src/main/java/org/xwiki/android/sync/activities/AccountListAdapter.kt @@ -5,6 +5,7 @@ import android.content.Context import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.widget.BaseAdapter import android.widget.LinearLayout import android.widget.TextView import androidx.recyclerview.widget.RecyclerView @@ -13,30 +14,56 @@ import org.xwiki.android.sync.utils.AccountClickListener class AccountListAdapter ( private val mContext: Context, - private var availableAccounts : Array?, + private var availableAccounts : Array, private val listener : AccountClickListener) - : RecyclerView.Adapter() { + : BaseAdapter() { - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AccountListViewHolder { - return AccountListViewHolder(LayoutInflater.from(mContext).inflate(R.layout.account_list_layout, parent, false)) + override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View { + var convertView = convertView + val viewHolder: AccountListViewHolder + + if (convertView == null) { + val inflater = LayoutInflater.from(mContext) + convertView = inflater.inflate(R.layout.account_list_layout, null) + viewHolder = AccountListViewHolder(convertView!!) + convertView.tag = viewHolder + } else { + viewHolder = convertView.tag as AccountListViewHolder + } + + val account = getItem(position) + viewHolder.tvAccountName.text = account.name + viewHolder.tvAccountType.text = account.type + + viewHolder.llAccountItem.setOnClickListener { + listener.onItemClicked(account) + } + + return convertView } - override fun getItemCount(): Int { - return availableAccounts?.size!! + override fun getItem(position: Int): Account { + return availableAccounts[position] } - override fun onBindViewHolder(holder: AccountListViewHolder, position: Int) { - holder.tvAccountName.text = availableAccounts?.get(position)?.name + override fun getItemId(position: Int): Long { + return position.toLong() + } - holder.llAccountItem.setOnClickListener { - listener.onItemClicked(availableAccounts?.get(position)?.name) - } + override fun getCount(): Int { + return availableAccounts.size } } -class AccountListViewHolder (view: View) : RecyclerView.ViewHolder(view) { +private class AccountListViewHolder (view: View) { + val tvAccountName : TextView + val tvAccountType : TextView + val llAccountItem : LinearLayout - val tvAccountName = view.findViewById(R.id.tvAccountName) - val llAccountItem = view.findViewById(R.id.llAccountItem) + init { + tvAccountName = view.findViewById(R.id.tvAccountName) + tvAccountType = view.findViewById(R.id.tvAccountType) + llAccountItem = view.findViewById(R.id.llAccountItem) + } } \ No newline at end of file diff --git a/app/src/main/java/org/xwiki/android/sync/activities/SelectAccountActivity.kt b/app/src/main/java/org/xwiki/android/sync/activities/SelectAccountActivity.kt index a04b5821..662dd7a8 100644 --- a/app/src/main/java/org/xwiki/android/sync/activities/SelectAccountActivity.kt +++ b/app/src/main/java/org/xwiki/android/sync/activities/SelectAccountActivity.kt @@ -1,6 +1,8 @@ package org.xwiki.android.sync.activities +import android.accounts.Account import android.accounts.AccountManager +import android.app.Activity import android.content.Intent import android.os.Bundle import android.widget.Toast @@ -16,8 +18,6 @@ class SelectAccountActivity : BaseActivity(), AccountClickListener { lateinit var binding : ActSelectAccountBinding - - override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = DataBindingUtil.setContentView(this, R.layout.act_select_account) @@ -26,17 +26,14 @@ class SelectAccountActivity : BaseActivity(), AccountClickListener { val availableAccountsList = mAccountManager.getAccountsByType(ACCOUNT_TYPE) val adapter: AccountListAdapter = AccountListAdapter(this, availableAccountsList, this) - binding.rvAvailableAccounts.adapter = adapter - binding.rvAvailableAccounts.layoutManager = LinearLayoutManager(this) - + binding.lvAvailableAccounts.adapter = adapter } - override fun onItemClicked(accountName: String?) { - val syncActivityIntent = Intent(this, SyncSettingsActivity::class.java) - syncActivityIntent.putExtra(AccountManager.KEY_ACCOUNT_NAME, accountName) - startActivity( - syncActivityIntent - ) + override fun onItemClicked(selectedAccount: Account) { + val intent = Intent() + intent.putExtra(AccountManager.KEY_ACCOUNT_NAME, selectedAccount.name) + intent.putExtra(AccountManager.KEY_ACCOUNT_TYPE, selectedAccount.type) + setResult(Activity.RESULT_OK, intent) finish() } } \ No newline at end of file diff --git a/app/src/main/java/org/xwiki/android/sync/activities/SyncSettingsActivity.kt b/app/src/main/java/org/xwiki/android/sync/activities/SyncSettingsActivity.kt index d22f5730..56403919 100644 --- a/app/src/main/java/org/xwiki/android/sync/activities/SyncSettingsActivity.kt +++ b/app/src/main/java/org/xwiki/android/sync/activities/SyncSettingsActivity.kt @@ -2,6 +2,7 @@ package org.xwiki.android.sync.activities import android.accounts.Account import android.accounts.AccountManager +import android.app.Activity import android.content.ComponentName import android.content.ContentResolver import android.content.Context @@ -113,6 +114,8 @@ class SyncSettingsActivity : BaseActivity() { private lateinit var currentUserAccountName : String + private lateinit var currentUserAccountType : String + /** * Init all views and other activity objects * @@ -123,7 +126,19 @@ class SyncSettingsActivity : BaseActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = DataBindingUtil.setContentView(this, R.layout.activity_sync_settings) - currentUserAccountName = intent.getStringExtra(AccountManager.KEY_ACCOUNT_NAME) + + if (intent.extras.get("account") != null) { + val intentAccount : Account = intent.extras.get("account") as Account + currentUserAccountName = intentAccount.name + currentUserAccountType = intentAccount.type + } else { + currentUserAccountName = intent.getStringExtra(AccountManager.KEY_ACCOUNT_NAME) + currentUserAccountType = intent.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE) + } + + binding.tvSelectedSyncAcc.text = currentUserAccountName + binding.tvSelectedSyncType.text = currentUserAccountType + binding.versionCheck.text = String.format( getString(R.string.versionTemplate), getAppVersionName(this) @@ -149,10 +164,29 @@ class SyncSettingsActivity : BaseActivity() { } } + binding.rvChangeSelectedAccount.setOnClickListener { + val intent : Intent = Intent (this, SelectAccountActivity::class.java) + startActivityForResult(intent, 1000) + } chosenSyncType = getValue(this, SYNC_TYPE, SYNC_TYPE_ALL_USERS) binding.selectSpinner.setSelection(chosenSyncType) } + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + if (requestCode == 1000) { + if (resultCode == Activity.RESULT_OK) { + if (data != null) { + currentUserAccountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME) + currentUserAccountType = data.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE) + + binding.tvSelectedSyncAcc.text = currentUserAccountName + binding.tvSelectedSyncType.text = currentUserAccountType + } + } + } + + } + /** * Show progress bar if need or hide otherwise. * diff --git a/app/src/main/java/org/xwiki/android/sync/utils/AccountClickListener.kt b/app/src/main/java/org/xwiki/android/sync/utils/AccountClickListener.kt index b2ab6d34..b1fa6d82 100644 --- a/app/src/main/java/org/xwiki/android/sync/utils/AccountClickListener.kt +++ b/app/src/main/java/org/xwiki/android/sync/utils/AccountClickListener.kt @@ -1,5 +1,7 @@ package org.xwiki.android.sync.utils +import android.accounts.Account + interface AccountClickListener { - fun onItemClicked (accountName: String?) + fun onItemClicked (selectedAccount : Account) } \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_edit_black_24dp.xml b/app/src/main/res/drawable/ic_edit_black_24dp.xml new file mode 100644 index 00000000..2ab2fb75 --- /dev/null +++ b/app/src/main/res/drawable/ic_edit_black_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/layout/account_list_layout.xml b/app/src/main/res/layout/account_list_layout.xml index c0756cfe..237c363f 100644 --- a/app/src/main/res/layout/account_list_layout.xml +++ b/app/src/main/res/layout/account_list_layout.xml @@ -9,7 +9,21 @@ android:id="@+id/tvAccountName" android:layout_width="match_parent" android:layout_height="wrap_content" - android:textSize="20sp" - android:layout_margin="16dp"/> + android:textSize="18sp" + android:textColor="#000000" + android:layout_marginRight="16dp" + android:layout_marginEnd="16dp" + android:layout_marginTop="8dp"/> + + + \ No newline at end of file diff --git a/app/src/main/res/layout/act_select_account.xml b/app/src/main/res/layout/act_select_account.xml index a9a77502..090ea7ec 100644 --- a/app/src/main/res/layout/act_select_account.xml +++ b/app/src/main/res/layout/act_select_account.xml @@ -27,14 +27,14 @@ android:layout_marginLeft="16dp"/> - - + diff --git a/app/src/main/res/layout/activity_sync_settings.xml b/app/src/main/res/layout/activity_sync_settings.xml index be349392..8d1a4501 100644 --- a/app/src/main/res/layout/activity_sync_settings.xml +++ b/app/src/main/res/layout/activity_sync_settings.xml @@ -1,10 +1,10 @@ - + - + @@ -19,35 +19,123 @@ android:orientation="vertical" android:layout_alignParentTop="true" android:layout_above="@+id/nextButton"> - - + android:orientation="vertical"> + + + + android:layout_marginStart="8dp" + android:padding="@dimen/defaultViewSmallPadding" + android:layout_marginLeft="8dp"/>