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