OSDN Git Service

ライブラリを更新2。
authorMRSa <mrsa@myad.jp>
Sat, 10 Oct 2020 06:27:50 +0000 (15:27 +0900)
committerMRSa <mrsa@myad.jp>
Sat, 10 Oct 2020 06:27:50 +0000 (15:27 +0900)
app/src/main/AndroidManifest.xml
app/src/main/java/jp/osdn/gokigen/mangle/MainActivity.kt
app/src/main/java/jp/osdn/gokigen/mangle/StorageOperationWithPermission.kt
app/src/main/java/jp/osdn/gokigen/mangle/liveview/storeimage/StoreImage.kt
app/src/main/java/jp/osdn/gokigen/mangle/operation/FileControl.kt
app/src/main/java/jp/osdn/gokigen/mangle/preference/IPreferencePropertyAccessor.kt
app/src/main/java/jp/osdn/gokigen/mangle/preference/PreferenceValueInitializer.kt
build.gradle

index 15d432f..e3c6c65 100644 (file)
@@ -6,6 +6,7 @@
     <uses-permission android:name="android.permission.CAMERA" />
     <uses-permission android:name="android.permission.VIBRATE" />
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
+    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
 
     <application
         android:allowBackup="true"
index b42255d..14f253d 100644 (file)
@@ -11,6 +11,8 @@ import android.widget.Toast
 import androidx.appcompat.app.AppCompatActivity
 import androidx.core.app.ActivityCompat
 import androidx.core.content.ContextCompat
+import androidx.preference.PreferenceManager
+import jp.osdn.gokigen.mangle.preference.PreferenceValueInitializer
 import jp.osdn.gokigen.mangle.scene.MainButtonHandler
 import jp.osdn.gokigen.mangle.scene.SceneChanger
 import jp.osdn.gokigen.mangle.scene.ShowMessage
@@ -33,6 +35,19 @@ class MainActivity : AppCompatActivity()
         supportActionBar?.hide()
         window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
 
+        try
+        {
+           PreferenceValueInitializer().initializeStorageLocationPreferences(
+               PreferenceManager.getDefaultSharedPreferences(
+                   this
+               )
+           )
+        }
+        catch (e: Exception)
+        {
+            e.printStackTrace()
+        }
+
         if (allPermissionsGranted())
         {
             checkMediaWritePermission()
@@ -63,7 +78,11 @@ class MainActivity : AppCompatActivity()
         }
     }
 
-    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray)
+    override fun onRequestPermissionsResult(
+        requestCode: Int,
+        permissions: Array<String>,
+        grantResults: IntArray
+    )
     {
         if (requestCode == REQUEST_CODE_PERMISSIONS)
         {
@@ -89,14 +108,35 @@ class MainActivity : AppCompatActivity()
         {
             Log.v(TAG, " WRITE PERMISSION GRANTED  ${data}")
         }
+        if ((requestCode == REQUEST_CODE_OPEN_DOCUMENT_TREE)&&(resultCode == RESULT_OK))
+        {
+            Log.v(TAG, " WRITE PERMISSION GRANTED  ${data}")
+
+            data?.data?.also { uri ->
+                val contentResolver = applicationContext.contentResolver
+                val takeFlags: Int =
+                    Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
+                contentResolver.takePersistableUriPermission(uri, takeFlags)
+                PreferenceValueInitializer().storeStorageLocationPreference(
+                    PreferenceManager.getDefaultSharedPreferences(
+                        this
+                    ), uri
+                )
+            }
+        }
     }
 
     companion object
     {
         private const val REQUEST_CODE_PERMISSIONS = 10
         const val REQUEST_CODE_MEDIA_EDIT = 12
-        private val REQUIRED_PERMISSIONS = arrayOf(Manifest.permission.CAMERA,
-                                                   Manifest.permission.VIBRATE,
-                                                   Manifest.permission.WRITE_EXTERNAL_STORAGE)
+        const val REQUEST_CODE_OPEN_DOCUMENT_TREE = 20
+
+        private val REQUIRED_PERMISSIONS = arrayOf(
+            Manifest.permission.CAMERA,
+            Manifest.permission.VIBRATE,
+            Manifest.permission.WRITE_EXTERNAL_STORAGE,
+            Manifest.permission.READ_EXTERNAL_STORAGE
+        )
     }
 }
index e188195..255ce82 100644 (file)
@@ -1,13 +1,16 @@
 package jp.osdn.gokigen.mangle
 
 import android.content.ContentResolver
-import android.content.ContentValues
+import android.content.Intent
 import android.net.Uri
 import android.os.Build
 import android.os.Environment
+import android.provider.DocumentsContract
 import android.provider.MediaStore
 import androidx.annotation.RequiresApi
 import androidx.fragment.app.FragmentActivity
+import androidx.preference.PreferenceManager
+import jp.osdn.gokigen.mangle.preference.IPreferencePropertyAccessor
 import java.io.File
 
 /**
@@ -21,7 +24,22 @@ class StorageOperationWithPermission(private val activity: FragmentActivity)
     {
         try
         {
+            val mediaLocation = PreferenceManager.getDefaultSharedPreferences(activity).getString(IPreferencePropertyAccessor.EXTERNAL_STORAGE_LOCATION, "")
+            if ((mediaLocation != null)&&(mediaLocation.length > 1))
+            {
+                return
+            }
+
+            val path = Environment.DIRECTORY_DCIM + File.separator + activity.getString(R.string.app_location)
+            val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
+            intent.addCategory(Intent.CATEGORY_OPENABLE)
+            intent.setType("*/*")
+            intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, path)
 
