浏览代码

open file from notification (#4019)

open file from notification
Tobias Kaminsky 6 年之前
父节点
当前提交
fabe3b2332

+ 14 - 2
src/main/java/com/owncloud/android/jobs/NotificationJob.java

@@ -57,6 +57,8 @@ import com.owncloud.android.lib.resources.notifications.DeleteNotificationRemote
 import com.owncloud.android.lib.resources.notifications.GetNotificationRemoteOperation;
 import com.owncloud.android.lib.resources.notifications.models.Action;
 import com.owncloud.android.lib.resources.notifications.models.Notification;
+import com.owncloud.android.lib.resources.notifications.models.RichObject;
+import com.owncloud.android.ui.activity.FileDisplayActivity;
 import com.owncloud.android.ui.activity.NotificationsActivity;
 import com.owncloud.android.ui.notifications.NotificationUtils;
 import com.owncloud.android.utils.PushUtils;
@@ -150,9 +152,19 @@ public class NotificationJob extends Job {
     }
 
     private void sendNotification(Notification notification, Account account) {
-        Intent intent = new Intent(context, NotificationsActivity.class);
-        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
+        RichObject file = notification.subjectRichParameters.get("file");
+
+        Intent intent;
+        if (file == null) {
+            intent = new Intent(context, NotificationsActivity.class);
+        } else {
+            intent = new Intent(context, FileDisplayActivity.class);
+            intent.setAction(Intent.ACTION_VIEW);
+            intent.putExtra(FileDisplayActivity.KEY_FILE_ID, file.id);
+        }
         intent.putExtra(KEY_NOTIFICATION_ACCOUNT, account.name);
+        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
+
         PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT);
         int pushNotificationId = randomId.nextInt();
 

+ 16 - 10
src/main/java/com/owncloud/android/ui/activity/FileDisplayActivity.java

@@ -176,6 +176,7 @@ public class FileDisplayActivity extends FileActivity
 
     public static final String TAG_PUBLIC_LINK = "PUBLIC_LINK";
     public static final String FTAG_CHOOSER_DIALOG = "CHOOSER_DIALOG";
+    public static final String KEY_FILE_ID = "KEY_FILE_ID";
 
     private static final String KEY_WAITING_TO_PREVIEW = "WAITING_TO_PREVIEW";
     private static final String KEY_SYNC_IN_PROGRESS = "SYNC_IN_PROGRESS";
@@ -2622,25 +2623,30 @@ public class FileDisplayActivity extends FileActivity
     }
 
     private void handleOpenFileViaIntent(Intent intent) {
-        showLoadingDialog("Retrieving file…");
+        showLoadingDialog(getString(R.string.retrieving_file));
 
         String accountName = intent.getStringExtra("KEY_ACCOUNT");
 
-        Account newAccount = getUserAccountManager().getAccountByName(accountName);
+        Account newAccount;
+        if (accountName == null) {
+            newAccount = getAccount();
+        } else {
+            newAccount = getUserAccountManager().getAccountByName(accountName);
 
-        if (newAccount == null) {
-            dismissLoadingDialog();
-            DisplayUtils.showSnackMessage(this, "Associated account not found!");
-            return;
-        }
+            if (newAccount == null) {
+                dismissLoadingDialog();
+                DisplayUtils.showSnackMessage(this, getString(R.string.associated_account_not_found));
+                return;
+            }
 
-        setAccount(newAccount);
+            setAccount(newAccount);
+        }
 
-        String fileId = String.valueOf(intent.getStringExtra("KEY_FILE_ID"));
+        String fileId = String.valueOf(intent.getStringExtra(KEY_FILE_ID));
 
         if ("null".equals(fileId)) {
             dismissLoadingDialog();
-            DisplayUtils.showSnackMessage(this, "Error retrieving file");
+            DisplayUtils.showSnackMessage(this, getString(R.string.error_retrieving_file));
             return;
         }
 

+ 15 - 2
src/main/java/com/owncloud/android/ui/adapter/NotificationListAdapter.java

@@ -52,6 +52,7 @@ import com.owncloud.android.lib.common.OwnCloudClient;
 import com.owncloud.android.lib.resources.notifications.models.Action;
 import com.owncloud.android.lib.resources.notifications.models.Notification;
 import com.owncloud.android.lib.resources.notifications.models.RichObject;
+import com.owncloud.android.ui.activity.FileDisplayActivity;
 import com.owncloud.android.ui.activity.NotificationsActivity;
 import com.owncloud.android.ui.asynctasks.DeleteNotificationTask;
 import com.owncloud.android.ui.asynctasks.NotificationExecuteActionTask;
@@ -74,6 +75,7 @@ import butterknife.ButterKnife;
  * This Adapter populates a RecyclerView with all notifications for an account within the app.
  */
 public class NotificationListAdapter extends RecyclerView.Adapter<NotificationListAdapter.NotificationViewHolder> {
+    private static final String FILE = "file";
     private StyleSpan styleSpanBold = new StyleSpan(Typeface.BOLD);
     private ForegroundColorSpan foregroundColorSpanBlack = new ForegroundColorSpan(Color.BLACK);
 
@@ -106,8 +108,9 @@ public class NotificationListAdapter extends RecyclerView.Adapter<NotificationLi
         holder.dateTime.setText(DisplayUtils.getRelativeTimestamp(notificationsActivity,
                 notification.getDatetime().getTime()));
 
+        RichObject file = notification.subjectRichParameters.get(FILE);
         String subject = notification.getSubject();
-        if (!TextUtils.isEmpty(notification.getLink())) {
+        if (file == null && !TextUtils.isEmpty(notification.getLink())) {
             subject = subject + " ↗";
             holder.subject.setTypeface(holder.subject.getTypeface(), Typeface.BOLD);
             holder.subject.setOnClickListener(v -> openLink(notification.getLink()));
@@ -118,6 +121,16 @@ public class NotificationListAdapter extends RecyclerView.Adapter<NotificationLi
             } else {
                 holder.subject.setText(subject);
             }
+
+            if (file != null && !TextUtils.isEmpty(file.id)) {
+                holder.subject.setOnClickListener(v -> {
+                    Intent intent = new Intent(notificationsActivity, FileDisplayActivity.class);
+                    intent.setAction(Intent.ACTION_VIEW);
+                    intent.putExtra(FileDisplayActivity.KEY_FILE_ID, file.id);
+
+                    notificationsActivity.startActivity(intent);
+                });
+            }
         }
 
         holder.message.setText(notification.getMessage());
@@ -136,7 +149,7 @@ public class NotificationListAdapter extends RecyclerView.Adapter<NotificationLi
         LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
             ViewGroup.LayoutParams.WRAP_CONTENT);
         params.setMargins(20, 0, 20, 0);
-        
+
         for (Action action : notification.getActions()) {
             button = new MaterialButton(notificationsActivity);
 

+ 3 - 0
src/main/res/values/strings.xml

@@ -865,4 +865,7 @@
     <string name="shared_avatar_desc">Avatar from shared user</string>
     <string name="shared_with_you_by">Shared with you by %1$s</string>
     <string name="reshare_not_allowed">Resharing is not allowed</string>
+    <string name="retrieving_file">Retrieving file…</string>
+    <string name="associated_account_not_found">Associated account not found!</string>
+    <string name="error_retrieving_file">Error retrieving file</string>
 </resources>