List of usage examples for android.accounts OperationCanceledException OperationCanceledException
public OperationCanceledException(Throwable cause)
From source file:Main.java
/** * get one JSONArray from a JSONArray according to tag and name pair * @param nameTag// w w w. j av a 2s . com * @param name * @param jsonArray * @return * @throws JSONException */ public static JSONArray getCorespondingJsonArray(String nameTag, String name, String arrayTag, JSONArray jsonArray) throws JSONException, OperationCanceledException { JSONObject org; String temp; for (int i = 0, size = jsonArray.length(); i < size; i++) { org = (JSONObject) jsonArray.opt(i); temp = org.getString(nameTag); if (temp.equals(name)) return org.getJSONArray(arrayTag); } throw new OperationCanceledException("array not found"); }
From source file:Main.java
/** * replaces a list of files with another list of files, indicated by names * @param originalList/* w w w . j ava 2 s . c o m*/ * @param newList * @return */ public static void replaceFiles(ArrayList<String> originalList, ArrayList<String> newList) throws UnsupportedOperationException, OperationCanceledException { if (originalList.size() != newList.size()) throw new UnsupportedOperationException(); else { String name = null; for (int i = 0, size = originalList.size(); i < size; i++) { name = originalList.get(i); File f = new File(name); File newF = new File(newList.get(i)); if (f.exists() && newF.exists()) { if (f.delete()) { File temp = new File(name); newF.renameTo(temp); } else { throw new OperationCanceledException("Delete failed"); } } else { throw new UnsupportedOperationException("Wrong lists of file names"); } } } }
From source file:ro.weednet.contactssync.syncadapter.SyncAdapter.java
@Override public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {/*from www.j av a 2 s . c om*/ String authtoken = null; try { // ContactManager.setAccountContactsVisibility(getContext(), account, true); ContactsSync app = ContactsSync.getInstance(); if (app.getSyncWifiOnly() && !app.wifiConnected()) { throw new OperationCanceledException("not on wifi"); } authtoken = mAccountManager.blockingGetAuthToken(account, Constants.AUTHTOKEN_TYPE, NOTIFY_AUTH_FAILURE); if (authtoken == null) { throw new AuthenticationException(); } final long groupId = ContactManager.ensureGroupExists(mContext, account); final Uri rawContactsUri = RawContacts.CONTENT_URI.buildUpon() .appendQueryParameter(RawContacts.ACCOUNT_NAME, account.name) .appendQueryParameter(RawContacts.ACCOUNT_TYPE, account.type).build(); List<RawContact> localContacts = ContactManager.getLocalContacts(mContext, rawContactsUri); if (app.getFullSync()) { ContactManager.deleteContacts(mContext, localContacts); localContacts.clear(); app.clearFullSync(); } NetworkUtilities nu = new NetworkUtilities(authtoken, mContext); List<RawContact> rawContacts = nu.getContacts(account); List<RawContact> syncedContacts = ContactManager.updateContacts(mContext, account, rawContacts, groupId, app.getJoinById(), app.getSyncAllContacts()); ContactManager.deleteMissingContacts(mContext, localContacts, syncedContacts); if (app.getJoinById()) { ContactManager.addJoins(mContext, account, rawContacts); } if (app.getSyncType() == ContactsSync.SyncType.HARD) { localContacts = ContactManager.getLocalContacts(mContext, rawContactsUri); ContactManager.updateContactDetails(mContext, localContacts, nu); } else if (app.getSyncType() == ContactsSync.SyncType.MEDIUM) { List<RawContact> starredContacts = ContactManager.getStarredContacts(mContext, rawContactsUri); ContactManager.updateContactDetails(mContext, starredContacts, nu); } NotificationManager mNotificationManager = (NotificationManager) getContext() .getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.cancelAll(); } catch (final AuthenticatorException e) { Log.e(TAG, "AuthenticatorException", e); syncResult.stats.numParseExceptions++; showNotificationError("Error connecting to facebook"); } catch (final OperationCanceledException e) { Log.e(TAG, "OperationCanceledExcetpion", e); } catch (final IOException e) { Log.e(TAG, "IOException", e); syncResult.stats.numIoExceptions++; showNotificationError("Error connecting to facebook"); } catch (final AuthenticationException e) { Log.e(TAG, "AuthenticationException", e); syncResult.stats.numAuthExceptions++; if (authtoken != null) { mAccountManager.invalidateAuthToken(account.type, authtoken); } try { authtoken = mAccountManager.blockingGetAuthToken(account, Constants.AUTHTOKEN_TYPE, NOTIFY_AUTH_FAILURE); } catch (OperationCanceledException e1) { Log.e(TAG, "OperationCanceledExcetpion", e); } catch (AuthenticatorException e1) { Log.e(TAG, "AuthenticatorException", e); syncResult.stats.numParseExceptions++; } catch (IOException e1) { Log.e(TAG, "IOException", e); syncResult.stats.numIoExceptions++; } } catch (final ParseException e) { Log.e(TAG, "ParseException", e); syncResult.stats.numParseExceptions++; showNotificationError("Error parsing the information from facebook"); } catch (final JSONException e) { Log.e(TAG, "JSONException", e); syncResult.stats.numParseExceptions++; showNotificationError("Error parsing the information from facebook"); } catch (final Exception e) { Log.e(TAG, "Unknown exception", e); } }
From source file:com.github.vseguip.sweet.contacts.SweetContactSync.java
@Override public void onPerformSync(final Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) { Log.i(TAG, "onPerformSync()"); // Get preferences SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(mContext); boolean fullSync = settings.getBoolean(mContext.getString(R.string.full_sync), false); if (fullSync) mAccountManager.setUserData(account, LAST_SYNC_KEY, null); performNetOperation(new SugarRunnable(account, syncResult, new ISugarRunnable() { @Override/*from w w w . j av a2 s. c o m*/ public void run() throws URISyntaxException, OperationCanceledException, AuthenticatorException, IOException, AuthenticationException { Log.i(TAG, "Running PerformSync closure()"); mAuthToken = mAccountManager.blockingGetAuthToken(account, AUTH_TOKEN_TYPE, true); SugarAPI sugar = SugarAPIFactory.getSugarAPI(mAccountManager, account); String lastDate = mAccountManager.getUserData(account, LAST_SYNC_KEY); List<ISweetContact> contacts = null; try { contacts = fetchContacts(sugar, lastDate); } catch (AuthenticationException ex) { // maybe expired session, invalidate token and request new // one mAccountManager.invalidateAuthToken(account.type, mAuthToken); mAuthToken = mAccountManager.blockingGetAuthToken(account, AUTH_TOKEN_TYPE, false); } catch (NullPointerException npe) { // maybe expired session, invalidate token and request new // one mAccountManager.invalidateAuthToken(account.type, mAuthToken); mAuthToken = mAccountManager.blockingGetAuthToken(account, AUTH_TOKEN_TYPE, false); } // try again, it could be due to an expired session if (contacts == null) { contacts = fetchContacts(sugar, lastDate); } List<ISweetContact> modifiedContacts = ContactManager.getLocallyModifiedContacts(mContext, account); List<ISweetContact> createdContacts = ContactManager.getLocallyCreatedContacts(mContext, account); // Get latest date from server for (ISweetContact c : contacts) { String contactDate = c.getDateModified(); if ((lastDate == null) || (lastDate.compareTo(contactDate) < 0)) { lastDate = contactDate; } } // Determine conflicting contacts Set<String> conflictSet = getConflictSet(contacts, modifiedContacts); Map<String, ISweetContact> conflictingSugarContacts = filterIds(contacts, conflictSet); Map<String, ISweetContact> conflictingLocalContacts = filterIds(modifiedContacts, conflictSet); if (modifiedContacts.size() > 0) { // Send modified local non conflicting contacts to the // server List<String> newIds = sugar.sendNewContacts(mAuthToken, modifiedContacts, false); if (newIds.size() != modifiedContacts.size()) { throw new OperationCanceledException("Error updating local contacts in the remote server"); } ContactManager.cleanDirtyFlag(mContext, modifiedContacts); } if (createdContacts.size() > 0) { List<String> newIds = sugar.sendNewContacts(mAuthToken, createdContacts, true); if (newIds.size() != createdContacts.size()) { // something wrong happened, it's probable the user will // have to clear the data throw new OperationCanceledException("Error creating local contacts in the remote server"); } ContactManager.assignSourceIds(mContext, createdContacts, newIds); ContactManager.cleanDirtyFlag(mContext, createdContacts); } // Sync remote contacts locally. if (contacts.size() > 0) { ContactManager.syncContacts(mContext, account, contacts); } // resolve remaining conflicts List<ISweetContact> resolvedContacts = new ArrayList<ISweetContact>(); for (String id : conflictSet) { ISweetContact local = conflictingLocalContacts.get(id); ISweetContact remote = conflictingSugarContacts.get(id); if (local.equals(remote)) { // no need to sync resolvedContacts.add(local); conflictingLocalContacts.remove(id); conflictingSugarContacts.remove(id); } else { Log.i(TAG, "Local contact differs from remote contact " + local.getFirstName() + " " + local.getLastName()); if (local.equalUIFields(remote)) { // Differed in a non visible field like the account // id or similar, use server version and resolve // automatically resolvedContacts.add(remote); conflictingLocalContacts.remove(id); conflictingSugarContacts.remove(id); } } } ContactManager.cleanDirtyFlag(mContext, resolvedContacts); if (conflictingLocalContacts.size() > 0) { // Create a notification that can launch an mActivity to // resolve the pending conflict NotificationManager nm = (NotificationManager) mContext .getSystemService(Context.NOTIFICATION_SERVICE); Notification notify = new Notification(R.drawable.icon, mContext.getString(R.string.notify_sync_conflict_ticket), System.currentTimeMillis()); Intent intent = new Intent(mContext, SweetConflictResolveActivity.class); intent.putExtra("account", account); SweetConflictResolveActivity.storeConflicts(conflictingLocalContacts, conflictingSugarContacts); notify.setLatestEventInfo(mContext, mContext.getString(R.string.notify_sync_conflict_title), mContext.getString(R.string.notify_sync_conflict_message), PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT)); nm.notify(SweetConflictResolveActivity.NOTIFY_CONFLICT, SweetConflictResolveActivity.NOTIFY_CONTACT, notify); throw new OperationCanceledException("Pending conflicts"); } // Save the last sync time in the account if all went ok mAccountManager.setUserData(account, LAST_SYNC_KEY, lastDate); } })); }