adding some checks to Kitkat SD card permission handling

This commit is contained in:
tibbi 2016-11-06 00:23:36 +01:00
parent 76c2312b6d
commit d74800dcc2
18 changed files with 151 additions and 6 deletions

View File

@ -87,4 +87,12 @@ public class Config {
public void setLastFlashlightState(boolean enabled) {
mPrefs.edit().putBoolean(Constants.LAST_FLASHLIGHT_STATE, enabled).apply();
}
public String getTreeUri() {
return mPrefs.getString(Constants.TREE_URI, "");
}
public void setTreeUri(String uri) {
mPrefs.edit().putString(Constants.TREE_URI, uri).apply();
}
}

View File

@ -5,6 +5,8 @@ public class Constants {
public static final int ORIENT_LANDSCAPE_LEFT = 1;
public static final int ORIENT_LANDSCAPE_RIGHT = 2;
public static final String TREE_URI = "tree_uri";
// shared preferences
public static final String PREFS_KEY = "Camera";
public static final String IS_FIRST_RUN = "is_first_run";

View File

@ -1,6 +1,9 @@
package com.simplemobiletools.camera.activities
import android.annotation.TargetApi
import android.app.Activity
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.support.v4.app.TaskStackBuilder
import android.view.Menu
@ -8,10 +11,17 @@ import android.view.MenuItem
import android.view.View
import android.widget.AdapterView
import com.simplemobiletools.camera.R
import com.simplemobiletools.camera.dialogs.WritePermissionDialog
import com.simplemobiletools.camera.extensions.isKitkat
import com.simplemobiletools.camera.extensions.isPathOnSD
import com.simplemobiletools.filepicker.dialogs.FilePickerDialog
import kotlinx.android.synthetic.main.activity_settings.*
import java.io.File
class SettingsActivity : SimpleActivity() {
val OPEN_DOCUMENT_TREE = 1
var mCurrPath: String = ""
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_settings)
@ -49,22 +59,58 @@ class SettingsActivity : SimpleActivity() {
}
private fun setupSavePhotosFolder() {
var currPath = mConfig.savePhotosFolder
settings_save_photos.text = currPath.substring(currPath.lastIndexOf("/") + 1)
mCurrPath = mConfig.savePhotosFolder
settings_save_photos.text = mCurrPath.substring(mCurrPath.lastIndexOf("/") + 1)
settings_save_photos_holder.setOnClickListener {
FilePickerDialog(this, currPath, false, false, false, object: FilePickerDialog.OnFilePickerListener {
FilePickerDialog(this, mCurrPath, false, false, false, object : FilePickerDialog.OnFilePickerListener {
override fun onFail(error: FilePickerDialog.FilePickerResult) {
}
override fun onSuccess(pickedPath: String) {
currPath = pickedPath.trimEnd('/')
mConfig.savePhotosFolder = currPath
settings_save_photos.text = currPath.substring(currPath.lastIndexOf("/") + 1)
mCurrPath = pickedPath.trimEnd('/')
if (!File(pickedPath).canWrite() && isPathOnSD(pickedPath) && isKitkat() && mConfig.treeUri.isEmpty()) {
WritePermissionDialog(this@SettingsActivity, object : WritePermissionDialog.OnWritePermissionListener {
override fun onCancelled() {
mCurrPath = mConfig.savePhotosFolder
}
override fun onConfirmed() {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
startActivityForResult(intent, OPEN_DOCUMENT_TREE)
}
})
} else {
mConfig.savePhotosFolder = mCurrPath
settings_save_photos.text = mCurrPath.substring(mCurrPath.lastIndexOf("/") + 1)
}
}
})
}
}
@TargetApi(Build.VERSION_CODES.KITKAT)
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
super.onActivityResult(requestCode, resultCode, resultData)
if (requestCode == OPEN_DOCUMENT_TREE) {
if (resultCode == Activity.RESULT_OK && resultData != null) {
mConfig.savePhotosFolder = mCurrPath
settings_save_photos.text = mCurrPath.substring(mCurrPath.lastIndexOf("/") + 1)
saveTreeUri(resultData)
} else {
mCurrPath = mConfig.savePhotosFolder
}
}
}
@TargetApi(Build.VERSION_CODES.KITKAT)
private fun saveTreeUri(resultData: Intent) {
val treeUri = resultData.data
mConfig.treeUri = resultData.data.toString()
val takeFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
contentResolver.takePersistableUriPermission(treeUri, takeFlags)
}
private fun setupSound() {
settings_sound.isChecked = mConfig.isSoundEnabled
settings_sound_holder.setOnClickListener {

View File

@ -0,0 +1,34 @@
package com.simplemobiletools.camera.dialogs
import android.content.Context
import android.support.v7.app.AlertDialog
import android.view.LayoutInflater
import com.simplemobiletools.camera.R
class WritePermissionDialog(val context: Context, val listener: OnWritePermissionListener) {
var dialog: AlertDialog? = null
init {
val view = LayoutInflater.from(context).inflate(R.layout.dialog_write_permission, null)
dialog = AlertDialog.Builder(context)
.setTitle(context.resources.getString(R.string.confirm_storage_access_title))
.setView(view)
.setPositiveButton(R.string.ok, { dialog, which -> dialogConfirmed() })
.setOnCancelListener { listener?.onCancelled() }
.create()
dialog?.show()
}
private fun dialogConfirmed() {
dialog?.dismiss()
listener.onConfirmed()
}
interface OnWritePermissionListener {
fun onConfirmed()
fun onCancelled()
}
}

View File

@ -0,0 +1,9 @@
package com.simplemobiletools.camera.extensions
import android.content.Context
import android.os.Build
import com.simplemobiletools.filepicker.extensions.getSDCardPath
fun Context.isPathOnSD(path: String) = path.startsWith(getSDCardPath())
fun Context.isKitkat() = Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialog_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="@dimen/activity_margin">
<TextView
android:id="@+id/dialog_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_margin"
android:text="@string/confirm_storage_access_text"/>
<ImageView
android:id="@+id/dialog_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="@mipmap/write_storage"/>
</LinearLayout>

Binary file not shown.

After

Width:  |  Height:  |  Size: 26 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 54 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB

View File

@ -12,6 +12,9 @@
<string name="no_audio_permissions">Wir benötigen Zugriff auf das Mirkofon um Videos aufnehmen zu können</string>
<string name="no_gallery_app_available">Keine Galerie App verfügbar</string>
<string name="no_valid_resolution_found">Keine gültige Auflösung mit gewähltem Seitenverhältnis gefunden, nutze maximale Auflösung</string>
<string name="ok">OK</string>
<string name="confirm_storage_access_title">Confirm external storage access</string>
<string name="confirm_storage_access_text">Please choose the root folder of the SD card to grant write access on the next screen</string>
<!-- Settings -->
<string name="settings">Einstellungen</string>

View File

@ -12,6 +12,9 @@
<string name="no_audio_permissions">È necessario l\'accesso al microfono per registrare i video</string>
<string name="no_gallery_app_available">Nessuna app galleria disponibile</string>
<string name="no_valid_resolution_found">No valid resolution with selected aspect ratio found, using max resolution</string>
<string name="ok">OK</string>
<string name="confirm_storage_access_title">Confirm external storage access</string>
<string name="confirm_storage_access_text">Please choose the root folder of the SD card to grant write access on the next screen</string>
<!-- Settings -->
<string name="settings">Impostazioni</string>

View File

@ -12,6 +12,9 @@
<string name="no_audio_permissions">ビデオを記録するためにオーディオのアクセス許可が必要です</string>
<string name="no_gallery_app_available">利用可能なギャラリーアプリがありません</string>
<string name="no_valid_resolution_found">No valid resolution with selected aspect ratio found, using max resolution</string>
<string name="ok">OK</string>
<string name="confirm_storage_access_title">Confirm external storage access</string>
<string name="confirm_storage_access_text">Please choose the root folder of the SD card to grant write access on the next screen</string>
<!-- Settings -->
<string name="settings">設定</string>

View File

@ -12,6 +12,9 @@
<string name="no_audio_permissions">A permissão de áudio é necessária para a gravação de vídeos</string>
<string name="no_gallery_app_available">Nenhuma aplicação de galeria disponível</string>
<string name="no_valid_resolution_found">Nenhuma resolução encontrada que seja compatível com o rácio de aspeto selecionado, a utilizar a resolução máxima</string>
<string name="ok">OK</string>
<string name="confirm_storage_access_title">Confirm external storage access</string>
<string name="confirm_storage_access_text">Please choose the root folder of the SD card to grant write access on the next screen</string>
<!-- Settings -->
<string name="settings">Definições</string>

View File

@ -12,6 +12,9 @@
<string name="no_audio_permissions">Нам нужно аудио разрешение для записи видео</string>
<string name="no_gallery_app_available">Нет доступного приложения-галереи</string>
<string name="no_valid_resolution_found">No valid resolution with selected aspect ratio found, using max resolution</string>
<string name="ok">OK</string>
<string name="confirm_storage_access_title">Confirm external storage access</string>
<string name="confirm_storage_access_text">Please choose the root folder of the SD card to grant write access on the next screen</string>
<!-- Settings -->
<string name="settings">Настройки</string>

View File

@ -12,6 +12,9 @@
<string name="no_audio_permissions">För att spela in video krävs ljudrättigheter</string>
<string name="no_gallery_app_available">Ingen galleri-app finns tillgänglig</string>
<string name="no_valid_resolution_found">No valid resolution with selected aspect ratio found, using max resolution</string>
<string name="ok">OK</string>
<string name="confirm_storage_access_title">Confirm external storage access</string>
<string name="confirm_storage_access_text">Please choose the root folder of the SD card to grant write access on the next screen</string>
<!-- Settings -->
<string name="settings">Inställningar</string>

View File

@ -12,6 +12,9 @@
<string name="no_audio_permissions">We need the audio permission for recording videos</string>
<string name="no_gallery_app_available">No gallery app available</string>
<string name="no_valid_resolution_found">No valid resolution with selected aspect ratio found, using max resolution</string>
<string name="ok">OK</string>
<string name="confirm_storage_access_title">Confirm external storage access</string>
<string name="confirm_storage_access_text">Please choose the root folder of the SD card to grant write access on the next screen</string>
<!-- Settings -->
<string name="settings">Settings</string>