A01e : multi-wifi camera control
Révision | f5cbf93aaffde2e1c65bcc2fa2650afff5ba2d97 (tree) |
---|---|
l'heure | 2021-01-04 18:05:04 |
Auteur | MRSa <mrsa@myad...> |
Commiter | MRSa |
preferenceアクセスをさらに整理。
@@ -36,7 +36,7 @@ class MainActivity : AppCompatActivity() | ||
36 | 36 | |
37 | 37 | try |
38 | 38 | { |
39 | - PreferenceValueInitializer().initializeStorageLocationPreferences(this) | |
39 | + PreferenceValueInitializer().initializePreferences(this) | |
40 | 40 | } |
41 | 41 | catch (e: Exception) |
42 | 42 | { |
@@ -14,7 +14,6 @@ import androidx.fragment.app.FragmentActivity | ||
14 | 14 | import jp.osdn.gokigen.mangle.operation.imagefile.IImageStoreGrant |
15 | 15 | import jp.osdn.gokigen.mangle.preference.IPreferencePropertyAccessor |
16 | 16 | import jp.osdn.gokigen.mangle.preference.PreferenceAccessWrapper |
17 | -import jp.osdn.gokigen.mangle.preference.PreferenceValueInitializer | |
18 | 17 | import java.io.File |
19 | 18 | |
20 | 19 | /** |
@@ -40,7 +39,7 @@ class StorageOperationWithPermission(private val activity: FragmentActivity) : I | ||
40 | 39 | val path = Environment.DIRECTORY_DCIM + File.separator + activity.getString(R.string.app_location) |
41 | 40 | val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) |
42 | 41 | intent.addCategory(Intent.CATEGORY_OPENABLE) |
43 | - intent.setType("*/*") | |
42 | + intent.type = "*/*" | |
44 | 43 | intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, path) |
45 | 44 | |
46 | 45 | activity.startActivityForResult( |
@@ -59,18 +58,18 @@ class StorageOperationWithPermission(private val activity: FragmentActivity) : I | ||
59 | 58 | { |
60 | 59 | if (resultCode == AppCompatActivity.RESULT_OK) |
61 | 60 | { |
62 | - Log.v(TAG, " DOCUMENT TREE GRANTED ${data}") | |
61 | + Log.v(TAG, " DOCUMENT TREE GRANTED $data") | |
63 | 62 | data?.data?.also { uri -> |
64 | 63 | val contentResolver = activity.applicationContext.contentResolver |
65 | 64 | val takeFlags: Int = |
66 | 65 | Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
67 | 66 | contentResolver.takePersistableUriPermission(uri, takeFlags) |
68 | - PreferenceValueInitializer().storeStorageLocationPreference(activity, uri) | |
67 | + PreferenceAccessWrapper(activity).putString(IPreferencePropertyAccessor.EXTERNAL_STORAGE_LOCATION, uri.toString()) | |
69 | 68 | } |
70 | 69 | } |
71 | 70 | else |
72 | 71 | { |
73 | - Log.v(TAG, " DOCUMENT TREE DENIED ${resultCode}") | |
72 | + Log.v(TAG, " DOCUMENT TREE DENIED $resultCode") | |
74 | 73 | } |
75 | 74 | } |
76 | 75 |
@@ -1,9 +0,0 @@ | ||
1 | -package jp.osdn.gokigen.mangle.preference; | |
2 | - | |
3 | -import androidx.annotation.NonNull; | |
4 | - | |
5 | -public interface IPreferenceAccessWrapper | |
6 | -{ | |
7 | - @NonNull String getString(@NonNull String key, @NonNull String defaultValue); | |
8 | - boolean getBoolean(@NonNull String key, boolean defaultValue); | |
9 | -} |
@@ -1,15 +1,10 @@ | ||
1 | 1 | package jp.osdn.gokigen.mangle.preference; |
2 | 2 | |
3 | 3 | import android.content.Context; |
4 | -import android.net.Uri; | |
5 | 4 | |
6 | 5 | import androidx.annotation.NonNull; |
7 | 6 | |
8 | 7 | public interface IPreferenceValueInitializer |
9 | 8 | { |
10 | 9 | void initializePreferences(@NonNull Context context); |
11 | - | |
12 | - // external Storageを使用するための設定 | |
13 | - void initializeStorageLocationPreferences(@NonNull Context context); | |
14 | - void storeStorageLocationPreference(@NonNull Context context, @NonNull Uri uri); | |
15 | 10 | } |
@@ -1,13 +1,15 @@ | ||
1 | 1 | package jp.osdn.gokigen.mangle.preference |
2 | 2 | |
3 | 3 | import android.content.Context |
4 | +import android.content.SharedPreferences | |
5 | +import androidx.preference.PreferenceDataStore | |
4 | 6 | import androidx.preference.PreferenceManager |
5 | 7 | |
6 | -class PreferenceAccessWrapper(context : Context) : IPreferenceAccessWrapper | |
8 | +class PreferenceAccessWrapper(context : Context) : PreferenceDataStore() | |
7 | 9 | { |
8 | 10 | private val preferences = PreferenceManager.getDefaultSharedPreferences(context) |
9 | 11 | |
10 | - override fun getString(key : String, defaultValue : String) : String | |
12 | + override fun getString(key : String, defaultValue : String?) : String | |
11 | 13 | { |
12 | 14 | try |
13 | 15 | { |
@@ -21,7 +23,7 @@ class PreferenceAccessWrapper(context : Context) : IPreferenceAccessWrapper | ||
21 | 23 | { |
22 | 24 | e.printStackTrace() |
23 | 25 | } |
24 | - return (defaultValue) | |
26 | + return (defaultValue ?: "") | |
25 | 27 | } |
26 | 28 | |
27 | 29 | override fun getBoolean(key : String, defaultValue : Boolean) : Boolean |
@@ -37,4 +39,17 @@ class PreferenceAccessWrapper(context : Context) : IPreferenceAccessWrapper | ||
37 | 39 | return (defaultValue) |
38 | 40 | } |
39 | 41 | |
42 | + override fun putString(key: String, value: String?) | |
43 | + { | |
44 | + try | |
45 | + { | |
46 | + val editor : SharedPreferences.Editor = preferences.edit() | |
47 | + editor.putString(key, value) | |
48 | + editor.apply() | |
49 | + } | |
50 | + catch (e : Exception) | |
51 | + { | |
52 | + e.printStackTrace() | |
53 | + } | |
54 | + } | |
40 | 55 | } |
@@ -11,10 +11,29 @@ class PreferenceValueInitializer() : IPreferenceValueInitializer | ||
11 | 11 | { |
12 | 12 | try |
13 | 13 | { |
14 | + initializeApplicationPreferences(context) | |
15 | + } | |
16 | + catch (e : Exception) | |
17 | + { | |
18 | + e.printStackTrace() | |
19 | + } | |
20 | + } | |
21 | + | |
22 | + private fun initializeApplicationPreferences(context : Context) | |
23 | + { | |
24 | + try | |
25 | + { | |
14 | 26 | val preferences = PreferenceManager.getDefaultSharedPreferences(context) ?: return |
15 | 27 | val items : Map<String, *> = preferences.all |
16 | 28 | val editor : SharedPreferences.Editor = preferences.edit() |
17 | 29 | |
30 | + if (!items.containsKey(IPreferencePropertyAccessor.EXTERNAL_STORAGE_LOCATION)) | |
31 | + { | |
32 | + editor.putString( | |
33 | + IPreferencePropertyAccessor.EXTERNAL_STORAGE_LOCATION, | |
34 | + IPreferencePropertyAccessor.EXTERNAL_STORAGE_LOCATION_DEFAULT_VALUE | |
35 | + ) | |
36 | + } | |
18 | 37 | if (!items.containsKey(IPreferencePropertyAccessor.PREFERENCE_NOTIFICATIONS)) |
19 | 38 | { |
20 | 39 | editor.putBoolean( |
@@ -82,39 +101,4 @@ class PreferenceValueInitializer() : IPreferenceValueInitializer | ||
82 | 101 | e.printStackTrace() |
83 | 102 | } |
84 | 103 | } |
85 | - | |
86 | - override fun initializeStorageLocationPreferences(context : Context) | |
87 | - { | |
88 | - try | |
89 | - { | |
90 | - val preferences = PreferenceManager.getDefaultSharedPreferences(context) ?: return | |
91 | - val items : Map<String, *> = preferences.all | |
92 | - val editor : SharedPreferences.Editor = preferences.edit() | |
93 | - if (!items.containsKey(IPreferencePropertyAccessor.EXTERNAL_STORAGE_LOCATION)) | |
94 | - { | |
95 | - editor.putString(IPreferencePropertyAccessor.EXTERNAL_STORAGE_LOCATION, IPreferencePropertyAccessor.EXTERNAL_STORAGE_LOCATION_DEFAULT_VALUE) | |
96 | - } | |
97 | - editor.apply() | |
98 | - } | |
99 | - catch (e : Exception) | |
100 | - { | |
101 | - e.printStackTrace() | |
102 | - } | |
103 | - | |
104 | - } | |
105 | - | |
106 | - override fun storeStorageLocationPreference(context : Context, uri : Uri) | |
107 | - { | |
108 | - try | |
109 | - { | |
110 | - val preferences = PreferenceManager.getDefaultSharedPreferences(context) ?: return | |
111 | - val editor : SharedPreferences.Editor = preferences.edit() | |
112 | - editor.putString(IPreferencePropertyAccessor.EXTERNAL_STORAGE_LOCATION, uri.toString()) | |
113 | - editor.apply() | |
114 | - } | |
115 | - catch (e : Exception) | |
116 | - { | |
117 | - e.printStackTrace() | |
118 | - } | |
119 | - } | |
120 | 104 | } |