Search in sources :

Example 56 with AccountManager

use of android.accounts.AccountManager in project Android-Developers-Samples by johnjohndoe.

the class SyncUtils method CreateSyncAccount.

/**
     * Create an entry for this application in the system account list, if it isn't already there.
     *
     * @param context Context
     */
@TargetApi(Build.VERSION_CODES.FROYO)
public static void CreateSyncAccount(Context context) {
    boolean newAccount = false;
    boolean setupComplete = PreferenceManager.getDefaultSharedPreferences(context).getBoolean(PREF_SETUP_COMPLETE, false);
    // Create account, if it's missing. (Either first run, or user has deleted account.)
    Account account = GenericAccountService.GetAccount(ACCOUNT_TYPE);
    AccountManager accountManager = (AccountManager) context.getSystemService(Context.ACCOUNT_SERVICE);
    if (accountManager.addAccountExplicitly(account, null, null)) {
        // Inform the system that this account supports sync
        ContentResolver.setIsSyncable(account, CONTENT_AUTHORITY, 1);
        // Inform the system that this account is eligible for auto sync when the network is up
        ContentResolver.setSyncAutomatically(account, CONTENT_AUTHORITY, true);
        // Recommend a schedule for automatic synchronization. The system may modify this based
        // on other scheduled syncs and network utilization.
        ContentResolver.addPeriodicSync(account, CONTENT_AUTHORITY, new Bundle(), SYNC_FREQUENCY);
        newAccount = true;
    }
    // the account list, so wee need to check both.)
    if (newAccount || !setupComplete) {
        TriggerRefresh();
        PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean(PREF_SETUP_COMPLETE, true).commit();
    }
}
Also used : Account(android.accounts.Account) Bundle(android.os.Bundle) AccountManager(android.accounts.AccountManager) TargetApi(android.annotation.TargetApi)

Example 57 with AccountManager

use of android.accounts.AccountManager in project AndroidSDK-RecipeBook by gabu.

the class Recipe098 method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    // GoogleTransportを作る
    mTransport = GoogleTransport.create();
    GoogleHeaders headers = (GoogleHeaders) mTransport.defaultHeaders;
    // "[company-id]-[app-name]-[app-version]"という形式で
    // アプリケーション名をセット
    headers.setApplicationName("gabu-recipe-98");
    // バージョンをセット
    headers.gdataVersion = "3";
    // AtomParserを作る
    AtomParser parser = new AtomParser();
    // GoogleDocumentsListのネームスペースをセット
    parser.namespaceDictionary = Namespace.DICTIONARY;
    // GoogleTransportにAtomParserをセット
    mTransport.addParser(parser);
    // HttpTransportにApacheHttpTransportのインスタンスをセット
    // これをやっておかないとExceptionが発生します。
    HttpTransport.setLowLevelHttpTransport(ApacheHttpTransport.INSTANCE);
    // AccountManagerを取得
    AccountManager manager = AccountManager.get(this);
    // Googleアカウントの一覧を取得
    Account[] accounts = manager.getAccountsByType("com.google");
    // サンプルなので暫定的に1つ目を取得
    Account acount = accounts[0];
    // 認証のためのauth tokenを取得
    AccountManagerFuture<Bundle> f = manager.getAuthToken(acount, DOCS_AUTH_TOKEN_TYPE, null, this, null, null);
    try {
        Bundle b = f.getResult();
        mAuthToken = b.getString(AccountManager.KEY_AUTHTOKEN);
    //            Log.d(TAG, "authToken=" + mAuthToken);
    } catch (OperationCanceledException e) {
        e.printStackTrace();
    } catch (AuthenticatorException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // GoogleTransportにauth tokenをセット
    // これで認証ヘッダを自動的に付けてくれます。
    ((GoogleHeaders) mTransport.defaultHeaders).setGoogleLogin(mAuthToken);
    // Googleドキュメントの一覧を取得するURLを作成
    String url = GOOGLE_DOCS_API_URL + "default/private/full/-/document";
    // GoogleTransportからGETリクエストを生成
    HttpRequest request = mTransport.buildGetRequest();
    // URLをセット
    request.setUrl(url);
    try {
        // HTTPリクエストを実行してレスポンスをパース
        DocumentListFeed feed = request.execute().parseAs(DocumentListFeed.class);
        // DocsAdapterを生成
        DocsAdapter adapter = new DocsAdapter(getApplicationContext(), 0, feed.entries);
        // ListViewにDocsAdapterをセット
        ListView listView = (ListView) findViewById(R.id.list_view);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(mOnItemClickListener);
    } catch (IOException e) {
        handleException(e);
    }
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) Account(android.accounts.Account) GoogleHeaders(com.google.api.client.googleapis.GoogleHeaders) Bundle(android.os.Bundle) OperationCanceledException(android.accounts.OperationCanceledException) AuthenticatorException(android.accounts.AuthenticatorException) IOException(java.io.IOException) AtomParser(com.google.api.client.xml.atom.AtomParser) ListView(android.widget.ListView) AccountManager(android.accounts.AccountManager)

