Skip to content

Commit fdf0656

Browse files
committed
extracted getAndroidFilesDir
1 parent 12341dd commit fdf0656

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

objectbox-java/src/main/java/io/objectbox/BoxStoreBuilder.java

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.util.ArrayList;
2222
import java.util.List;
2323

24+
import javax.annotation.Nonnull;
25+
2426
import io.objectbox.annotation.apihint.Internal;
2527
import io.objectbox.ideasonly.ModelUpdate;
2628

@@ -146,18 +148,7 @@ public BoxStoreBuilder androidContext(Object context) {
146148
if (context == null) {
147149
throw new NullPointerException("Context may not be null");
148150
}
149-
File filesDir;
150-
try {
151-
Method getFilesDir = context.getClass().getMethod("getFilesDir");
152-
filesDir = (File) getFilesDir.invoke(context);
153-
} catch (Exception e) {
154-
throw new RuntimeException(
155-
"Could not init with given Android context (must be sub class of android.content.Context)", e);
156-
}
157-
if (filesDir == null) {
158-
throw new IllegalStateException("Android files dir is null");
159-
}
160-
File baseDir = new File(filesDir, "objectbox");
151+
File baseDir = new File(getAndroidFilesDir(context), "objectbox");
161152
if (!baseDir.exists()) {
162153
boolean ok = baseDir.mkdirs();
163154
if (!ok) {
@@ -172,6 +163,23 @@ public BoxStoreBuilder androidContext(Object context) {
172163
return this;
173164
}
174165

166+
@Nonnull
167+
private File getAndroidFilesDir(Object context) {
168+
File filesDir;
169+
try {
170+
Method getFilesDir = context.getClass().getMethod("getFilesDir");
171+
filesDir = (File) getFilesDir.invoke(context);
172+
} catch (Exception e) {
173+
throw new RuntimeException(
174+
"Could not init with given Android context (must be sub class of android.content.Context)", e);
175+
}
176+
if (filesDir == null) {
177+
// TODO should we consider https://issuetracker.google.com/issues/36918154 ?
178+
throw new IllegalStateException("Android files dir is null");
179+
}
180+
return filesDir;
181+
}
182+
175183
/**
176184
* Sets the maximum number of concurrent readers. For most applications, the default is fine (> 100 readers).
177185
* <p>

0 commit comments

Comments
 (0)