Commit 4c76d6ca authored by Leonardo Aramaki's avatar Leonardo Aramaki

Request for 'WRITE_EXTERNAL_STORAGE' permission

parent 3f4f20cb
package chat.rocket.android.chatroom.adapter package chat.rocket.android.chatroom.adapter
import android.Manifest
import android.app.Activity
import android.graphics.Color import android.graphics.Color
import android.media.MediaScannerConnection import android.media.MediaScannerConnection
import android.net.Uri import android.net.Uri
import android.os.Environment import android.os.Environment
import android.support.v7.widget.Toolbar import android.support.v7.widget.Toolbar
import android.view.ContextThemeWrapper
import android.view.View import android.view.View
import android.widget.Toast import android.widget.Toast
import chat.rocket.android.R import chat.rocket.android.R
import chat.rocket.android.chatroom.viewmodel.ImageAttachmentViewModel import chat.rocket.android.chatroom.viewmodel.ImageAttachmentViewModel
import chat.rocket.android.helper.AndroidPermissionsHelper
import chat.rocket.android.widget.emoji.EmojiReactionListener import chat.rocket.android.widget.emoji.EmojiReactionListener
import com.facebook.binaryresource.FileBinaryResource import com.facebook.binaryresource.FileBinaryResource
import com.facebook.cache.common.CacheKey import com.facebook.cache.common.CacheKey
...@@ -82,6 +86,10 @@ class ImageAttachmentViewHolder(itemView: View, ...@@ -82,6 +86,10 @@ class ImageAttachmentViewHolder(itemView: View,
} }
private fun saveImage(): Boolean { private fun saveImage(): Boolean {
if (!canWriteToExternalStorage()) {
checkWritingPermission()
return false
}
if (ImagePipelineFactory.getInstance().mainFileCache.hasKey(cacheKey)) { if (ImagePipelineFactory.getInstance().mainFileCache.hasKey(cacheKey)) {
val context = itemView.context val context = itemView.context
val resource = ImagePipelineFactory.getInstance().mainFileCache.getResource(cacheKey) val resource = ImagePipelineFactory.getInstance().mainFileCache.getResource(cacheKey)
...@@ -109,4 +117,18 @@ class ImageAttachmentViewHolder(itemView: View, ...@@ -109,4 +117,18 @@ class ImageAttachmentViewHolder(itemView: View,
} }
return true return true
} }
private fun canWriteToExternalStorage(): Boolean {
return AndroidPermissionsHelper.checkPermission(itemView.context, Manifest.permission.WRITE_EXTERNAL_STORAGE)
}
private fun checkWritingPermission() {
val context = itemView.context
if (context is ContextThemeWrapper && context.baseContext is Activity) {
val activity = context.baseContext as Activity
AndroidPermissionsHelper.requestPermission(activity,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
AndroidPermissionsHelper.WRITE_EXTERNAL_STORAGE_CODE)
}
}
} }
\ No newline at end of file
package chat.rocket.android.helper
import android.app.Activity
import android.content.Context
import android.content.pm.PackageManager
import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat
object AndroidPermissionsHelper {
const val WRITE_EXTERNAL_STORAGE_CODE = 1
fun checkPermission(context: Context, permission: String): Boolean {
return ContextCompat.checkSelfPermission(context, permission) ==
PackageManager.PERMISSION_GRANTED
}
fun requestPermission(context: Activity, permission: String, requestCode: Int) {
ActivityCompat.requestPermissions(context, arrayOf(permission), requestCode)
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment