am c13c53c5: am ca8053d7: am 707ca769: Handle Cloud Sync and SpellChecker settings when permission changed

* commit 'c13c53c5d5f02881ba5914c6db6041a20514bcfc':
  Handle Cloud Sync and SpellChecker settings when permission changed
This commit is contained in:
Mohammadinamul Sheik 2015-07-24 04:46:32 +00:00 committed by Android Git Automerger
commit 4e7ed10c35
4 changed files with 92 additions and 18 deletions

View file

@ -19,6 +19,7 @@ package com.android.inputmethod.latin.settings;
import static com.android.inputmethod.latin.settings.LocalSettingsConstants.PREF_ACCOUNT_NAME; import static com.android.inputmethod.latin.settings.LocalSettingsConstants.PREF_ACCOUNT_NAME;
import static com.android.inputmethod.latin.settings.LocalSettingsConstants.PREF_ENABLE_CLOUD_SYNC; import static com.android.inputmethod.latin.settings.LocalSettingsConstants.PREF_ENABLE_CLOUD_SYNC;
import android.Manifest;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.Context; import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
@ -40,6 +41,7 @@ import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.accounts.AccountStateChangedListener; import com.android.inputmethod.latin.accounts.AccountStateChangedListener;
import com.android.inputmethod.latin.accounts.LoginAccountUtils; import com.android.inputmethod.latin.accounts.LoginAccountUtils;
import com.android.inputmethod.latin.define.ProductionFlags; import com.android.inputmethod.latin.define.ProductionFlags;
import com.android.inputmethod.latin.permissions.PermissionsUtil;
import com.android.inputmethod.latin.utils.ManagedProfileUtils; import com.android.inputmethod.latin.utils.ManagedProfileUtils;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
@ -254,11 +256,14 @@ public final class AccountsSettingsFragment extends SubScreenFragment {
if (!ProductionFlags.ENABLE_USER_HISTORY_DICTIONARY_SYNC) { if (!ProductionFlags.ENABLE_USER_HISTORY_DICTIONARY_SYNC) {
return; return;
} }
final String[] accountsForLogin = boolean hasAccountsPermission = PermissionsUtil.checkAllPermissionsGranted(
LoginAccountUtils.getAccountsForLogin(getActivity()); getActivity(), Manifest.permission.READ_CONTACTS);
final String currentAccount = getSignedInAccountName();
if (!mManagedProfileBeingDetected.get() && final String[] accountsForLogin = hasAccountsPermission ?
LoginAccountUtils.getAccountsForLogin(getActivity()) : new String[0];
final String currentAccount = hasAccountsPermission ? getSignedInAccountName() : null;
if (hasAccountsPermission && !mManagedProfileBeingDetected.get() &&
!mHasManagedProfile.get() && accountsForLogin.length > 0) { !mHasManagedProfile.get() && accountsForLogin.length > 0) {
// Sync can be used by user; enable all preferences. // Sync can be used by user; enable all preferences.
enableSyncPreferences(accountsForLogin, currentAccount); enableSyncPreferences(accountsForLogin, currentAccount);
@ -266,26 +271,35 @@ public final class AccountsSettingsFragment extends SubScreenFragment {
// Sync cannot be used by user; disable all preferences. // Sync cannot be used by user; disable all preferences.
disableSyncPreferences(); disableSyncPreferences();
} }
refreshSyncSettingsMessaging(mManagedProfileBeingDetected.get(), refreshSyncSettingsMessaging(hasAccountsPermission, mManagedProfileBeingDetected.get(),
mHasManagedProfile.get(), accountsForLogin.length > 0, mHasManagedProfile.get(), accountsForLogin.length > 0,
currentAccount); currentAccount);
} }
/** /**
* @param hasAccountsPermission whether the app has the permission to read accounts.
* @param managedProfileBeingDetected whether we are in process of determining work profile. * @param managedProfileBeingDetected whether we are in process of determining work profile.
* @param hasManagedProfile whether the device has work profile. * @param hasManagedProfile whether the device has work profile.
* @param hasAccountsForLogin whether the device has enough accounts for login. * @param hasAccountsForLogin whether the device has enough accounts for login.
* @param currentAccount the account currently selected in the application. * @param currentAccount the account currently selected in the application.
*/ */
private void refreshSyncSettingsMessaging(boolean managedProfileBeingDetected, private void refreshSyncSettingsMessaging(boolean hasAccountsPermission,
boolean hasManagedProfile, boolean hasAccountsForLogin, String currentAccount) { boolean managedProfileBeingDetected,
boolean hasManagedProfile,
boolean hasAccountsForLogin,
String currentAccount) {
if (!ProductionFlags.ENABLE_USER_HISTORY_DICTIONARY_SYNC) { if (!ProductionFlags.ENABLE_USER_HISTORY_DICTIONARY_SYNC) {
return; return;
} }
// If we are determining eligiblity, we show empty summaries. if (!hasAccountsPermission) {
// Once we have some deterministic result, we set summaries based on different results. mEnableSyncPreference.setChecked(false);
if (managedProfileBeingDetected) { mEnableSyncPreference.setSummary(getString(R.string.cloud_sync_summary_disabled));
mAccountSwitcher.setSummary("");
return;
} else if (managedProfileBeingDetected) {
// If we are determining eligiblity, we show empty summaries.
// Once we have some deterministic result, we set summaries based on different results.
mEnableSyncPreference.setSummary(""); mEnableSyncPreference.setSummary("");
mAccountSwitcher.setSummary(""); mAccountSwitcher.setSummary("");
} else if (hasManagedProfile) { } else if (hasManagedProfile) {
@ -462,7 +476,7 @@ public final class AccountsSettingsFragment extends SubScreenFragment {
new DialogInterface.OnClickListener() { new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(final DialogInterface dialog, public void onClick(final DialogInterface dialog,
final int which) { final int which) {
if (which == DialogInterface.BUTTON_POSITIVE) { if (which == DialogInterface.BUTTON_POSITIVE) {
final Context context = getActivity(); final Context context = getActivity();
final String[] accountsForLogin = final String[] accountsForLogin =
@ -473,9 +487,9 @@ public final class AccountsSettingsFragment extends SubScreenFragment {
.show(); .show();
} }
} }
}) })
.setNegativeButton(R.string.cloud_sync_cancel, null) .setNegativeButton(R.string.cloud_sync_cancel, null)
.create(); .create();
optInDialog.setOnShowListener(this); optInDialog.setOnShowListener(this);
optInDialog.show(); optInDialog.show();
} }

