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