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