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();
}
}
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()));
}
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();
}
}
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;
}
}
}
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];
}
Aggregations