Browse Source

New class ErrorMessageAdapter to choose proper error message to show to the user depending on the results of operations

masensio 11 năm trước cách đây
mục cha
commit
99dcc9c5d5

+ 10 - 9
src/com/owncloud/android/files/services/FileDownloader.java

@@ -44,6 +44,7 @@ import com.owncloud.android.ui.activity.FileActivity;
 import com.owncloud.android.ui.activity.FileDisplayActivity;
 import com.owncloud.android.ui.preview.PreviewImageActivity;
 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.NotificationBuilderWithProgressBar;
 
@@ -461,9 +462,9 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
     private void notifyDownloadResult(DownloadFileOperation download, RemoteOperationResult downloadResult) {
         mNotificationManager.cancel(R.string.downloader_download_in_progress_ticker);
         if (!downloadResult.isCancelled()) {
-            int tickerId = (downloadResult.isSuccess()) ? R.string.downloader_download_succeeded_ticker : R.string.downloader_download_failed_ticker;
-            int contentId = (downloadResult.isSuccess()) ? R.string.downloader_download_succeeded_content : R.string.downloader_download_failed_content;
-
+            int tickerId = (downloadResult.isSuccess()) ? R.string.downloader_download_succeeded_ticker : 
+                R.string.downloader_download_failed_ticker;
+            
             boolean needsToUpdateCredentials = (downloadResult.getCode() == ResultCode.UNAUTHORIZED ||
                                                   (downloadResult.isIdPRedirection()
                                                         && mDownloadClient.getCredentials() == null));
@@ -488,9 +489,8 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
                 updateAccountCredentials.addFlags(Intent.FLAG_FROM_BACKGROUND);
                 mNotificationBuilder
                     .setContentIntent(PendingIntent.getActivity(
-                        this, (int) System.currentTimeMillis(), updateAccountCredentials, PendingIntent.FLAG_ONE_SHOT
-                    ))
-                    .setContentText(String.format(getString(contentId), new File(download.getSavePath()).getName()));
+                        this, (int) System.currentTimeMillis(), updateAccountCredentials, PendingIntent.FLAG_ONE_SHOT));
+                
                 mDownloadClient = null;   // grant that future retries on the same account will get the fresh credentials
                 
             } else {
@@ -511,10 +511,11 @@ public class FileDownloader extends Service implements OnDatatransferProgressLis
                 }
                 mNotificationBuilder
                     .setContentIntent(PendingIntent.getActivity(
-                        this, (int) System.currentTimeMillis(), showDetailsIntent, 0
-                    ))
-                    .setContentText(String.format(getString(contentId), new File(download.getSavePath()).getName()));  
+                        this, (int) System.currentTimeMillis(), showDetailsIntent, 0));
             }
+            
+            mNotificationBuilder.setContentText(ErrorMessageAdapter.getErrorCauseMessage(downloadResult, download, getResources()));
+            
             mNotificationManager.notify(tickerId, mNotificationBuilder.build());
         }
     }

+ 4 - 18
src/com/owncloud/android/files/services/FileUploader.java

@@ -54,6 +54,7 @@ import com.owncloud.android.ui.activity.FileDisplayActivity;
 import com.owncloud.android.ui.activity.InstantUploadActivity;
 import com.owncloud.android.ui.preview.PreviewImageActivity;
 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.NotificationBuilderWithProgressBar;
 
@@ -752,10 +753,7 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
                 ))
                 .setTicker(getString(R.string.uploader_upload_succeeded_ticker))
                 .setContentTitle(getString(R.string.uploader_upload_succeeded_ticker))
-                .setContentText(
-                        String.format(getString(R.string.uploader_upload_succeeded_content_single),
-                        upload.getFileName())
-                );
+                .setContentText(ErrorMessageAdapter.getErrorCauseMessage(uploadResult, upload, getResources()));
 
             mNotificationManager.notify(R.string.uploader_upload_in_progress_ticker, mNotificationBuilder.build());  // NOT
                                                                                                                      // AN
@@ -785,6 +783,7 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
             .setContentTitle(getString(tickerId))
             .setAutoCancel(true);
             
+            content =  ErrorMessageAdapter.getErrorCauseMessage(uploadResult, upload, getResources());
             
             if (needsToUpdateCredentials) {
                 // let the user update credentials with one click
@@ -797,23 +796,10 @@ public class FileUploader extends Service implements OnDatatransferProgressListe
                 errorBuilder.setContentIntent(PendingIntent.getActivity(
                     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
             } else {
                 // 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
                 // db entry

+ 80 - 0
src/com/owncloud/android/utils/ErrorMessageAdapter.java

@@ -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;
+    }
+}