1aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan/*
2aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan * Copyright (C) 2011 The Android Open Source Project
3aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan *
4aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan * Licensed under the Apache License, Version 2.0 (the "License");
5aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan * you may not use this file except in compliance with the License.
6aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan * You may obtain a copy of the License at
7aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan *
8aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan *      http://www.apache.org/licenses/LICENSE-2.0
9aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan *
10aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan * Unless required by applicable law or agreed to in writing, software
11aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan * distributed under the License is distributed on an "AS IS" BASIS,
12aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan * See the License for the specific language governing permissions and
14aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan * limitations under the License.
15aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan */
16aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan
17aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuanpackage com.android.contacts.util;
18aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan
19aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuanimport android.accounts.AccountManager;
20aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuanimport android.accounts.AccountManagerCallback;
21aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuanimport android.accounts.AccountManagerFuture;
2287309946d854d4e52447f892401bd6c3803f940dKatherine Kuanimport android.accounts.AuthenticatorDescription;
23aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuanimport android.accounts.AuthenticatorException;
24aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuanimport android.accounts.OperationCanceledException;
25aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuanimport android.app.Activity;
26aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuanimport android.content.Context;
27aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuanimport android.content.SharedPreferences;
28aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuanimport android.os.Bundle;
29aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuanimport android.preference.PreferenceManager;
30aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuanimport android.util.Log;
31aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan
32e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Chengimport com.android.contacts.R;
33428f008513d1591cc08fcfe2cf0c9237fb313241Chiao Chengimport com.android.contacts.common.model.account.GoogleAccountType;
34e0b2f1e2d01d1ac52ba207dc7ce76971d853298eChiao Cheng
35aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuanimport java.io.IOException;
36aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan
37aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan/**
38aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan * Utility class for controlling whether the standard "no account" prompt on launch is shown.
39aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan */
40aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuanpublic class AccountPromptUtils {
41aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan
42aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan    private static final String TAG = AccountPromptUtils.class.getSimpleName();
43aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan
44aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan    /** {@link SharedPreferences} key for whether or not the "no account" prompt should be shown. */
45aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan    private static final String KEY_SHOW_ACCOUNT_PROMPT = "settings.showAccountPrompt";
46aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan
47aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan    /**
48aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan     * The following intent keys are understood by the {@link AccountManager} and should not be
49aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan     * changed unless the API changes.
50aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan     */
51aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan    private static final String KEY_INTRO_MESSAGE = "introMessage";
52aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan    private static final String KEY_ALLOW_SKIP_ACCOUNT_SETUP = "allowSkip";
53aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan    private static final String KEY_USER_SKIPPED_ACCOUNT_SETUP = "setupSkipped";
54aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan
55aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan    private static SharedPreferences getSharedPreferences(Context context) {
56aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan        return PreferenceManager.getDefaultSharedPreferences(context);
57aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan    }
58aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan
59aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan    /**
60aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan     * Returns true if the "no account" prompt should be shown
6187309946d854d4e52447f892401bd6c3803f940dKatherine Kuan     * (according to {@link SharedPreferences}), otherwise return false. Since this prompt is
6287309946d854d4e52447f892401bd6c3803f940dKatherine Kuan     * Google-specific for the time being, this method will also return false if the Google
6387309946d854d4e52447f892401bd6c3803f940dKatherine Kuan     * account type is not available from the {@link AccountManager}.
64aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan     */
65aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan    public static boolean shouldShowAccountPrompt(Context context) {
6687309946d854d4e52447f892401bd6c3803f940dKatherine Kuan        // TODO: Remove the filtering of account types once there is an API in
6787309946d854d4e52447f892401bd6c3803f940dKatherine Kuan        // {@link AccountManager} to show a similar account prompt
6887309946d854d4e52447f892401bd6c3803f940dKatherine Kuan        // (see {@link AccountManager#addAccount()} in {@link #launchAccountPrompt()}
6987309946d854d4e52447f892401bd6c3803f940dKatherine Kuan        // for any type of account. Bug: 5375902
7087309946d854d4e52447f892401bd6c3803f940dKatherine Kuan        AuthenticatorDescription[] allTypes =
7187309946d854d4e52447f892401bd6c3803f940dKatherine Kuan                AccountManager.get(context).getAuthenticatorTypes();
7287309946d854d4e52447f892401bd6c3803f940dKatherine Kuan        for (AuthenticatorDescription authenticatorType : allTypes) {
7387309946d854d4e52447f892401bd6c3803f940dKatherine Kuan            if (GoogleAccountType.ACCOUNT_TYPE.equals(authenticatorType.type)) {
7487309946d854d4e52447f892401bd6c3803f940dKatherine Kuan                return getSharedPreferences(context).getBoolean(KEY_SHOW_ACCOUNT_PROMPT, true);
7587309946d854d4e52447f892401bd6c3803f940dKatherine Kuan            }
7687309946d854d4e52447f892401bd6c3803f940dKatherine Kuan        }
7787309946d854d4e52447f892401bd6c3803f940dKatherine Kuan        return false;
78aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan    }
79aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan
80aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan    /**
81aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan     * Remember to never show the "no account" prompt again by saving this to
82aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan     * {@link SharedPreferences}.
83aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan     */
84aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan    public static void neverShowAccountPromptAgain(Context context) {
85aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan        getSharedPreferences(context).edit()
86aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan                .putBoolean(KEY_SHOW_ACCOUNT_PROMPT, false)
87aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan                .apply();
88aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan    }
89aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan
90aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan    /**
91aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan     * Launch the "no account" prompt. (We assume the caller has already verified that the prompt
92aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan     * can be shown, so checking the {@link #KEY_SHOW_ACCOUNT_PROMPT} value in
93aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan     * {@link SharedPreferences} will not be done in this method).
94aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan     */
95aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan    public static void launchAccountPrompt(Activity activity) {
96aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan        Bundle options = new Bundle();
97aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan        options.putCharSequence(KEY_INTRO_MESSAGE, activity.getString(R.string.no_account_prompt));
98aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan        options.putBoolean(KEY_ALLOW_SKIP_ACCOUNT_SETUP, true);
99aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan        AccountManager.get(activity).addAccount(GoogleAccountType.ACCOUNT_TYPE, null, null, options,
100aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan                activity, getAccountManagerCallback(activity), null);
101aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan    }
102aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan
103aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan    private static AccountManagerCallback<Bundle> getAccountManagerCallback(
104aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan            final Activity activity) {
105aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan        return new AccountManagerCallback<Bundle>() {
106aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan            @Override
107aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan            public void run(AccountManagerFuture<Bundle> future) {
108aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan                if (future.isCancelled()) {
109aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan                    // The account creation process was canceled
110aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan                    return;
111aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan                }
112aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan                try {
113aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan                    Bundle result = future.getResult();
114aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan                    if (result.getBoolean(KEY_USER_SKIPPED_ACCOUNT_SETUP)) {
115aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan                        AccountPromptUtils.neverShowAccountPromptAgain(activity);
116aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan                    }
117aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan                } catch (OperationCanceledException ignore) {
118aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan                    Log.e(TAG, "Account setup error: account creation process canceled");
119aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan                } catch (IOException ignore) {
120aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan                    Log.e(TAG, "Account setup error: No authenticator was registered for this"
121aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan                            + "account type or the authenticator failed to respond");
122aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan                } catch (AuthenticatorException ignore) {
123aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan                    Log.e(TAG, "Account setup error: Authenticator experienced an I/O problem");
124aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan                }
125aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan            }
126aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan        };
127aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan    }
128aaa2ad5d450d1923dae4fdf76cd050cd8a069040Katherine Kuan}
129