@@ -96,8 +96,6 @@ public class BoxStoreBuilder {
96
96
97
97
int debugFlags ;
98
98
99
- private boolean android ;
100
-
101
99
boolean debugRelations ;
102
100
103
101
int fileMode ;
@@ -187,9 +185,7 @@ public BoxStoreBuilder name(String name) {
187
185
public BoxStoreBuilder directory (File directory ) {
188
186
checkIsNull (name , "Already has name, cannot assign directory" );
189
187
checkIsNull (inMemory , "Already set to in-memory database, cannot assign directory" );
190
- if (!android ) {
191
- checkIsNull (baseDirectory , "Already has base directory, cannot assign directory" );
192
- }
188
+ checkIsNull (baseDirectory , "Already has base directory, cannot assign directory" );
193
189
this .directory = directory ;
194
190
return this ;
195
191
}
@@ -232,41 +228,25 @@ private static void checkIsNull(@Nullable Object value, String errorMessage) {
232
228
233
229
/**
234
230
* Use on Android to pass a <a href="https://developer.android.com/reference/android/content/Context">Context</a>
235
- * for loading the native library and, if {@link #inMemory(String)} was not called before, creating the base
231
+ * for loading the native library and, if not an {@link #inMemory(String)} database, for creating the base
236
232
* directory for database files in the
237
233
* <a href="https://developer.android.com/reference/android/content/Context#getFilesDir()">files directory of the app</a>.
238
234
* <p>
239
- * In more detail, this assigns the base directory (see {@link #baseDirectory}) to
235
+ * In more detail, upon {@link #build()} assigns the base directory (see {@link #baseDirectory}) to
240
236
* {@code context.getFilesDir() + "/objectbox/"}.
241
237
* Thus, when using the default name (also "objectbox", unless overwritten using {@link #name(String)}), the default
242
238
* location of database files will be "objectbox/objectbox/" inside the app's files directory.
243
239
* If a custom name is specified, for example with {@code name("foobar")}, it would become "objectbox/foobar/".
244
240
* <p>
245
- * Alternatively, use {@link #baseDirectory(File)} or {@link #directory(File)}.
241
+ * Use {@link #baseDirectory(File)} or {@link #directory(File)} to specify a different directory for the database
242
+ * files.
246
243
*/
247
244
public BoxStoreBuilder androidContext (Object context ) {
248
245
//noinspection ConstantConditions Annotation does not enforce non-null.
249
246
if (context == null ) {
250
247
throw new NullPointerException ("Context may not be null" );
251
248
}
252
249
this .context = getApplicationContext (context );
253
-
254
- // Only create directories if not already an in-memory database.
255
- // Note: this will still create directories if this is called before switching to an in-memory database.
256
- if (inMemory == null ) {
257
- File baseDir = getAndroidBaseDir (context );
258
- if (!baseDir .exists ()) {
259
- baseDir .mkdir ();
260
- if (!baseDir .exists ()) { // check baseDir.exists() because of potential concurrent processes
261
- throw new RuntimeException ("Could not init Android base dir at " + baseDir .getAbsolutePath ());
262
- }
263
- }
264
- if (!baseDir .isDirectory ()) {
265
- throw new RuntimeException ("Android base dir is not a dir: " + baseDir .getAbsolutePath ());
266
- }
267
- baseDirectory = baseDir ;
268
- }
269
- android = true ;
270
250
return this ;
271
251
}
272
252
@@ -627,12 +607,30 @@ byte[] buildFlatStoreOptions(String canonicalPath) {
627
607
}
628
608
629
609
/**
630
- * Builds a {@link BoxStore} using any given configuration.
610
+ * Builds a {@link BoxStore} using the current configuration of this builder.
611
+ *
612
+ * <p>If {@link #androidContext(Object)} was called and no {@link #directory(File)} or {@link #baseDirectory(File)}
613
+ * is configured, creates and sets {@link #baseDirectory(File)} as explained in {@link #androidContext(Object)}.
631
614
*/
632
615
public BoxStore build () {
616
+ // If in-memory, use a special directory (it will never be created)
633
617
if (inMemory != null ) {
634
618
directory = new File (BoxStore .IN_MEMORY_PREFIX + inMemory );
635
619
}
620
+ // On Android, create and set base directory if no directory is explicitly configured
621
+ if (directory == null && baseDirectory == null && context != null ) {
622
+ File baseDir = getAndroidBaseDir (context );
623
+ if (!baseDir .exists ()) {
624
+ baseDir .mkdir ();
625
+ if (!baseDir .exists ()) { // check baseDir.exists() because of potential concurrent processes
626
+ throw new RuntimeException ("Could not init Android base dir at " + baseDir .getAbsolutePath ());
627
+ }
628
+ }
629
+ if (!baseDir .isDirectory ()) {
630
+ throw new RuntimeException ("Android base dir is not a dir: " + baseDir .getAbsolutePath ());
631
+ }
632
+ baseDirectory = baseDir ;
633
+ }
636
634
if (directory == null ) {
637
635
directory = getDbDir (baseDirectory , name );
638
636
}
0 commit comments