View file

@ -32,7 +32,7 @@ import android.util.Log;
* A base abstract class for a {@link PreferenceFragment} that implements a nested * A base abstract class for a {@link PreferenceFragment} that implements a nested
* {@link PreferenceScreen} of the main preference screen. * {@link PreferenceScreen} of the main preference screen.
*/ */
abstract class SubScreenFragment extends PreferenceFragment public abstract class SubScreenFragment extends PreferenceFragment
implements OnSharedPreferenceChangeListener { implements OnSharedPreferenceChangeListener {
private OnSharedPreferenceChangeListener mSharedPreferenceChangeListener; private OnSharedPreferenceChangeListener mSharedPreferenceChangeListener;

View file

@ -16,6 +16,7 @@
package com.android.inputmethod.latin.spellcheck; package com.android.inputmethod.latin.spellcheck;
import com.android.inputmethod.latin.permissions.PermissionsManager;
import com.android.inputmethod.latin.utils.FragmentUtils; import com.android.inputmethod.latin.utils.FragmentUtils;
import android.annotation.TargetApi; import android.annotation.TargetApi;
@ -23,11 +24,13 @@ import android.content.Intent;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceActivity; import android.preference.PreferenceActivity;
import android.support.v4.app.ActivityCompat;
/** /**
* Spell checker preference screen. * Spell checker preference screen.
*/ */
public final class SpellCheckerSettingsActivity extends PreferenceActivity { public final class SpellCheckerSettingsActivity extends PreferenceActivity
implements ActivityCompat.OnRequestPermissionsResultCallback {
private static final String DEFAULT_FRAGMENT = SpellCheckerSettingsFragment.class.getName(); private static final String DEFAULT_FRAGMENT = SpellCheckerSettingsFragment.class.getName();
@Override @Override
@ -48,4 +51,11 @@ public final class SpellCheckerSettingsActivity extends PreferenceActivity {
public boolean isValidFragment(String fragmentName) { public boolean isValidFragment(String fragmentName) {
return FragmentUtils.isValidFragment(fragmentName); return FragmentUtils.isValidFragment(fragmentName);
} }
@Override
public void onRequestPermissionsResult(
int requestCode, String[] permissions, int[] grantResults) {
PermissionsManager.get(this).onRequestPermissionsResult(
requestCode, permissions, grantResults);
}
} }

View file

@ -16,18 +16,31 @@
package com.android.inputmethod.latin.spellcheck; package com.android.inputmethod.latin.spellcheck;
import android.Manifest;
import android.content.SharedPreferences;
import android.os.Bundle; import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen; import android.preference.PreferenceScreen;
import android.preference.SwitchPreference;
import android.text.TextUtils;
import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.permissions.PermissionsManager;
import com.android.inputmethod.latin.permissions.PermissionsUtil;
import com.android.inputmethod.latin.settings.SubScreenFragment;
import com.android.inputmethod.latin.settings.TwoStatePreferenceHelper; import com.android.inputmethod.latin.settings.TwoStatePreferenceHelper;
import com.android.inputmethod.latin.utils.ApplicationUtils; import com.android.inputmethod.latin.utils.ApplicationUtils;
import static com.android.inputmethod.latin.permissions.PermissionsManager.get;
/** /**
* Preference screen. * Preference screen.
*/ */
public final class SpellCheckerSettingsFragment extends PreferenceFragment { public final class SpellCheckerSettingsFragment extends SubScreenFragment
implements SharedPreferences.OnSharedPreferenceChangeListener,
PermissionsManager.PermissionsResultCallback {
private SwitchPreference mLookupContactsPreference;
@Override @Override
public void onActivityCreated(final Bundle savedInstanceState) { public void onActivityCreated(final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
@ -36,5 +49,42 @@ public final class SpellCheckerSettingsFragment extends PreferenceFragment {
preferenceScreen.setTitle(ApplicationUtils.getActivityTitleResId( preferenceScreen.setTitle(ApplicationUtils.getActivityTitleResId(
getActivity(), SpellCheckerSettingsActivity.class)); getActivity(), SpellCheckerSettingsActivity.class));
TwoStatePreferenceHelper.replaceCheckBoxPreferencesBySwitchPreferences(preferenceScreen); TwoStatePreferenceHelper.replaceCheckBoxPreferencesBySwitchPreferences(preferenceScreen);
mLookupContactsPreference = (SwitchPreference) findPreference(
AndroidSpellCheckerService.PREF_USE_CONTACTS_KEY);
turnOffLookupContactsIfNoPermission();
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (!TextUtils.equals(key, AndroidSpellCheckerService.PREF_USE_CONTACTS_KEY)) {
return;
}
if (!sharedPreferences.getBoolean(key, false)) {
// don't care if the preference is turned off.
return;
}
// Check for permissions.
if (PermissionsUtil.checkAllPermissionsGranted(
getActivity() /* context */, Manifest.permission.READ_CONTACTS)) {
return; // all permissions granted, no need to request permissions.
}
get(getActivity() /* context */).requestPermissions(this /* PermissionsResultCallback */,
getActivity() /* activity */, Manifest.permission.READ_CONTACTS);
}
@Override
public void onRequestPermissionsResult(boolean allGranted) {
turnOffLookupContactsIfNoPermission();
}
private void turnOffLookupContactsIfNoPermission() {
if (!PermissionsUtil.checkAllPermissionsGranted(
getActivity(), Manifest.permission.READ_CONTACTS)) {
mLookupContactsPreference.setChecked(false);
}
} }
} }