• R/O
  • SSH
  • HTTPS

util: コミット


コミットメタ情報

リビジョン228 (tree)
日時2020-08-13 04:21:34
作者hirukawa_ryo

ログメッセージ

androidx-lifecycle-util 0.1.1
Java 1.6でコンパイルできるように修正しました。

変更サマリ

差分

--- androidx-lifecycle-util/trunk/src/main/java/net/osdn/util/androidx/lifecycle/RestorableViewModel.java (revision 227)
+++ androidx-lifecycle-util/trunk/src/main/java/net/osdn/util/androidx/lifecycle/RestorableViewModel.java (revision 228)
@@ -6,6 +6,12 @@
66 import androidx.lifecycle.MutableLiveData;
77 import androidx.lifecycle.ViewModel;
88
9+import java.io.BufferedInputStream;
10+import java.io.BufferedOutputStream;
11+import java.io.ByteArrayOutputStream;
12+import java.io.File;
13+import java.io.FileInputStream;
14+import java.io.FileOutputStream;
915 import java.io.IOException;
1016 import java.io.UncheckedIOException;
1117 import java.nio.file.Files;
@@ -24,26 +30,17 @@
2430 private static final String VALUES = "values";
2531 private static final String KEYS = "keys";
2632
27- private static Map<Class<? extends RestorableViewModel>, RestorableViewModel> viewModelStore;
28-
29- private static Map<Class<? extends RestorableViewModel>, RestorableViewModel> getViewModelStore() {
30- if(viewModelStore == null) {
31- viewModelStore = new HashMap<>();
32- }
33- return viewModelStore;
34- }
35-
36- private Path file;
33+ private File file;
3734 private boolean isSaveRequired = true;
3835 private Map<String, Object> state;
39- private Map<String, MutableLiveData<?>> cache = new HashMap<>();
36+ private Map<String, MutableLiveData<?>> cache = new HashMap<String, MutableLiveData<?>>();
4037
41- public void initialize(Path file, boolean restore, Bundle defaultState) {
38+ public void initialize(File file, boolean restore, Bundle defaultState) {
4239 if(this.file != null || this.state != null) {
4340 throw new IllegalStateException("Already initialized");
4441 }
4542 this.file = file;
46- this.state = new HashMap<>();
43+ this.state = new HashMap<String, Object>();
4744 if(defaultState != null) {
4845 for (String key : defaultState.keySet()) {
4946 state.put(key, defaultState.get(key));
@@ -62,7 +59,7 @@
6259 try {
6360 byte[] data;
6461 synchronized (getClass()) {
65- data = Files.readAllBytes(file);
62+ data = readAllBytes(file);
6663 }
6764 Parcel parcel = Parcel.obtain();
6865 parcel.unmarshall(data, 0, data.length);
@@ -84,8 +81,8 @@
8481
8582 @SuppressWarnings({"rawtypes", "unchecked"})
8683 /* package private */ void saveState() {
87- ArrayList keys = new ArrayList<>(state.size());
88- ArrayList values = new ArrayList<>(state.size());
84+ ArrayList keys = new ArrayList(state.size());
85+ ArrayList values = new ArrayList(state.size());
8986 for(Map.Entry<String, Object> entry : state.entrySet()) {
9087 keys.add(entry.getKey());
9188 values.add(entry.getValue());
@@ -97,14 +94,10 @@
9794 bundle.writeToParcel(parcel, 0);
9895 try {
9996 synchronized (getClass()) {
100- Files.write(file, parcel.marshall(),
101- StandardOpenOption.CREATE,
102- StandardOpenOption.TRUNCATE_EXISTING,
103- StandardOpenOption.WRITE,
104- StandardOpenOption.SYNC);
97+ writeAllBytes(file, parcel.marshall());
10598 }
10699 } catch(IOException e) {
107- throw new UncheckedIOException(e);
100+ throw new RuntimeException(e);
108101 }
109102 }
110103
@@ -119,7 +112,7 @@
119112 T value = (T)state.get(key);
120113 liveData = new RestorableLiveData<T>(key, value);
121114 } else {
122- liveData = new RestorableLiveData<>(key);
115+ liveData = new RestorableLiveData<T>(key);
123116 }
124117 cache.put(key, liveData);
125118 return liveData;
@@ -136,7 +129,7 @@
136129 T value = (T)state.get(key);
137130 liveData = new RestorableLiveData<T>(key, value);
138131 } else {
139- liveData = new RestorableLiveData<>(key, initialValue);
132+ liveData = new RestorableLiveData<T>(key, initialValue);
140133 }
141134 cache.put(key, liveData);
142135 return liveData;
@@ -166,4 +159,38 @@
166159 super.setValue(value);
167160 }
168161 }
162+
163+ private static byte[] readAllBytes(File file) throws IOException {
164+ ByteArrayOutputStream out = null;
165+ BufferedInputStream in = null;
166+ try {
167+ in = new BufferedInputStream(new FileInputStream(file));
168+ out = new ByteArrayOutputStream();
169+ byte[] buf = new byte[8192];
170+ int size;
171+ while ((size = in.read(buf, 0, 8192)) != -1) {
172+ out.write(buf, 0, size);
173+ }
174+ } finally {
175+ if(out != null) {
176+ try { out.close(); } catch(IOException e) {}
177+ }
178+ if(in != null) {
179+ try { in.close(); } catch(IOException e) {}
180+ }
181+ }
182+ return out.toByteArray();
183+ }
184+
185+ private static void writeAllBytes(File file, byte[] data) throws IOException {
186+ BufferedOutputStream out = null;
187+ try {
188+ out = new BufferedOutputStream(new FileOutputStream(file, false));
189+ out.write(data);
190+ } finally {
191+ if(out != null) {
192+ try { out.close(); } catch(IOException e) {}
193+ }
194+ }
195+ }
169196 }
--- androidx-lifecycle-util/trunk/src/main/java/net/osdn/util/androidx/lifecycle/RestorableViewModelProvider.java (revision 227)
+++ androidx-lifecycle-util/trunk/src/main/java/net/osdn/util/androidx/lifecycle/RestorableViewModelProvider.java (revision 228)
@@ -10,33 +10,29 @@
1010 import androidx.lifecycle.LifecycleObserver;
1111 import androidx.lifecycle.OnLifecycleEvent;
1212
13-import java.io.IOException;
14-import java.io.UncheckedIOException;
15-import java.nio.file.Files;
16-import java.nio.file.Path;
13+import java.io.File;
1714 import java.util.HashMap;
1815 import java.util.Map;
19-import java.util.stream.Stream;
2016
2117 public class RestorableViewModelProvider implements LifecycleObserver {
2218
23- private static final String DIR = "restorable-view-model-states";
19+ private static final String DIR = ".restorable-viewmodel-states";
2420
2521 private static Map<Class<?>, Map<Class<? extends RestorableViewModel>, RestorableViewModel>> viewModelStoreByClass;
2622
2723 private static Map<Class<? extends RestorableViewModel>, RestorableViewModel> getViewModelStoreByClass(Class<?> cls) {
2824 if(viewModelStoreByClass == null) {
29- viewModelStoreByClass = new HashMap<>();
25+ viewModelStoreByClass = new HashMap<Class<?>, Map<Class<? extends RestorableViewModel>, RestorableViewModel>>();
3026 }
3127 Map<Class<? extends RestorableViewModel>, RestorableViewModel> viewModelStore = viewModelStoreByClass.get(cls);
3228 if(viewModelStore == null) {
33- viewModelStore = new HashMap<>();
29+ viewModelStore = new HashMap<Class<? extends RestorableViewModel>, RestorableViewModel>();
3430 viewModelStoreByClass.put(cls, viewModelStore);
3531 }
3632 return viewModelStore;
3733 }
3834
39- private Path dir;
35+ private File dir;
4036 private boolean restore;
4137 private Bundle defaultState;
4238 private Lifecycle lifecycle;
@@ -62,7 +58,7 @@
6258 }
6359
6460 private RestorableViewModelProvider(Context context, String name, Bundle savedInstanceState) {
65- this.dir = context.getNoBackupFilesDir().toPath().resolve(DIR).resolve("provider-" + name);
61+ this.dir = new File(new File(context.getNoBackupFilesDir(), DIR), ".provider-" + name);
6662 this.restore = savedInstanceState != null;
6763
6864 if(!restore) {
@@ -95,20 +91,14 @@
9591 }
9692
9793 private void clearAllStates() {
98- if(!Files.isDirectory(dir)) {
94+ if(!dir.exists() || !dir.isDirectory()) {
9995 return;
10096 }
10197
102- try (Stream<Path> files = Files.list(dir)) {
103- files.forEach(file -> {
104- try {
105- Files.delete(file);
106- } catch(IOException ignore) {
107- ignore.printStackTrace();
108- }
109- });
110- } catch(IOException ignore) {
111- ignore.printStackTrace();
98+ for(File file : dir.listFiles()) {
99+ if(file.delete() == false) {
100+ throw new RuntimeException("Can't delete file: " + file);
101+ }
112102 }
113103 }
114104
@@ -116,23 +106,23 @@
116106 @SuppressWarnings("unchecked")
117107 T viewModel = (T)viewModelStore.get(viewModelClass);
118108 if(viewModel == null) {
119- if(!Files.exists(dir)) {
120- try {
121- Files.createDirectories(dir);
122- } catch(IOException e) {
123- throw new UncheckedIOException(e);
109+ if(!dir.exists()) {
110+ if(dir.mkdirs() == false) {
111+ throw new RuntimeException("Can't create directory: " + dir);
124112 }
125113 }
126- if(!Files.isDirectory(dir)) {
127- throw new IllegalStateException("Not a directory: " + dir);
114+ if(!dir.isDirectory()) {
115+ throw new RuntimeException("Not a directory: " + dir);
128116 }
129- Path file = dir.resolve("state-" + viewModelClass.getCanonicalName());
117+ File file = new File(dir, ".state-" + viewModelClass.getCanonicalName());
130118 try {
131119 viewModel = viewModelClass.newInstance();
132120 viewModel.initialize(file, restore, defaultState);
133121 viewModel.setSaveRequired(isSaveRequired);
134- } catch (IllegalAccessException | InstantiationException e) {
122+ } catch(IllegalAccessException e) {
135123 throw new RuntimeException(e);
124+ } catch(InstantiationException e) {
125+ throw new RuntimeException(e);
136126 }
137127 viewModelStore.put(viewModelClass, viewModel);
138128 }
旧リポジトリブラウザで表示