Home | History | Annotate | Download | only in accounts
      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 com.android.tv.settings.R;
     20 import com.android.tv.settings.dialog.old.Action;
     21 import com.android.tv.settings.dialog.old.ActionFragment;
     22 import com.android.tv.settings.dialog.old.ContentFragment;
     23 import com.android.tv.settings.dialog.old.DialogActivity;
     24 import com.android.tv.settings.widget.SettingsToast;
     25 
     26 import android.accounts.Account;
     27 import android.accounts.AccountManager;
     28 import android.accounts.AccountManagerCallback;
     29 import android.accounts.AccountManagerFuture;
     30 import android.accounts.AuthenticatorException;
     31 import android.accounts.OperationCanceledException;
     32 import android.app.ActivityManager;
     33 import android.os.Bundle;
     34 import android.os.Handler;
     35 import android.util.Log;
     36 
     37 import java.io.IOException;
     38 import java.util.ArrayList;
     39 
     40 /**
     41  * OK / Cancel dialog.
     42  */
     43 public class RemoveAccountDialog extends DialogActivity implements AccountManagerCallback<Boolean> {
     44 
     45     private static final String TAG = "RemoveAccountDialog";
     46 
     47     private static final String KEY_OK = "ok";
     48     private static final String KEY_CANCEL = "cancel";
     49     private String mAccountName;
     50     private boolean mIsRemoving;
     51 
     52     @Override
     53     protected void onCreate(Bundle savedInstanceState) {
     54         super.onCreate(savedInstanceState);
     55         mAccountName = getIntent().getStringExtra(AccountSettingsActivity.EXTRA_ACCOUNT);
     56         setContentAndActionFragments(ContentFragment.newInstance(
     57                 getString(R.string.account_remove), mAccountName, "",
     58                 R.drawable.ic_settings_remove, getResources().getColor(R.color.icon_background)),
     59             ActionFragment.newInstance(getActions()));
     60     }
     61 
     62     @Override
     63     public void onActionClicked(Action action) {
     64         if (KEY_OK.equals(action.getKey())) {
     65             if (ActivityManager.isUserAMonkey()) {
     66                 // Don't let the monkey remove accounts.
     67                 finish();
     68                 return;
     69             }
     70             // Block this from happening more than once.
     71             if (mIsRemoving) {
     72                 return;
     73             }
     74             mIsRemoving = true;
     75             AccountManager manager = AccountManager.get(getApplicationContext());
     76             Account account = null;
     77             for (Account accountLoop : manager.getAccounts()) {
     78                 if (accountLoop.name.equals(mAccountName)) {
     79                     account = accountLoop;
     80                     break;
     81                 }
     82             }
     83             manager.removeAccount(account, this, new Handler());
     84         } else {
     85             finish();
     86         }
     87     }
     88 
     89     private ArrayList<Action> getActions() {
     90         ArrayList<Action> actions = new ArrayList<Action>();
     91         actions.add(new Action.Builder()
     92             .key(KEY_CANCEL)
     93             .title(getString(R.string.settings_cancel))
     94             .build());
     95         actions.add(new Action.Builder()
     96             .key(KEY_OK)
     97             .title(getString(R.string.settings_ok))
     98             .build());
     99         return actions;
    100     }
    101 
    102 
    103     @Override
    104     public void run(AccountManagerFuture<Boolean> future) {
    105         if (!isResumed()) {
    106             return;
    107         }
    108         try {
    109             if (!future.getResult()) {
    110                 // Wasn't removed, toast this.
    111                 SettingsToast.makeText(this, R.string.account_remove_failed,
    112                         SettingsToast.LENGTH_LONG)
    113                         .show();
    114             }
    115         } catch (OperationCanceledException e) {
    116             Log.e(TAG, "Could not remove", e);
    117         } catch (AuthenticatorException e) {
    118             Log.e(TAG, "Could not remove", e);
    119         } catch (IOException e) {
    120             Log.e(TAG, "Could not remove", e);
    121         }
    122         finish();
    123     }
    124 }
    125