Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ca8053d7 authored by Mohammadinamul Sheik's avatar Mohammadinamul Sheik Committed by Android Git Automerger
Browse files

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

* commit '707ca769':
  Handle Cloud Sync and SpellChecker settings when permission changed
parents 992144a8 707ca769
Loading
Loading
Loading
Loading
+28 −14
Original line number Diff line number Diff line
@@ -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);

        final String[] accountsForLogin = hasAccountsPermission ?
                LoginAccountUtils.getAccountsForLogin(getActivity()) : new String[0];
        final String currentAccount = hasAccountsPermission ? getSignedInAccountName() : null;

        if (!mManagedProfileBeingDetected.get() &&
        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 (!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.
        if (managedProfileBeingDetected) {
            mEnableSyncPreference.setSummary("");
            mAccountSwitcher.setSummary("");
        } else if (hasManagedProfile) {
+1 −1
Original line number Diff line number Diff line
@@ -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;

+11 −1
Original line number Diff line number Diff line
@@ -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);
    }
}
+52 −2
Original line number Diff line number Diff line
@@ -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);
        }
    }
}