get Account Manager Callback - Android Account

Android examples for Account:Account Information

Description

get Account Manager Callback

Demo Code

/*/* w  w w  .j  a va2 s.  com*/
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import android.accounts.AccountManager;
import android.accounts.AccountManagerCallback;
import android.accounts.AccountManagerFuture;
import android.accounts.AuthenticatorDescription;
import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import com.android.contacts.R;
import com.android.contacts.common.model.account.GoogleAccountType;
import java.io.IOException;

public class Main{
    private static final String TAG = AccountPromptUtils.class
            .getSimpleName();
    /** {@link SharedPreferences} key for whether or not the "no account" prompt should be shown. */
    private static final String KEY_SHOW_ACCOUNT_PROMPT = "settings.showAccountPrompt";
    private static final String KEY_USER_SKIPPED_ACCOUNT_SETUP = "setupSkipped";
    private static AccountManagerCallback<Bundle> getAccountManagerCallback(
            final Activity activity) {
        return new AccountManagerCallback<Bundle>() {
            @Override
            public void run(AccountManagerFuture<Bundle> future) {
                if (future.isCancelled()) {
                    // The account creation process was canceled
                    activity.finish();
                    return;
                }
                try {
                    Bundle result = future.getResult();
                    if (result.getBoolean(KEY_USER_SKIPPED_ACCOUNT_SETUP)) {
                        AccountPromptUtils
                                .neverShowAccountPromptAgain(activity);
                    }
                } catch (OperationCanceledException ignore) {
                    Log.e(TAG,
                            "Account setup error: account creation process canceled");
                } catch (IOException ignore) {
                    Log.e(TAG,
                            "Account setup error: No authenticator was registered for this"
                                    + "account type or the authenticator failed to respond");
                } catch (AuthenticatorException ignore) {
                    Log.e(TAG,
                            "Account setup error: Authenticator experienced an I/O problem");
                }
            }
        };
    }
    /**
     * Remember to never show the "no account" prompt again by saving this to
     * {@link SharedPreferences}.
     */
    public static void neverShowAccountPromptAgain(Context context) {
        getSharedPreferences(context).edit()
                .putBoolean(KEY_SHOW_ACCOUNT_PROMPT, false).apply();
    }
    private static SharedPreferences getSharedPreferences(Context context) {
        return PreferenceManager.getDefaultSharedPreferences(context);
    }
}

Related Tutorials