mirror of
https://github.com/nextcloud/android.git
synced 2024-11-28 02:06:56 +03:00
New class ErrorMessageAdapter to choose proper error message to show to the user depending on the results of operations
This commit is contained in:
parent
f849570b0a
commit
99dcc9c5d5
3 changed files with 94 additions and 27 deletions
|
@ -44,6 +44,7 @@ import com.owncloud.android.ui.activity.FileActivity;
|
||||||
import com.owncloud.android.ui.activity.FileDisplayActivity;
|
import com.owncloud.android.ui.activity.FileDisplayActivity;
|
||||||
import com.owncloud.android.ui.preview.PreviewImageActivity;
|
import com.owncloud.android.ui.preview.PreviewImageActivity;
|
||||||
import com.owncloud.android.ui.preview.PreviewImageFragment;
|
import com.owncloud.android.ui.preview.PreviewImageFragment;
|
||||||
|
import com.owncloud.android.utils.ErrorMessageAdapter;
|
||||||
import com.owncloud.android.utils.Log_OC;
|
import com.owncloud.android.utils.Log_OC;
|
||||||
import com.owncloud.android.utils.NotificationBuilderWithProgressBar;
|
import com.owncloud.android.utils.NotificationBuilderWithProgressBar;
|
||||||
|
|
||||||
|
@ -461,9 +462,9 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
|
||||||
private void notifyDownloadResult(DownloadFileOperation download, RemoteOperationResult downloadResult) {
|
private void notifyDownloadResult(DownloadFileOperation download, RemoteOperationResult downloadResult) {
|
||||||
mNotificationManager.cancel(R.string.downloader_download_in_progress_ticker);
|
mNotificationManager.cancel(R.string.downloader_download_in_progress_ticker);
|
||||||
if (!downloadResult.isCancelled()) {
|
if (!downloadResult.isCancelled()) {
|
||||||
int tickerId = (downloadResult.isSuccess()) ? R.string.downloader_download_succeeded_ticker : R.string.downloader_download_failed_ticker;
|
int tickerId = (downloadResult.isSuccess()) ? R.string.downloader_download_succeeded_ticker :
|
||||||
int contentId = (downloadResult.isSuccess()) ? R.string.downloader_download_succeeded_content : R.string.downloader_download_failed_content;
|
R.string.downloader_download_failed_ticker;
|
||||||
|
|
||||||
boolean needsToUpdateCredentials = (downloadResult.getCode() == ResultCode.UNAUTHORIZED ||
|
boolean needsToUpdateCredentials = (downloadResult.getCode() == ResultCode.UNAUTHORIZED ||
|
||||||
(downloadResult.isIdPRedirection()
|
(downloadResult.isIdPRedirection()
|
||||||
&& mDownloadClient.getCredentials() == null));
|
&& mDownloadClient.getCredentials() == null));
|
||||||
|
@ -488,9 +489,8 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
|
||||||
updateAccountCredentials.addFlags(Intent.FLAG_FROM_BACKGROUND);
|
updateAccountCredentials.addFlags(Intent.FLAG_FROM_BACKGROUND);
|
||||||
mNotificationBuilder
|
mNotificationBuilder
|
||||||
.setContentIntent(PendingIntent.getActivity(
|
.setContentIntent(PendingIntent.getActivity(
|
||||||
this, (int) System.currentTimeMillis(), updateAccountCredentials, PendingIntent.FLAG_ONE_SHOT
|
this, (int) System.currentTimeMillis(), updateAccountCredentials, PendingIntent.FLAG_ONE_SHOT));
|
||||||
))
|
|
||||||
.setContentText(String.format(getString(contentId), new File(download.getSavePath()).getName()));
|
|
||||||
mDownloadClient = null; // grant that future retries on the same account will get the fresh credentials
|
mDownloadClient = null; // grant that future retries on the same account will get the fresh credentials
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
@ -511,10 +511,11 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
|
||||||
}
|
}
|
||||||
mNotificationBuilder
|
mNotificationBuilder
|
||||||
.setContentIntent(PendingIntent.getActivity(
|
.setContentIntent(PendingIntent.getActivity(
|
||||||
this, (int) System.currentTimeMillis(), showDetailsIntent, 0
|
this, (int) System.currentTimeMillis(), showDetailsIntent, 0));
|
||||||
))
|
|
||||||
.setContentText(String.format(getString(contentId), new File(download.getSavePath()).getName()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mNotificationBuilder.setContentText(ErrorMessageAdapter.getErrorCauseMessage(downloadResult, download, getResources()));
|
||||||
|
|
||||||
mNotificationManager.notify(tickerId, mNotificationBuilder.build());
|
mNotificationManager.notify(tickerId, mNotificationBuilder.build());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,6 +54,7 @@ import com.owncloud.android.ui.activity.FileDisplayActivity;
|
||||||
import com.owncloud.android.ui.activity.InstantUploadActivity;
|
import com.owncloud.android.ui.activity.InstantUploadActivity;
|
||||||
import com.owncloud.android.ui.preview.PreviewImageActivity;
|
import com.owncloud.android.ui.preview.PreviewImageActivity;
|
||||||
import com.owncloud.android.ui.preview.PreviewImageFragment;
|
import com.owncloud.android.ui.preview.PreviewImageFragment;
|
||||||
|
import com.owncloud.android.utils.ErrorMessageAdapter;
|
||||||
import com.owncloud.android.utils.Log_OC;
|
import com.owncloud.android.utils.Log_OC;
|
||||||
import com.owncloud.android.utils.NotificationBuilderWithProgressBar;
|
import com.owncloud.android.utils.NotificationBuilderWithProgressBar;
|
||||||
|
|
||||||
|
@ -752,10 +753,7 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
|
||||||
))
|
))
|
||||||
.setTicker(getString(R.string.uploader_upload_succeeded_ticker))
|
.setTicker(getString(R.string.uploader_upload_succeeded_ticker))
|
||||||
.setContentTitle(getString(R.string.uploader_upload_succeeded_ticker))
|
.setContentTitle(getString(R.string.uploader_upload_succeeded_ticker))
|
||||||
.setContentText(
|
.setContentText(ErrorMessageAdapter.getErrorCauseMessage(uploadResult, upload, getResources()));
|
||||||
String.format(getString(R.string.uploader_upload_succeeded_content_single),
|
|
||||||
upload.getFileName())
|
|
||||||
);
|
|
||||||
|
|
||||||
mNotificationManager.notify(R.string.uploader_upload_in_progress_ticker, mNotificationBuilder.build()); // NOT
|
mNotificationManager.notify(R.string.uploader_upload_in_progress_ticker, mNotificationBuilder.build()); // NOT
|
||||||
// AN
|
// AN
|
||||||
|
@ -785,6 +783,7 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
|
||||||
.setContentTitle(getString(tickerId))
|
.setContentTitle(getString(tickerId))
|
||||||
.setAutoCancel(true);
|
.setAutoCancel(true);
|
||||||
|
|
||||||
|
content = ErrorMessageAdapter.getErrorCauseMessage(uploadResult, upload, getResources());
|
||||||
|
|
||||||
if (needsToUpdateCredentials) {
|
if (needsToUpdateCredentials) {
|
||||||
// let the user update credentials with one click
|
// let the user update credentials with one click
|
||||||
|
@ -797,23 +796,10 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
|
||||||
errorBuilder.setContentIntent(PendingIntent.getActivity(
|
errorBuilder.setContentIntent(PendingIntent.getActivity(
|
||||||
this, (int) System.currentTimeMillis(), updateAccountCredentials, PendingIntent.FLAG_ONE_SHOT
|
this, (int) System.currentTimeMillis(), updateAccountCredentials, PendingIntent.FLAG_ONE_SHOT
|
||||||
));
|
));
|
||||||
content = String.format(getString(R.string.uploader_upload_failed_content_single), upload.getFileName());
|
|
||||||
mUploadClient = null; // grant that future retries on the same account will get the fresh credentials
|
mUploadClient = null; // grant that future retries on the same account will get the fresh credentials
|
||||||
} else {
|
} else {
|
||||||
// TODO put something smart in the contentIntent below
|
// TODO put something smart in the contentIntent below
|
||||||
|
|
||||||
if (uploadResult.getCode() == ResultCode.LOCAL_STORAGE_FULL
|
|
||||||
|| uploadResult.getCode() == ResultCode.LOCAL_STORAGE_NOT_COPIED) {
|
|
||||||
// TODO we need a class to provide error messages for the users
|
|
||||||
// from a RemoteOperationResult and a RemoteOperation
|
|
||||||
content = String.format(getString(R.string.error__upload__local_file_not_copied), upload.getFileName(),
|
|
||||||
getString(R.string.app_name));
|
|
||||||
} else if (uploadResult.getCode() == ResultCode.QUOTA_EXCEEDED) {
|
|
||||||
content = getString(R.string.failed_upload_quota_exceeded_text);
|
|
||||||
} else {
|
|
||||||
content = String
|
|
||||||
.format(getString(R.string.uploader_upload_failed_content_single), upload.getFileName());
|
|
||||||
}
|
|
||||||
|
|
||||||
// we add only for instant-uploads the InstantUploadActivity and the
|
// we add only for instant-uploads the InstantUploadActivity and the
|
||||||
// db entry
|
// db entry
|
||||||
|
|
80
src/com/owncloud/android/utils/ErrorMessageAdapter.java
Normal file
80
src/com/owncloud/android/utils/ErrorMessageAdapter.java
Normal file
|
@ -0,0 +1,80 @@
|
||||||
|
/* ownCloud Android client application
|
||||||
|
* Copyright (C) 2014 ownCloud Inc.
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License version 2,
|
||||||
|
* as published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
package com.owncloud.android.utils;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
import android.content.res.Resources;
|
||||||
|
|
||||||
|
import com.owncloud.android.R;
|
||||||
|
import com.owncloud.android.lib.common.operations.RemoteOperation;
|
||||||
|
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
|
||||||
|
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
|
||||||
|
import com.owncloud.android.operations.DownloadFileOperation;
|
||||||
|
import com.owncloud.android.operations.UploadFileOperation;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class to choose proper error messages to show to the user depending on the results of operations, always following the same policy
|
||||||
|
*
|
||||||
|
* @author masensio
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class ErrorMessageAdapter {
|
||||||
|
|
||||||
|
public ErrorMessageAdapter() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getErrorCauseMessage(RemoteOperationResult result, RemoteOperation operation, Resources res) {
|
||||||
|
|
||||||
|
String message = null;
|
||||||
|
|
||||||
|
if (operation instanceof UploadFileOperation) {
|
||||||
|
|
||||||
|
if (result.isSuccess()) {
|
||||||
|
message = String.format(res.getString(R.string.uploader_upload_succeeded_content_single),
|
||||||
|
((UploadFileOperation) operation).getFileName());
|
||||||
|
} else {
|
||||||
|
if (result.getCode() == ResultCode.LOCAL_STORAGE_FULL
|
||||||
|
|| result.getCode() == ResultCode.LOCAL_STORAGE_NOT_COPIED) {
|
||||||
|
message = String.format(res.getString(R.string.error__upload__local_file_not_copied),
|
||||||
|
((UploadFileOperation) operation).getFileName());
|
||||||
|
} else if (result.getCode() == ResultCode.QUOTA_EXCEEDED) {
|
||||||
|
message = res.getString(R.string.failed_upload_quota_exceeded_text);
|
||||||
|
} else {
|
||||||
|
message = String.format(res.getString(R.string.uploader_upload_failed_content_single),
|
||||||
|
((UploadFileOperation) operation).getFileName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if (operation instanceof DownloadFileOperation) {
|
||||||
|
|
||||||
|
if (result.isSuccess()) {
|
||||||
|
message = String.format(res.getString(R.string.downloader_download_succeeded_content),
|
||||||
|
new File(((DownloadFileOperation) operation).getSavePath()).getName());
|
||||||
|
} else {
|
||||||
|
message = String.format(res.getString(R.string.downloader_download_failed_content),
|
||||||
|
new File(((DownloadFileOperation) operation).getSavePath()).getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue