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

Commit 854edce6 authored by Jatin Matani's avatar Jatin Matani
Browse files

Turn off cloud sync if we have managed profiles

UserManager#getUserProfiles > 1 implies managed
profile.

Workflow:
- Disable sync prefs
- Execute an async task to check for managed profile; if
  managed profile is found, remove the sync prefs. If not, enable
  the preference;
- Move the logging pref from Advanced to Account & Privacy.

Bug:19230544
Change-Id: I4dbd1fe8433b3263ccc74c35dc0ee0bb371122b3
parent c83352d5
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -48,4 +48,11 @@
        android:title="@string/clear_sync_data_title"
        android:summary="@string/clear_sync_data_summary"
        android:dependency="pref_enable_cloud_sync" />

    <!-- Title will be set programmatically to embed application name -->
    <CheckBoxPreference
        android:key="pref_enable_metrics_logging"
        android:summary="@string/enable_metrics_logging_summary"
        android:defaultValue="true"
        android:persistent="true" />
</PreferenceScreen>
+0 −6
Original line number Diff line number Diff line
@@ -43,12 +43,6 @@
        android:summary="@string/prefs_enable_emoji_alt_physical_key_summary"
        android:defaultValue="true"
        android:persistent="true" />
    <!-- title will be set programmatically to embed application name -->
    <CheckBoxPreference
        android:key="pref_enable_metrics_logging"
        android:summary="@string/enable_metrics_logging_summary"
        android:defaultValue="true"
        android:persistent="true" />
    <PreferenceScreen
        android:fragment="com.android.inputmethod.latin.settings.DebugSettingsFragment"
        android:key="screen_debug"
+85 −32
Original line number Diff line number Diff line
@@ -23,10 +23,14 @@ import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.SwitchPreference;
import android.preference.TwoStatePreference;
import android.text.TextUtils;
import android.widget.ListView;
import android.widget.Toast;
@@ -36,6 +40,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.utils.ManagedProfileUtils;

import javax.annotation.Nullable;

