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.Dialog; 20 import android.app.settings.SettingsEnums; 21 import android.content.ContentResolver; 22 import android.content.Context; 23 import android.content.DialogInterface; 24 import android.os.Bundle; 25 import android.os.Process; 26 import android.os.UserHandle; 27 import android.os.UserManager; 28 import android.util.Log; 29 30 import androidx.appcompat.app.AlertDialog; 31 import androidx.fragment.app.Fragment; 32 import androidx.preference.Preference; 33 import androidx.preference.SwitchPreference; 34 35 import com.android.settings.R; 36 import com.android.settings.core.PreferenceControllerMixin; 37 import com.android.settings.core.instrumentation.InstrumentedDialogFragment; 38 import com.android.settingslib.core.AbstractPreferenceController; 39 40 public class AutoSyncDataPreferenceController extends AbstractPreferenceController 41 implements PreferenceControllerMixin { 42 43 private static final String TAG = "AutoSyncDataController"; 44 private static final String TAG_CONFIRM_AUTO_SYNC_CHANGE = "confirmAutoSyncChange"; 45 private static final String KEY_AUTO_SYNC_ACCOUNT = "auto_sync_account_data"; 46 47 protected final UserManager mUserManager; 48 private final Fragment mParentFragment; 49 50 protected UserHandle mUserHandle; 51 AutoSyncDataPreferenceController(Context context, Fragment parent)52 public AutoSyncDataPreferenceController(Context context, Fragment parent) { 53 super(context); 54 mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE); 55 mParentFragment = parent; 56 mUserHandle = Process.myUserHandle(); 57 } 58 59 @Override updateState(Preference preference)60 public void updateState(Preference preference) { 61 SwitchPreference switchPreference = (SwitchPreference) preference; 62 switchPreference.setChecked(ContentResolver.getMasterSyncAutomaticallyAsUser( 63 mUserHandle.getIdentifier())); 64 } 65 66 @Override handlePreferenceTreeClick(Preference preference)67 public boolean handlePreferenceTreeClick(Preference preference) { 68 if (getPreferenceKey().equals(preference.getKey())) { 69 SwitchPreference switchPreference = (SwitchPreference) preference; 70 boolean checked = switchPreference.isChecked(); 71 switchPreference.setChecked(!checked); 72 if (ActivityManager.isUserAMonkey()) { 73 Log.d(TAG, "ignoring monkey's attempt to flip sync state"); 74 } else { 75 ConfirmAutoSyncChangeFragment.show(mParentFragment, checked, mUserHandle, 76 switchPreference); 77 } 78 return true; 79 } 80 return false; 81 } 82 83 @Override isAvailable()84 public boolean isAvailable() { 85 return !mUserManager.isManagedProfile() 86 && (mUserManager.isRestrictedProfile() 87 || mUserManager.getProfiles(UserHandle.myUserId()).size() == 1); 88 } 89 90 @Override getPreferenceKey()91 public String getPreferenceKey() { 92 return KEY_AUTO_SYNC_ACCOUNT; 93 } 94 95 /** 96 * Dialog to inform user about changing auto-sync setting 97 */ 98 public static class ConfirmAutoSyncChangeFragment extends InstrumentedDialogFragment implements 99 DialogInterface.OnClickListener { 100 private static final String SAVE_ENABLING = "enabling"; 101 private static final String SAVE_USER_HANDLE = "userHandle"; 102 boolean mEnabling; 103 UserHandle mUserHandle; 104 SwitchPreference mPreference; 105 show(Fragment parent, boolean enabling, UserHandle userHandle, SwitchPreference preference)106 public static void show(Fragment parent, boolean enabling, UserHandle userHandle, 107 SwitchPreference preference) { 108 if (!parent.isAdded()) return; 109 110 final ConfirmAutoSyncChangeFragment dialog = new ConfirmAutoSyncChangeFragment(); 111 dialog.mEnabling = enabling; 112 dialog.mUserHandle = userHandle; 113 dialog.setTargetFragment(parent, 0); 114 dialog.mPreference = preference; 115 dialog.show(parent.getFragmentManager(), TAG_CONFIRM_AUTO_SYNC_CHANGE); 116 } 117 118 @Override onCreateDialog(Bundle savedInstanceState)119 public Dialog onCreateDialog(Bundle savedInstanceState) { 120 final Context context = getActivity(); 121 if (savedInstanceState != null) { 122 mEnabling = savedInstanceState.getBoolean(SAVE_ENABLING); 123 mUserHandle = (UserHandle) savedInstanceState.getParcelable(SAVE_USER_HANDLE); 124 } 125 126 final AlertDialog.Builder builder = new AlertDialog.Builder(context); 127 if (!mEnabling) { 128 builder.setTitle(R.string.data_usage_auto_sync_off_dialog_title); 129 builder.setMessage(R.string.data_usage_auto_sync_off_dialog); 130 } else { 131 builder.setTitle(R.string.data_usage_auto_sync_on_dialog_title); 132 builder.setMessage(R.string.data_usage_auto_sync_on_dialog); 133 } 134 135 builder.setPositiveButton(android.R.string.ok, this); 136 builder.setNegativeButton(android.R.string.cancel, null); 137 138 return builder.create(); 139 } 140 141 @Override onSaveInstanceState(Bundle outState)142 public void onSaveInstanceState(Bundle outState) { 143 super.onSaveInstanceState(outState); 144 outState.putBoolean(SAVE_ENABLING, mEnabling); 145 outState.putParcelable(SAVE_USER_HANDLE, mUserHandle); 146 } 147 148 @Override getMetricsCategory()149 public int getMetricsCategory() { 150 return SettingsEnums.DIALOG_CONFIRM_AUTO_SYNC_CHANGE; 151 } 152 153 @Override onClick(DialogInterface dialog, int which)154 public void onClick(DialogInterface dialog, int which) { 155 if (which == DialogInterface.BUTTON_POSITIVE) { 156 ContentResolver.setMasterSyncAutomaticallyAsUser(mEnabling, 157 mUserHandle.getIdentifier()); 158 if (mPreference != null) { 159 mPreference.setChecked(mEnabling); 160 } 161 } 162 } 163 } 164 165 } 166