21
21
import java .util .ArrayList ;
22
22
import java .util .List ;
23
23
24
+ import javax .annotation .Nonnull ;
25
+
24
26
import io .objectbox .annotation .apihint .Internal ;
25
27
import io .objectbox .ideasonly .ModelUpdate ;
26
28
@@ -146,18 +148,7 @@ public BoxStoreBuilder androidContext(Object context) {
146
148
if (context == null ) {
147
149
throw new NullPointerException ("Context may not be null" );
148
150
}
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" );
161
152
if (!baseDir .exists ()) {
162
153
boolean ok = baseDir .mkdirs ();
163
154
if (!ok ) {
@@ -172,6 +163,23 @@ public BoxStoreBuilder androidContext(Object context) {
172
163
return this ;
173
164
}
174
165
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
+
175
183
/**
176
184
* Sets the maximum number of concurrent readers. For most applications, the default is fine (> 100 readers).
177
185
* <p>
0 commit comments