Skip to content

Commit

Permalink
Merge pull request nextcloud#12527 from nextcloud/feature/global_uplo…
Browse files Browse the repository at this point in the history
…ad_pause

Add global upload pause button
  • Loading branch information
JonasMayerDev authored Feb 13, 2024
2 parents d560f20 + 4e184d1 commit 90f588d
Show file tree
Hide file tree
Showing 17 changed files with 283 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ class BackgroundJobFactory @Inject constructor(
viewThemeUtils.get(),
localBroadcastManager.get(),
backgroundJobManager.get(),
preferences,
context,
params
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class FileUploadHelper {
}
}

private fun cancelAndRestartUploadJob(user: User) {
fun cancelAndRestartUploadJob(user: User) {
backgroundJobManager.run {
cancelFilesUploadJob(user)
startFilesUploadJob(user)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import com.nextcloud.client.device.PowerManagementService
import com.nextcloud.client.jobs.BackgroundJobManager
import com.nextcloud.client.jobs.BackgroundJobManagerImpl
import com.nextcloud.client.network.ConnectivityService
import com.nextcloud.client.preferences.AppPreferences
import com.nextcloud.model.WorkerState
import com.nextcloud.model.WorkerStateLiveData
import com.owncloud.android.datamodel.FileDataStorageManager
Expand All @@ -58,6 +59,7 @@ class FileUploadWorker(
val viewThemeUtils: ViewThemeUtils,
val localBroadcastManager: LocalBroadcastManager,
private val backgroundJobManager: BackgroundJobManager,
val preferences: AppPreferences,
val context: Context,
params: WorkerParameters
) : Worker(context, params), OnDatatransferProgressListener {
Expand Down Expand Up @@ -136,11 +138,20 @@ class FileUploadWorker(
WorkerStateLiveData.instance().setWorkState(WorkerState.Idle)
}

@Suppress("ReturnCount")
private fun retrievePagesBySortingUploadsByID(): Result {
val accountName = inputData.getString(ACCOUNT) ?: return Result.failure()
var currentPage = uploadsStorageManager.getCurrentAndPendingUploadsForAccountPageAscById(-1, accountName)

while (currentPage.isNotEmpty() && !isStopped) {
if (preferences.isGlobalUploadPaused) {
Log_OC.d(TAG, "Upload is paused, skip uploading files!")
notificationManager.notifyPaused(
intents.notificationStartIntent(null)
)
return Result.success()
}

Log_OC.d(TAG, "Handling ${currentPage.size} uploads for account $accountName")
val lastId = currentPage.last().uploadId
uploadFiles(currentPage, accountName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ class FileUploaderIntents(private val context: Context) {
)
}

fun notificationStartIntent(operation: UploadFileOperation): PendingIntent {
fun notificationStartIntent(operation: UploadFileOperation?): PendingIntent {
val intent = UploadListActivity.createIntent(
operation.file,
operation.user,
operation?.file,
operation?.user,
Intent.FLAG_ACTIVITY_CLEAR_TOP,
context
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,14 @@ import com.owncloud.android.operations.UploadFileOperation
import com.owncloud.android.ui.notifications.NotificationUtils
import com.owncloud.android.utils.theme.ViewThemeUtils

class UploadNotificationManager(private val context: Context, private val viewThemeUtils: ViewThemeUtils) {
class UploadNotificationManager(private val context: Context, 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))
Expand All @@ -54,25 +51,26 @@ 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 {
setSmallIcon(R.drawable.notification_icon)
setOngoing(true)
setTicker(context.getString(R.string.foreground_service_upload))
notificationBuilder.run {
setContentTitle(context.getString(R.string.uploader_upload_in_progress_ticker))
setProgress(100, 0, false)
setContentText(
String.format(
context.getString(R.string.uploader_upload_in_progress),
0,
upload.fileName
)
)
setTicker(context.getString(R.string.foreground_service_upload))
setProgress(100, 0, false)
setOngoing(true)
clearActions()

addAction(
Expand All @@ -81,10 +79,6 @@ class UploadNotificationManager(private val context: Context, private val viewTh
pendingIntent
)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
setChannelId(NotificationUtils.NOTIFICATION_CHANNEL_UPLOAD)
}

setContentIntent(startIntent)
}

Expand Down Expand Up @@ -192,4 +186,18 @@ class UploadNotificationManager(private val context: Context, private val viewTh
fun dismissWorkerNotifications() {
notificationManager.cancel(ID)
}

fun notifyPaused(intent: PendingIntent) {
notificationBuilder.apply {
setContentTitle(context.getString(R.string.upload_global_pause_title))
setTicker(context.getString(R.string.upload_global_pause_title))
setOngoing(true)
setAutoCancel(false)
setProgress(0, 0, false)
clearActions()
setContentIntent(intent)
}

showNotification()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,10 @@ default void onDarkThemeModeChanged(DarkMode mode) {

void setCalendarLastBackup(long timestamp);

boolean isGlobalUploadPaused();

void setGlobalUploadPaused(boolean globalPausedState);

void setPdfZoomTipShownCount(int count);

int getPdfZoomTipShownCount();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ public final class AppPreferencesImpl implements AppPreferences {
private static final String PREF__CALENDAR_AUTOMATIC_BACKUP = "calendar_automatic_backup";
private static final String PREF__CALENDAR_LAST_BACKUP = "calendar_last_backup";

private static final String PREF__GLOBAL_PAUSE_STATE = "global_pause_state";

private static final String PREF__PDF_ZOOM_TIP_SHOWN = "pdf_zoom_tip_shown";
private static final String PREF__MEDIA_FOLDER_LAST_PATH = "media_folder_last_path";

Expand Down Expand Up @@ -741,6 +743,16 @@ public void setCalendarLastBackup(long timestamp) {
preferences.edit().putLong(PREF__CALENDAR_LAST_BACKUP, timestamp).apply();
}

@Override
public boolean isGlobalUploadPaused() {
return preferences.getBoolean(PREF__GLOBAL_PAUSE_STATE,false);
}

@Override
public void setGlobalUploadPaused(boolean globalPausedState) {
preferences.edit().putBoolean(PREF__GLOBAL_PAUSE_STATE, globalPausedState).apply();
}

@Override
public void setPdfZoomTipShownCount(int count) {
preferences.edit().putInt(PREF__PDF_ZOOM_TIP_SHOWN, count).apply();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1155,6 +1155,10 @@ public boolean isDrawerIndicatorAvailable() {
return true;
}

public AppPreferences getAppPreferences(){
return preferences;
}

@Override
protected void onStart() {
super.onStart();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package com.owncloud.android.ui.activity;

import android.accounts.Account;
import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
Expand All @@ -49,7 +50,6 @@
import com.owncloud.android.databinding.UploadListLayoutBinding;
import com.owncloud.android.datamodel.OCFile;
import com.owncloud.android.datamodel.UploadsStorageManager;
import com.owncloud.android.db.OCUpload;
import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.common.utils.Log_OC;
Expand All @@ -65,11 +65,11 @@
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.recyclerview.widget.GridLayoutManager;
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 {

Expand Down Expand Up @@ -210,13 +210,17 @@ private void loadItems() {
private void refresh() {
backgroundJobManager.startImmediateFilesSyncJob(false, true);

if(uploadsStorageManager.getFailedUploads().length > 0){
new Thread(() -> FileUploadHelper.Companion.instance().retryFailedUploads(
uploadsStorageManager,
connectivityService,
userAccountManager,
powerManagementService))
.start();
if (uploadsStorageManager.getFailedUploads().length > 0) {
new Thread(() -> {
FileUploadHelper.Companion.instance().retryFailedUploads(
uploadsStorageManager,
connectivityService,
accountManager,
powerManagementService);
this.runOnUiThread(() -> {
uploadListAdapter.loadUploadItemsFromDb();
});
}).start();
DisplayUtils.showSnackMessage(this, R.string.uploader_local_files_uploaded);
}

Expand Down Expand Up @@ -265,13 +269,47 @@ protected void onPause() {
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.activity_upload_list, menu);

updateGlobalPauseIcon(menu.getItem(0));
return true;
}

@SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT")
private void updateGlobalPauseIcon(MenuItem pauseMenuItem) {
if (pauseMenuItem.getItemId() != R.id.action_toggle_global_pause) {
return;
}

int iconId;
String title;
if (preferences.isGlobalUploadPaused()) {
iconId = R.drawable.ic_play;
title = getString(R.string.upload_action_global_upload_resume);
} else {
iconId = R.drawable.ic_pause;
title = getString(R.string.upload_action_global_upload_pause);
}

pauseMenuItem.setIcon(iconId);
pauseMenuItem.setTitle(title);
}

@SuppressLint("NotifyDataSetChanged")
private void toggleGlobalPause(MenuItem pauseMenuItem) {
preferences.setGlobalUploadPaused(!preferences.isGlobalUploadPaused());
updateGlobalPauseIcon(pauseMenuItem);

for (User user : accountManager.getAllUsers()) {
if (user != null) {
FileUploadHelper.Companion.instance().cancelAndRestartUploadJob(user);
}
}

uploadListAdapter.notifyDataSetChanged();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
boolean retval = true;

int itemId = item.getItemId();

if (itemId == android.R.id.home) {
Expand All @@ -280,17 +318,13 @@ public boolean onOptionsItemSelected(MenuItem item) {
} else {
openDrawer();
}
} else if (itemId == R.id.action_clear_failed_uploads) {
for (OCUpload upload : uploadsStorageManager.getFailedButNotDelayedUploadsForCurrentAccount()){
uploadListAdapter.cancelOldErrorNotification(upload);
}
uploadsStorageManager.clearFailedButNotDelayedUploads();
uploadListAdapter.loadUploadItemsFromDb();
} else if (itemId == R.id.action_toggle_global_pause) {
toggleGlobalPause(item);
} else {
retval = super.onOptionsItemSelected(item);
return super.onOptionsItemSelected(item);
}

return retval;
return true;
}

@Override
Expand Down
Loading

0 comments on commit 90f588d

Please sign in to comment.