Example 58 with AccountManager

use of android.accounts.AccountManager in project AndroidSDK-RecipeBook by gabu.

the class Recipe101 method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mTransport = GoogleTransport.create();
    GoogleHeaders headers = (GoogleHeaders) mTransport.defaultHeaders;
    // "[company-id]-[app-name]-[app-version]"という形式で
    // アプリケーション名をセット
    headers.setApplicationName("gabu-recipe-101");
    // バージョンをセット
    headers.gdataVersion = "2";
    // AtomParserを作る
    AtomParser parser = new AtomParser();
    // GoogleCalendarのネームスペースをセット
    parser.namespaceDictionary = Namespace.DICTIONARY;
    // GoogleTransportにAtomParserをセット
    mTransport.addParser(parser);
    // HttpTransportにApacheHttpTransportのインスタンスをセット
    // これをやっておかないとExceptionが発生します。
    HttpTransport.setLowLevelHttpTransport(ApacheHttpTransport.INSTANCE);
    // AccountManagerを取得
    AccountManager manager = AccountManager.get(this);
    // Googleアカウントの一覧を取得
    Account[] accounts = manager.getAccountsByType("com.google");
    // サンプルなので暫定的に1つ目を取得
    Account acount = accounts[0];
    // 認証のためのauth tokenを取得
    AccountManagerFuture<Bundle> f = manager.getAuthToken(acount, CAL_AUTH_TOKEN_TYPE, null, this, null, null);
    try {
        Bundle b = f.getResult();
        mAuthToken = b.getString(AccountManager.KEY_AUTHTOKEN);
    //            Log.d(TAG, "authToken=" + mAuthToken);
    } catch (OperationCanceledException e) {
        e.printStackTrace();
    } catch (AuthenticatorException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // GoogleTransportにauth tokenをセット
    // これで認証ヘッダを自動的に付けてくれます。
    ((GoogleHeaders) mTransport.defaultHeaders).setGoogleLogin(mAuthToken);
    // GoogleTransportからGETリクエストを生成
    HttpRequest request = mTransport.buildGetRequest();
    // Googleカレンダーの一覧を取得するURLを作成
    // 共有しているカレンダーも含む
    //        String url = GOOGLE_CAL_API_URL + "default/allcalendars/full";
    // 自分がオーナーのカレンダーのみ
    String url = GOOGLE_CAL_API_URL + "default/owncalendars/full";
    // URLをセット
    request.setUrl(url);
    try {
        // HTTPリクエストを実行してレスポンスをパース
        CalendarFeed feed = request.execute().parseAs(CalendarFeed.class);
        for (CalendarEntry entry : feed.entries) {
            Log.d(TAG, entry.title + ", " + entry.id);
        }
        // サンプルのためカレンダーの1つ目を取得
        CalendarEntry entry = feed.entries.get(0);
        // このuserIDがカレンダーを一意に特定するIDになります。
        String userID = entry.getUserID();
        // 何も指定せずに予定の一覧を取得
        url = GOOGLE_CAL_API_URL + userID + "/private/full";
        debug(request, url);
    // 2010年6月の予定の一覧を取得
    //            url = createMonthlyUrl(userID, 2010, 6);
    //            debug(request, url);
    // 2010年6月30日の予定の一覧
    //            url = createDailyUrl(userID, 2010, 6, 30);
    //            debug(request, url);
    } catch (IOException e) {
        handleException(e);
    }
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) Account(android.accounts.Account) GoogleHeaders(com.google.api.client.googleapis.GoogleHeaders) Bundle(android.os.Bundle) OperationCanceledException(android.accounts.OperationCanceledException) AuthenticatorException(android.accounts.AuthenticatorException) IOException(java.io.IOException) AtomParser(com.google.api.client.xml.atom.AtomParser) AccountManager(android.accounts.AccountManager)

