Doris Ling | fd06d2f | 2016-12-29 14:41:15 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.accounts.Account; |
| 19 | import android.accounts.AccountManager; |
Doris Ling | fd06d2f | 2016-12-29 14:41:15 -0800 | [diff] [blame] | 20 | import android.accounts.AuthenticatorException; |
| 21 | import android.accounts.OperationCanceledException; |
| 22 | import android.app.Activity; |
Doris Ling | fd06d2f | 2016-12-29 14:41:15 -0800 | [diff] [blame] | 23 | import android.app.Dialog; |
Fan Zhang | 31b2100 | 2019-01-16 13:49:47 -0800 | [diff] [blame] | 24 | import android.app.settings.SettingsEnums; |
Doris Ling | fd06d2f | 2016-12-29 14:41:15 -0800 | [diff] [blame] | 25 | import android.content.Context; |
| 26 | import android.content.DialogInterface; |
Tony Mak | ef4c839 | 2017-03-07 13:37:38 +0000 | [diff] [blame] | 27 | import android.content.Intent; |
Doris Ling | fd06d2f | 2016-12-29 14:41:15 -0800 | [diff] [blame] | 28 | import android.os.Bundle; |
Tony Mak | ef4c839 | 2017-03-07 13:37:38 +0000 | [diff] [blame] | 29 | import android.os.UserHandle; |
phweiss | 7a34e9c | 2017-06-02 19:38:50 +0200 | [diff] [blame] | 30 | import android.os.UserManager; |
Fan Zhang | e3ecf5b | 2019-04-23 15:09:39 -0700 | [diff] [blame] | 31 | import android.util.Log; |
Doris Ling | fd06d2f | 2016-12-29 14:41:15 -0800 | [diff] [blame] | 32 | import android.view.View; |
| 33 | import android.view.View.OnClickListener; |
| 34 | import android.widget.Button; |
| 35 | |
Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 36 | import androidx.appcompat.app.AlertDialog; |
| 37 | import androidx.fragment.app.Fragment; |
| 38 | import androidx.preference.PreferenceScreen; |
| 39 | |
Doris Ling | fd06d2f | 2016-12-29 14:41:15 -0800 | [diff] [blame] | 40 | import com.android.settings.R; |
Tony Mantler | 1d583e1 | 2017-06-13 13:09:25 -0700 | [diff] [blame] | 41 | import com.android.settings.core.PreferenceControllerMixin; |
Doris Ling | fd06d2f | 2016-12-29 14:41:15 -0800 | [diff] [blame] | 42 | import com.android.settings.core.instrumentation.InstrumentedDialogFragment; |
yuemingw | 5f0f6b9 | 2018-03-12 16:38:50 +0000 | [diff] [blame] | 43 | import com.android.settingslib.RestrictedLockUtils; |
| 44 | import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; |
Philip P. Moltmann | e3f7211 | 2018-08-28 15:01:43 -0700 | [diff] [blame] | 45 | import com.android.settingslib.RestrictedLockUtilsInternal; |
Tony Mantler | 1d583e1 | 2017-06-13 13:09:25 -0700 | [diff] [blame] | 46 | import com.android.settingslib.core.AbstractPreferenceController; |
tmfang | dce94bb | 2018-11-26 18:41:01 +0800 | [diff] [blame] | 47 | import com.android.settingslib.widget.LayoutPreference; |
Doris Ling | fd06d2f | 2016-12-29 14:41:15 -0800 | [diff] [blame] | 48 | |
| 49 | import java.io.IOException; |
| 50 | |
Tony Mantler | 1d583e1 | 2017-06-13 13:09:25 -0700 | [diff] [blame] | 51 | public class RemoveAccountPreferenceController extends AbstractPreferenceController |
| 52 | implements PreferenceControllerMixin, OnClickListener { |
Doris Ling | fd06d2f | 2016-12-29 14:41:15 -0800 | [diff] [blame] | 53 | |
| 54 | private static final String KEY_REMOVE_ACCOUNT = "remove_account"; |
| 55 | |
| 56 | private Account mAccount; |
| 57 | private Fragment mParentFragment; |
Tony Mak | ef4c839 | 2017-03-07 13:37:38 +0000 | [diff] [blame] | 58 | private UserHandle mUserHandle; |
Doris Ling | fd06d2f | 2016-12-29 14:41:15 -0800 | [diff] [blame] | 59 | |
| 60 | public RemoveAccountPreferenceController(Context context, Fragment parent) { |
| 61 | super(context); |
| 62 | mParentFragment = parent; |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | public void displayPreference(PreferenceScreen screen) { |
| 67 | super.displayPreference(screen); |
Fan Zhang | 7db118e | 2019-02-14 12:25:29 -0800 | [diff] [blame] | 68 | final LayoutPreference removeAccountPreference = screen.findPreference(KEY_REMOVE_ACCOUNT); |
Doris Ling | fd06d2f | 2016-12-29 14:41:15 -0800 | [diff] [blame] | 69 | Button removeAccountButton = (Button) removeAccountPreference.findViewById(R.id.button); |
| 70 | removeAccountButton.setOnClickListener(this); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public boolean isAvailable() { |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public String getPreferenceKey() { |
| 80 | return KEY_REMOVE_ACCOUNT; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public void onClick(View v) { |
yuemingw | 5f0f6b9 | 2018-03-12 16:38:50 +0000 | [diff] [blame] | 85 | if (mUserHandle != null) { |
Philip P. Moltmann | e3f7211 | 2018-08-28 15:01:43 -0700 | [diff] [blame] | 86 | final EnforcedAdmin admin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced( |
| 87 | mContext, UserManager.DISALLOW_MODIFY_ACCOUNTS, mUserHandle.getIdentifier()); |
yuemingw | 5f0f6b9 | 2018-03-12 16:38:50 +0000 | [diff] [blame] | 88 | if (admin != null) { |
| 89 | RestrictedLockUtils.sendShowAdminSupportDetailsIntent(mContext, admin); |
| 90 | return; |
| 91 | } |
phweiss | 7a34e9c | 2017-06-02 19:38:50 +0200 | [diff] [blame] | 92 | } |
yuemingw | 5f0f6b9 | 2018-03-12 16:38:50 +0000 | [diff] [blame] | 93 | |
Tony Mak | ef4c839 | 2017-03-07 13:37:38 +0000 | [diff] [blame] | 94 | ConfirmRemoveAccountDialog.show(mParentFragment, mAccount, mUserHandle); |
Doris Ling | fd06d2f | 2016-12-29 14:41:15 -0800 | [diff] [blame] | 95 | } |
| 96 | |
Tony Mak | ef4c839 | 2017-03-07 13:37:38 +0000 | [diff] [blame] | 97 | public void init(Account account, UserHandle userHandle) { |
Doris Ling | fd06d2f | 2016-12-29 14:41:15 -0800 | [diff] [blame] | 98 | mAccount = account; |
Tony Mak | ef4c839 | 2017-03-07 13:37:38 +0000 | [diff] [blame] | 99 | mUserHandle = userHandle; |
Doris Ling | fd06d2f | 2016-12-29 14:41:15 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Dialog to confirm with user about account removal |
| 104 | */ |
| 105 | public static class ConfirmRemoveAccountDialog extends InstrumentedDialogFragment implements |
| 106 | DialogInterface.OnClickListener { |
Tony Mak | ef4c839 | 2017-03-07 13:37:38 +0000 | [diff] [blame] | 107 | private static final String KEY_ACCOUNT = "account"; |
Doris Ling | fd06d2f | 2016-12-29 14:41:15 -0800 | [diff] [blame] | 108 | private static final String REMOVE_ACCOUNT_DIALOG = "confirmRemoveAccount"; |
| 109 | private Account mAccount; |
Tony Mak | ef4c839 | 2017-03-07 13:37:38 +0000 | [diff] [blame] | 110 | private UserHandle mUserHandle; |
Doris Ling | fd06d2f | 2016-12-29 14:41:15 -0800 | [diff] [blame] | 111 | |
Tony Mak | ef4c839 | 2017-03-07 13:37:38 +0000 | [diff] [blame] | 112 | public static ConfirmRemoveAccountDialog show( |
| 113 | Fragment parent, Account account, UserHandle userHandle) { |
Doris Ling | fd06d2f | 2016-12-29 14:41:15 -0800 | [diff] [blame] | 114 | if (!parent.isAdded()) { |
| 115 | return null; |
| 116 | } |
| 117 | final ConfirmRemoveAccountDialog dialog = new ConfirmRemoveAccountDialog(); |
Tony Mak | ef4c839 | 2017-03-07 13:37:38 +0000 | [diff] [blame] | 118 | Bundle bundle = new Bundle(); |
| 119 | bundle.putParcelable(KEY_ACCOUNT, account); |
| 120 | bundle.putParcelable(Intent.EXTRA_USER, userHandle); |
| 121 | dialog.setArguments(bundle); |
Doris Ling | fd06d2f | 2016-12-29 14:41:15 -0800 | [diff] [blame] | 122 | dialog.setTargetFragment(parent, 0); |
| 123 | dialog.show(parent.getFragmentManager(), REMOVE_ACCOUNT_DIALOG); |
| 124 | return dialog; |
| 125 | } |
| 126 | |
| 127 | @Override |
Tony Mak | ef4c839 | 2017-03-07 13:37:38 +0000 | [diff] [blame] | 128 | public void onCreate(Bundle savedInstanceState) { |
| 129 | super.onCreate(savedInstanceState); |
| 130 | final Bundle arguments = getArguments(); |
| 131 | mAccount = arguments.getParcelable(KEY_ACCOUNT); |
| 132 | mUserHandle = arguments.getParcelable(Intent.EXTRA_USER); |
| 133 | } |
| 134 | |
| 135 | @Override |
Doris Ling | fd06d2f | 2016-12-29 14:41:15 -0800 | [diff] [blame] | 136 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 137 | final Context context = getActivity(); |
Doris Ling | fd06d2f | 2016-12-29 14:41:15 -0800 | [diff] [blame] | 138 | return new AlertDialog.Builder(context) |
Fan Zhang | aab36de | 2018-03-30 16:58:28 -0700 | [diff] [blame] | 139 | .setTitle(R.string.really_remove_account_title) |
| 140 | .setMessage(R.string.really_remove_account_message) |
| 141 | .setNegativeButton(android.R.string.cancel, null) |
| 142 | .setPositiveButton(R.string.remove_account_label, this) |
| 143 | .create(); |
Doris Ling | fd06d2f | 2016-12-29 14:41:15 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | @Override |
Doris Ling | fd06d2f | 2016-12-29 14:41:15 -0800 | [diff] [blame] | 147 | public int getMetricsCategory() { |
Fan Zhang | 31b2100 | 2019-01-16 13:49:47 -0800 | [diff] [blame] | 148 | return SettingsEnums.DIALOG_ACCOUNT_SYNC_REMOVE; |
Doris Ling | fd06d2f | 2016-12-29 14:41:15 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public void onClick(DialogInterface dialog, int which) { |
| 153 | Activity activity = getTargetFragment().getActivity(); |
| 154 | AccountManager.get(activity).removeAccountAsUser(mAccount, activity, |
Fan Zhang | e3ecf5b | 2019-04-23 15:09:39 -0700 | [diff] [blame] | 155 | future -> { |
| 156 | final Activity targetActivity = getTargetFragment().getActivity(); |
| 157 | if (targetActivity == null || targetActivity.isFinishing()) { |
| 158 | Log.w(TAG, "Activity is no longer alive, skipping results"); |
| 159 | return; |
| 160 | } |
| 161 | boolean failed = true; |
| 162 | try { |
| 163 | if (future.getResult() |
| 164 | .getBoolean(AccountManager.KEY_BOOLEAN_RESULT)) { |
| 165 | failed = false; |
Doris Ling | fd06d2f | 2016-12-29 14:41:15 -0800 | [diff] [blame] | 166 | } |
Fan Zhang | e3ecf5b | 2019-04-23 15:09:39 -0700 | [diff] [blame] | 167 | } catch (OperationCanceledException |
| 168 | | IOException |
| 169 | | AuthenticatorException e) { |
| 170 | // handled below |
| 171 | } |
| 172 | if (failed) { |
| 173 | RemoveAccountFailureDialog.show(getTargetFragment()); |
| 174 | } else { |
| 175 | targetActivity.finish(); |
Doris Ling | fd06d2f | 2016-12-29 14:41:15 -0800 | [diff] [blame] | 176 | } |
Tony Mak | ef4c839 | 2017-03-07 13:37:38 +0000 | [diff] [blame] | 177 | }, null, mUserHandle); |
Doris Ling | fd06d2f | 2016-12-29 14:41:15 -0800 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Dialog to tell user about account removal failure |
| 183 | */ |
| 184 | public static class RemoveAccountFailureDialog extends InstrumentedDialogFragment { |
| 185 | |
| 186 | private static final String FAILED_REMOVAL_DIALOG = "removeAccountFailed"; |
| 187 | |
| 188 | public static void show(Fragment parent) { |
| 189 | if (!parent.isAdded()) { |
| 190 | return; |
| 191 | } |
| 192 | final RemoveAccountFailureDialog dialog = new RemoveAccountFailureDialog(); |
| 193 | dialog.setTargetFragment(parent, 0); |
Sunny Shao | d843ee8 | 2019-05-30 11:22:39 +0800 | [diff] [blame] | 194 | try { |
| 195 | dialog.show(parent.getFragmentManager(), FAILED_REMOVAL_DIALOG); |
| 196 | } catch (IllegalStateException e) { |
| 197 | Log.w(TAG, "Can't show RemoveAccountFailureDialog. " + e.getMessage()); |
| 198 | } |
Doris Ling | fd06d2f | 2016-12-29 14:41:15 -0800 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | @Override |
| 202 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 203 | final Context context = getActivity(); |
| 204 | |
| 205 | return new AlertDialog.Builder(context) |
Fan Zhang | aab36de | 2018-03-30 16:58:28 -0700 | [diff] [blame] | 206 | .setTitle(R.string.really_remove_account_title) |
| 207 | .setMessage(R.string.remove_account_failed) |
| 208 | .setPositiveButton(android.R.string.ok, null) |
| 209 | .create(); |
Doris Ling | fd06d2f | 2016-12-29 14:41:15 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | @Override |
| 213 | public int getMetricsCategory() { |
Fan Zhang | 31b2100 | 2019-01-16 13:49:47 -0800 | [diff] [blame] | 214 | return SettingsEnums.DIALOG_ACCOUNT_SYNC_FAILED_REMOVAL; |
Doris Ling | fd06d2f | 2016-12-29 14:41:15 -0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | } |
| 218 | } |