2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.android.settings.accounts;
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;
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;
38 public class AutoSyncDataPreferenceController extends PreferenceController {
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";
44 protected final UserManager mUserManager;
45 private final Fragment mParentFragment;
47 protected UserHandle mUserHandle;
49 public AutoSyncDataPreferenceController(Context context, Fragment parent) {
51 mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
52 mParentFragment = parent;
53 mUserHandle = Process.myUserHandle();
57 public void updateState(Preference preference) {
58 SwitchPreference switchPreference = (SwitchPreference) preference;
59 switchPreference.setChecked(ContentResolver.getMasterSyncAutomaticallyAsUser(
60 mUserHandle.getIdentifier()));
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");
72 ConfirmAutoSyncChangeFragment.show(mParentFragment, checked, mUserHandle,
81 public boolean isAvailable() {
82 return !mUserManager.isManagedProfile()
83 && (mUserManager.isLinkedUser()
84 || mUserManager.getProfiles(UserHandle.myUserId()).size() == 1);
88 public String getPreferenceKey() {
89 return KEY_AUTO_SYNC_ACCOUNT;
93 * Dialog to inform user about changing auto-sync setting
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";
100 UserHandle mUserHandle;
101 SwitchPreference mPreference;
103 public static void show(Fragment parent, boolean enabling, UserHandle userHandle,
104 SwitchPreference preference) {
105 if (!parent.isAdded()) return;
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);
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);
123 final AlertDialog.Builder builder = new AlertDialog.Builder(context);
125 builder.setTitle(R.string.data_usage_auto_sync_off_dialog_title);
126 builder.setMessage(R.string.data_usage_auto_sync_off_dialog);
128 builder.setTitle(R.string.data_usage_auto_sync_on_dialog_title);
129 builder.setMessage(R.string.data_usage_auto_sync_on_dialog);
132 builder.setPositiveButton(android.R.string.ok, this);
133 builder.setNegativeButton(android.R.string.cancel, null);
135 return builder.create();
139 public void onSaveInstanceState(Bundle outState) {
140 super.onSaveInstanceState(outState);
141 outState.putBoolean(SAVE_ENABLING, mEnabling);
142 outState.putParcelable(SAVE_USER_HANDLE, mUserHandle);
146 public int getMetricsCategory() {
147 return MetricsProto.MetricsEvent.DIALOG_CONFIRM_AUTO_SYNC_CHANGE;
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);