Преглед изворни кода

fix lint and incorporate changes by alper

Signed-off-by: Jonas Mayer <jonas.a.mayer@gmx.net>
Jonas Mayer пре 1 година
родитељ
комит
955de94ea1

+ 8 - 8
app/src/main/java/com/nextcloud/client/jobs/upload/UploadNotificationManager.kt

@@ -37,16 +37,12 @@ import com.owncloud.android.utils.theme.ViewThemeUtils
 
 class UploadNotificationManager(private val context: Context, private val viewThemeUtils: ViewThemeUtils) {
     companion object {
-
         private const val ID = 411
     }
 
     private var notification: Notification? = null
-    private var notificationBuilder: NotificationCompat.Builder
-    private val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
-
-    init {
-        notificationBuilder = NotificationUtils.newNotificationBuilder(context, viewThemeUtils).apply {
+    private var notificationBuilder: NotificationCompat.Builder =
+        NotificationUtils.newNotificationBuilder(context, viewThemeUtils).apply {
             setContentTitle(context.getString(R.string.foreground_service_upload))
             setSmallIcon(R.drawable.notification_icon)
             setLargeIcon(BitmapFactory.decodeResource(context.resources, R.drawable.notification_icon))
@@ -55,13 +51,15 @@ class UploadNotificationManager(private val context: Context, private val viewTh
                 setChannelId(NotificationUtils.NOTIFICATION_CHANNEL_UPLOAD)
             }
         }
+    private val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
 
+    init {
         notification = notificationBuilder.build()
     }
 
     @Suppress("MagicNumber")
     fun prepareForStart(upload: UploadFileOperation, pendingIntent: PendingIntent, startIntent: PendingIntent) {
-        notificationBuilder = NotificationUtils.newNotificationBuilder(context, viewThemeUtils).apply {
+        notificationBuilder.run {
             setSmallIcon(R.drawable.notification_icon)
             setOngoing(true)
             setTicker(context.getString(R.string.foreground_service_upload))
@@ -195,12 +193,14 @@ class UploadNotificationManager(private val context: Context, private val viewTh
     }
 
     fun notifyPaused(startIntent: PendingIntent) {
-        notificationBuilder = NotificationUtils.newNotificationBuilder(context, viewThemeUtils).apply {
+        notificationBuilder.apply {
             setSmallIcon(R.drawable.notification_icon)
             setOngoing(true)
+            setAutoCancel(false)
             setTicker(context.getString(R.string.upload_global_pause))
             setContentTitle(context.getString(R.string.upload_global_pause_title))
             setContentText(context.getString(R.string.upload_global_pause))
+            setProgress(0, 0, false)
             clearActions()
 
             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

+ 31 - 28
app/src/main/java/com/owncloud/android/ui/activity/UploadListActivity.java

@@ -68,9 +68,8 @@ import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 
 /**
- * Activity listing pending, active, and completed uploads. User can delete
- * completed uploads from view. Content of this list of coming from
- * {@link UploadsStorageManager}.
+ * Activity listing pending, active, and completed uploads. User can delete completed uploads from view. Content of this
+ * list of coming from {@link UploadsStorageManager}.
  */
 public class UploadListActivity extends FileActivity {
 
@@ -211,7 +210,7 @@ public class UploadListActivity extends FileActivity {
     private void refresh() {
         backgroundJobManager.startImmediateFilesSyncJob(false, true);
 
-        if(uploadsStorageManager.getFailedUploads().length > 0){
+        if (uploadsStorageManager.getFailedUploads().length > 0) {
             new Thread(() -> {
                 FileUploadHelper.Companion.instance().retryFailedUploads(
                     uploadsStorageManager,
@@ -267,32 +266,46 @@ public class UploadListActivity extends FileActivity {
     }
 
     @Override
-    @SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT")
     public boolean onCreateOptionsMenu(Menu menu) {
         MenuInflater inflater = getMenuInflater();
         inflater.inflate(R.menu.activity_upload_list, menu);
+        updateGlobalPauseIcon(menu.getItem(0));
+        return true;
+    }
 
-        if (menu.getItem(0).getItemId() == R.id.action_toggle_global_pause){
-            if (preferences.getGlobalUploadPaused()){
-                menu.getItem(0).setIcon(android.R.drawable.ic_media_play);
-                menu.getItem(0).setTitle(getApplicationContext().getString(
+    @SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT")
+    private void updateGlobalPauseIcon(MenuItem pauseMenuItem) {
+        if (pauseMenuItem.getItemId() == R.id.action_toggle_global_pause) {
+            if (preferences.getGlobalUploadPaused()) {
+                pauseMenuItem.setIcon(android.R.drawable.ic_media_play);
+                pauseMenuItem.setTitle(getApplicationContext().getString(
                     R.string.upload_action_global_upload_resume
-                                                                          ));
-            }else{
-                menu.getItem(0).setIcon(android.R.drawable.ic_media_pause);
-                menu.getItem(0).setTitle(getApplicationContext().getString(
+                                                                        ));
+            } else {
+                pauseMenuItem.setIcon(android.R.drawable.ic_media_pause);
+                pauseMenuItem.setTitle(getApplicationContext().getString(
                     R.string.upload_action_global_upload_pause
-                                                                          ));
+                                                                        ));
             }
 
         }
+    }
 
-        return true;
+    @SuppressLint("NotifyDataSetChanged")
+    private void toggleGlobalPause(MenuItem pauseMenuItem) {
+        preferences.setGlobalUploadPaused(!preferences.getGlobalUploadPaused());
+        updateGlobalPauseIcon(pauseMenuItem);
+
+        for (User user : accountManager.getAllUsers()) {
+            if (user != null) {
+                FileUploadHelper.Companion.instance().cancelAndRestartUploadJob(user);
+            }
+        }
+
+        uploadListAdapter.notifyDataSetChanged();
     }
 
     @Override
-    @SuppressLint("NotifyDataSetChanged")
-    @SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT")
     public boolean onOptionsItemSelected(MenuItem item) {
 
         int itemId = item.getItemId();
@@ -304,17 +317,7 @@ public class UploadListActivity extends FileActivity {
                 openDrawer();
             }
         } else if (itemId == R.id.action_toggle_global_pause) {
-            preferences.setGlobalUploadPaused(!preferences.getGlobalUploadPaused());
-            if (preferences.getGlobalUploadPaused()){
-                item.setIcon(android.R.drawable.ic_media_play);
-            }else{
-                item.setIcon(android.R.drawable.ic_media_pause);
-            }
-
-            for (User user: accountManager.getAllUsers()){
-                if (user != null) FileUploadHelper.Companion.instance().cancelAndRestartUploadJob(user);
-            }
-            uploadListAdapter.notifyDataSetChanged();
+            toggleGlobalPause(item);
         }
 
         return true;

+ 68 - 68
app/src/main/java/com/owncloud/android/ui/adapter/UploadListAdapter.java

@@ -122,57 +122,56 @@ public class UploadListAdapter extends SectionedRecyclerViewAdapter<SectionedVie
         headerViewHolder.binding.uploadListTitle.setOnClickListener(v -> toggleSectionExpanded(section));
 
         switch (group.type) {
-            case CURRENT: case FINISHED:
-                headerViewHolder.binding.uploadListAction.setImageResource(R.drawable.ic_close);
-                break;
-            case FAILED:
-                headerViewHolder.binding.uploadListAction.setImageResource(R.drawable.ic_dots_vertical);
-                break;
-
+            case CURRENT, FINISHED -> headerViewHolder.binding.uploadListAction.setImageResource(R.drawable.ic_close);
+            case FAILED -> headerViewHolder.binding.uploadListAction.setImageResource(R.drawable.ic_dots_vertical);
         }
 
         headerViewHolder.binding.uploadListAction.setOnClickListener(v -> {
-            PopupMenu popup;
-            final Optional<User> optionalUser = parentActivity.getUser();
             switch (group.type) {
                 case CURRENT -> {
+                    // cancel all current uploads
                     for (OCUpload upload : group.getItems()) {
                         uploadHelper.cancelFileUpload(upload.getRemotePath(), upload.getAccountName());
                     }
                     loadUploadItemsFromDb();
                 }
                 case FINISHED -> {
+                    // clear successfully uploaded section
                     uploadsStorageManager.clearSuccessfulUploads();
                     loadUploadItemsFromDb();
                 }
                 case FAILED -> {
-                    popup = new PopupMenu(MainApp.getAppContext(), headerViewHolder.binding.uploadListAction);
-                    popup.inflate(R.menu.upload_list_failed_options);
-                    popup.setOnMenuItemClickListener(i -> {
-                        int itemId = i.getItemId();
-
-                        if (itemId == R.id.action_upload_list_failed_clear) {
-                            uploadsStorageManager.clearFailedButNotDelayedUploads();
-                            loadUploadItemsFromDb();
-                        } else {
+                    // show popup with option clear or retry filed uploads
+                    createFailedPopupMenu(headerViewHolder);
+                }
+            }
+        });
+    }
 
-                            new Thread(() -> {
-                                FileUploadHelper.Companion.instance().retryFailedUploads(
-                                    uploadsStorageManager,
-                                    connectivityService,
-                                    accountManager,
-                                    powerManagementService);
-                                parentActivity.runOnUiThread(this::loadUploadItemsFromDb);
-                            }).start();
+    private void createFailedPopupMenu(HeaderViewHolder headerViewHolder) {
+        PopupMenu failedPopup = new PopupMenu(MainApp.getAppContext(), headerViewHolder.binding.uploadListAction);
+        failedPopup.inflate(R.menu.upload_list_failed_options);
+        failedPopup.setOnMenuItemClickListener(i -> {
+            int itemId = i.getItemId();
+
+            if (itemId == R.id.action_upload_list_failed_clear) {
+                uploadsStorageManager.clearFailedButNotDelayedUploads();
+                loadUploadItemsFromDb();
+            } else {
 
+                new Thread(() -> {
+                    FileUploadHelper.Companion.instance().retryFailedUploads(
+                        uploadsStorageManager,
+                        connectivityService,
+                        accountManager,
+                        powerManagementService);
+                    parentActivity.runOnUiThread(this::loadUploadItemsFromDb);
+                }).start();
 
-                        }
-                        return true;
-                    });
-                    popup.show();
-                }
             }
+            return true;
         });
+        failedPopup.show();
     }
 
     @Override
@@ -255,7 +254,7 @@ public class UploadListAdapter extends SectionedRecyclerViewAdapter<SectionedVie
         // file size
         if (item.getFileSize() != 0) {
             itemViewHolder.binding.uploadFileSize.setText(String.format("%s, ",
-                    DisplayUtils.bytesToHumanReadable(item.getFileSize())));
+                                                                        DisplayUtils.bytesToHumanReadable(item.getFileSize())));
         } else {
             itemViewHolder.binding.uploadFileSize.setText("");
         }
@@ -330,7 +329,7 @@ public class UploadListAdapter extends SectionedRecyclerViewAdapter<SectionedVie
         }
 
         // show status if same file conflict or local file deleted
-        if (item.getUploadStatus() == UploadStatus.UPLOAD_SUCCEEDED && item.getLastResult() != UploadResult.UPLOADED){
+        if (item.getUploadStatus() == UploadStatus.UPLOAD_SUCCEEDED && item.getLastResult() != UploadResult.UPLOADED) {
             itemViewHolder.binding.uploadStatus.setVisibility(View.VISIBLE);
             itemViewHolder.binding.uploadDate.setVisibility(View.GONE);
             itemViewHolder.binding.uploadFileSize.setVisibility(View.GONE);
@@ -398,16 +397,16 @@ public class UploadListAdapter extends SectionedRecyclerViewAdapter<SectionedVie
                     DisplayUtils.showSnackMessage(
                         v.getRootView().findViewById(android.R.id.content),
                         R.string.local_file_not_found_message
-                    );
+                                                 );
                 }
             });
-        } else if (item.getUploadStatus() == UploadStatus.UPLOAD_SUCCEEDED){
+        } else if (item.getUploadStatus() == UploadStatus.UPLOAD_SUCCEEDED) {
             itemViewHolder.binding.uploadListItemLayout.setOnClickListener(v -> onUploadedItemClick(item));
         }
 
 
         // click on thumbnail to open locally
-        if (item.getUploadStatus() != UploadStatus.UPLOAD_SUCCEEDED){
+        if (item.getUploadStatus() != UploadStatus.UPLOAD_SUCCEEDED) {
             itemViewHolder.binding.thumbnail.setOnClickListener(v -> onUploadingItemClick(item));
         }
 
@@ -420,17 +419,17 @@ public class UploadListAdapter extends SectionedRecyclerViewAdapter<SectionedVie
         fakeFileToCheatThumbnailsCacheManagerInterface.setMimeType(item.getMimeType());
 
         boolean allowedToCreateNewThumbnail = ThumbnailsCacheManager.cancelPotentialThumbnailWork(
-                fakeFileToCheatThumbnailsCacheManagerInterface, itemViewHolder.binding.thumbnail
-        );
+            fakeFileToCheatThumbnailsCacheManagerInterface, itemViewHolder.binding.thumbnail
+                                                                                                 );
 
         // TODO this code is duplicated; refactor to a common place
         if (MimeTypeUtil.isImage(fakeFileToCheatThumbnailsCacheManagerInterface)
-                && fakeFileToCheatThumbnailsCacheManagerInterface.getRemoteId() != null &&
-                item.getUploadStatus() == UploadStatus.UPLOAD_SUCCEEDED) {
+            && fakeFileToCheatThumbnailsCacheManagerInterface.getRemoteId() != null &&
+            item.getUploadStatus() == UploadStatus.UPLOAD_SUCCEEDED) {
             // Thumbnail in Cache?
             Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(
-                    String.valueOf(fakeFileToCheatThumbnailsCacheManagerInterface.getRemoteId())
-            );
+                String.valueOf(fakeFileToCheatThumbnailsCacheManagerInterface.getRemoteId())
+                                                                            );
             if (thumbnail != null && !fakeFileToCheatThumbnailsCacheManagerInterface.isUpdateThumbnailNeeded()) {
                 itemViewHolder.binding.thumbnail.setImageBitmap(thumbnail);
             } else {
@@ -438,11 +437,11 @@ public class UploadListAdapter extends SectionedRecyclerViewAdapter<SectionedVie
                 Optional<User> user = parentActivity.getUser();
                 if (allowedToCreateNewThumbnail && user.isPresent()) {
                     final ThumbnailsCacheManager.ThumbnailGenerationTask task =
-                            new ThumbnailsCacheManager.ThumbnailGenerationTask(
-                                itemViewHolder.binding.thumbnail,
-                                parentActivity.getStorageManager(),
-                                user.get()
-                            );
+                        new ThumbnailsCacheManager.ThumbnailGenerationTask(
+                            itemViewHolder.binding.thumbnail,
+                            parentActivity.getStorageManager(),
+                            user.get()
+                        );
                     if (thumbnail == null) {
                         if (MimeTypeUtil.isVideo(fakeFileToCheatThumbnailsCacheManagerInterface)) {
                             thumbnail = ThumbnailsCacheManager.mDefaultVideo;
@@ -451,20 +450,20 @@ public class UploadListAdapter extends SectionedRecyclerViewAdapter<SectionedVie
                         }
                     }
                     final ThumbnailsCacheManager.AsyncThumbnailDrawable asyncDrawable =
-                            new ThumbnailsCacheManager.AsyncThumbnailDrawable(
-                                parentActivity.getResources(),
-                                thumbnail,
-                                task
-                            );
+                        new ThumbnailsCacheManager.AsyncThumbnailDrawable(
+                            parentActivity.getResources(),
+                            thumbnail,
+                            task
+                        );
                     itemViewHolder.binding.thumbnail.setImageDrawable(asyncDrawable);
                     task.execute(new ThumbnailsCacheManager.ThumbnailGenerationTaskObject(
-                            fakeFileToCheatThumbnailsCacheManagerInterface, null));
+                        fakeFileToCheatThumbnailsCacheManagerInterface, null));
                 }
             }
 
             if ("image/png".equals(item.getMimeType())) {
                 itemViewHolder.binding.thumbnail.setBackgroundColor(parentActivity.getResources()
-                        .getColor(R.color.bg_default));
+                                                                        .getColor(R.color.bg_default));
             }
 
 
@@ -472,14 +471,14 @@ public class UploadListAdapter extends SectionedRecyclerViewAdapter<SectionedVie
             File file = new File(item.getLocalPath());
             // Thumbnail in Cache?
             Bitmap thumbnail = ThumbnailsCacheManager.getBitmapFromDiskCache(
-                    String.valueOf(file.hashCode()));
+                String.valueOf(file.hashCode()));
             if (thumbnail != null) {
                 itemViewHolder.binding.thumbnail.setImageBitmap(thumbnail);
             } else {
                 // generate new Thumbnail
                 if (allowedToCreateNewThumbnail) {
                     final ThumbnailsCacheManager.ThumbnailGenerationTask task =
-                            new ThumbnailsCacheManager.ThumbnailGenerationTask(itemViewHolder.binding.thumbnail);
+                        new ThumbnailsCacheManager.ThumbnailGenerationTask(itemViewHolder.binding.thumbnail);
 
                     if (MimeTypeUtil.isVideo(file)) {
                         thumbnail = ThumbnailsCacheManager.mDefaultVideo;
@@ -499,7 +498,7 @@ public class UploadListAdapter extends SectionedRecyclerViewAdapter<SectionedVie
 
             if ("image/png".equalsIgnoreCase(item.getMimeType())) {
                 itemViewHolder.binding.thumbnail.setBackgroundColor(parentActivity.getResources()
-                        .getColor(R.color.bg_default));
+                                                                        .getColor(R.color.bg_default));
             }
         } else {
             if (optionalUser.isPresent()) {
@@ -630,8 +629,7 @@ public class UploadListAdapter extends SectionedRecyclerViewAdapter<SectionedVie
     }
 
     /**
-     * Gets the status text to show to the user according to the status and last result of the
-     * the given upload.
+     * Gets the status text to show to the user according to the status and last result of the the given upload.
      *
      * @param upload Upload to describe.
      * @return Text describing the status of the given upload.
@@ -712,8 +710,8 @@ public class UploadListAdapter extends SectionedRecyclerViewAdapter<SectionedVie
             case SSL_RECOVERABLE_PEER_UNVERIFIED:
                 status =
                     parentActivity.getString(
-                                R.string.uploads_view_upload_status_failed_ssl_certificate_not_trusted
-                        );
+                        R.string.uploads_view_upload_status_failed_ssl_certificate_not_trusted
+                                            );
                 break;
             case UNKNOWN:
                 status = parentActivity.getString(R.string.uploads_view_upload_status_unknown_fail);
@@ -723,7 +721,7 @@ public class UploadListAdapter extends SectionedRecyclerViewAdapter<SectionedVie
                 break;
             case DELAYED_IN_POWER_SAVE_MODE:
                 status = parentActivity.getString(
-                        R.string.uploads_view_upload_status_waiting_exit_power_save_mode);
+                    R.string.uploads_view_upload_status_waiting_exit_power_save_mode);
                 break;
             case VIRUS_DETECTED:
                 status = parentActivity.getString(R.string.uploads_view_upload_status_virus_detected);
@@ -798,17 +796,17 @@ public class UploadListAdapter extends SectionedRecyclerViewAdapter<SectionedVie
      */
     private void onUploadedItemClick(OCUpload upload) {
         final OCFile file = parentActivity.getStorageManager().getFileByEncryptedRemotePath(upload.getRemotePath());
-        if (file == null){
+        if (file == null) {
             DisplayUtils.showSnackMessage(parentActivity, R.string.error_retrieving_file);
             Log_OC.i(TAG, "Could not find uploaded file on remote.");
             return;
         }
 
-        if (PreviewImageFragment.canBePreviewed(file)){
+        if (PreviewImageFragment.canBePreviewed(file)) {
             //show image preview and stay in uploads tab
             Intent intent = FileDisplayActivity.openFileIntent(parentActivity, parentActivity.getUser().get(), file);
             parentActivity.startActivity(intent);
-        }else{
+        } else {
             Intent intent = new Intent(parentActivity, FileDisplayActivity.class);
             intent.setAction(Intent.ACTION_VIEW);
             intent.putExtra(FileDisplayActivity.KEY_FILE_PATH, upload.getRemotePath());
@@ -904,14 +902,16 @@ public class UploadListAdapter extends SectionedRecyclerViewAdapter<SectionedVie
         }
     }
 
-    public void cancelOldErrorNotification(OCUpload upload){
+    public void cancelOldErrorNotification(OCUpload upload) {
 
         if (mNotificationManager == null) {
             mNotificationManager = (NotificationManager) parentActivity.getSystemService(parentActivity.NOTIFICATION_SERVICE);
         }
 
-        if (upload == null) return;
-        mNotificationManager.cancel(NotificationUtils.createUploadNotificationTag(upload.getRemotePath(),upload.getLocalPath()),
+        if (upload == null) {
+            return;
+        }
+        mNotificationManager.cancel(NotificationUtils.createUploadNotificationTag(upload.getRemotePath(), upload.getLocalPath()),
                                     FileUploadWorker.NOTIFICATION_ERROR_ID);
 
     }

+ 0 - 2
app/src/main/res/values/strings.xml

@@ -522,8 +522,6 @@
     <string name="share_room_clarification">%1$s (conversation)</string>
     <string name="share_known_remote_on_clarification">on %1$s</string>
 
-    <string name="action_clear_failed_uploads">Clear failed uploads</string>
-
     <string name="action_switch_grid_view">Grid view</string>
     <string name="action_switch_list_view">List view</string>