Search in sources :

Example 1 with OperationCanceledException

use of android.accounts.OperationCanceledException in project AndroidSyncProviderDemo by c99koder.

the class ContactsSyncAdapterService method performSync.

private static void performSync(Context context, Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) throws OperationCanceledException {
    HashMap<String, SyncEntry> localContacts = new HashMap<String, SyncEntry>();
    mContentResolver = context.getContentResolver();
    Log.i(TAG, "performSync: " + account.toString());
    // Load the local contacts
    Uri rawContactUri = RawContacts.CONTENT_URI.buildUpon().appendQueryParameter(RawContacts.ACCOUNT_NAME, account.name).appendQueryParameter(RawContacts.ACCOUNT_TYPE, account.type).build();
    Cursor c1 = mContentResolver.query(rawContactUri, new String[] { BaseColumns._ID, UsernameColumn, PhotoTimestampColumn }, null, null, null);
    while (c1.moveToNext()) {
        SyncEntry entry = new SyncEntry();
        entry.raw_id = c1.getLong(c1.getColumnIndex(BaseColumns._ID));
        entry.photo_timestamp = c1.getLong(c1.getColumnIndex(PhotoTimestampColumn));
        localContacts.put(c1.getString(1), entry);
    }
    ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();
    try {
        // status message
        if (localContacts.get("efudd") == null) {
            addContact(account, "Elmer Fudd", "efudd");
        } else {
            if (localContacts.get("efudd").photo_timestamp == null || System.currentTimeMillis() > (localContacts.get("efudd").photo_timestamp + 604800000L)) {
                //You would probably download an image file and just pass the bytes, but this sample doesn't use network so we'll decode and re-compress the icon resource to get the bytes
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon);
                icon.compress(CompressFormat.PNG, 0, stream);
                updateContactPhoto(operationList, localContacts.get("efudd").raw_id, stream.toByteArray());
            }
            updateContactStatus(operationList, localContacts.get("efudd").raw_id, "hunting wabbits");
        }
        if (operationList.size() > 0)
            mContentResolver.applyBatch(ContactsContract.AUTHORITY, operationList);
    } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
}
Also used : ContentProviderOperation(android.content.ContentProviderOperation) Bitmap(android.graphics.Bitmap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Cursor(android.database.Cursor) Uri(android.net.Uri) OperationCanceledException(android.accounts.OperationCanceledException)

Example 2 with OperationCanceledException

use of android.accounts.OperationCanceledException in project android_frameworks_base by ParanoidAndroid.

the class KeyguardAccountView method asyncCheckPassword.

private void asyncCheckPassword() {
    mCallback.userActivity(AWAKE_POKE_MILLIS);
    final String login = mLogin.getText().toString();
    final String password = mPassword.getText().toString();
    Account account = findIntendedAccount(login);
    if (account == null) {
        postOnCheckPasswordResult(false);
        return;
    }
    getProgressDialog().show();
    Bundle options = new Bundle();
    options.putString(AccountManager.KEY_PASSWORD, password);
    AccountManager.get(mContext).confirmCredentialsAsUser(account, options, null, /* activity */
    new AccountManagerCallback<Bundle>() {

        public void run(AccountManagerFuture<Bundle> future) {
            try {
                mCallback.userActivity(AWAKE_POKE_MILLIS);
                final Bundle result = future.getResult();
                final boolean verified = result.getBoolean(AccountManager.KEY_BOOLEAN_RESULT);
                postOnCheckPasswordResult(verified);
            } catch (OperationCanceledException e) {
                postOnCheckPasswordResult(false);
            } catch (IOException e) {
                postOnCheckPasswordResult(false);
            } catch (AuthenticatorException e) {
                postOnCheckPasswordResult(false);
            } finally {
                mLogin.post(new Runnable() {

                    public void run() {
                        getProgressDialog().hide();
                    }
                });
            }
        }
    }, null, /* handler */
    new UserHandle(mLockPatternUtils.getCurrentUser()));
}
Also used : Account(android.accounts.Account) Bundle(android.os.Bundle) OperationCanceledException(android.accounts.OperationCanceledException) UserHandle(android.os.UserHandle) AuthenticatorException(android.accounts.AuthenticatorException) IOException(java.io.IOException)

Example 3 with OperationCanceledException

use of android.accounts.OperationCanceledException in project Reader by TheKeeperOfPie.

the class Reddit method getTokenBlocking.

public void getTokenBlocking() {
    if (account == null) {
        getApplicationWideTokenBlocking();
        return;
    }
    String token = accountManager.peekAuthToken(account, Reddit.AUTH_TOKEN_FULL_ACCESS);
    accountManager.invalidateAuthToken(Reddit.ACCOUNT_TYPE, token);
    accountManager.invalidateAuthToken(Reddit.ACCOUNT_TYPE, tokenAuth);
    final AccountManagerFuture<Bundle> future = accountManager.getAuthToken(account, Reddit.AUTH_TOKEN_FULL_ACCESS, null, true, null, null);
    try {
        Bundle bundle = future.getResult();
        tokenAuth = bundle.getString(AccountManager.KEY_AUTHTOKEN);
        try {
            timeExpire = Long.parseLong(accountManager.getUserData(account, ActivityLogin.KEY_TIME_EXPIRATION));
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
    } catch (OperationCanceledException | AuthenticatorException | IOException e) {
        e.printStackTrace();
    }
}
Also used : Bundle(android.os.Bundle) OperationCanceledException(android.accounts.OperationCanceledException) AuthenticatorException(android.accounts.AuthenticatorException) IOException(java.io.IOException)

Example 4 with OperationCanceledException

use of android.accounts.OperationCanceledException in project PocketHub by pockethub.

the class AccountUtils method updateAccount.

/**
     * Update account
     *
     * @param account
     * @param activity
     * @return true if account was updated, false otherwise
     */
public static boolean updateAccount(final Account account, final Activity activity) {
    int count = UPDATE_COUNT.get();
    synchronized (UPDATE_COUNT) {
        // while the lock was being waited for
        if (count != UPDATE_COUNT.get()) {
            return true;
        }
        AccountManager manager = AccountManager.get(activity);
        try {
            if (!hasAuthenticator(manager)) {
                throw new AuthenticatorConflictException();
            }
            manager.updateCredentials(account, ACCOUNT_TYPE, null, activity, null, null).getResult();
            UPDATE_COUNT.incrementAndGet();
            return true;
        } catch (OperationCanceledException e) {
            Log.d(TAG, "Excepting retrieving account", e);
            activity.finish();
            return false;
        } catch (AccountsException e) {
            Log.d(TAG, "Excepting retrieving account", e);
            return false;
        } catch (AuthenticatorConflictException e) {
            activity.runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    showConflictMessage(activity);
                }
            });
            return false;
        } catch (IOException e) {
            Log.d(TAG, "Excepting retrieving account", e);
            return false;
        }
    }
}
Also used : OperationCanceledException(android.accounts.OperationCanceledException) AccountManager(android.accounts.AccountManager) IOException(java.io.IOException) AccountsException(android.accounts.AccountsException)

Example 5 with OperationCanceledException

use of android.accounts.OperationCanceledException in project PocketHub by pockethub.

the class AccountUtils method getAccount.

/**
     * Get account used for authentication
     *
     * @param manager
     * @param activity
     * @return account
     * @throws IOException
     * @throws AccountsException
     */
public static Account getAccount(final AccountManager manager, final Activity activity) throws IOException, AccountsException {
    final boolean loggable = Log.isLoggable(TAG, DEBUG);
    if (loggable) {
        Log.d(TAG, "Getting account");
    }
    if (activity == null) {
        throw new IllegalArgumentException("Activity cannot be null");
    }
    if (activity.isFinishing()) {
        throw new OperationCanceledException();
    }
    Account[] accounts;
    try {
        if (!hasAuthenticator(manager)) {
            throw new AuthenticatorConflictException();
        }
        while ((accounts = getAccounts(manager)).length == 0) {
            if (loggable) {
                Log.d(TAG, "No GitHub accounts for activity=" + activity);
            }
            Bundle result = manager.addAccount(ACCOUNT_TYPE, null, null, null, activity, null, null).getResult();
            if (loggable) {
                Log.d(TAG, "Added account " + result.getString(KEY_ACCOUNT_NAME));
            }
        }
    } catch (OperationCanceledException e) {
        Log.d(TAG, "Excepting retrieving account", e);
        activity.finish();
        throw e;
    } catch (AccountsException e) {
        Log.d(TAG, "Excepting retrieving account", e);
        throw e;
    } catch (AuthenticatorConflictException e) {
        activity.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                showConflictMessage(activity);
            }
        });
        throw e;
    } catch (IOException e) {
        Log.d(TAG, "Excepting retrieving account", e);
        throw e;
    }
    if (loggable) {
        Log.d(TAG, "Returning account " + accounts[0].name);
    }
    return accounts[0];
}
Also used : Account(android.accounts.Account) Bundle(android.os.Bundle) OperationCanceledException(android.accounts.OperationCanceledException) IOException(java.io.IOException) AccountsException(android.accounts.AccountsException)

Aggregations

OperationCanceledException (android.accounts.OperationCanceledException)15 IOException (java.io.IOException)13 AuthenticatorException (android.accounts.AuthenticatorException)11 Bundle (android.os.Bundle)11 Account (android.accounts.Account)8 AccountManager (android.accounts.AccountManager)7 Intent (android.content.Intent)3 AccountsException (android.accounts.AccountsException)2 Cursor (android.database.Cursor)2 UserHandle (android.os.UserHandle)2 GoogleHeaders (com.google.api.client.googleapis.GoogleHeaders)2 HttpRequest (com.google.api.client.http.HttpRequest)2 AtomParser (com.google.api.client.xml.atom.AtomParser)2 AccountManagerCallback (android.accounts.AccountManagerCallback)1 PendingIntent (android.app.PendingIntent)1 ContentProviderClient (android.content.ContentProviderClient)1 ContentProviderOperation (android.content.ContentProviderOperation)1 Bitmap (android.graphics.Bitmap)1 Uri (android.net.Uri)1 RemoteException (android.os.RemoteException)1