165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/* 265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Copyright (C) 2014 The Android Open Source Project 365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * 465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Licensed under the Apache License, Version 2.0 (the "License"); 565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * you may not use this file except in compliance with the License. 665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * You may obtain a copy of the License at 765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * 865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * http://www.apache.org/licenses/LICENSE-2.0 965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * 1065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * Unless required by applicable law or agreed to in writing, software 1165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * distributed under the License is distributed on an "AS IS" BASIS, 1265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * See the License for the specific language governing permissions and 1465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * limitations under the License. 1565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */ 1665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 1765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepackage com.android.tv.settings.accounts; 1865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 1965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport com.android.tv.settings.R; 2065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport com.android.tv.settings.dialog.old.Action; 2165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport com.android.tv.settings.dialog.old.ActionFragment; 2265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport com.android.tv.settings.dialog.old.ContentFragment; 2365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport com.android.tv.settings.dialog.old.DialogActivity; 2465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport com.android.tv.settings.widget.SettingsToast; 2565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 2665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.accounts.Account; 2765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.accounts.AccountManager; 2865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.accounts.AccountManagerCallback; 2965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.accounts.AccountManagerFuture; 3065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.accounts.AuthenticatorException; 3165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.accounts.OperationCanceledException; 3265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.app.ActivityManager; 3365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.os.Bundle; 3465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.os.Handler; 3565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport android.util.Log; 3665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 3765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport java.io.IOException; 3865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Laneimport java.util.ArrayList; 3965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 4065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane/** 4165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane * OK / Cancel dialog. 4265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane */ 4365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lanepublic class RemoveAccountDialog extends DialogActivity implements AccountManagerCallback<Boolean> { 4465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 4565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane private static final String TAG = "RemoveAccountDialog"; 4665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 4765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane private static final String KEY_OK = "ok"; 4865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane private static final String KEY_CANCEL = "cancel"; 4965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane private String mAccountName; 5065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane private boolean mIsRemoving; 5165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 5265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane @Override 5365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane protected void onCreate(Bundle savedInstanceState) { 5465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane super.onCreate(savedInstanceState); 5565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane mAccountName = getIntent().getStringExtra(AccountSettingsActivity.EXTRA_ACCOUNT); 5665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane setContentAndActionFragments(ContentFragment.newInstance( 5765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane getString(R.string.account_remove), mAccountName, "", 5865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane R.drawable.ic_settings_remove, getResources().getColor(R.color.icon_background)), 5965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane ActionFragment.newInstance(getActions())); 6065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 6165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 6265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane @Override 6365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane public void onActionClicked(Action action) { 6465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane if (KEY_OK.equals(action.getKey())) { 6565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane if (ActivityManager.isUserAMonkey()) { 6665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane // Don't let the monkey remove accounts. 6765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane finish(); 6865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane return; 6965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 7065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane // Block this from happening more than once. 7165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane if (mIsRemoving) { 7265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane return; 7365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 7465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane mIsRemoving = true; 7565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane AccountManager manager = AccountManager.get(getApplicationContext()); 7665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane Account account = null; 7765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane for (Account accountLoop : manager.getAccounts()) { 7865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane if (accountLoop.name.equals(mAccountName)) { 7965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane account = accountLoop; 8065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane break; 8165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 8265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 8365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane manager.removeAccount(account, this, new Handler()); 8465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } else { 8565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane finish(); 8665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 8765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 8865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 8965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane private ArrayList<Action> getActions() { 9065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane ArrayList<Action> actions = new ArrayList<Action>(); 9165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane actions.add(new Action.Builder() 9265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane .key(KEY_CANCEL) 9365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane .title(getString(R.string.settings_cancel)) 9465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane .build()); 9565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane actions.add(new Action.Builder() 9665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane .key(KEY_OK) 9765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane .title(getString(R.string.settings_ok)) 9865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane .build()); 9965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane return actions; 10065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 10165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 10265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane 10365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane @Override 10465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane public void run(AccountManagerFuture<Boolean> future) { 10565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane if (!isResumed()) { 10665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane return; 10765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 10865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane try { 10965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane if (!future.getResult()) { 11065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane // Wasn't removed, toast this. 11165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane SettingsToast.makeText(this, R.string.account_remove_failed, 11265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane SettingsToast.LENGTH_LONG) 11365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane .show(); 11465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 11565a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } catch (OperationCanceledException e) { 11665a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane Log.e(TAG, "Could not remove", e); 11765a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } catch (AuthenticatorException e) { 11865a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane Log.e(TAG, "Could not remove", e); 11965a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } catch (IOException e) { 12065a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane Log.e(TAG, "Could not remove", e); 12165a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 12265a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane finish(); 12365a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane } 12465a5a7d84ad9b5324ae53eda526e39e513473af7Christopher Lane} 125