1 /* 2 * Copyright (C) 2014 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 17 package com.android.tv.settings.accounts; 18 19 import android.accounts.Account; 20 import android.accounts.AccountManager; 21 import android.accounts.AccountManagerCallback; 22 import android.accounts.AccountManagerFuture; 23 import android.accounts.AuthenticatorException; 24 import android.accounts.OperationCanceledException; 25 import android.app.Activity; 26 import android.app.ActivityManager; 27 import android.os.Bundle; 28 import android.os.Handler; 29 import android.util.Log; 30 import android.widget.Toast; 31 32 import androidx.annotation.NonNull; 33 import androidx.leanback.app.GuidedStepFragment; 34 import androidx.leanback.widget.GuidanceStylist; 35 import androidx.leanback.widget.GuidedAction; 36 37 import com.android.tv.settings.R; 38 39 import java.io.IOException; 40 import java.util.List; 41 42 /** 43 * OK / Cancel dialog. 44 */ 45 public class RemoveAccountDialog extends Activity implements AccountManagerCallback<Bundle> { 46 47 private static final String TAG = "RemoveAccountDialog"; 48 49 @Override onCreate(Bundle savedInstanceState)50 protected void onCreate(Bundle savedInstanceState) { 51 super.onCreate(savedInstanceState); 52 if (savedInstanceState == null) { 53 GuidedStepFragment.addAsRoot(this, RemoveAccountFragment.newInstance( 54 getIntent().getStringExtra(AccountSyncActivity.EXTRA_ACCOUNT)), 55 android.R.id.content); 56 } 57 } 58 59 @Override run(AccountManagerFuture<Bundle> future)60 public void run(AccountManagerFuture<Bundle> future) { 61 if (!isResumed()) { 62 if (!isFinishing()) { 63 finish(); 64 } 65 return; 66 } 67 try { 68 if (!future.getResult().getBoolean(AccountManager.KEY_BOOLEAN_RESULT)) { 69 // Wasn't removed, toast this. 70 Toast.makeText(this, R.string.account_remove_failed, 71 Toast.LENGTH_LONG) 72 .show(); 73 } 74 } catch (OperationCanceledException | AuthenticatorException | IOException e) { 75 Log.e(TAG, "Could not remove", e); 76 } 77 finish(); 78 } 79 80 public static class RemoveAccountFragment extends GuidedStepFragment { 81 private static final String ARG_ACCOUNT_NAME = "accountName"; 82 private static final int ID_OK = 1; 83 private static final int ID_CANCEL = 0; 84 private String mAccountName; 85 private boolean mIsRemoving; 86 newInstance(String accountName)87 public static RemoveAccountFragment newInstance(String accountName) { 88 final RemoveAccountFragment f = new RemoveAccountFragment(); 89 final Bundle b = new Bundle(1); 90 b.putString(ARG_ACCOUNT_NAME, accountName); 91 f.setArguments(b); 92 return f; 93 } 94 95 @Override onCreate(Bundle savedInstanceState)96 public void onCreate(Bundle savedInstanceState) { 97 mAccountName = getArguments().getString(ARG_ACCOUNT_NAME); 98 99 super.onCreate(savedInstanceState); 100 } 101 102 @NonNull 103 @Override onCreateGuidance(Bundle savedInstanceState)104 public GuidanceStylist.Guidance onCreateGuidance(Bundle savedInstanceState) { 105 return new GuidanceStylist.Guidance( 106 getString(R.string.account_remove), 107 null, 108 mAccountName, 109 getActivity().getDrawable(R.drawable.ic_delete_132dp)); 110 } 111 112 @Override onGuidedActionClicked(GuidedAction action)113 public void onGuidedActionClicked(GuidedAction action) { 114 final RemoveAccountDialog activity = (RemoveAccountDialog) getActivity(); 115 if (action.getId() == ID_OK) { 116 if (ActivityManager.isUserAMonkey()) { 117 // Don't let the monkey remove accounts. 118 activity.finish(); 119 return; 120 } 121 // Block this from happening more than once. 122 if (mIsRemoving) { 123 return; 124 } 125 mIsRemoving = true; 126 AccountManager manager = AccountManager.get(activity.getApplicationContext()); 127 Account account = null; 128 for (Account accountLoop : manager.getAccounts()) { 129 if (accountLoop.name.equals(mAccountName)) { 130 account = accountLoop; 131 break; 132 } 133 } 134 manager.removeAccount(account, activity, activity, new Handler()); 135 } else { 136 activity.finish(); 137 } 138 } 139 140 @Override onCreateActions(@onNull List<GuidedAction> actions, Bundle savedInstanceState)141 public void onCreateActions(@NonNull List<GuidedAction> actions, 142 Bundle savedInstanceState) { 143 actions.add(new GuidedAction.Builder() 144 .id(ID_CANCEL) 145 .title(getString(android.R.string.cancel)) 146 .build()); 147 actions.add(new GuidedAction.Builder() 148 .id(ID_OK) 149 .title(getString(android.R.string.ok)) 150 .build()); 151 } 152 } 153 } 154