aboutsummaryrefslogtreecommitdiffstats
path: root/java/src/com/android/inputmethod/latin/settings/AccountsSettingsFragment.java
diff options
context:
space:
mode:
authorSandeep Siddhartha <sansid@google.com>2014-09-16 15:53:32 -0700
committerSandeep Siddhartha <sansid@google.com>2014-09-18 10:39:30 -0700
commit84185148c5506cd58ae6870102de6538bbc35042 (patch)
tree580b70b02705857c48938ceadf8de4b5bf14105a /java/src/com/android/inputmethod/latin/settings/AccountsSettingsFragment.java
parent27bb70d6a049098f1d1186e2d69c86347e43679f (diff)
downloadlatinime-84185148c5506cd58ae6870102de6538bbc35042.tar.gz
latinime-84185148c5506cd58ae6870102de6538bbc35042.tar.xz
latinime-84185148c5506cd58ae6870102de6538bbc35042.zip
Add account listing and preference integration for current account
Bug: 17464068 Change-Id: Idb68a6012b285d6bc4632414bb6d11131148cf67
Diffstat (limited to 'java/src/com/android/inputmethod/latin/settings/AccountsSettingsFragment.java')
-rw-r--r--java/src/com/android/inputmethod/latin/settings/AccountsSettingsFragment.java123
1 files changed, 117 insertions, 6 deletions
diff --git a/java/src/com/android/inputmethod/latin/settings/AccountsSettingsFragment.java b/java/src/com/android/inputmethod/latin/settings/AccountsSettingsFragment.java
index e9e73c735..06ab1e2d2 100644
--- a/java/src/com/android/inputmethod/latin/settings/AccountsSettingsFragment.java
+++ b/java/src/com/android/inputmethod/latin/settings/AccountsSettingsFragment.java
@@ -16,25 +16,41 @@
package com.android.inputmethod.latin.settings;
+import android.app.AlertDialog;
import android.content.Context;
+import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.preference.Preference;
+import android.preference.Preference.OnPreferenceClickListener;
+import android.text.TextUtils;
+import android.widget.ListView;
+import android.widget.Toast;
import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.SubtypeSwitcher;
import com.android.inputmethod.latin.define.ProductionFlags;
+import com.android.inputmethod.latin.utils.LoginAccountUtils;
+
+import javax.annotation.Nullable;
/**
* "Accounts & Privacy" settings sub screen.
*
* This settings sub screen handles the following preferences:
- * - TODO: Account selection/management for IME
- * - TODO: Sync preferences
- * - TODO: Privacy preferences
+ * <li> Account selection/management for IME
+ * <li> TODO: Sync preferences
+ * <li> TODO: Privacy preferences
*/
public final class AccountsSettingsFragment extends SubScreenFragment {
+ static final String PREF_ACCCOUNT_SWITCHER = "account_switcher";
+
+ private final DialogInterface.OnClickListener mAccountSelectedListener =
+ new AccountSelectedListener();
+ private final DialogInterface.OnClickListener mAccountSignedOutListener =
+ new AccountSignedOutListener();
+
@Override
public void onCreate(final Bundle icicle) {
super.onCreate(icicle);
@@ -74,9 +90,104 @@ public final class AccountsSettingsFragment extends SubScreenFragment {
}
private void refreshAccountSelection() {
- // TODO: Fetch the currently selected account.
- // Set the summary for the account preference.
- // Depending on the account selection, enable/disable preferences that
+ final String currentAccount = getCurrentlySelectedAccount();
+ final Preference accountSwitcher = findPreference(PREF_ACCCOUNT_SWITCHER);
+ if (currentAccount == null) {
+ // No account is currently selected.
+ accountSwitcher.setSummary(getString(R.string.no_accounts_selected));
+ } else {
+ // Set the currently selected account.
+ accountSwitcher.setSummary(getString(R.string.account_selected, currentAccount));
+ }
+ final Context context = getActivity();
+ final String[] accountsForLogin = LoginAccountUtils.getAccountsForLogin(context);
+ accountSwitcher.setOnPreferenceClickListener(new OnPreferenceClickListener() {
+ @Override
+ public boolean onPreferenceClick(Preference preference) {
+ if (accountsForLogin.length == 0) {
+ // TODO: Handle account addition.
+ Toast.makeText(getActivity(),
+ getString(R.string.account_select_cancel), Toast.LENGTH_SHORT).show();
+ } else {
+ createAccountPicker(accountsForLogin, currentAccount).show();
+ }
+ return true;
+ }
+ });
+
+ // TODO: Depending on the account selection, enable/disable preferences that
// depend on an account.
}
+
+ @Nullable
+ private String getCurrentlySelectedAccount() {
+ return getSharedPreferences().getString(Settings.PREF_ACCOUNT_NAME, null);
+ }
+
+ /**
+ * Creates an account picker dialog showing the given accounts in a list and selecting
+ * the selected account by default.
+ * The list of accounts must not be null/empty.
+ *
+ * Package-private for testing.
+ */
+ AlertDialog createAccountPicker(final String[] accounts,
+ final String selectedAccount) {
+ if (accounts == null || accounts.length == 0) {
+ throw new IllegalArgumentException("List of accounts must not be empty");
+ }
+
+ // See if the currently selected account is in the list.
+ // If it is, the entry is selected, and a sign-out button is provided.
+ // If it isn't, select the 0th account by default which will get picked up
+ // if the user presses OK.
+ int index = 0;
+ boolean isSignedIn = false;
+ for (int i = 0; i < accounts.length; i++) {
+ if (TextUtils.equals(accounts[i], selectedAccount)) {
+ index = i;
+ isSignedIn = true;
+ break;
+ }
+ }
+ final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
+ .setTitle(R.string.account_select_title)
+ .setSingleChoiceItems(accounts, index, null)
+ .setPositiveButton(R.string.account_select_ok, mAccountSelectedListener)
+ .setNegativeButton(R.string.account_select_cancel, null);
+ if (isSignedIn) {
+ builder.setNeutralButton(R.string.account_select_sign_out, mAccountSignedOutListener);
+ }
+ return builder.create();
+ }
+
+ /**
+ * Listener for an account being selected from the picker.
+ * Persists the account to shared preferences.
+ */
+ class AccountSelectedListener implements DialogInterface.OnClickListener {
+ @Override
+ public void onClick(DialogInterface dialog, int which) {
+ final ListView lv = ((AlertDialog)dialog).getListView();
+ final Object selectedItem = lv.getItemAtPosition(lv.getCheckedItemPosition());
+ getSharedPreferences()
+ .edit()
+ .putString(Settings.PREF_ACCOUNT_NAME, (String) selectedItem)
+ .apply();
+ }
+ }
+
+ /**
+ * Listener for sign-out being initiated from from the picker.
+ * Removed the account from shared preferences.
+ */
+ class AccountSignedOutListener implements DialogInterface.OnClickListener {
+ @Override
+ public void onClick(DialogInterface dialog, int which) {
+ getSharedPreferences()
+ .edit()
+ .remove(Settings.PREF_ACCOUNT_NAME)
+ .apply();
+ }
+ }
}