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