Java源码示例:android.accounts.AccountsException
示例1
static boolean isIoError(Throwable ex) {
while (ex != null) {
if (isMaxConnections(ex.getMessage()) ||
ex instanceof IOException ||
ex instanceof ConnectionException ||
ex instanceof AccountsException ||
"failed to connect".equals(ex.getMessage()))
return true;
ex = ex.getCause();
}
return false;
}
示例2
/**
* Adds a Google account to the device.
*
* @param username Username of the account to add (including @gmail.com).
* @param password Password of the account to add.
*/
@Rpc(
description =
"Add a Google (GMail) account to the device, with account data sync disabled.")
public void addAccount(String username, String password)
throws AccountSnippetException, AccountsException, IOException {
// Check for existing account. If we try to re-add an existing account, Android throws an
// exception that says "Account does not exist or not visible. Maybe change pwd?" which is
// a little hard to understand.
if (listAccounts().contains(username)) {
throw new AccountSnippetException(
"Account " + username + " already exists on the device");
}
Bundle addAccountOptions = new Bundle();
addAccountOptions.putString("username", username);
addAccountOptions.putString("password", password);
AccountManagerFuture<Bundle> future =
mAccountManager.addAccount(
GOOGLE_ACCOUNT_TYPE,
AUTH_TOKEN_TYPE,
null /* requiredFeatures */,
addAccountOptions,
null /* activity */,
null /* authCallback */,
null /* handler */);
Bundle result = future.getResult();
if (result.containsKey(AccountManager.KEY_ERROR_CODE)) {
throw new AccountSnippetException(
String.format(
Locale.US,
"Failed to add account due to code %d: %s",
result.getInt(AccountManager.KEY_ERROR_CODE),
result.getString(AccountManager.KEY_ERROR_MESSAGE)));
}
// Disable sync to avoid test flakiness as accounts fetch additional data.
// It takes a while for all sync adapters to be populated, so register for broadcasts when
// sync is starting and disable them there.
// NOTE: this listener is NOT unregistered because several sync requests for the new account
// will come in over time.
Account account = new Account(username, GOOGLE_ACCOUNT_TYPE);
Object handle =
ContentResolver.addStatusChangeListener(
ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE
| ContentResolver.SYNC_OBSERVER_TYPE_PENDING,
which -> {
for (SyncAdapterType adapter : ContentResolver.getSyncAdapterTypes()) {
// Ignore non-Google account types.
if (!adapter.accountType.equals(GOOGLE_ACCOUNT_TYPE)) {
continue;
}
// If a content provider is not whitelisted, then disable it.
// Because startSync and stopSync synchronously update the whitelist
// and sync settings, writelock both the whitelist check and the
// call to sync together.
mLock.writeLock().lock();
try {
if (!isAdapterWhitelisted(username, adapter.authority)) {
updateSync(account, adapter.authority, false /* sync */);
}
} finally {
mLock.writeLock().unlock();
}
}
});
mSyncStatusObserverHandles.add(handle);
}
示例3
@Override
public void httpRequestError(IHttpRequestControl httpRequestControl, Throwable e) {
LoggerManager.e(TAG, "httpRequestError:" + e.getMessage());
int reason = R.string.fast_exception_other_error;
// int code = FastError.EXCEPTION_OTHER_ERROR;
if (!NetworkUtil.isConnected(App.getContext())) {
reason = R.string.fast_exception_network_not_connected;
} else {
//网络异常--继承于AccountsException
if (e instanceof NetworkErrorException) {
reason = R.string.fast_exception_network_error;
//账户异常
} else if (e instanceof AccountsException) {
reason = R.string.fast_exception_accounts;
//连接异常--继承于SocketException
} else if (e instanceof ConnectException) {
reason = R.string.fast_exception_connect;
//socket异常
} else if (e instanceof SocketException) {
reason = R.string.fast_exception_socket;
// http异常
} else if (e instanceof HttpException) {
reason = R.string.fast_exception_http;
//DNS错误
} else if (e instanceof UnknownHostException) {
reason = R.string.fast_exception_unknown_host;
} else if (e instanceof JsonSyntaxException
|| e instanceof JsonIOException
|| e instanceof JsonParseException) {
//数据格式化错误
reason = R.string.fast_exception_json_syntax;
} else if (e instanceof SocketTimeoutException || e instanceof TimeoutException) {
reason = R.string.fast_exception_time_out;
} else if (e instanceof ClassCastException) {
reason = R.string.fast_exception_class_cast;
}
}
if (httpRequestControl == null || httpRequestControl.getStatusLayoutManager() == null) {
ToastUtil.show(reason);
return;
}
SmartRefreshLayout smartRefreshLayout = httpRequestControl.getRefreshLayout();
BaseQuickAdapter adapter = httpRequestControl.getRecyclerAdapter();
StatusLayoutManager statusLayoutManager = httpRequestControl.getStatusLayoutManager();
int page = httpRequestControl.getCurrentPage();
if (smartRefreshLayout != null) {
smartRefreshLayout.finishRefresh(false);
}
if (adapter != null) {
adapter.loadMoreComplete();
if (statusLayoutManager == null) {
return;
}
//初始页
if (page == 0) {
// if (!NetworkUtil.isConnected(App.getContext())) {
// //可自定义网络错误页面展示
// statusLayoutManager.showCustomLayout(R.layout.layout_status_layout_manager_error);
// } else {
statusLayoutManager.showErrorLayout();
// }
return;
}
//可根据不同错误展示不同错误布局 showCustomLayout(R.layout.xxx);
statusLayoutManager.showErrorLayout();
}
}
示例4
@Override
public void httpRequestError(IHttpRequestControl httpRequestControl, Throwable e) {
LoggerManager.e(TAG, "httpRequestError:" + e.getMessage());
int reason = R.string.fast_exception_other_error;
// int code = FastError.EXCEPTION_OTHER_ERROR;
if (!NetworkUtil.isConnected(App.getContext())) {
reason = R.string.fast_exception_network_not_connected;
} else {
//网络异常--继承于AccountsException
if (e instanceof NetworkErrorException) {
reason = R.string.fast_exception_network_error;
//账户异常
} else if (e instanceof AccountsException) {
reason = R.string.fast_exception_accounts;
//连接异常--继承于SocketException
} else if (e instanceof ConnectException) {
reason = R.string.fast_exception_connect;
//socket异常
} else if (e instanceof SocketException) {
reason = R.string.fast_exception_socket;
// http异常
} else if (e instanceof HttpException) {
reason = R.string.fast_exception_http;
//DNS错误
} else if (e instanceof UnknownHostException) {
reason = R.string.fast_exception_unknown_host;
} else if (e instanceof JsonSyntaxException
|| e instanceof JsonIOException
|| e instanceof JsonParseException) {
//数据格式化错误
reason = R.string.fast_exception_json_syntax;
} else if (e instanceof SocketTimeoutException || e instanceof TimeoutException) {
reason = R.string.fast_exception_time_out;
} else if (e instanceof ClassCastException) {
reason = R.string.fast_exception_class_cast;
}
}
if (httpRequestControl == null || httpRequestControl.getStatusLayoutManager() == null) {
ToastUtil.show(reason);
return;
}
SmartRefreshLayout smartRefreshLayout = httpRequestControl.getRefreshLayout();
BaseQuickAdapter adapter = httpRequestControl.getRecyclerAdapter();
StatusLayoutManager statusLayoutManager = httpRequestControl.getStatusLayoutManager();
int page = httpRequestControl.getCurrentPage();
if (smartRefreshLayout != null) {
smartRefreshLayout.finishRefresh(false);
}
if (adapter != null) {
adapter.loadMoreComplete();
if (statusLayoutManager == null) {
return;
}
//初始页
if (page == 0) {
// if (!NetworkUtil.isConnected(App.getContext())) {
// //可自定义网络错误页面展示
// statusLayoutManager.showCustomLayout(R.layout.layout_status_layout_manager_error);
// } else {
statusLayoutManager.showErrorLayout();
// }
return;
}
//可根据不同错误展示不同错误布局 showCustomLayout(R.layout.xxx);
statusLayoutManager.showErrorLayout();
}
}
示例5
/**
* Core download method: requests a file to download and stores it.
*
* @param downloadKey Key to access the download to perform, contained in mPendingDownloads
*/
private void downloadFile(String downloadKey) {
/*Log_OC.v( "NOW " + TAG + ", thread " + Thread.currentThread().getName(),
"Getting download of " + downloadKey);*/
mCurrentDownload = mPendingDownloads.get(downloadKey);
if (mCurrentDownload != null) {
// Detect if the account exists
if (AccountUtils.exists(mCurrentDownload.getAccount(), getApplicationContext())) {
Log_OC.d(TAG, "Account " + mCurrentDownload.getAccount().name + " exists");
notifyDownloadStart(mCurrentDownload);
RemoteOperationResult downloadResult = null;
try {
/// prepare client object to send the request to the ownCloud server
if (mCurrentAccount == null ||
!mCurrentAccount.equals(mCurrentDownload.getAccount())) {
mCurrentAccount = mCurrentDownload.getAccount();
mStorageManager = new FileDataStorageManager(
mCurrentAccount,
getContentResolver()
);
} // else, reuse storage manager from previous operation
// always get client from client manager, to get fresh credentials in case
// of update
OwnCloudAccount ocAccount = new OwnCloudAccount(mCurrentAccount, this);
mDownloadClient = OwnCloudClientManagerFactory.getDefaultSingleton().
getClientFor(ocAccount, this);
/// perform the download
/*Log_OC.v( "NOW " + TAG + ", thread " + Thread.currentThread().getName(),
"Executing download of " + mCurrentDownload.getRemotePath());*/
downloadResult = mCurrentDownload.execute(mDownloadClient);
if (downloadResult.isSuccess()) {
saveDownloadedFile();
}
} catch (AccountsException | IOException e) {
Log_OC.e(TAG, "Error while trying to get authorization for "
+ mCurrentAccount.name, e);
downloadResult = new RemoteOperationResult(e);
} finally {
/*Log_OC.v( "NOW " + TAG + ", thread " + Thread.currentThread().getName(),
"Removing payload " + mCurrentDownload.getRemotePath());*/
Pair<DownloadFileOperation, String> removeResult =
mPendingDownloads.removePayload(mCurrentAccount,
mCurrentDownload.getRemotePath());
/// notify result
notifyDownloadResult(mCurrentDownload, downloadResult);
sendBroadcastDownloadFinished(mCurrentDownload, downloadResult,
removeResult.second);
}
} else {
// Cancel the transfer
Log_OC.d(TAG, "Account " + mCurrentDownload.getAccount().toString() +
" doesn't exist");
cancelDownloadsForAccount(mCurrentDownload.getAccount());
}
}
}
示例6
/**
* Core upload method: sends the file(s) to upload
*
* @param uploadKey Key to access the upload to perform, contained in
* mPendingUploads
*/
public void uploadFile(String uploadKey) {
synchronized (mPendingUploads) {
mCurrentUpload = mPendingUploads.get(uploadKey);
}
if (mCurrentUpload != null) {
// Detect if the account exists
if (AccountUtils.exists(mCurrentUpload.getAccount(), getApplicationContext())) {
Log_OC.d(TAG, "Account " + mCurrentUpload.getAccount().name + " exists");
notifyUploadStart(mCurrentUpload);
RemoteOperationResult uploadResult = null, grantResult;
try {
/// prepare client object to send requests to the ownCloud server
if (mUploadClient == null ||
!mLastAccount.equals(mCurrentUpload.getAccount())) {
mLastAccount = mCurrentUpload.getAccount();
mStorageManager =
new FileDataStorageManager(mLastAccount, getContentResolver());
OwnCloudAccount ocAccount = new OwnCloudAccount(mLastAccount, this);
mUploadClient = OwnCloudClientManagerFactory.getDefaultSingleton().
getClientFor(ocAccount, this);
}
/// check the existence of the parent folder for the file to upload
String remoteParentPath = new File(mCurrentUpload.getRemotePath()).getParent();
remoteParentPath = remoteParentPath.endsWith(OCFile.PATH_SEPARATOR) ?
remoteParentPath : remoteParentPath + OCFile.PATH_SEPARATOR;
grantResult = grantFolderExistence(remoteParentPath);
/// perform the upload
if (grantResult.isSuccess()) {
OCFile parent = mStorageManager.getFileByPath(remoteParentPath);
mCurrentUpload.getFile().setParentId(parent.getFileId());
uploadResult = mCurrentUpload.execute(mUploadClient);
if (uploadResult.isSuccess()) {
saveUploadedFile();
}
} else {
uploadResult = grantResult;
}
} catch (AccountsException | IOException e) {
Log_OC.e(TAG, "Error while trying to get autorization for " +
mLastAccount.name, e);
uploadResult = new RemoteOperationResult(e);
} finally {
synchronized (mPendingUploads) {
mPendingUploads.remove(uploadKey);
Log_OC.i(TAG, "Remove CurrentUploadItem from pending upload Item Map.");
}
if (uploadResult != null && uploadResult.isException()) {
// enforce the creation of a new client object for next uploads;
// this grant that a new socket will be created in the future if
// the current exception is due to an abrupt lose of network connection
mUploadClient = null;
}
}
/// notify result
notifyUploadResult(uploadResult, mCurrentUpload);
sendFinalBroadcast(mCurrentUpload, uploadResult);
} else {
// Cancel the transfer
Log_OC.d(TAG, "Account " + mCurrentUpload.getAccount().toString() +
" doesn't exist");
cancelUploadForAccount(mCurrentUpload.getAccount().name);
}
}
}