@@ -74,7 +79,7 @@ public final class AccountsSettingsFragment extends SubScreenFragment {
    /**
     * Enable sync checkbox pref.
     */
    private CheckBoxPreference mEnableSyncPreference;
    private TwoStatePreference mEnableSyncPreference;

    /**
     * Enable sync checkbox pref.
@@ -86,32 +91,86 @@ public final class AccountsSettingsFragment extends SubScreenFragment {
     */
    private Preference mClearSyncDataPreference;

    /**
     * Account switcher preference.
     */
    private Preference mAccountSwitcher;


    @Override
    public void onCreate(final Bundle icicle) {
        super.onCreate(icicle);
        addPreferencesFromResource(R.xml.prefs_screen_accounts);

        if (!ProductionFlags.ENABLE_ACCOUNT_SIGN_IN) {
            removePreference(PREF_ACCCOUNT_SWITCHER);
            removePreference(PREF_ENABLE_CLOUD_SYNC);
            removePreference(PREF_SYNC_NOW);
            removePreference(PREF_CLEAR_SYNC_DATA);
        if (ProductionFlags.IS_METRICS_LOGGING_SUPPORTED) {
            final Preference enableMetricsLogging =
                    findPreference(Settings.PREF_ENABLE_METRICS_LOGGING);
            final Resources res = getResources();
            if (enableMetricsLogging != null) {
                final String enableMetricsLoggingTitle = res.getString(
                        R.string.enable_metrics_logging, getApplicationName());
                enableMetricsLogging.setTitle(enableMetricsLoggingTitle);
            }
        } else {
            removePreference(Settings.PREF_ENABLE_METRICS_LOGGING);
        }

        if (!ProductionFlags.ENABLE_USER_HISTORY_DICTIONARY_SYNC) {
            removePreference(PREF_ENABLE_CLOUD_SYNC);
            removePreference(PREF_SYNC_NOW);
            removePreference(PREF_CLEAR_SYNC_DATA);
            removeSyncPreferences();
        } else {
            mEnableSyncPreference = (CheckBoxPreference) findPreference(PREF_ENABLE_SYNC_NOW);
            disableSyncPreferences();
            final AsyncTask<Void, Void, Void> checkManagedProfileTask =
                    new AsyncTask<Void, Void, Void>() {
                        @Override
                        protected Void doInBackground(Void... params) {
                            if (ManagedProfileUtils.hasManagedWorkProfile(getActivity())) {
                                removeSyncPreferences();
                            } else {
                                enableSyncPreferences();
                            }
                            return null;
                        }
                    };
            checkManagedProfileTask.execute();
        }
    }

    private void enableSyncPreferences() {
        mAccountSwitcher = findPreference(PREF_ACCCOUNT_SWITCHER);
        mAccountSwitcher.setEnabled(true);

        mEnableSyncPreference = (TwoStatePreference) findPreference(PREF_ENABLE_SYNC_NOW);
        mEnableSyncPreference.setEnabled(true);
        mEnableSyncPreference.setOnPreferenceClickListener(mEnableSyncClickListener);

        mSyncNowPreference = findPreference(PREF_SYNC_NOW);
        mSyncNowPreference.setEnabled(true);
        mSyncNowPreference.setOnPreferenceClickListener(mSyncNowListener);

        mClearSyncDataPreference = findPreference(PREF_CLEAR_SYNC_DATA);
        mSyncNowPreference.setEnabled(true);
        mClearSyncDataPreference.setOnPreferenceClickListener(mDeleteSyncDataListener);
    }

    private void disableSyncPreferences() {
        mAccountSwitcher = findPreference(PREF_ACCCOUNT_SWITCHER);
        mAccountSwitcher.setEnabled(false);

        mEnableSyncPreference = (TwoStatePreference) findPreference(PREF_ENABLE_SYNC_NOW);
        mEnableSyncPreference.setEnabled(false);

        mSyncNowPreference = findPreference(PREF_SYNC_NOW);
        mSyncNowPreference.setEnabled(false);

        mClearSyncDataPreference = findPreference(PREF_CLEAR_SYNC_DATA);
        mSyncNowPreference.setEnabled(false);
    }

    private void removeSyncPreferences() {
        removePreference(PREF_ACCCOUNT_SWITCHER);
        removePreference(PREF_ENABLE_CLOUD_SYNC);
        removePreference(PREF_SYNC_NOW);
        removePreference(PREF_CLEAR_SYNC_DATA);
    }

    @Override
@@ -126,8 +185,7 @@ public final class AccountsSettingsFragment extends SubScreenFragment {
            refreshAccountAndDependentPreferences(prefs.getString(PREF_ACCOUNT_NAME, null));
        } else if (TextUtils.equals(key, PREF_ENABLE_CLOUD_SYNC)) {
            final boolean syncEnabled = prefs.getBoolean(PREF_ENABLE_CLOUD_SYNC, false);
            mEnableSyncPreference = (CheckBoxPreference)findPreference(PREF_ENABLE_SYNC_NOW);
            mEnableSyncPreference.setChecked(syncEnabled);
            mEnableSyncPreference = (TwoStatePreference) findPreference(PREF_ENABLE_SYNC_NOW);
            if (syncEnabled) {
                mEnableSyncPreference.setSummary(R.string.cloud_sync_summary);
            } else {
@@ -147,27 +205,22 @@ public final class AccountsSettingsFragment extends SubScreenFragment {
            return;
        }

        final Preference accountSwitcher = findPreference(PREF_ACCCOUNT_SWITCHER);
        if (currentAccount == null) {
            // No account is currently selected; switch enable sync preference off.
            accountSwitcher.setSummary(getString(R.string.no_accounts_selected));
            mAccountSwitcher.setSummary(getString(R.string.no_accounts_selected));
            mEnableSyncPreference.setChecked(false);
        } else {
            // Set the currently selected account as the summary text.
            accountSwitcher.setSummary(getString(R.string.account_selected, currentAccount));
            mAccountSwitcher.setSummary(getString(R.string.account_selected, currentAccount));
        }

        // Set up on click listener for the account picker preference.
        accountSwitcher.setOnPreferenceClickListener(new OnPreferenceClickListener() {
        mAccountSwitcher.setOnPreferenceClickListener(new OnPreferenceClickListener() {
                @Override
                public boolean onPreferenceClick(final Preference preference) {
                    final String[] accountsForLogin =
                            LoginAccountUtils.getAccountsForLogin(getActivity());
                    if (accountsForLogin.length == 0) {
                        // TODO: Handle account addition.
                        Toast.makeText(getActivity(), getString(R.string.account_select_cancel),
                                Toast.LENGTH_SHORT).show();
                    } else {
                    if (accountsForLogin.length > 0) {
                        // TODO: Add addition of account.
                        createAccountPicker(accountsForLogin, currentAccount,
                                new AccountChangedListener(null)).show();
                    }
@@ -236,9 +289,9 @@ public final class AccountsSettingsFragment extends SubScreenFragment {
        /**
         * Represents preference that should be changed based on account chosen.
         */
        private CheckBoxPreference mDependentPreference;
        private TwoStatePreference mDependentPreference;

        AccountChangedListener(final CheckBoxPreference dependentPreference) {
        AccountChangedListener(final TwoStatePreference dependentPreference) {
            mDependentPreference = dependentPreference;
        }

@@ -300,7 +353,7 @@ public final class AccountsSettingsFragment extends SubScreenFragment {
                                    }
                                }
                             })
                    .setNegativeButton(R.string.clear_sync_data_cancel, null /* OnClickListener */)
                    .setNegativeButton(R.string.cloud_sync_cancel, null /* OnClickListener */)
                    .create();
            confirmationDialog.show();
            return true;
@@ -313,7 +366,7 @@ public final class AccountsSettingsFragment extends SubScreenFragment {
    class EnableSyncClickListener implements Preference.OnPreferenceClickListener {
        @Override
        public boolean onPreferenceClick(final Preference preference) {
            final CheckBoxPreference syncPreference = (CheckBoxPreference) preference;
            final TwoStatePreference syncPreference = (TwoStatePreference) preference;
            if (syncPreference.isChecked()) {
                // Uncheck for now.
                syncPreference.setChecked(false);
+0 −16
Original line number Diff line number Diff line
@@ -87,22 +87,6 @@ public final class AdvancedSettingsFragment extends SubScreenFragment {
                    Settings.readKeyPreviewPopupEnabled(prefs, res));
        }

        // If metrics logging isn't supported, or account sign in is enabled
        // don't show the logging preference.
        // TODO: Eventually when we enable account sign in by default,
        // we'll remove logging preference from here.
        if (ProductionFlags.IS_METRICS_LOGGING_SUPPORTED) {
            final Preference enableMetricsLogging =
                    findPreference(Settings.PREF_ENABLE_METRICS_LOGGING);
            if (enableMetricsLogging != null) {
                final String enableMetricsLoggingTitle = res.getString(
                        R.string.enable_metrics_logging, getApplicationName());
                enableMetricsLogging.setTitle(enableMetricsLoggingTitle);
            }
        } else {
            removePreference(Settings.PREF_ENABLE_METRICS_LOGGING);
        }

        setupKeypressVibrationDurationSettings();
        setupKeypressSoundVolumeSettings();
        setupKeyLongpressTimeoutSettings();
+68 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.inputmethod.latin.utils;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.os.UserHandle;
import android.os.UserManager;
import android.util.Log;

import java.util.List;

/**
 * Utility for determining if the device has managed profiles.
 */
public class ManagedProfileUtils {
    private static final boolean DEBUG = false;
    private static final String TAG = ManagedProfileUtils.class.getSimpleName();

    private ManagedProfileUtils() {
        // This utility class is not publicly instantiable.
    }

    /**
     * Note that {@link UserManager#getUserProfiles} has been introduced
     * in API level 21 (Build.VERSION_CODES.LOLLIPOP).
     */
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public static boolean hasManagedWorkProfile(final Context context) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            return false;
        }

        final UserManager userManagerService =
                (UserManager) context.getSystemService(Context.USER_SERVICE);
        if (userManagerService != null) {
            if (DEBUG) {
                Log.d(TAG, "Detecting managed profile...");
            }
            final List<UserHandle> userProfiles = userManagerService.getUserProfiles();
            if (userProfiles.size() > 1) {
                if (DEBUG) {
                    Log.d(TAG, "More than one user profile => Managed profile exists.");
                }
                return true;
            }
        }
        if (DEBUG) {
            Log.d(TAG, "Managed profile not detected.");
        }
        return false;
    }
}