OSDN Git Service

preferenceアクセスを一つのパッケージに寄せる。
authorMRSa <mrsa@myad.jp>
Mon, 4 Jan 2021 06:53:23 +0000 (15:53 +0900)
committerMRSa <mrsa@myad.jp>
Mon, 4 Jan 2021 06:53:23 +0000 (15:53 +0900)
16 files changed:
app/build.gradle
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/image/CameraLiveViewListenerImpl.kt
app/src/main/java/jp/osdn/gokigen/mangle/liveview/storeimage/StoreImage.kt
app/src/main/java/jp/osdn/gokigen/mangle/operation/CameraControl.kt
app/src/main/java/jp/osdn/gokigen/mangle/operation/imagefile/FileControl.kt
app/src/main/java/jp/osdn/gokigen/mangle/operation/imagefile/ImageStoreExternal.kt
app/src/main/java/jp/osdn/gokigen/mangle/preference/IPreferenceAccessWrapper.java [new file with mode: 0644]
app/src/main/java/jp/osdn/gokigen/mangle/preference/IPreferenceValueInitializer.java [new file with mode: 0644]
app/src/main/java/jp/osdn/gokigen/mangle/preference/MainPreferenceFragment.kt
app/src/main/java/jp/osdn/gokigen/mangle/preference/PreferenceAccessWrapper.kt [new file with mode: 0644]
app/src/main/java/jp/osdn/gokigen/mangle/preference/PreferenceValueInitializer.kt
app/src/main/java/jp/osdn/gokigen/mangle/scene/SceneChanger.kt
app/src/main/java/jp/osdn/gokigen/mangle/utils/preferences/MyPreferenceDataStore.kt [new file with mode: 0644]
build.gradle

index b6d01c3..6847f49 100644 (file)
@@ -33,7 +33,7 @@ dependencies {
     implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
     implementation 'androidx.core:core-ktx:1.3.2'
     implementation 'androidx.appcompat:appcompat:1.2.0'
-    implementation 'androidx.constraintlayout:constraintlayout:2.0.2'
+    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
     implementation 'androidx.preference:preference-ktx:1.1.1'
     implementation 'com.google.android.material:material:1.2.1'
 
@@ -41,8 +41,10 @@ dependencies {
     androidTestImplementation 'androidx.test.ext:junit:1.1.2'
     androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
 
-    def camerax_version = "1.0.0-beta10"
+    def camerax_version = "1.0.0-rc01"
     implementation "androidx.camera:camera-camera2:$camerax_version"
     implementation "androidx.camera:camera-lifecycle:$camerax_version"
-    implementation "androidx.camera:camera-view:1.0.0-alpha17"
+
+    implementation "androidx.camera:camera-view:1.0.0-alpha20"
+    implementation "androidx.datastore:datastore-preferences:1.0.0-alpha05"
 }
index bb14815..70c40d9 100644 (file)
@@ -11,7 +11,6 @@ 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
@@ -19,7 +18,6 @@ import jp.osdn.gokigen.mangle.scene.ShowMessage
 
 class MainActivity : AppCompatActivity()
 {
-    private val TAG = toString()
     private val mainButtonHandler : MainButtonHandler = MainButtonHandler(this)
     private val showMessage : ShowMessage = ShowMessage(this)
     private val accessPermission : IScopedStorageAccessPermission? = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { StorageOperationWithPermission(this) } else { null }
@@ -38,11 +36,7 @@ class MainActivity : AppCompatActivity()
 
         try
         {
-           PreferenceValueInitializer().initializeStorageLocationPreferences(
-               PreferenceManager.getDefaultSharedPreferences(
-                   this
-               )
-           )
+           PreferenceValueInitializer().initializeStorageLocationPreferences(this)
         }
         catch (e: Exception)
         {
@@ -117,6 +111,8 @@ class MainActivity : AppCompatActivity()
 
     companion object
     {
+        private val TAG = MainActivity::class.java.simpleName
+
         private const val REQUEST_CODE_PERMISSIONS = 10
         const val REQUEST_CODE_MEDIA_EDIT = 12
         const val REQUEST_CODE_OPEN_DOCUMENT_TREE = 20
index 99d88c9..65ff0d3 100644 (file)
@@ -11,9 +11,9 @@ import android.util.Log
 import androidx.annotation.RequiresApi
 import androidx.appcompat.app.AppCompatActivity
 import androidx.fragment.app.FragmentActivity
-import androidx.preference.PreferenceManager
 import jp.osdn.gokigen.mangle.operation.imagefile.IImageStoreGrant
 import jp.osdn.gokigen.mangle.preference.IPreferencePropertyAccessor
+import jp.osdn.gokigen.mangle.preference.PreferenceAccessWrapper
 import jp.osdn.gokigen.mangle.preference.PreferenceValueInitializer
 import java.io.File
 
@@ -31,8 +31,8 @@ class StorageOperationWithPermission(private val activity: FragmentActivity) : I
     {
         try
         {
-            val mediaLocation = PreferenceManager.getDefaultSharedPreferences(activity).getString(IPreferencePropertyAccessor.EXTERNAL_STORAGE_LOCATION, "")
-            if ((mediaLocation != null)&&(mediaLocation.length > 1))
+            val mediaLocation = PreferenceAccessWrapper(activity).getString(IPreferencePropertyAccessor.EXTERNAL_STORAGE_LOCATION, "")
+            if (mediaLocation.length > 1)
             {
                 return
             }
@@ -53,31 +53,6 @@ class StorageOperationWithPermission(private val activity: FragmentActivity) : I
         {
            e.printStackTrace()
         }
-
-/*
-
-        try
-        {
-            val path = Environment.DIRECTORY_DCIM + File.separator + activity.getString(R.string.app_location)
-            val extStorageUri = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
-            val values = ContentValues()
-            values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")
-            values.put(MediaStore.Images.Media.RELATIVE_PATH, path)
-            values.put(MediaStore.Images.Media.DATA, path + File.separator)
-            val imageUri = activity.contentResolver.insert(extStorageUri, values)
-            if (imageUri != null)
-            {
-                val urisToModify: List<Uri> = listOf(imageUri)
-                val contentResolver: ContentResolver = activity.contentResolver
-                val editPendingIntent = MediaStore.createWriteRequest(contentResolver, urisToModify)
-                activity.startIntentSenderForResult(editPendingIntent.intentSender, MainActivity.REQUEST_CODE_MEDIA_EDIT, null, 0, 0, 0)
-            }
-        }
-        catch (e : Exception)
-        {
-            e.printStackTrace()
-        }
-*/
     }
 
     override fun responseStorageAccessFrameworkLocation(resultCode: Int, data: Intent?)
@@ -90,11 +65,7 @@ class StorageOperationWithPermission(private val activity: FragmentActivity) : I
                 val takeFlags: Int =
                     Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
                 contentResolver.takePersistableUriPermission(uri, takeFlags)
-                PreferenceValueInitializer().storeStorageLocationPreference(
-                    PreferenceManager.getDefaultSharedPreferences(
-                        activity
-                    ), uri
-                )
+                PreferenceValueInitializer().storeStorageLocationPreference(activity, uri)
             }
         }
         else
@@ -123,11 +94,11 @@ class StorageOperationWithPermission(private val activity: FragmentActivity) : I
     {
         if (resultCode == AppCompatActivity.RESULT_OK)
         {
-            Log.v(TAG, " WRITE PERMISSION GRANTED  ${data}")
+            Log.v(TAG, " WRITE PERMISSION GRANTED  $data")
         }
         else
         {
-            Log.v(TAG, " WRITE PERMISSION DENIED  ${resultCode}")
+            Log.v(TAG, " WRITE PERMISSION DENIED  $resultCode")
         }
     }
 
index a0a57ed..289daef 100644 (file)
@@ -6,12 +6,12 @@ import android.graphics.*
 import android.graphics.ImageFormat.NV21
 import androidx.camera.core.ImageAnalysis
 import androidx.camera.core.ImageProxy
-import androidx.preference.PreferenceManager
 import jp.osdn.gokigen.mangle.R
 import jp.osdn.gokigen.mangle.liveview.ILiveViewRefresher
 import jp.osdn.gokigen.mangle.liveview.bitmapconvert.IPreviewImageConverter
 import jp.osdn.gokigen.mangle.liveview.bitmapconvert.ImageConvertFactory
 import jp.osdn.gokigen.mangle.preference.IPreferencePropertyAccessor
+import jp.osdn.gokigen.mangle.preference.PreferenceAccessWrapper
 import java.io.ByteArrayOutputStream
 import java.util.*
 
@@ -183,8 +183,8 @@ class CameraLiveViewListenerImpl(private val context: Context) : IImageDataRecei
 
     private fun setupLiveviewCache()
     {
-        val preference = PreferenceManager.getDefaultSharedPreferences(context)
-        if ((preference == null)||(!preference.getBoolean(IPreferencePropertyAccessor.CACHE_LIVE_VIEW_PICTURES, false)))
+        val preference = PreferenceAccessWrapper(context)
+        if (!preference.getBoolean(IPreferencePropertyAccessor.CACHE_LIVE_VIEW_PICTURES, false))
         {
             return
         }
@@ -193,14 +193,7 @@ class CameraLiveViewListenerImpl(private val context: Context) : IImageDataRecei
         val nofCachePics = preference.getString(IPreferencePropertyAccessor.NUMBER_OF_CACHE_PICTURES, IPreferencePropertyAccessor.NUMBER_OF_CACHE_PICTURES_DEFAULT_VALUE)
         try
         {
-            if (nofCachePics != null)
-            {
-                maxCachePics = nofCachePics.toInt()
-            }
-            else
-            {
-                maxCachePics = 500
-            }
+            maxCachePics = nofCachePics.toInt()
         }
         catch (e: Exception)
         {
index d6c526a..563776e 100644 (file)
@@ -1,6 +1,6 @@
 package jp.osdn.gokigen.mangle.liveview.storeimage
 
-import android.content.ContentResolver
+
 import android.content.ContentValues
 import android.database.DatabaseUtils
 import android.graphics.Bitmap
@@ -9,14 +9,11 @@ import android.os.Build
 import android.os.Environment
 import android.provider.MediaStore
 import android.util.Log
-import androidx.annotation.RequiresApi
 import androidx.fragment.app.FragmentActivity
-import androidx.preference.PreferenceManager
-import jp.osdn.gokigen.mangle.MainActivity
 import jp.osdn.gokigen.mangle.R
-import jp.osdn.gokigen.mangle.StorageOperationWithPermission
 import jp.osdn.gokigen.mangle.liveview.image.IImageProvider
 import jp.osdn.gokigen.mangle.preference.IPreferencePropertyAccessor
+import jp.osdn.gokigen.mangle.preference.PreferenceAccessWrapper
 import java.io.File
 import java.io.FileOutputStream
 import java.text.SimpleDateFormat
@@ -24,8 +21,6 @@ import java.util.*
 
 class StoreImage(private val context: FragmentActivity, private val imageProvider: IImageProvider, private val dumpLog : Boolean = false) : IStoreImage
 {
-    private val TAG = toString()
-    private val FILENAME_FORMAT = "yyyyMMdd_HHmmss"
 
     override fun doStore(target: Bitmap?)
     {
@@ -34,11 +29,7 @@ class StoreImage(private val context: FragmentActivity, private val imageProvide
             // 保存処理(プログレスダイアログ(「保存中...」)を表示
 
             val bitmapToStore = target ?: imageProvider.getImage()
-            val preference = PreferenceManager.getDefaultSharedPreferences(context)
-            val isLocalLocation  = preference.getBoolean(
-                IPreferencePropertyAccessor.PREFERENCE_SAVE_LOCAL_LOCATION,
-                IPreferencePropertyAccessor.PREFERENCE_SAVE_LOCAL_LOCATION_DEFAULT_VALUE
-            )
+            val isLocalLocation  = PreferenceAccessWrapper(context).getBoolean(IPreferencePropertyAccessor.PREFERENCE_SAVE_LOCAL_LOCATION, IPreferencePropertyAccessor.PREFERENCE_SAVE_LOCAL_LOCATION_DEFAULT_VALUE)
             if (isLocalLocation)
             {
                 saveImageLocal(bitmapToStore)
@@ -84,11 +75,7 @@ class StoreImage(private val context: FragmentActivity, private val imageProvide
 */
         try
         {
-            val preference = PreferenceManager.getDefaultSharedPreferences(context)
-            val isLocalLocation  = preference.getBoolean(
-                IPreferencePropertyAccessor.PREFERENCE_SAVE_LOCAL_LOCATION,
-                IPreferencePropertyAccessor.PREFERENCE_SAVE_LOCAL_LOCATION_DEFAULT_VALUE
-            )
+            val isLocalLocation  = PreferenceAccessWrapper(context).getBoolean(IPreferencePropertyAccessor.PREFERENCE_SAVE_LOCAL_LOCATION, IPreferencePropertyAccessor.PREFERENCE_SAVE_LOCAL_LOCATION_DEFAULT_VALUE)
             if (isLocalLocation)
             {
                 saveImageLocal(target)
@@ -236,4 +223,10 @@ class StoreImage(private val context: FragmentActivity, private val imageProvide
         return (Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED)
     }
 
+    companion object
+    {
+        private val  TAG = this.toString()
+        private const val FILENAME_FORMAT = "yyyyMMdd_HHmmss"
+    }
+
 }
index 25b75fc..da7e1b9 100644 (file)
@@ -27,11 +27,6 @@ class CameraControl(private val activity : FragmentActivity, private val accessP
     private lateinit var storeImage : StoreImage
     private var cameraIsStarted = false
 
-    init
-    {
-        //val preference = PreferenceManager.getDefaultSharedPreferences(activity)
-    }
-
     override fun initialize()
     {
         Log.v(TAG, " initialize()")
index 1ea4293..817aa60 100644 (file)
@@ -5,11 +5,11 @@ import android.util.Log
 import android.view.View
 import androidx.camera.core.ImageCapture
 import androidx.fragment.app.FragmentActivity
-import androidx.preference.PreferenceManager
 import jp.osdn.gokigen.mangle.IScopedStorageAccessPermission
 import jp.osdn.gokigen.mangle.R
 import jp.osdn.gokigen.mangle.liveview.storeimage.IStoreImage
 import jp.osdn.gokigen.mangle.preference.IPreferencePropertyAccessor
+import jp.osdn.gokigen.mangle.preference.PreferenceAccessWrapper
 
 class FileControl(private val context: FragmentActivity, private val storeImage : IStoreImage, accessRequest : IScopedStorageAccessPermission?) : View.OnClickListener
 {
@@ -41,7 +41,7 @@ class FileControl(private val context: FragmentActivity, private val storeImage
         try
         {
             //  preferenceから設定を取得する
-            val preference = PreferenceManager.getDefaultSharedPreferences(context)
+            val preference = PreferenceAccessWrapper(context)
             val isLocalLocation  = preference.getBoolean(
                 IPreferencePropertyAccessor.PREFERENCE_SAVE_LOCAL_LOCATION,
                 IPreferencePropertyAccessor.PREFERENCE_SAVE_LOCAL_LOCATION_DEFAULT_VALUE
index dbc3af0..b025601 100644 (file)
@@ -12,11 +12,11 @@ import androidx.camera.core.ImageCapture
 import androidx.camera.core.ImageCaptureException
 import androidx.core.content.ContextCompat
 import androidx.fragment.app.FragmentActivity
-import androidx.preference.PreferenceManager
 import com.google.android.material.snackbar.Snackbar
 import jp.osdn.gokigen.mangle.IScopedStorageAccessPermission
 import jp.osdn.gokigen.mangle.R
 import jp.osdn.gokigen.mangle.preference.IPreferencePropertyAccessor
+import jp.osdn.gokigen.mangle.preference.PreferenceAccessWrapper
 import java.io.File
 import java.io.OutputStream
 import java.text.SimpleDateFormat
@@ -31,12 +31,8 @@ class ImageStoreExternal(private val context: FragmentActivity, private val acce
         var prefString : String? = ""
         try
         {
-            prefString = PreferenceManager.getDefaultSharedPreferences(context).getString(
-                IPreferencePropertyAccessor.EXTERNAL_STORAGE_LOCATION, IPreferencePropertyAccessor.EXTERNAL_STORAGE_LOCATION_DEFAULT_VALUE)
-            if (prefString != null)
-            {
-                uriString = Uri.decode(prefString)
-            }
+            prefString = PreferenceAccessWrapper(context).getString(IPreferencePropertyAccessor.EXTERNAL_STORAGE_LOCATION, IPreferencePropertyAccessor.EXTERNAL_STORAGE_LOCATION_DEFAULT_VALUE)
+            uriString = Uri.decode(prefString)
             if (uriString.isEmpty())
             {
                 // 設定がない場合はデフォルトの場所に...
diff --git a/app/src/main/java/jp/osdn/gokigen/mangle/preference/IPreferenceAccessWrapper.java b/app/src/main/java/jp/osdn/gokigen/mangle/preference/IPreferenceAccessWrapper.java
new file mode 100644 (file)
index 0000000..03f195c
--- /dev/null
@@ -0,0 +1,9 @@
+package jp.osdn.gokigen.mangle.preference;
+
+import androidx.annotation.NonNull;
+
+public interface IPreferenceAccessWrapper
+{
+    @NonNull String getString(@NonNull String key, @NonNull String defaultValue);
+    boolean getBoolean(@NonNull String key, boolean defaultValue);
+}
diff --git a/app/src/main/java/jp/osdn/gokigen/mangle/preference/IPreferenceValueInitializer.java b/app/src/main/java/jp/osdn/gokigen/mangle/preference/IPreferenceValueInitializer.java
new file mode 100644 (file)
index 0000000..ae680e5
--- /dev/null
@@ -0,0 +1,15 @@
+package jp.osdn.gokigen.mangle.preference;
+
+import android.content.Context;
+import android.net.Uri;
+
+import androidx.annotation.NonNull;
+
+public interface IPreferenceValueInitializer
+{
+    void initializePreferences(@NonNull Context context);
+
+    // external Storageを使用するための設定
+    void initializeStorageLocationPreferences(@NonNull Context context);
+    void storeStorageLocationPreference(@NonNull Context context, @NonNull Uri uri);
+}
index 0b82037..8fd51af 100644 (file)
@@ -15,15 +15,9 @@ import jp.osdn.gokigen.mangle.R
 
 class MainPreferenceFragment : PreferenceFragmentCompat(), OnSharedPreferenceChangeListener, Preference.OnPreferenceClickListener
 {
-    private val TAG = toString()
     private lateinit var preferences : SharedPreferences
     private lateinit var changeScene : IChangeScene
 
-    companion object
-    {
-        fun newInstance() = MainPreferenceFragment().apply { }
-    }
-
     fun setSceneChanger(changer : IChangeScene)
     {
         this.changeScene = changer
@@ -50,7 +44,7 @@ class MainPreferenceFragment : PreferenceFragmentCompat(), OnSharedPreferenceCha
         Log.v(TAG, " onAttach() : ")
 
         preferences = PreferenceManager.getDefaultSharedPreferences(context)
-        PreferenceValueInitializer().initializePreferences(preferences)
+        PreferenceValueInitializer().initializePreferences(context)
         preferences.registerOnSharedPreferenceChangeListener(this)
     }
 
@@ -82,4 +76,10 @@ class MainPreferenceFragment : PreferenceFragmentCompat(), OnSharedPreferenceCha
         }
         return (ret)
     }
+
+    companion object
+    {
+        fun newInstance() = MainPreferenceFragment().apply { }
+        private val TAG = MainPreferenceFragment::class.java.simpleName
+    }
 }
diff --git a/app/src/main/java/jp/osdn/gokigen/mangle/preference/PreferenceAccessWrapper.kt b/app/src/main/java/jp/osdn/gokigen/mangle/preference/PreferenceAccessWrapper.kt
new file mode 100644 (file)
index 0000000..5811c83
--- /dev/null
@@ -0,0 +1,40 @@
+package jp.osdn.gokigen.mangle.preference
+
+import android.content.Context
+import androidx.preference.PreferenceManager
+
+class PreferenceAccessWrapper(context : Context) : IPreferenceAccessWrapper
+{
+    private val preferences = PreferenceManager.getDefaultSharedPreferences(context)
+
+    override fun getString(key : String, defaultValue : String) : String
+    {
+        try
+        {
+            val value = preferences.getString(key, defaultValue)
+            if (value != null)
+            {
+                return (value)
+            }
+        }
+        catch (e : Exception)
+        {
+            e.printStackTrace()
+        }
+        return (defaultValue)
+    }
+
+    override fun getBoolean(key : String, defaultValue : Boolean) : Boolean
+    {
+        try
+        {
+            return (preferences.getBoolean(key, defaultValue))
+        }
+        catch (e : Exception)
+        {
+            e.printStackTrace()
+        }
+        return (defaultValue)
+    }
+
+}
index 0a4dd90..aa75344 100644 (file)
 package jp.osdn.gokigen.mangle.preference
 
+import android.content.Context
 import android.content.SharedPreferences
 import android.net.Uri
+import androidx.preference.PreferenceManager
 
-class PreferenceValueInitializer
+class PreferenceValueInitializer() : IPreferenceValueInitializer
 {
-    fun initializePreferences(preferences: SharedPreferences)
+    override fun initializePreferences(context : Context)
     {
-        val items : Map<String, *> = preferences.all
-        val editor : SharedPreferences.Editor = preferences.edit()
-
-        if (!items.containsKey(IPreferencePropertyAccessor.PREFERENCE_NOTIFICATIONS))
-        {
-            editor.putBoolean(
-                IPreferencePropertyAccessor.PREFERENCE_NOTIFICATIONS,
-                IPreferencePropertyAccessor.PREFERENCE_NOTIFICATIONS_DEFAULT_VALUE
-            )
-        }
-        if (!items.containsKey(IPreferencePropertyAccessor.PREFERENCE_USE_CAMERA_X_PREVIEW))
-        {
-            editor.putBoolean(
-                IPreferencePropertyAccessor.PREFERENCE_USE_CAMERA_X_PREVIEW,
-                IPreferencePropertyAccessor.PREFERENCE_USE_CAMERA_X_PREVIEW_DEFAULT_VALUE
-            )
-        }
-        if (!items.containsKey(IPreferencePropertyAccessor.PREFERENCE_SAVE_LOCAL_LOCATION))
-        {
-            editor.putBoolean(
-                IPreferencePropertyAccessor.PREFERENCE_SAVE_LOCAL_LOCATION,
-                IPreferencePropertyAccessor.PREFERENCE_SAVE_LOCAL_LOCATION_DEFAULT_VALUE
-            )
-        }
-        if (!items.containsKey(IPreferencePropertyAccessor.SHOW_GRID_STATUS))
-        {
-            editor.putBoolean(
-                IPreferencePropertyAccessor.SHOW_GRID_STATUS,
-                IPreferencePropertyAccessor.SHOW_GRID_STATUS_DEFAULT_VALUE
-            )
-        }
-        if (!items.containsKey(IPreferencePropertyAccessor.CACHE_LIVE_VIEW_PICTURES))
-        {
-            editor.putBoolean(
-                IPreferencePropertyAccessor.CACHE_LIVE_VIEW_PICTURES,
-                IPreferencePropertyAccessor.CACHE_LIVE_VIEW_PICTURES_DEFAULT_VALUE
-            )
-        }
-        if (!items.containsKey(IPreferencePropertyAccessor.NUMBER_OF_CACHE_PICTURES))
-        {
-            editor.putString(
-                IPreferencePropertyAccessor.NUMBER_OF_CACHE_PICTURES,
-                IPreferencePropertyAccessor.NUMBER_OF_CACHE_PICTURES_DEFAULT_VALUE
-            )
-        }
-        if (!items.containsKey(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW))
-        {
-            editor.putBoolean(
-                IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW,
-                IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW_DEFAULT_VALUE
-            )
-        }
-        if (!items.containsKey(IPreferencePropertyAccessor.CAPTURE_ONLY_LIVE_VIEW))
+        try
         {
-            editor.putBoolean(
-                IPreferencePropertyAccessor.CAPTURE_ONLY_LIVE_VIEW,
-                IPreferencePropertyAccessor.CAPTURE_ONLY_LIVE_VIEW_DEFAULT_VALUE
-            )
+            val preferences = PreferenceManager.getDefaultSharedPreferences(context) ?: return
+            val items : Map<String, *> = preferences.all
+            val editor : SharedPreferences.Editor = preferences.edit()
+
+            if (!items.containsKey(IPreferencePropertyAccessor.PREFERENCE_NOTIFICATIONS))
+            {
+                editor.putBoolean(
+                    IPreferencePropertyAccessor.PREFERENCE_NOTIFICATIONS,
+                    IPreferencePropertyAccessor.PREFERENCE_NOTIFICATIONS_DEFAULT_VALUE
+                )
+            }
+            if (!items.containsKey(IPreferencePropertyAccessor.PREFERENCE_USE_CAMERA_X_PREVIEW))
+            {
+                editor.putBoolean(
+                    IPreferencePropertyAccessor.PREFERENCE_USE_CAMERA_X_PREVIEW,
+                    IPreferencePropertyAccessor.PREFERENCE_USE_CAMERA_X_PREVIEW_DEFAULT_VALUE
+                )
+            }
+            if (!items.containsKey(IPreferencePropertyAccessor.PREFERENCE_SAVE_LOCAL_LOCATION))
+            {
+                editor.putBoolean(
+                    IPreferencePropertyAccessor.PREFERENCE_SAVE_LOCAL_LOCATION,
+                    IPreferencePropertyAccessor.PREFERENCE_SAVE_LOCAL_LOCATION_DEFAULT_VALUE
+                )
+            }
+            if (!items.containsKey(IPreferencePropertyAccessor.SHOW_GRID_STATUS))
+            {
+                editor.putBoolean(
+                    IPreferencePropertyAccessor.SHOW_GRID_STATUS,
+                    IPreferencePropertyAccessor.SHOW_GRID_STATUS_DEFAULT_VALUE
+                )
+            }
+            if (!items.containsKey(IPreferencePropertyAccessor.CACHE_LIVE_VIEW_PICTURES))
+            {
+                editor.putBoolean(
+                    IPreferencePropertyAccessor.CACHE_LIVE_VIEW_PICTURES,
+                    IPreferencePropertyAccessor.CACHE_LIVE_VIEW_PICTURES_DEFAULT_VALUE
+                )
+            }
+            if (!items.containsKey(IPreferencePropertyAccessor.NUMBER_OF_CACHE_PICTURES))
+            {
+                editor.putString(
+                    IPreferencePropertyAccessor.NUMBER_OF_CACHE_PICTURES,
+                    IPreferencePropertyAccessor.NUMBER_OF_CACHE_PICTURES_DEFAULT_VALUE
+                )
+            }
+            if (!items.containsKey(IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW))
+            {
+                editor.putBoolean(
+                    IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW,
+                    IPreferencePropertyAccessor.CAPTURE_BOTH_CAMERA_AND_LIVE_VIEW_DEFAULT_VALUE
+                )
+            }
+            if (!items.containsKey(IPreferencePropertyAccessor.CAPTURE_ONLY_LIVE_VIEW))
+            {
+                editor.putBoolean(
+                    IPreferencePropertyAccessor.CAPTURE_ONLY_LIVE_VIEW,
+                    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()
         }
-        if (!items.containsKey(IPreferencePropertyAccessor.EXTERNAL_STORAGE_LOCATION))
+        catch (e : Exception)
         {
-            editor.putString(IPreferencePropertyAccessor.EXTERNAL_STORAGE_LOCATION, IPreferencePropertyAccessor.EXTERNAL_STORAGE_LOCATION_DEFAULT_VALUE)
+            e.printStackTrace()
         }
-        editor.apply()
     }
 
-    fun initializeStorageLocationPreferences(preferences: SharedPreferences)
+    override fun initializeStorageLocationPreferences(context : Context)
     {
-        val items : Map<String, *> = preferences.all
-        val editor : SharedPreferences.Editor = preferences.edit()
-        if (!items.containsKey(IPreferencePropertyAccessor.EXTERNAL_STORAGE_LOCATION))
+        try
         {
-            editor.putString(IPreferencePropertyAccessor.EXTERNAL_STORAGE_LOCATION, IPreferencePropertyAccessor.EXTERNAL_STORAGE_LOCATION_DEFAULT_VALUE)
+            val preferences = PreferenceManager.getDefaultSharedPreferences(context) ?: return
+            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()
         }
-        editor.apply()
+        catch (e : Exception)
+        {
+            e.printStackTrace()
+        }
+
     }
 
-    fun storeStorageLocationPreference(preferences: SharedPreferences, uri : Uri)
+    override fun storeStorageLocationPreference(context : Context, uri : Uri)
     {
         try
         {
+            val preferences = PreferenceManager.getDefaultSharedPreferences(context) ?: return
             val editor : SharedPreferences.Editor = preferences.edit()
             editor.putString(IPreferencePropertyAccessor.EXTERNAL_STORAGE_LOCATION, uri.toString())
             editor.apply()
@@ -97,5 +117,4 @@ class PreferenceValueInitializer
             e.printStackTrace()
         }
     }
-
 }
index 3e42741..8c64d7b 100644 (file)
@@ -5,7 +5,6 @@ import android.util.Log
 import androidx.fragment.app.Fragment
 import androidx.fragment.app.FragmentActivity
 import androidx.fragment.app.FragmentTransaction
-import androidx.preference.PreferenceManager
 import jp.osdn.gokigen.mangle.IScopedStorageAccessPermission
 import jp.osdn.gokigen.mangle.R
 import jp.osdn.gokigen.mangle.liveview.LiveImageViewFragment
@@ -13,6 +12,7 @@ import jp.osdn.gokigen.mangle.logcat.LogCatFragment
 import jp.osdn.gokigen.mangle.operation.CameraControl
 import jp.osdn.gokigen.mangle.preference.IPreferencePropertyAccessor
 import jp.osdn.gokigen.mangle.preference.MainPreferenceFragment
+import jp.osdn.gokigen.mangle.preference.PreferenceAccessWrapper
 import jp.osdn.gokigen.mangle.preview.PreviewFragment
 import jp.osdn.gokigen.mangle.utils.ConfirmationDialog
 import jp.osdn.gokigen.mangle.utils.ConfirmationDialog.Callback
@@ -64,10 +64,7 @@ class SceneChanger(private val activity: FragmentActivity, private val informati
     {
         try
         {
-            val isCameraXPreview  = PreferenceManager.getDefaultSharedPreferences(activity).getBoolean(
-                IPreferencePropertyAccessor.PREFERENCE_USE_CAMERA_X_PREVIEW,
-                IPreferencePropertyAccessor.PREFERENCE_USE_CAMERA_X_PREVIEW_DEFAULT_VALUE
-            )
+            val isCameraXPreview  = PreferenceAccessWrapper(activity).getBoolean(IPreferencePropertyAccessor.PREFERENCE_USE_CAMERA_X_PREVIEW, IPreferencePropertyAccessor.PREFERENCE_USE_CAMERA_X_PREVIEW_DEFAULT_VALUE)
             if (isCameraXPreview)
             {
                 initializeFragmentForPreview()
diff --git a/app/src/main/java/jp/osdn/gokigen/mangle/utils/preferences/MyPreferenceDataStore.kt b/app/src/main/java/jp/osdn/gokigen/mangle/utils/preferences/MyPreferenceDataStore.kt
new file mode 100644 (file)
index 0000000..0237551
--- /dev/null
@@ -0,0 +1,19 @@
+package jp.osdn.gokigen.mangle.utils.preferences
+
+import androidx.preference.PreferenceDataStore
+
+class MyPreferenceDataStore: PreferenceDataStore()
+{
+    override fun putString(key: String, value: String?)
+    {
+        // Save the value somewhere
+    }
+
+    override fun getString(key: String, defValue: String?): String
+    {
+        // Retrieve the value
+
+        return ("")
+    }
+
+}
\ No newline at end of file
index 8a34660..59f630a 100644 (file)
@@ -1,12 +1,12 @@
 // Top-level build file where you can add configuration options common to all sub-projects/modules.
 buildscript {
-    ext.kotlin_version = "1.4.10"
+    ext.kotlin_version = "1.4.21"
     repositories {
         google()
         jcenter()
     }
     dependencies {
-        classpath 'com.android.tools.build:gradle:4.1.0'
+        classpath 'com.android.tools.build:gradle:4.1.1'
         classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
 
         // NOTE: Do not place your application dependencies here; they belong