1 /* 2 * Copyright (C) 2018 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.car.settings.accounts; 17 18 import android.car.drivingstate.CarUxRestrictions; 19 import android.car.userlib.CarUserManagerHelper; 20 import android.content.ContentResolver; 21 import android.content.Context; 22 import android.os.UserHandle; 23 24 import androidx.annotation.VisibleForTesting; 25 import androidx.preference.TwoStatePreference; 26 27 import com.android.car.settings.R; 28 import com.android.car.settings.common.ConfirmationDialogFragment; 29 import com.android.car.settings.common.FragmentController; 30 import com.android.car.settings.common.PreferenceController; 31 32 /** 33 * Controller for the preference that allows the user to toggle automatic syncing of accounts. 34 * 35 * <p>Copied from {@link com.android.settings.users.AutoSyncDataPreferenceController} 36 */ 37 public class AccountAutoSyncPreferenceController extends PreferenceController<TwoStatePreference> { 38 39 private final UserHandle mUserHandle; 40 /** 41 * Argument key to store a value that indicates whether the account auto sync is being enabled 42 * or disabled. 43 */ 44 @VisibleForTesting 45 static final String KEY_ENABLING = "ENABLING"; 46 /** Argument key to store user handle. */ 47 @VisibleForTesting 48 static final String KEY_USER_HANDLE = "USER_HANDLE"; 49 50 @VisibleForTesting 51 final ConfirmationDialogFragment.ConfirmListener mConfirmListener = arguments -> { 52 boolean enabling = arguments.getBoolean(KEY_ENABLING); 53 UserHandle userHandle = arguments.getParcelable(KEY_USER_HANDLE); 54 ContentResolver.setMasterSyncAutomaticallyAsUser(enabling, userHandle.getIdentifier()); 55 getPreference().setChecked(enabling); 56 }; 57 AccountAutoSyncPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)58 public AccountAutoSyncPreferenceController(Context context, String preferenceKey, 59 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 60 super(context, preferenceKey, fragmentController, uxRestrictions); 61 CarUserManagerHelper carUserManagerHelper = new CarUserManagerHelper(context); 62 mUserHandle = carUserManagerHelper.getCurrentProcessUserInfo().getUserHandle(); 63 } 64 65 @Override getPreferenceType()66 protected Class<TwoStatePreference> getPreferenceType() { 67 return TwoStatePreference.class; 68 } 69 70 @Override updateState(TwoStatePreference preference)71 protected void updateState(TwoStatePreference preference) { 72 preference.setChecked(ContentResolver.getMasterSyncAutomaticallyAsUser( 73 mUserHandle.getIdentifier())); 74 } 75 76 @Override onCreateInternal()77 protected void onCreateInternal() { 78 // If the dialog is still up, reattach the preference 79 ConfirmationDialogFragment dialog = 80 (ConfirmationDialogFragment) getFragmentController().findDialogByTag( 81 ConfirmationDialogFragment.TAG); 82 83 ConfirmationDialogFragment.resetListeners(dialog, mConfirmListener, /* rejectListener= */ 84 null); 85 } 86 87 @Override handlePreferenceChanged(TwoStatePreference preference, Object checked)88 protected boolean handlePreferenceChanged(TwoStatePreference preference, Object checked) { 89 getFragmentController().showDialog( 90 getAutoSyncChangeConfirmationDialogFragment((boolean) checked), 91 ConfirmationDialogFragment.TAG); 92 // The dialog will change the state of the preference if the user confirms, so don't handle 93 // it here 94 return false; 95 } 96 getAutoSyncChangeConfirmationDialogFragment( boolean enabling)97 private ConfirmationDialogFragment getAutoSyncChangeConfirmationDialogFragment( 98 boolean enabling) { 99 int dialogTitle; 100 int dialogMessage; 101 102 if (enabling) { 103 dialogTitle = R.string.data_usage_auto_sync_on_dialog_title; 104 dialogMessage = R.string.data_usage_auto_sync_on_dialog; 105 } else { 106 dialogTitle = R.string.data_usage_auto_sync_off_dialog_title; 107 dialogMessage = R.string.data_usage_auto_sync_off_dialog; 108 } 109 110 ConfirmationDialogFragment dialogFragment = 111 new ConfirmationDialogFragment.Builder(getContext()) 112 .setTitle(dialogTitle).setMessage(dialogMessage) 113 .setPositiveButton(android.R.string.ok, mConfirmListener) 114 .setNegativeButton(android.R.string.cancel, /* rejectListener= */ null) 115 .addArgumentBoolean(KEY_ENABLING, enabling) 116 .addArgumentParcelable(KEY_USER_HANDLE, mUserHandle) 117 .build(); 118 119 return dialogFragment; 120 } 121 }