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.inputmethod.latin.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.content.Context;
26import android.os.Bundle;
27import android.os.Handler;
28
29import java.io.IOException;
30
31/**
32 * Utility class that handles generation/invalidation of auth tokens in the app.
33 */
34public class AuthUtils {
35    private final AccountManager mAccountManager;
36
37    public AuthUtils(Context context) {
38        mAccountManager = AccountManager.get(context);
39    }
40
41    /**
42     * @see AccountManager#invalidateAuthToken(String, String)
43     */
44    public void invalidateAuthToken(final String accountType, final String authToken) {
45        mAccountManager.invalidateAuthToken(accountType, authToken);
46    }
47
48    /**
49     * @see AccountManager#getAuthToken(
50     *              Account, String, Bundle, boolean, AccountManagerCallback, Handler)
51     */
52    public AccountManagerFuture<Bundle> getAuthToken(final Account account,
53            final String authTokenType, final Bundle options, final boolean notifyAuthFailure,
54            final AccountManagerCallback<Bundle> callback, final Handler handler) {
55        return mAccountManager.getAuthToken(account, authTokenType, options, notifyAuthFailure,
56                callback, handler);
57    }
58
59    /**
60     * @see AccountManager#blockingGetAuthToken(Account, String, boolean)
61     */
62    public String blockingGetAuthToken(final Account account, final String authTokenType,
63            final boolean notifyAuthFailure) throws OperationCanceledException,
64            AuthenticatorException, IOException {
65        return mAccountManager.blockingGetAuthToken(account, authTokenType, notifyAuthFailure);
66    }
67}
68