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; 20 import android.accounts.AuthenticatorException; 21 import android.accounts.OperationCanceledException; 22 import android.app.Activity; 23 import android.app.Dialog; 24 import android.app.settings.SettingsEnums; 25 import android.content.Context; 26 import android.content.DialogInterface; 27 import android.content.Intent; 28 import android.os.Bundle; 29 import android.os.UserHandle; 30 import android.os.UserManager; 31 import android.util.Log; 32 import android.view.View; 33 import android.view.View.OnClickListener; 34 import android.widget.Button; 35 36 import androidx.appcompat.app.AlertDialog; 37 import androidx.fragment.app.Fragment; 38 import androidx.preference.PreferenceScreen; 39 40 import com.android.settings.R; 41 import com.android.settings.core.PreferenceControllerMixin; 42 import com.android.settings.core.instrumentation.InstrumentedDialogFragment; 43 import com.android.settingslib.RestrictedLockUtils; 44 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; 45 import com.android.settingslib.RestrictedLockUtilsInternal; 46 import com.android.settingslib.core.AbstractPreferenceController; 47 import com.android.settingslib.widget.LayoutPreference; 48 49 import java.io.IOException; 50 51 public class RemoveAccountPreferenceController extends AbstractPreferenceController 52 implements PreferenceControllerMixin, OnClickListener { 53 54 private static final String KEY_REMOVE_ACCOUNT = "remove_account"; 55 56 private Account mAccount; 57 private Fragment mParentFragment; 58 private UserHandle mUserHandle; 59 RemoveAccountPreferenceController(Context context, Fragment parent)60 public RemoveAccountPreferenceController(Context context, Fragment parent) { 61 super(context); 62 mParentFragment = parent; 63 } 64 65 @Override displayPreference(PreferenceScreen screen)66 public void displayPreference(PreferenceScreen screen) { 67 super.displayPreference(screen); 68 final LayoutPreference removeAccountPreference = screen.findPreference(KEY_REMOVE_ACCOUNT); 69 Button removeAccountButton = (Button) removeAccountPreference.findViewById(R.id.button); 70 removeAccountButton.setOnClickListener(this); 71 } 72 73 @Override isAvailable()74 public boolean isAvailable() { 75 return true; 76 } 77 78 @Override getPreferenceKey()79 public String getPreferenceKey() { 80 return KEY_REMOVE_ACCOUNT; 81 } 82 83 @Override onClick(View v)84 public void onClick(View v) { 85 if (mUserHandle != null) { 86 final EnforcedAdmin admin = RestrictedLockUtilsInternal.checkIfRestrictionEnforced( 87 mContext, UserManager.DISALLOW_MODIFY_ACCOUNTS, mUserHandle.getIdentifier()); 88 if (admin != null) { 89 RestrictedLockUtils.sendShowAdminSupportDetailsIntent(mContext, admin); 90 return; 91 } 92 } 93 94 ConfirmRemoveAccountDialog.show(mParentFragment, mAccount, mUserHandle); 95 } 96 init(Account account, UserHandle userHandle)97 public void init(Account account, UserHandle userHandle) { 98 mAccount = account; 99 mUserHandle = userHandle; 100 } 101 102 /** 103 * Dialog to confirm with user about account removal 104 */ 105 public static class ConfirmRemoveAccountDialog extends InstrumentedDialogFragment implements 106 DialogInterface.OnClickListener { 107 private static final String KEY_ACCOUNT = "account"; 108 private static final String REMOVE_ACCOUNT_DIALOG = "confirmRemoveAccount"; 109 private Account mAccount; 110 private UserHandle mUserHandle; 111 show( Fragment parent, Account account, UserHandle userHandle)112 public static ConfirmRemoveAccountDialog show( 113 Fragment parent, Account account, UserHandle userHandle) { 114 if (!parent.isAdded()) { 115 return null; 116 } 117 final ConfirmRemoveAccountDialog dialog = new ConfirmRemoveAccountDialog(); 118 Bundle bundle = new Bundle(); 119 bundle.putParcelable(KEY_ACCOUNT, account); 120 bundle.putParcelable(Intent.EXTRA_USER, userHandle); 121 dialog.setArguments(bundle); 122 dialog.setTargetFragment(parent, 0); 123 dialog.show(parent.getFragmentManager(), REMOVE_ACCOUNT_DIALOG); 124 return dialog; 125 } 126 127 @Override onCreate(Bundle savedInstanceState)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 onCreateDialog(Bundle savedInstanceState)136 public Dialog onCreateDialog(Bundle savedInstanceState) { 137 final Context context = getActivity(); 138 return new AlertDialog.Builder(context) 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(); 144 } 145 146 @Override getMetricsCategory()147 public int getMetricsCategory() { 148 return SettingsEnums.DIALOG_ACCOUNT_SYNC_REMOVE; 149 } 150 151 @Override onClick(DialogInterface dialog, int which)152 public void onClick(DialogInterface dialog, int which) { 153 Activity activity = getTargetFragment().getActivity(); 154 AccountManager.get(activity).removeAccountAsUser(mAccount, activity, 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; 166 } 167 } catch (OperationCanceledException 168 | IOException 169 | AuthenticatorException e) { 170 // handled below 171 } 172 if (failed) { 173 RemoveAccountFailureDialog.show(getTargetFragment()); 174 } else { 175 targetActivity.finish(); 176 } 177 }, null, mUserHandle); 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 show(Fragment parent)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); 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 } 199 } 200 201 @Override onCreateDialog(Bundle savedInstanceState)202 public Dialog onCreateDialog(Bundle savedInstanceState) { 203 final Context context = getActivity(); 204 205 return new AlertDialog.Builder(context) 206 .setTitle(R.string.really_remove_account_title) 207 .setMessage(R.string.remove_account_failed) 208 .setPositiveButton(android.R.string.ok, null) 209 .create(); 210 } 211 212 @Override getMetricsCategory()213 public int getMetricsCategory() { 214 return SettingsEnums.DIALOG_ACCOUNT_SYNC_FAILED_REMOVAL; 215 } 216 217 } 218 } 219