+            activity.startActivityForResult(
+                Intent(Intent.ACTION_OPEN_DOCUMENT_TREE),
+                MainActivity.REQUEST_CODE_OPEN_DOCUMENT_TREE
+            )
 
         }
         catch (e : Exception)
index f9cc2ba..0641e51 100644 (file)
@@ -231,7 +231,6 @@ class StoreImage(private val context: FragmentActivity, private val imageProvide
         }
     }
 
-
     private fun isExternalStorageWritable(): Boolean
     {
         return (Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED)
index 717297b..4d08af3 100644 (file)
@@ -15,7 +15,6 @@ import androidx.fragment.app.FragmentActivity
 import androidx.preference.PreferenceManager
 import com.google.android.material.snackbar.Snackbar
 import jp.osdn.gokigen.mangle.R
-import jp.osdn.gokigen.mangle.StorageOperationWithPermission
 import jp.osdn.gokigen.mangle.liveview.storeimage.IStoreImage
 import jp.osdn.gokigen.mangle.preference.IPreferencePropertyAccessor
 import java.io.File
@@ -28,9 +27,6 @@ class FileControl(private val context: FragmentActivity, private val storeImage
     private val FILENAME_FORMAT = "yyyyMMdd_HHmmss"
     private var imageCapture: ImageCapture? = null
 
-    //private lateinit var outputDirectory: File
-    //private var isLocalLocation : Boolean = false
-
     init
     {
     }
@@ -86,7 +82,7 @@ class FileControl(private val context: FragmentActivity, private val storeImage
                 {
                     val savedUri = Uri.fromFile(photoFile)
                     val msg = context.getString(R.string.capture_success) + " $savedUri"
-                    //Toast.makeText(context.baseContext, msg, Toast.LENGTH_SHORT).show()
+
                     Snackbar.make(
                         context.findViewById<androidx.constraintlayout.widget.ConstraintLayout>(
                             R.id.main_layout
@@ -142,8 +138,6 @@ class FileControl(private val context: FragmentActivity, private val storeImage
             values.put(MediaStore.Images.Media.RELATIVE_PATH, path)
             values.put(MediaStore.Images.Media.IS_PENDING, true)
             extStorageUri = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
-            //extStorageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
-
             values.put(MediaStore.Images.Media.DATA, outputDir.absolutePath + File.separator + photoFile)
         }
         else
@@ -159,7 +153,7 @@ class FileControl(private val context: FragmentActivity, private val storeImage
 
             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R)
             {
-                Log.v(TAG, "  ===== StorageOperationWithPermission() : ${imageUri} =====")
+                Log.v(TAG, "  ===== StorageOperationWithPermission() : $imageUri =====")
                 //StorageOperationWithPermission(context).requestAndroidRMediaPermission(imageUri)
             }
 
@@ -237,7 +231,7 @@ class FileControl(private val context: FragmentActivity, private val storeImage
             )
             if (captureBothCamera)
             {
-                val thread = Thread(Runnable { storeImage.doStore() })
+                val thread = Thread { storeImage.doStore() }
                 try
                 {
                     thread.start()
index a760421..fd8f692 100644 (file)
@@ -35,6 +35,8 @@ interface IPreferencePropertyAccessor
         // --- HIDDEN
         const val SHOW_GRID_STATUS = "show_grid"
         const val SHOW_GRID_STATUS_DEFAULT_VALUE = false
+        const val EXTERNAL_STORAGE_LOCATION = "external_storage_location"
+        const val EXTERNAL_STORAGE_LOCATION_DEFAULT_VALUE = ""
     }
 
 }
index de8c4ff..0a4dd90 100644 (file)
@@ -1,6 +1,7 @@
 package jp.osdn.gokigen.mangle.preference
 
 import android.content.SharedPreferences
+import android.net.Uri
 
 class PreferenceValueInitializer
 {
@@ -65,7 +66,36 @@ class PreferenceValueInitializer
                 IPreferencePropertyAccessor.CAPTURE_ONLY_LIVE_VIEW_DEFAULT_VALUE
             )
         }
+        if (!items.containsKey(IPreferencePropertyAccessor.EXTERNAL_STORAGE_LOCATION))
+        {
+            editor.putString(IPreferencePropertyAccessor.EXTERNAL_STORAGE_LOCATION, IPreferencePropertyAccessor.EXTERNAL_STORAGE_LOCATION_DEFAULT_VALUE)
+        }
         editor.apply()
     }
 
+    fun initializeStorageLocationPreferences(preferences: SharedPreferences)
+    {
+        val items : Map<String, *> = preferences.all
+        val editor : SharedPreferences.Editor = preferences.edit()
+        if (!items.containsKey(IPreferencePropertyAccessor.EXTERNAL_STORAGE_LOCATION))
+        {
+            editor.putString(IPreferencePropertyAccessor.EXTERNAL_STORAGE_LOCATION, IPreferencePropertyAccessor.EXTERNAL_STORAGE_LOCATION_DEFAULT_VALUE)
+        }
+        editor.apply()
+    }
+
+    fun storeStorageLocationPreference(preferences: SharedPreferences, uri : Uri)
+    {
+        try
+        {
+            val editor : SharedPreferences.Editor = preferences.edit()
+            editor.putString(IPreferencePropertyAccessor.EXTERNAL_STORAGE_LOCATION, uri.toString())
+            editor.apply()
+        }
+        catch (e : Exception)
+        {
+            e.printStackTrace()
+        }
+    }
+
 }
index 29beb85..251c446 100644 (file)
@@ -6,7 +6,7 @@ buildscript {
         jcenter()
     }
     dependencies {
-        classpath 'com.android.tools.build:gradle:4.0.1'
+        classpath 'com.android.tools.build:gradle:4.0.2'
         classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
 
         // NOTE: Do not place your application dependencies here; they belong