瀏覽代碼

Merge pull request #12111 from nextcloud/fix/SyncFileNotEnoughSpaceDialogFragment

Build Fix
Andy Scherzinger 1 年之前
父節點
當前提交
1868d03a26

+ 67 - 51
app/src/main/java/com/owncloud/android/files/services/FileDownloader.kt

@@ -69,7 +69,6 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings
 import java.io.File
 import java.security.SecureRandom
 import java.util.AbstractList
-import java.util.Vector
 import javax.inject.Inject
 
 class FileDownloader : Service(), OnDatatransferProgressListener, OnAccountsUpdateListener {
@@ -184,54 +183,75 @@ class FileDownloader : Service(), OnDatatransferProgressListener, OnAccountsUpda
         if (!intent.hasExtra(EXTRA_USER) || !intent.hasExtra(EXTRA_FILE)) {
             Log_OC.e(TAG, "Not enough information provided in intent")
             return START_NOT_STICKY
-        } else {
-            val user = intent.getParcelableExtra<User>(EXTRA_USER)
-            val file = intent.getParcelableExtra<OCFile>(EXTRA_FILE)
-            val behaviour = intent.getStringExtra(OCFileListFragment.DOWNLOAD_BEHAVIOUR)
-            var downloadType: DownloadType? = DownloadType.DOWNLOAD
-            if (intent.hasExtra(DOWNLOAD_TYPE)) {
-                downloadType = intent.getSerializableExtra(DOWNLOAD_TYPE) as DownloadType?
-            }
-            val activityName = intent.getStringExtra(SendShareDialog.ACTIVITY_NAME)
-            val packageName = intent.getStringExtra(SendShareDialog.PACKAGE_NAME)
-            conflictUploadId = intent.getLongExtra(ConflictsResolveActivity.EXTRA_CONFLICT_UPLOAD_ID, -1)
-            val requestedDownloads: AbstractList<String> = Vector()
-            try {
-                val newDownload = DownloadFileOperation(
-                    user,
-                    file,
-                    behaviour,
-                    activityName,
-                    packageName,
-                    baseContext,
-                    downloadType
-                )
-                newDownload.addDatatransferProgressListener(this)
-                newDownload.addDatatransferProgressListener(mBinder as FileDownloaderBinder?)
-                val putResult = mPendingDownloads.putIfAbsent(
-                    user!!.accountName,
-                    file!!.remotePath,
-                    newDownload
-                )
-                if (putResult != null) {
-                    val downloadKey = putResult.first
-                    requestedDownloads.add(downloadKey)
-                    sendBroadcastNewDownload(newDownload, putResult.second)
-                } // else, file already in the queue of downloads; don't repeat the request
-            } catch (e: IllegalArgumentException) {
-                Log_OC.e(TAG, "Not enough information provided in intent: " + e.message)
-                return START_NOT_STICKY
-            }
-            if (requestedDownloads.size > 0) {
-                val msg = mServiceHandler!!.obtainMessage()
-                msg.arg1 = startId
-                msg.obj = requestedDownloads
-                mServiceHandler!!.sendMessage(msg)
+        }
+
+        val user = intent.getParcelableExtra<User>(EXTRA_USER)
+        val file = intent.getParcelableExtra<OCFile>(EXTRA_FILE)
+        val behaviour = intent.getStringExtra(OCFileListFragment.DOWNLOAD_BEHAVIOUR)
+        var downloadType: DownloadType? = DownloadType.DOWNLOAD
+        if (intent.hasExtra(DOWNLOAD_TYPE)) {
+            downloadType = intent.getSerializableExtra(DOWNLOAD_TYPE) as DownloadType?
+        }
+        val activityName = intent.getStringExtra(SendShareDialog.ACTIVITY_NAME)
+        val packageName = intent.getStringExtra(SendShareDialog.PACKAGE_NAME)
+        conflictUploadId = intent.getLongExtra(ConflictsResolveActivity.EXTRA_CONFLICT_UPLOAD_ID, -1)
+
+        val requestedDownloads = handleDownloadRequest(user, file, behaviour, downloadType, activityName, packageName)
+
+        if (requestedDownloads.isNotEmpty()) {
+            val msg = mServiceHandler?.obtainMessage()
+            msg?.arg1 = startId
+            msg?.obj = requestedDownloads
+            msg?.let {
+                mServiceHandler?.sendMessage(it)
             }
         }
+
         return START_NOT_STICKY
     }
 
+    @Suppress("LongParameterList")
+    private fun handleDownloadRequest(
+        user: User?,
+        file: OCFile?,
+        behaviour: String?,
+        downloadType: DownloadType?,
+        activityName: String?,
+        packageName: String?
+    ): List<String> {
+        val requestedDownloads: MutableList<String> = ArrayList()
+
+        if (user == null || file == null) {
+            return requestedDownloads
+        }
+
+        try {
+            val newDownload = DownloadFileOperation(
+                user,
+                file,
+                behaviour,
+                activityName,
+                packageName,
+                baseContext,
+                downloadType
+            )
+            newDownload.addDatatransferProgressListener(this)
+            newDownload.addDatatransferProgressListener(mBinder as FileDownloaderBinder?)
+
+            val putResult = mPendingDownloads.putIfAbsent(user.accountName, file.remotePath, newDownload)
+
+            if (putResult != null) {
+                val downloadKey = putResult.first
+                requestedDownloads.add(downloadKey)
+                sendBroadcastNewDownload(newDownload, putResult.second)
+            }
+        } catch (e: IllegalArgumentException) {
+            Log_OC.e(TAG, "Not enough information provided in intent: " + e.message)
+        }
+
+        return requestedDownloads
+    }
+
     /**
      * Provides a binder object that clients can use to perform operations on the queue of downloads,
      * excepting the addition of new files.
@@ -286,13 +306,9 @@ class FileDownloader : Service(), OnDatatransferProgressListener, OnAccountsUpda
             if (download != null) {
                 download.cancel()
             } else {
-                if (mCurrentDownload != null && currentUser.isPresent &&
-                    mCurrentDownload!!
-                        .remotePath
-                        .startsWith(file.remotePath) && account.name == currentUser.get().accountName
-                ) {
-                    mCurrentDownload!!.cancel()
-                }
+                mCurrentDownload?.takeIf {
+                    it.remotePath.startsWith(file.remotePath) && account.name == currentUser?.get()?.accountName
+                }?.cancel()
             }
         }
 

+ 24 - 21
app/src/main/java/com/owncloud/android/ui/activity/FolderPickerActivity.kt

@@ -399,31 +399,34 @@ open class FolderPickerActivity :
     }
 
     override fun onClick(v: View) {
-        if (v == mCancelBtn) {
-            finish()
-        } else if (v == mCopyBtn || v == mMoveBtn) {
-            val i = intent
-            val resultData = Intent()
-            resultData.putExtra(EXTRA_FOLDER, listOfFilesFragment!!.currentFile)
-            val targetFiles = i.getParcelableArrayListExtra<Parcelable>(EXTRA_FILES)
-            if (targetFiles != null) {
-                resultData.putParcelableArrayListExtra(EXTRA_FILES, targetFiles)
-            }
+        when (v) {
+            mCancelBtn -> finish()
+            mCopyBtn, mMoveBtn -> copyOrMove(v)
+        }
+    }
 
-            mTargetFilePaths.let {
-                if (it != null) {
-                    if (v == mCopyBtn) {
-                        fileOperationsHelper.moveOrCopyFiles(OperationsService.ACTION_COPY_FILE, it, file)
-                    } else {
-                        fileOperationsHelper.moveOrCopyFiles(OperationsService.ACTION_MOVE_FILE, it, file)
-                    }
-                }
+    private fun copyOrMove(v: View) {
+        val i = intent
+        val resultData = Intent()
+        resultData.putExtra(EXTRA_FOLDER, listOfFilesFragment?.currentFile)
+
+        i.getParcelableArrayListExtra<Parcelable>(EXTRA_FILES)?.let { targetFiles ->
+            resultData.putParcelableArrayListExtra(EXTRA_FILES, targetFiles)
+        }
 
-                resultData.putStringArrayListExtra(EXTRA_FILE_PATHS, it)
+        mTargetFilePaths?.let {
+            val action = when (v) {
+                mCopyBtn -> OperationsService.ACTION_COPY_FILE
+                mMoveBtn -> OperationsService.ACTION_MOVE_FILE
+                else -> throw IllegalArgumentException("Unknown operation")
             }
-            setResult(RESULT_OK, resultData)
-            finish()
+
+            fileOperationsHelper.moveOrCopyFiles(action, it, file)
+            resultData.putStringArrayListExtra(EXTRA_FILE_PATHS, it)
         }
+
+        setResult(RESULT_OK, resultData)
+        finish()
     }
 
     override fun onRemoteOperationFinish(operation: RemoteOperation<*>?, result: RemoteOperationResult<*>) {

+ 5 - 10
app/src/main/java/com/owncloud/android/ui/dialog/ConfirmationDialogFragment.kt

@@ -64,27 +64,22 @@ open class ConfirmationDialogFragment : DialogFragment(), Injectable {
     }
 
     override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
-        var messageArguments = requireArguments().getStringArray(ARG_MESSAGE_ARGUMENTS)
+        val messageArguments = requireArguments().getStringArray(ARG_MESSAGE_ARGUMENTS) ?: arrayOf<String>()
         val titleId = requireArguments().getInt(ARG_TITLE_ID, -1)
         val messageId = requireArguments().getInt(ARG_MESSAGE_RESOURCE_ID, -1)
         val positiveButtonTextId = requireArguments().getInt(ARG_POSITIVE_BTN_RES, -1)
         val negativeButtonTextId = requireArguments().getInt(ARG_NEGATIVE_BTN_RES, -1)
         val neutralButtonTextId = requireArguments().getInt(ARG_NEUTRAL_BTN_RES, -1)
 
-        if (messageArguments == null) {
-            messageArguments = arrayOf<String?>()
-        }
+        @Suppress("SpreadOperator")
+        val message = getString(messageId, *messageArguments)
 
         val builder = MaterialAlertDialogBuilder(requireActivity())
+            .setTitle(if (titleId == 0) { R.string.dialog_alert_title } else { titleId })
             .setIcon(com.owncloud.android.R.drawable.ic_warning)
             .setIconAttribute(R.attr.alertDialogIcon)
-            .setMessage(String.format(getString(messageId), messageArguments))
+            .setMessage(message)
 
-        if (titleId == 0) {
-            builder.setTitle(R.string.dialog_alert_title)
-        } else if (titleId != -1) {
-            builder.setTitle(titleId)
-        }
         if (positiveButtonTextId != -1) {
             builder.setPositiveButton(positiveButtonTextId) { dialog: DialogInterface, _: Int ->
                 mListener?.onConfirmation(tag)

+ 5 - 12
app/src/main/java/com/owncloud/android/ui/dialog/SyncFileNotEnoughSpaceDialogFragment.kt

@@ -24,27 +24,20 @@ import android.os.Build
 import android.os.Bundle
 import android.os.storage.StorageManager
 import androidx.annotation.RequiresApi
-import com.nextcloud.client.di.Injectable
 import com.owncloud.android.R
 import com.owncloud.android.datamodel.OCFile
 import com.owncloud.android.ui.dialog.ConfirmationDialogFragment.ConfirmationDialogFragmentListener
 import com.owncloud.android.ui.fragment.OCFileListFragment
 import com.owncloud.android.utils.DisplayUtils
-import com.owncloud.android.utils.theme.ViewThemeUtils
-import javax.inject.Inject
 
 /**
  * Dialog requiring confirmation when a file/folder is too "big" to be synchronized/downloaded on device.
  */
 class SyncFileNotEnoughSpaceDialogFragment :
     ConfirmationDialogFragment(),
-    ConfirmationDialogFragmentListener,
-    Injectable {
-    private var targetFile: OCFile? = null
+    ConfirmationDialogFragmentListener {
 
-    @JvmField
-    @Inject
-    var viewThemeUtils: ViewThemeUtils? = null
+    private var targetFile: OCFile? = null
 
     override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
         targetFile = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
@@ -62,7 +55,7 @@ class SyncFileNotEnoughSpaceDialogFragment :
     /**
      * (Only if file is a folder), will access the destination folder to allow user to choose what to synchronize
      */
-    override fun onConfirmation(callerTag: String) {
+    override fun onConfirmation(callerTag: String?) {
         val frag = targetFragment as OCFileListFragment?
 
         if (frag != null && targetFile != null) {
@@ -73,7 +66,7 @@ class SyncFileNotEnoughSpaceDialogFragment :
     /**
      * Will abort/cancel the process (is neutral to "hack" android button position ._.)
      */
-    override fun onNeutral(callerTag: String) {
+    override fun onNeutral(callerTag: String?) {
         // Nothing
     }
 
@@ -81,7 +74,7 @@ class SyncFileNotEnoughSpaceDialogFragment :
      * Will access to storage manager in order to empty useless files
      */
     @RequiresApi(api = Build.VERSION_CODES.N_MR1)
-    override fun onCancel(callerTag: String) {
+    override fun onCancel(callerTag: String?) {
         val storageIntent = Intent(StorageManager.ACTION_MANAGE_STORAGE)
         startActivityForResult(storageIntent, REQUEST_CODE_STORAGE)
     }