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

* commit '707ca7692a8666989b3fbe860bee28115103988e':
  Handle Cloud Sync and SpellChecker settings when permission changed
main
Mohammadinamul Sheik 2015-07-24 04:20:13 +00:00 committed by Android Git Automerger
commit ca8053d7f3
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_ENABLE_CLOUD_SYNC;
import android.Manifest;
import android.app.AlertDialog;
import android.content.Context;
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.LoginAccountUtils;
import com.android.inputmethod.latin.define.ProductionFlags;
import com.android.inputmethod.latin.permissions.PermissionsUtil;
import com.android.inputmethod.latin.utils.ManagedProfileUtils;
import java.util.concurrent.atomic.AtomicBoolean;
@ -254,11 +256,14 @@ public final class AccountsSettingsFragment extends SubScreenFragment {
if (!ProductionFlags.ENABLE_USER_HISTORY_DICTIONARY_SYNC) {
return;
}
final String[] accountsForLogin =
LoginAccountUtils.getAccountsForLogin(getActivity());
final String currentAccount = getSignedInAccountName();
boolean hasAccountsPermission = PermissionsUtil.checkAllPermissionsGranted(
getActivity(), Manifest.permission.READ_CONTACTS);
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) {
// Sync can be used by user; enable all preferences.
enableSyncPreferences(accountsForLogin, currentAccount);
@ -266,26 +271,35 @@ public final class AccountsSettingsFragment extends SubScreenFragment {
// Sync cannot be used by user; disable all preferences.
disableSyncPreferences();
}
refreshSyncSettingsMessaging(mManagedProfileBeingDetected.get(),
refreshSyncSettingsMessaging(hasAccountsPermission, mManagedProfileBeingDetected.get(),
mHasManagedProfile.get(), accountsForLogin.length > 0,
currentAccount);
}
/**
* @param hasAccountsPermission whether the app has the permission to read accounts.
* @param managedProfileBeingDetected whether we are in process of determining work profile.
* @param hasManagedProfile whether the device has work profile.
* @param hasAccountsForLogin whether the device has enough accounts for login.
* @param currentAccount the account currently selected in the application.
*/
private void refreshSyncSettingsMessaging(boolean managedProfileBeingDetected,
boolean hasManagedProfile, boolean hasAccountsForLogin, String currentAccount) {
private void refreshSyncSettingsMessaging(boolean hasAccountsPermission,
boolean managedProfileBeingDetected,
boolean hasManagedProfile,
boolean hasAccountsForLogin,
String currentAccount) {
if (!ProductionFlags.ENABLE_USER_HISTORY_DICTIONARY_SYNC) {
return;
}
// If we are determining eligiblity, we show empty summaries.
// Once we have some deterministic result, we set summaries based on different results.
if (managedProfileBeingDetected) {
if (!hasAccountsPermission) {
mEnableSyncPreference.setChecked(false);
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("");
mAccountSwitcher.setSummary("");
} else if (hasManagedProfile) {
@ -462,7 +476,7 @@ public final class AccountsSettingsFragment extends SubScreenFragment {
new DialogInterface.OnClickListener() {
@Override
public void onClick(final DialogInterface dialog,
final int which) {
final int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
final Context context = getActivity();
final String[] accountsForLogin =
@ -473,9 +487,9 @@ public final class AccountsSettingsFragment extends SubScreenFragment {
.show();
}
}
})
.setNegativeButton(R.string.cloud_sync_cancel, null)
.create();
})
.setNegativeButton(R.string.cloud_sync_cancel, null)
.create();
optInDialog.setOnShowListener(this);
optInDialog.show();
}

View File

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

View File

@ -16,6 +16,7 @@
package com.android.inputmethod.latin.spellcheck;
import com.android.inputmethod.latin.permissions.PermissionsManager;
import com.android.inputmethod.latin.utils.FragmentUtils;
import android.annotation.TargetApi;
@ -23,11 +24,13 @@ import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.support.v4.app.ActivityCompat;
/**
* 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();
@Override
@ -48,4 +51,11 @@ public final class SpellCheckerSettingsActivity extends PreferenceActivity {
public boolean isValidFragment(String 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;
import android.Manifest;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceFragment;
import android.preference.PreferenceScreen;
import android.preference.SwitchPreference;
import android.text.TextUtils;
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.utils.ApplicationUtils;
import static com.android.inputmethod.latin.permissions.PermissionsManager.get;
/**
* Preference screen.
*/
public final class SpellCheckerSettingsFragment extends PreferenceFragment {
public final class SpellCheckerSettingsFragment extends SubScreenFragment
implements SharedPreferences.OnSharedPreferenceChangeListener,
PermissionsManager.PermissionsResultCallback {
private SwitchPreference mLookupContactsPreference;
@Override
public void onActivityCreated(final Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
@ -36,5 +49,42 @@ public final class SpellCheckerSettingsFragment extends PreferenceFragment {
preferenceScreen.setTitle(ApplicationUtils.getActivityTitleResId(
getActivity(), SpellCheckerSettingsActivity.class));
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);
}
}
}