Browse Source

Improved error handling when copying shared link to clipboard

David A. Velasco 9 years ago
parent
commit
e43c1d6b3f

+ 3 - 0
res/values/strings.xml

@@ -324,6 +324,9 @@
 
     <string name="copy_link">Copy link</string>
     <string name="clipboard_text_copied">Copied to clipboard</string>
+    <string name="clipboard_no_text_to_copy">No text received to copy to clipboard</string>
+    <string name="clipboard_uxexpected_error">Unexpected error while copying to clipboard</string>
+    <string name="clipboard_label">Text copied from %1$s</string>
 
     <string name="error_cant_bind_to_operations_service">Critical error: cannot perform operations</string>
 

+ 15 - 3
src/com/owncloud/android/operations/CreateShareViaLinkOperation.java

@@ -41,6 +41,8 @@ import com.owncloud.android.lib.resources.shares.OCShare;
 import com.owncloud.android.lib.resources.shares.ShareType;
 import com.owncloud.android.operations.common.SyncOperation;
 
+import java.util.ArrayList;
+
 public class CreateShareViaLinkOperation extends SyncOperation {
 
     private String mPath;
@@ -101,9 +103,19 @@ public class CreateShareViaLinkOperation extends SyncOperation {
         
         if (result.isSuccess()) {
             if (result.getData().size() > 0) {
-                OCShare share = (OCShare) result.getData().get(0);
-                updateData(share);
-            } 
+                Object item = result.getData().get(0);
+                if (item instanceof  OCShare) {
+                    updateData((OCShare) item);
+                } else {
+                    ArrayList<Object> data = result.getData();
+                    result = new RemoteOperationResult(
+                        RemoteOperationResult.ResultCode.SHARE_NOT_FOUND
+                    );
+                    result.setData(data);
+                }
+            } else {
+                result = new RemoteOperationResult(RemoteOperationResult.ResultCode.SHARE_NOT_FOUND);
+            }
         }
         
         return result;

+ 37 - 24
src/com/owncloud/android/ui/activity/CopyToClipboardActivity.java

@@ -21,12 +21,13 @@
 package com.owncloud.android.ui.activity;
 
 import com.owncloud.android.R;
+import com.owncloud.android.lib.common.utils.Log_OC;
 
 import android.app.Activity;
 import android.content.ClipData;
 import android.content.Intent;
 import android.os.Bundle;
-import android.text.ClipboardManager;
+import android.content.ClipboardManager;
 import android.widget.Toast;
 
 /**
@@ -34,33 +35,45 @@ import android.widget.Toast;
  */
 @SuppressWarnings("deprecation")
 public class CopyToClipboardActivity extends Activity {
-    
+
+    private static final String TAG = CopyToClipboardActivity.class.getName();
+
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
-        
-        // get the clipboard system service
-        ClipboardManager clipboardManager = (ClipboardManager) this.getSystemService(CLIPBOARD_SERVICE);
-        
-        // get the text to copy into the clipboard 
-        Intent intent = getIntent();
-        CharSequence text = intent.getCharSequenceExtra(Intent.EXTRA_TEXT);
-        
-        // and put the text the clipboard
-        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
-            // API level >= 11 -> modern Clipboard
-            ClipData clip = ClipData.newPlainText("ownCloud was here", text);
-            ((android.content.ClipboardManager)clipboardManager).setPrimaryClip(clip);
-            
-        } else {
-            // API level >= 11 -> legacy Clipboard
-            clipboardManager.setText(text);    
+
+        try {
+
+            // get the clipboard system service
+            ClipboardManager clipboardManager = (ClipboardManager) this.getSystemService(CLIPBOARD_SERVICE);
+
+            // get the text to copy into the clipboard
+            Intent intent = getIntent();
+            CharSequence text = intent.getCharSequenceExtra(Intent.EXTRA_TEXT);
+
+            if (text != null && text.length() > 0) {
+                // minimum API level >= 11 -> only modern Clipboard
+                ClipData clip = ClipData.newPlainText(
+                    getString(R.string.clipboard_label, getString(R.string.app_name)),
+                    text
+                );
+                clipboardManager.setPrimaryClip(clip);
+
+                // API level < 11 -> legacy Clipboard - NOT SUPPORTED ANYMORE
+                // clipboardManager.setText(text);
+
+                // alert the user that the text is in the clipboard and we're done
+                Toast.makeText(this, R.string.clipboard_text_copied, Toast.LENGTH_SHORT).show();
+            } else {
+                Toast.makeText(this, R.string.clipboard_no_text_to_copy, Toast.LENGTH_SHORT).show();
+            }
+
+        } catch (Exception e) {
+            Toast.makeText(this, R.string.clipboard_uxexpected_error, Toast.LENGTH_SHORT).show();
+            Log_OC.e(TAG, "Exception caught while copying to clipboard", e);
         }
-        
-        // alert the user that the text is in the clipboard and we're done
-        Toast.makeText(this, R.string.clipboard_text_copied, Toast.LENGTH_SHORT).show();
-        
+
         finish();
-    }    
+    }
 
 }