From 065de047b3fe36bf2b4be0546a5f2598e95230ec Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 12 Nov 2016 16:03:25 +0100 Subject: [PATCH] move some helper functions in SimpleActivity --- .../filemanager/Constants.java | 3 ++ .../filemanager/activities/MainActivity.kt | 50 ++----------------- .../filemanager/activities/SimpleActivity.kt | 25 ++++++++++ .../filemanager/dialogs/CopyDialog.kt | 4 +- 4 files changed, 35 insertions(+), 47 deletions(-) diff --git a/app/src/main/java/com/simplemobiletools/filemanager/Constants.java b/app/src/main/java/com/simplemobiletools/filemanager/Constants.java index a185ca4d..49ec4816 100644 --- a/app/src/main/java/com/simplemobiletools/filemanager/Constants.java +++ b/app/src/main/java/com/simplemobiletools/filemanager/Constants.java @@ -9,4 +9,7 @@ public class Constants { public static final String IS_DARK_THEME = "is_dark_theme"; public static final String SHOW_HIDDEN = "show_hidden"; public static final String TREE_URI = "tree_uri"; + + // global intents + public static final int OPEN_DOCUMENT_TREE = 1000; } diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/MainActivity.kt index 36dfd2b7..61905910 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/MainActivity.kt @@ -1,11 +1,8 @@ package com.simplemobiletools.filemanager.activities import android.Manifest -import android.annotation.TargetApi -import android.app.Activity import android.content.Intent import android.content.pm.PackageManager -import android.os.Build import android.os.Bundle import android.os.Handler import android.support.v4.app.ActivityCompat @@ -15,11 +12,13 @@ import com.simplemobiletools.filemanager.Constants import com.simplemobiletools.filemanager.R import com.simplemobiletools.filemanager.fragments.ItemsFragment import com.simplemobiletools.filepicker.dialogs.StoragePickerDialog -import com.simplemobiletools.filepicker.dialogs.WritePermissionDialog -import com.simplemobiletools.filepicker.extensions.* +import com.simplemobiletools.filepicker.extensions.getInternalStoragePath +import com.simplemobiletools.filepicker.extensions.hasStoragePermission +import com.simplemobiletools.filepicker.extensions.toast import com.simplemobiletools.filepicker.models.FileDirItem import com.simplemobiletools.filepicker.views.Breadcrumbs import kotlinx.android.synthetic.main.activity_main.* +import java.io.File class MainActivity : SimpleActivity(), ItemsFragment.ItemInteractionListener, Breadcrumbs.BreadcrumbsListener { var mBasePath = getInternalStoragePath() @@ -125,55 +124,16 @@ class MainActivity : SimpleActivity(), ItemsFragment.ItemInteractionListener, Br } fun changePath(pickedPath: String) { - if (checkStupidAndroidFiveSDCardWritePermission(pickedPath)) { + if (!isShowingPermDialog(File(pickedPath))) { mBasePath = pickedPath openPath(pickedPath) } } - fun checkStupidAndroidFiveSDCardWritePermission(pickedPath: String): Boolean { - return if (applicationContext.needsStupidWritePermissions(pickedPath) && mConfig.treeUri.isEmpty()) { - WritePermissionDialog(this, object : WritePermissionDialog.OnConfirmedListener { - override fun onConfirmed() { - val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) - startActivityForResult(intent, OPEN_DOCUMENT_TREE_AND_CHANGE_PATH) - } - }) - false - } else - true - } - - public override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) { - super.onActivityResult(requestCode, resultCode, resultData) - if (requestCode == OPEN_DOCUMENT_TREE_AND_CHANGE_PATH) { - if (resultCode == Activity.RESULT_OK && resultData != null) { - saveTreeUri(resultData) - changePath(getSDCardPath()) - } else { - changePath(getInternalStoragePath()) - } - } else if (requestCode == OPEN_DOCUMENT_TREE && resultCode == Activity.RESULT_OK && resultData != null) { - saveTreeUri(resultData) - } - } - - @TargetApi(Build.VERSION_CODES.KITKAT) - private fun saveTreeUri(resultData: Intent) { - val treeUri = resultData.data - mConfig.treeUri = treeUri.toString() - - val takeFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION - contentResolver.takePersistableUriPermission(treeUri, takeFlags) - } - companion object { private val STORAGE_PERMISSION = 1 private val BACK_PRESS_TIMEOUT = 5000 - val OPEN_DOCUMENT_TREE_AND_CHANGE_PATH = 1 - val OPEN_DOCUMENT_TREE = 2 - private var mWasBackJustPressed: Boolean = false } } diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/SimpleActivity.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/SimpleActivity.kt index bd70489d..811b59df 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/SimpleActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/SimpleActivity.kt @@ -1,11 +1,18 @@ package com.simplemobiletools.filemanager.activities +import android.annotation.TargetApi +import android.app.Activity +import android.content.Intent +import android.os.Build import android.os.Bundle import android.support.v7.app.AppCompatActivity import android.view.MenuItem import com.simplemobiletools.filemanager.Config +import com.simplemobiletools.filemanager.Constants import com.simplemobiletools.filemanager.R +import com.simplemobiletools.filepicker.extensions.isShowingWritePermissions +import java.io.File open class SimpleActivity : AppCompatActivity() { lateinit var mConfig: Config @@ -25,4 +32,22 @@ open class SimpleActivity : AppCompatActivity() { else -> super.onOptionsItemSelected(item) } } + + override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) { + super.onActivityResult(requestCode, resultCode, resultData) + if (requestCode == Constants.OPEN_DOCUMENT_TREE && resultCode == Activity.RESULT_OK && resultData != null) { + saveTreeUri(resultData) + } + } + + @TargetApi(Build.VERSION_CODES.KITKAT) + fun saveTreeUri(resultData: Intent) { + val treeUri = resultData.data + mConfig.treeUri = treeUri.toString() + + val takeFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION + contentResolver.takePersistableUriPermission(treeUri, takeFlags) + } + + fun isShowingPermDialog(file: File) = isShowingWritePermissions(file, mConfig.treeUri, Constants.OPEN_DOCUMENT_TREE) } diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/CopyDialog.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/CopyDialog.kt index cbb93f00..6562ac4b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/CopyDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/CopyDialog.kt @@ -7,8 +7,8 @@ import android.support.v7.app.AlertDialog import android.view.LayoutInflater import android.view.WindowManager import com.simplemobiletools.filemanager.Config +import com.simplemobiletools.filemanager.Constants import com.simplemobiletools.filemanager.R -import com.simplemobiletools.filemanager.activities.MainActivity import com.simplemobiletools.filemanager.asynctasks.CopyTask import com.simplemobiletools.filepicker.dialogs.FilePickerDialog import com.simplemobiletools.filepicker.dialogs.WritePermissionDialog @@ -76,7 +76,7 @@ class CopyDialog(val activity: Activity, val files: ArrayList, val copyLis WritePermissionDialog(activity, object : WritePermissionDialog.OnConfirmedListener { override fun onConfirmed() { val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) - activity.startActivityForResult(intent, MainActivity.OPEN_DOCUMENT_TREE) + activity.startActivityForResult(intent, Constants.OPEN_DOCUMENT_TREE) } }) return@setOnClickListener