Skip to content

Commit

Permalink
Merge branch 'stable-3.22' into bump-3.22.3-rc3
Browse files Browse the repository at this point in the history
  • Loading branch information
AlvaroBrey authored Oct 27, 2022
2 parents deee205 + d39b192 commit 7116e27
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@

package com.owncloud.android.files.services

import org.junit.After
import org.junit.Before

class FileUploadWorkerIT : FileUploaderIT() {
@Before
fun enableWorker() {
fun forceUploadWorker() {
FileUploader.setForceNewUploadWorker(true)
}

@After
fun resetForceUploadWorker() {
FileUploader.setForceNewUploadWorker(false)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ internal class BackgroundJobManagerImpl(
.setInputData(data)
.build()

workManager.enqueue(request)
workManager.enqueueUniqueWork(JOB_FILES_UPLOAD + user.accountName, ExistingWorkPolicy.KEEP, request)
}

override fun getFileUploads(user: User): LiveData<List<JobInfo>> {
Expand Down
24 changes: 18 additions & 6 deletions app/src/main/java/com/nextcloud/client/jobs/FilesUploadWorker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import android.text.TextUtils
import androidx.core.app.NotificationCompat
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import androidx.work.Worker
Expand Down Expand Up @@ -74,14 +73,29 @@ class FilesUploadWorker(

override fun doWork(): Result {
val accountName = inputData.getString(ACCOUNT)
if (TextUtils.isEmpty(accountName)) {
if (accountName.isNullOrEmpty()) {
Log_OC.w(TAG, "User was null for file upload worker")
return Result.failure() // user account is needed
}

// get all pending uploads
var currentAndPendingUploadsForAccount =
uploadsStorageManager.getCurrentAndPendingUploadsForAccount(accountName)
while (currentAndPendingUploadsForAccount.isNotEmpty()) {
Log_OC.d(TAG, "Handling ${currentAndPendingUploadsForAccount.size} uploads for account $accountName")
handlePendingUploads(currentAndPendingUploadsForAccount, accountName)
currentAndPendingUploadsForAccount =
uploadsStorageManager.getCurrentAndPendingUploadsForAccount(accountName)
}

Log_OC.d(TAG, "No more pending uploads for account $accountName, stopping work")
return Result.success()
}

private fun handlePendingUploads(uploads: Array<OCUpload>, accountName: String) {
val user = userAccountManager.getUser(accountName)

// get all pending uploads
for (upload in uploadsStorageManager.getCurrentAndPendingUploadsForAccount(accountName!!)) {
for (upload in uploads) {
// create upload file operation
if (user.isPresent) {
val uploadFileOperation = createUploadFileOperation(upload, user.get())
Expand All @@ -100,8 +114,6 @@ class FilesUploadWorker(
uploadsStorageManager.removeUpload(upload.uploadId)
}
}

return Result.success()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class StackRemoteViewsFactory(
private var hasLoadMore = false

override fun onCreate() {
Log_OC.d(this, "onCreate")
Log_OC.d(TAG, "onCreate")
val appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)

widgetConfiguration = widgetRepository.getWidget(appWidgetId)
Expand All @@ -110,23 +110,27 @@ class StackRemoteViewsFactory(
override fun onDataSetChanged() {
CoroutineScope(Dispatchers.IO).launch {
try {
val client = clientFactory.createNextcloudClient(widgetConfiguration.user.get())
val result =
DashboardGetWidgetItemsRemoteOperation(widgetConfiguration.widgetId, LIMIT_SIZE).execute(client)
widgetItems = result.resultData[widgetConfiguration.widgetId] ?: emptyList()

hasLoadMore = widgetConfiguration.moreButton != null &&
widgetItems.size == LIMIT_SIZE
if (widgetConfiguration.user.isPresent) {
val client = clientFactory.createNextcloudClient(widgetConfiguration.user.get())
val result =
DashboardGetWidgetItemsRemoteOperation(widgetConfiguration.widgetId, LIMIT_SIZE).execute(client)
widgetItems = result.resultData[widgetConfiguration.widgetId] ?: emptyList()

hasLoadMore = widgetConfiguration.moreButton != null &&
widgetItems.size == LIMIT_SIZE
} else {
Log_OC.w(TAG, "User not present for widget update")
}
} catch (e: ClientFactory.CreationException) {
Log_OC.e(this, "Error updating widget", e)
Log_OC.e(TAG, "Error updating widget", e)
}
}

Log_OC.d("WidgetService", "onDataSetChanged")
Log_OC.d(TAG, "onDataSetChanged")
}

override fun onDestroy() {
Log_OC.d("WidgetService", "onDestroy")
Log_OC.d(TAG, "onDestroy")

widgetItems = emptyList()
}
Expand Down Expand Up @@ -195,7 +199,7 @@ class StackRemoteViewsFactory(
setImageViewBitmap(R.id.icon, glide.get())
}
} catch (e: Exception) {
Log_OC.d(this, "Error setting icon", e)
Log_OC.d(TAG, "Error setting icon", e)
setImageViewResource(R.id.icon, R.drawable.ic_dashboard)
}
}
Expand Down Expand Up @@ -238,6 +242,7 @@ class StackRemoteViewsFactory(
}

companion object {
private val TAG = DashboardWidgetService::class.simpleName
const val LIMIT_SIZE = 14
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,12 @@
import com.nextcloud.client.onboarding.FirstRunActivity;
import com.nextcloud.client.onboarding.OnboardingService;
import com.nextcloud.client.preferences.AppPreferences;
import com.nextcloud.java.util.Optional;
import com.owncloud.android.MainApp;
import com.owncloud.android.R;
import com.owncloud.android.databinding.AccountSetupBinding;
import com.owncloud.android.databinding.AccountSetupWebviewBinding;
import com.owncloud.android.datamodel.FileDataStorageManager;
import com.owncloud.android.lib.common.OwnCloudAccount;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.OwnCloudClientFactory;
Expand All @@ -103,10 +105,12 @@
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
import com.owncloud.android.lib.common.utils.Log_OC;
import com.owncloud.android.lib.resources.status.GetCapabilitiesRemoteOperation;
import com.owncloud.android.lib.resources.status.NextcloudVersion;
import com.owncloud.android.lib.resources.status.OCCapability;
import com.owncloud.android.lib.resources.status.OwnCloudVersion;
import com.owncloud.android.lib.resources.users.GetUserInfoRemoteOperation;
import com.owncloud.android.operations.DetectAuthenticationMethodOperation.AuthenticationMethod;
import com.owncloud.android.operations.GetCapabilitiesOperation;
import com.owncloud.android.operations.GetServerInfoOperation;
import com.owncloud.android.providers.DocumentsStorageProvider;
import com.owncloud.android.services.OperationsService;
Expand All @@ -119,21 +123,22 @@
import com.owncloud.android.utils.DisplayUtils;
import com.owncloud.android.utils.ErrorMessageAdapter;
import com.owncloud.android.utils.PermissionUtil;
import com.owncloud.android.utils.theme.CapabilityUtils;
import com.owncloud.android.utils.theme.ViewThemeUtils;

import java.io.InputStream;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.Executors;

import javax.inject.Inject;

import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
Expand Down Expand Up @@ -440,8 +445,13 @@ public void onPageFinished(WebView view, String url) {
accountSetupWebviewBinding.loginWebviewProgressBar.setVisibility(View.GONE);
accountSetupWebviewBinding.loginWebview.setVisibility(View.VISIBLE);

viewThemeUtils.platform.colorStatusBar(AuthenticatorActivity.this, primaryColor);
getWindow().setNavigationBarColor(primaryColor);
if (mServerInfo.mVersion != null && mServerInfo.mVersion.isOlderThan(NextcloudVersion.nextcloud_25)) {
viewThemeUtils.platform.colorStatusBar(AuthenticatorActivity.this, primaryColor);
getWindow().setNavigationBarColor(primaryColor);
} else {
viewThemeUtils.platform.resetStatusBar(AuthenticatorActivity.this);
getWindow().setNavigationBarColor(ContextCompat.getColor(AuthenticatorActivity.this, R.color.bg_default));
}
}

public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Expand Down Expand Up @@ -1132,15 +1142,7 @@ public void onAuthenticatorTaskCallback(RemoteOperationResult<UserInfo> result)

if (success) {
accountManager.setCurrentOwnCloudAccount(mAccount.name);
setupColorCapability();
if (onlyAdd) {
finish();
} else {
Intent i = new Intent(this, FileDisplayActivity.class);
i.setAction(FileDisplayActivity.RESTART);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
getUserCapabilitiesAndFinish();
} else {
// init webView again
if (accountSetupWebviewBinding != null) {
Expand Down Expand Up @@ -1192,15 +1194,36 @@ public void onAuthenticatorTaskCallback(RemoteOperationResult<UserInfo> result)
}
}

/**
* Caches a fake OCCapability with only the server color, so that it is immediately available for drawing the next
* screens
*/
private void setupColorCapability() {
final OCCapability colorCapability = new OCCapability();
colorCapability.setServerColor(colorUtil.colorToHexString(primaryColor));
colorCapability.setAccountName(mAccount.name);
CapabilityUtils.updateCapability(colorCapability);
private void endSuccess() {
if (onlyAdd) {
finish();
} else {
Intent i = new Intent(this, FileDisplayActivity.class);
i.setAction(FileDisplayActivity.RESTART);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
}

private void getUserCapabilitiesAndFinish() {
final Handler handler = new Handler();
final Optional<User> user = accountManager.getUser(mAccount.name);

if (user.isPresent()) {
Executors.newSingleThreadExecutor().execute(() -> {
try {
final FileDataStorageManager storageManager = new FileDataStorageManager(user.get(), getContentResolver());
new GetCapabilitiesOperation(storageManager).execute(MainApp.getAppContext());
handler.post(this::endSuccess);
} catch (Exception e) {
Log_OC.e(TAG, "Failed to fetch capabilities", e);
handler.post(this::endSuccess);
}
});
} else {
Log_OC.w(TAG, "User not present for fetching capabilities");
endSuccess();
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -912,19 +912,7 @@ public static void uploadNewFile(
boolean requiresCharging,
NameCollisionPolicy nameCollisionPolicy
) {
Intent intent = new Intent(context, FileUploader.class);

intent.putExtra(FileUploader.KEY_ACCOUNT, user.toPlatformAccount());
intent.putExtra(FileUploader.KEY_USER, user);
intent.putExtra(FileUploader.KEY_LOCAL_FILE, localPaths);
intent.putExtra(FileUploader.KEY_REMOTE_FILE, remotePaths);
intent.putExtra(FileUploader.KEY_MIME_TYPE, mimeTypes);
intent.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, behaviour);
intent.putExtra(FileUploader.KEY_CREATE_REMOTE_FOLDER, createRemoteFolder);
intent.putExtra(FileUploader.KEY_CREATED_BY, createdBy);
intent.putExtra(FileUploader.KEY_WHILE_ON_WIFI_ONLY, requiresWifi);
intent.putExtra(FileUploader.KEY_WHILE_CHARGING_ONLY, requiresCharging);
intent.putExtra(FileUploader.KEY_NAME_COLLISION_POLICY, nameCollisionPolicy);

if (useFilesUploadWorker(context)) {
new FilesUploadHelper().uploadNewFiles(user,
Expand All @@ -934,11 +922,28 @@ public static void uploadNewFile(
createdBy,
requiresWifi,
requiresCharging,
nameCollisionPolicy);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent);
nameCollisionPolicy,
behaviour);
} else {
context.startService(intent);
Intent intent = new Intent(context, FileUploader.class);

intent.putExtra(FileUploader.KEY_ACCOUNT, user.toPlatformAccount());
intent.putExtra(FileUploader.KEY_USER, user);
intent.putExtra(FileUploader.KEY_LOCAL_FILE, localPaths);
intent.putExtra(FileUploader.KEY_REMOTE_FILE, remotePaths);
intent.putExtra(FileUploader.KEY_MIME_TYPE, mimeTypes);
intent.putExtra(FileUploader.KEY_LOCAL_BEHAVIOUR, behaviour);
intent.putExtra(FileUploader.KEY_CREATE_REMOTE_FOLDER, createRemoteFolder);
intent.putExtra(FileUploader.KEY_CREATED_BY, createdBy);
intent.putExtra(FileUploader.KEY_WHILE_ON_WIFI_ONLY, requiresWifi);
intent.putExtra(FileUploader.KEY_WHILE_CHARGING_ONLY, requiresCharging);
intent.putExtra(FileUploader.KEY_NAME_COLLISION_POLICY, nameCollisionPolicy);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent);
} else {
context.startService(intent);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,13 +314,15 @@ private void searchAndDisplayAfterChangingFolder() {
}

private void runGallerySearchTask() {
photoSearchTask = new GallerySearchTask(this,
accountManager.getUser(),
mContainerActivity.getStorageManager(),
startDate,
endDate,
limit)
.execute();
if (mContainerActivity != null) {
photoSearchTask = new GallerySearchTask(this,
accountManager.getUser(),
mContainerActivity.getStorageManager(),
startDate,
endDate,
limit)
.execute();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ class FilesUploadHelper {
createdBy: Int,
requiresWifi: Boolean,
requiresCharging: Boolean,
nameCollisionPolicy: NameCollisionPolicy
nameCollisionPolicy: NameCollisionPolicy,
localBehavior: Int
) {
for (i in localPaths.indices) {
OCUpload(localPaths[i], remotePaths[i], user.accountName).apply {
Expand All @@ -62,6 +63,7 @@ class FilesUploadHelper {
uploadStatus = UploadsStorageManager.UploadStatus.UPLOAD_IN_PROGRESS
this.createdBy = createdBy
isCreateRemoteFolder = createRemoteFolder
localAction = localBehavior
}.also {
uploadsStorageManager.storeUpload(it)
}
Expand Down
14 changes: 10 additions & 4 deletions app/src/main/java/com/owncloud/android/utils/PermissionUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import android.content.pm.PackageManager
import android.content.pm.ResolveInfo
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.provider.Settings
import androidx.annotation.RequiresApi
Expand Down Expand Up @@ -213,10 +214,7 @@ object PermissionUtil {
if (shouldRequestPermission &&
activity.supportFragmentManager.findFragmentByTag(PERMISSION_CHOICE_DIALOG_TAG) == null
) {
activity.supportFragmentManager.setFragmentResultListener(
StoragePermissionDialogFragment.REQUEST_KEY,
activity
) { _, resultBundle ->
val listener: (requestKey: String, result: Bundle) -> Unit = { _, resultBundle ->
val result: StoragePermissionDialogFragment.Result? =
resultBundle.getParcelable(StoragePermissionDialogFragment.RESULT_KEY)
if (result != null) {
Expand All @@ -238,6 +236,14 @@ object PermissionUtil {
}
}

activity.runOnUiThread {
activity.supportFragmentManager.setFragmentResultListener(
StoragePermissionDialogFragment.REQUEST_KEY,
activity,
listener
)
}

val dialogFragment = StoragePermissionDialogFragment.newInstance(permissionRequired)
dialogFragment.show(activity.supportFragmentManager, PERMISSION_CHOICE_DIALOG_TAG)
}
Expand Down
Loading

0 comments on commit 7116e27

Please sign in to comment.