Home | History | Annotate | Download | only in users
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.settings.users;
     17 
     18 import android.app.ActivityManager;
     19 import android.app.AlertDialog;
     20 import android.app.Dialog;
     21 import android.app.Fragment;
     22 import android.content.ContentResolver;
     23 import android.content.Context;
     24 import android.content.DialogInterface;
     25 import android.os.Bundle;
     26 import android.os.Process;
     27 import android.os.UserHandle;
     28 import android.os.UserManager;
     29 import android.support.v14.preference.SwitchPreference;
     30 import android.support.v7.preference.Preference;
     31 import android.util.Log;
     32 
     33 import com.android.internal.logging.nano.MetricsProto;
     34 import com.android.settings.R;
     35 import com.android.settings.core.PreferenceControllerMixin;
     36 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
     37 import com.android.settingslib.core.AbstractPreferenceController;
     38 
     39 public class AutoSyncDataPreferenceController extends AbstractPreferenceController
     40         implements PreferenceControllerMixin {
     41 
     42     private static final String TAG = "AutoSyncDataController";
     43     private static final String TAG_CONFIRM_AUTO_SYNC_CHANGE = "confirmAutoSyncChange";
     44     private static final String KEY_AUTO_SYNC_ACCOUNT = "auto_sync_account_data";
     45 
     46     protected final UserManager mUserManager;
     47     private final Fragment mParentFragment;
     48 
     49     protected UserHandle mUserHandle;
     50 
     51     public AutoSyncDataPreferenceController(Context context, Fragment parent) {
     52         super(context);
     53         mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
     54         mParentFragment = parent;
     55         mUserHandle = Process.myUserHandle();
     56     }
     57 
     58     @Override
     59     public void updateState(Preference preference) {
     60         SwitchPreference switchPreference = (SwitchPreference) preference;
     61         switchPreference.setChecked(ContentResolver.getMasterSyncAutomaticallyAsUser(
     62                 mUserHandle.getIdentifier()));
     63     }
     64 
     65     @Override
     66     public boolean handlePreferenceTreeClick(Preference preference) {
     67         if (getPreferenceKey().equals(preference.getKey())) {
     68             SwitchPreference switchPreference = (SwitchPreference) preference;
     69             boolean checked = switchPreference.isChecked();
     70             switchPreference.setChecked(!checked);
     71             if (ActivityManager.isUserAMonkey()) {
     72                 Log.d(TAG, "ignoring monkey's attempt to flip sync state");
     73             } else {
     74                 ConfirmAutoSyncChangeFragment.show(mParentFragment, checked, mUserHandle,
     75                         switchPreference);
     76             }
     77             return true;
     78         }
     79         return false;
     80     }
     81 
     82     @Override
     83     public boolean isAvailable() {
     84         return !mUserManager.isManagedProfile()
     85                 && (mUserManager.isRestrictedProfile()
     86                 || mUserManager.getProfiles(UserHandle.myUserId()).size() == 1);
     87     }
     88 
     89     @Override
     90     public String getPreferenceKey() {
     91         return KEY_AUTO_SYNC_ACCOUNT;
     92     }
     93 
     94     /**
     95      * Dialog to inform user about changing auto-sync setting
     96      */
     97     public static class ConfirmAutoSyncChangeFragment extends InstrumentedDialogFragment implements
     98             DialogInterface.OnClickListener {
     99         private static final String SAVE_ENABLING = "enabling";
    100         private static final String SAVE_USER_HANDLE = "userHandle";
    101         boolean mEnabling;
    102         UserHandle mUserHandle;
    103         SwitchPreference mPreference;
    104 
    105         public static void show(Fragment parent, boolean enabling, UserHandle userHandle,
    106                 SwitchPreference preference) {
    107             if (!parent.isAdded()) return;
    108 
    109             final ConfirmAutoSyncChangeFragment dialog = new ConfirmAutoSyncChangeFragment();
    110             dialog.mEnabling = enabling;
    111             dialog.mUserHandle = userHandle;
    112             dialog.setTargetFragment(parent, 0);
    113             dialog.mPreference = preference;
    114             dialog.show(parent.getFragmentManager(), TAG_CONFIRM_AUTO_SYNC_CHANGE);
    115         }
    116 
    117         @Override
    118         public Dialog onCreateDialog(Bundle savedInstanceState) {
    119             final Context context = getActivity();
    120             if (savedInstanceState != null) {
    121                 mEnabling = savedInstanceState.getBoolean(SAVE_ENABLING);
    122                 mUserHandle = (UserHandle) savedInstanceState.getParcelable(SAVE_USER_HANDLE);
    123             }
    124 
    125             final AlertDialog.Builder builder = new AlertDialog.Builder(context);
    126             if (!mEnabling) {
    127                 builder.setTitle(R.string.data_usage_auto_sync_off_dialog_title);
    128                 builder.setMessage(R.string.data_usage_auto_sync_off_dialog);
    129             } else {
    130                 builder.setTitle(R.string.data_usage_auto_sync_on_dialog_title);
    131                 builder.setMessage(R.string.data_usage_auto_sync_on_dialog);
    132             }
    133 
    134             builder.setPositiveButton(android.R.string.ok, this);
    135             builder.setNegativeButton(android.R.string.cancel, null);
    136 
    137             return builder.create();
    138         }
    139 
    140         @Override
    141         public void onSaveInstanceState(Bundle outState) {
    142             super.onSaveInstanceState(outState);
    143             outState.putBoolean(SAVE_ENABLING, mEnabling);
    144             outState.putParcelable(SAVE_USER_HANDLE, mUserHandle);
    145         }
    146 
    147         @Override
    148         public int getMetricsCategory() {
    149             return MetricsProto.MetricsEvent.DIALOG_CONFIRM_AUTO_SYNC_CHANGE;
    150         }
    151 
    152         @Override
    153         public void onClick(DialogInterface dialog, int which) {
    154             if (which == DialogInterface.BUTTON_POSITIVE) {
    155                 ContentResolver.setMasterSyncAutomaticallyAsUser(mEnabling,
    156                         mUserHandle.getIdentifier());
    157                 if (mPreference != null) {
    158                     mPreference.setChecked(mEnabling);
    159                 }
    160             }
    161         }
    162     }
    163 
    164 }
    165