Example 59 with AccountManager

use of android.accounts.AccountManager in project materialistic by hidroh.

the class AppUtils method getCredentials.

@SuppressLint("MissingPermission")
public static Pair<String, String> getCredentials(Context context) {
    String username = Preferences.getUsername(context);
    if (TextUtils.isEmpty(username)) {
        return null;
    }
    AccountManager accountManager = AccountManager.get(context);
    Account[] accounts = accountManager.getAccountsByType(BuildConfig.APPLICATION_ID);
    for (Account account : accounts) {
        if (TextUtils.equals(username, account.name)) {
            return Pair.create(username, accountManager.getPassword(account));
        }
    }
    return null;
}
Also used : Account(android.accounts.Account) AccountManager(android.accounts.AccountManager) SuppressLint(android.annotation.SuppressLint)

Example 60 with AccountManager

use of android.accounts.AccountManager in project android-delicious by lexs.

the class ContainerActivity method logout.

private void logout() {
    Log.d(TAG, "Removing account");
    AccountManager accountManager = AccountManager.get(this);
    Account[] accounts = accountManager.getAccountsByType(Constants.ACCOUNT_TYPE);
    Account account = accounts[0];
    // Callback to wait for the account to actually be removed
    AccountManagerCallback<Boolean> callback = new AccountManagerCallback<Boolean>() {

        @Override
        public void run(AccountManagerFuture<Boolean> future) {
            try {
                if (future.getResult()) {
                    checkAccount();
                } else {
                    // Could not remove account, should not happen
                    Log.e(TAG, "Could not remove account");
                }
            } catch (Exception e) {
                Log.e(TAG, "Error fetching remove account result", e);
            }
        }
    };
    accountManager.removeAccount(account, callback, null);
}
Also used : Account(android.accounts.Account) DeliciousAccount(se.alexanderblom.delicious.DeliciousAccount) AccountManagerCallback(android.accounts.AccountManagerCallback) AccountManager(android.accounts.AccountManager) AccountManagerFuture(android.accounts.AccountManagerFuture)

Aggregations

AccountManager (android.accounts.AccountManager)105 Account (android.accounts.Account)76 Bundle (android.os.Bundle)14 IOException (java.io.IOException)11 Intent (android.content.Intent)10 OperationCanceledException (android.accounts.OperationCanceledException)7 AuthenticatorException (android.accounts.AuthenticatorException)5 View (android.view.View)5 TextView (android.widget.TextView)5 HashSet (java.util.HashSet)5 SharedPreferences (android.content.SharedPreferences)4 AccountManagerCallback (android.accounts.AccountManagerCallback)3 Paint (android.graphics.Paint)3 JSONException (org.json.JSONException)3 AccountsException (android.accounts.AccountsException)2 TargetApi (android.annotation.TargetApi)2 AlertDialog (android.app.AlertDialog)2 Context (android.content.Context)2 DialogInterface (android.content.DialogInterface)2 OperationApplicationException (android.content.OperationApplicationException)2