update Commons for easy runtime permissions handling

This commit is contained in:
tibbi 2017-10-21 19:40:46 +02:00
parent 6998b83ef2
commit a4b24a7deb
6 changed files with 45 additions and 60 deletions

View file

@ -37,7 +37,7 @@ android {
} }
dependencies { dependencies {
compile 'com.simplemobiletools:commons:2.30.4' compile 'com.simplemobiletools:commons:2.30.6'
compile 'com.davemorrissey.labs:subsampling-scale-image-view:3.6.0' compile 'com.davemorrissey.labs:subsampling-scale-image-view:3.6.0'
compile 'com.theartofdev.edmodo:android-image-cropper:2.4.0' compile 'com.theartofdev.edmodo:android-image-cropper:2.4.0'
compile 'com.bignerdranch.android:recyclerview-multiselect:0.2' compile 'com.bignerdranch.android:recyclerview-multiselect:0.2'

View file

@ -1,16 +1,13 @@
package com.simplemobiletools.gallery.activities package com.simplemobiletools.gallery.activities
import android.Manifest
import android.app.Activity import android.app.Activity
import android.content.ClipData import android.content.ClipData
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri import android.net.Uri
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.os.Handler import android.os.Handler
import android.provider.MediaStore import android.provider.MediaStore
import android.support.v4.app.ActivityCompat
import android.support.v7.widget.GridLayoutManager import android.support.v7.widget.GridLayoutManager
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
@ -21,6 +18,7 @@ import com.simplemobiletools.commons.dialogs.CreateNewFolderDialog
import com.simplemobiletools.commons.dialogs.FilePickerDialog import com.simplemobiletools.commons.dialogs.FilePickerDialog
import com.simplemobiletools.commons.dialogs.RadioGroupDialog import com.simplemobiletools.commons.dialogs.RadioGroupDialog
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE
import com.simplemobiletools.commons.helpers.SORT_BY_DATE_MODIFIED import com.simplemobiletools.commons.helpers.SORT_BY_DATE_MODIFIED
import com.simplemobiletools.commons.helpers.SORT_BY_DATE_TAKEN import com.simplemobiletools.commons.helpers.SORT_BY_DATE_TAKEN
import com.simplemobiletools.commons.models.RadioItem import com.simplemobiletools.commons.models.RadioItem
@ -40,7 +38,6 @@ import java.io.*
import java.util.* import java.util.*
class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
private val STORAGE_PERMISSION = 1
private val PICK_MEDIA = 2 private val PICK_MEDIA = 2
private val PICK_WALLPAPER = 3 private val PICK_WALLPAPER = 3
private val LAST_MEDIA_CHECK_PERIOD = 3000L private val LAST_MEDIA_CHECK_PERIOD = 3000L
@ -189,7 +186,8 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
} }
private fun tryloadGallery() { private fun tryloadGallery() {
if (hasWriteStoragePermission()) { handlePermission(PERMISSION_WRITE_STORAGE) {
if (it) {
if (config.showAll) if (config.showAll)
showAllMedia() showAllMedia()
else else
@ -197,17 +195,6 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
setupLayoutManager() setupLayoutManager()
checkIfColorChanged() checkIfColorChanged()
} else {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), STORAGE_PERMISSION)
}
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == STORAGE_PERMISSION) {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
getDirectories()
} else { } else {
toast(R.string.no_storage_permissions) toast(R.string.no_storage_permissions)
finish() finish()

View file

@ -21,6 +21,7 @@ import com.google.gson.Gson
import com.simplemobiletools.commons.dialogs.ConfirmationDialog import com.simplemobiletools.commons.dialogs.ConfirmationDialog
import com.simplemobiletools.commons.dialogs.RadioGroupDialog import com.simplemobiletools.commons.dialogs.RadioGroupDialog
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE
import com.simplemobiletools.commons.models.RadioItem import com.simplemobiletools.commons.models.RadioItem
import com.simplemobiletools.commons.views.MyScalableRecyclerView import com.simplemobiletools.commons.views.MyScalableRecyclerView
import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.R
@ -138,16 +139,19 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
} }
private fun tryloadGallery() { private fun tryloadGallery() {
if (hasWriteStoragePermission()) { handlePermission(PERMISSION_WRITE_STORAGE) {
if (it) {
val dirName = getHumanizedFilename(mPath) val dirName = getHumanizedFilename(mPath)
title = if (mShowAll) resources.getString(R.string.all_folders) else dirName title = if (mShowAll) resources.getString(R.string.all_folders) else dirName
getMedia() getMedia()
setupLayoutManager() setupLayoutManager()
checkIfColorChanged() checkIfColorChanged()
} else { } else {
toast(R.string.no_storage_permissions)
finish() finish()
} }
} }
}
private fun checkIfColorChanged() { private fun checkIfColorChanged() {
if (media_grid.adapter != null && getRecyclerAdapter().primaryColor != config.primaryColor) { if (media_grid.adapter != null && getRecyclerAdapter().primaryColor != config.primaryColor) {

View file

@ -1,21 +1,18 @@
package com.simplemobiletools.gallery.activities package com.simplemobiletools.gallery.activities
import android.Manifest
import android.content.Intent import android.content.Intent
import android.content.pm.PackageManager
import android.database.Cursor import android.database.Cursor
import android.graphics.Color import android.graphics.Color
import android.graphics.drawable.ColorDrawable import android.graphics.drawable.ColorDrawable
import android.net.Uri import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.provider.MediaStore import android.provider.MediaStore
import android.support.v4.app.ActivityCompat
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.View import android.view.View
import com.simplemobiletools.commons.extensions.hasWriteStoragePermission
import com.simplemobiletools.commons.extensions.scanPath import com.simplemobiletools.commons.extensions.scanPath
import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE
import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.extensions.* import com.simplemobiletools.gallery.extensions.*
import com.simplemobiletools.gallery.fragments.PhotoFragment import com.simplemobiletools.gallery.fragments.PhotoFragment
@ -29,7 +26,6 @@ import kotlinx.android.synthetic.main.fragment_holder.*
import java.io.File import java.io.File
open class PhotoVideoActivity : SimpleActivity(), ViewPagerFragment.FragmentListener { open class PhotoVideoActivity : SimpleActivity(), ViewPagerFragment.FragmentListener {
private val STORAGE_PERMISSION = 1
private var mMedium: Medium? = null private var mMedium: Medium? = null
private var mIsFullScreen = false private var mIsFullScreen = false
private var mIsFromGallery = false private var mIsFromGallery = false
@ -44,11 +40,13 @@ open class PhotoVideoActivity : SimpleActivity(), ViewPagerFragment.FragmentList
public override fun onCreate(savedInstanceState: Bundle?) { public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.fragment_holder) setContentView(R.layout.fragment_holder)
handlePermission(PERMISSION_WRITE_STORAGE) {
if (hasWriteStoragePermission()) { if (it) {
checkIntent(savedInstanceState) checkIntent(savedInstanceState)
} else { } else {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), STORAGE_PERMISSION) toast(R.string.no_storage_permissions)
finish()
}
} }
} }
@ -115,19 +113,6 @@ open class PhotoVideoActivity : SimpleActivity(), ViewPagerFragment.FragmentList
supportActionBar?.setBackgroundDrawable(resources.getDrawable(R.drawable.actionbar_gradient_background)) supportActionBar?.setBackgroundDrawable(resources.getDrawable(R.drawable.actionbar_gradient_background))
} }
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == STORAGE_PERMISSION) {
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
checkIntent()
} else {
toast(R.string.no_storage_permissions)
finish()
}
}
}
private fun sendViewPagerIntent(path: String) { private fun sendViewPagerIntent(path: String) {
Intent(this, ViewPagerActivity::class.java).apply { Intent(this, ViewPagerActivity::class.java).apply {
putExtra(IS_VIEW_INTENT, true) putExtra(IS_VIEW_INTENT, true)

View file

@ -26,6 +26,7 @@ import android.view.animation.DecelerateInterpolator
import com.simplemobiletools.commons.dialogs.PropertiesDialog import com.simplemobiletools.commons.dialogs.PropertiesDialog
import com.simplemobiletools.commons.dialogs.RenameItemDialog import com.simplemobiletools.commons.dialogs.RenameItemDialog
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE
import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.activities.MediaActivity.Companion.mMedia import com.simplemobiletools.gallery.activities.MediaActivity.Companion.mMedia
import com.simplemobiletools.gallery.adapters.MyPagerAdapter import com.simplemobiletools.gallery.adapters.MyPagerAdapter
@ -73,11 +74,17 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_medium) setContentView(R.layout.activity_medium)
if (!hasWriteStoragePermission()) { handlePermission(PERMISSION_WRITE_STORAGE) {
if (it) {
initViewPager()
} else {
toast(R.string.no_storage_permissions)
finish() finish()
return }
}
} }
private fun initViewPager() {
measureScreen() measureScreen()
val uri = intent.data val uri = intent.data
if (uri != null) { if (uri != null) {
@ -189,8 +196,9 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
if (!hasWriteStoragePermission()) { if (!hasPermission(PERMISSION_WRITE_STORAGE)) {
finish() finish()
return
} }
supportActionBar?.setBackgroundDrawable(resources.getDrawable(R.drawable.actionbar_gradient_background)) supportActionBar?.setBackgroundDrawable(resources.getDrawable(R.drawable.actionbar_gradient_background))

View file

@ -3,9 +3,10 @@ package com.simplemobiletools.gallery.asynctasks
import android.content.Context import android.content.Context
import android.os.AsyncTask import android.os.AsyncTask
import com.simplemobiletools.commons.extensions.getFilenameFromPath import com.simplemobiletools.commons.extensions.getFilenameFromPath
import com.simplemobiletools.commons.extensions.hasWriteStoragePermission import com.simplemobiletools.commons.extensions.hasPermission
import com.simplemobiletools.commons.extensions.internalStoragePath import com.simplemobiletools.commons.extensions.internalStoragePath
import com.simplemobiletools.commons.extensions.sdCardPath import com.simplemobiletools.commons.extensions.sdCardPath
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE
import com.simplemobiletools.commons.helpers.SORT_DESCENDING import com.simplemobiletools.commons.helpers.SORT_DESCENDING
import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.extensions.config import com.simplemobiletools.gallery.extensions.config
@ -21,7 +22,7 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
val mediaFetcher = MediaFetcher(context) val mediaFetcher = MediaFetcher(context)
override fun doInBackground(vararg params: Void): ArrayList<Directory> { override fun doInBackground(vararg params: Void): ArrayList<Directory> {
if (!context.hasWriteStoragePermission()) if (!context.hasPermission(PERMISSION_WRITE_STORAGE))
return ArrayList() return ArrayList()
val config = context.config val config = context.config