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