Skip to content

Commit

Permalink
Use of EXT_DIR with Android API 29 (#208)
Browse files Browse the repository at this point in the history
* Switch to alternative method to get the ExternalFilesDirectory and upgrade to API 29 for Travis and Robolectric

* Try to fix Travis issues

* Try to fix Travis issues

* Implement proposed changes regarding API 29 changes
  • Loading branch information
roedll authored and tony19 committed Nov 7, 2019
1 parent a05b6c4 commit e65d640
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
10 changes: 9 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
language: android
android:
components:
# Use the latest revision of Android SDK Tools
- tools
- platform-tools

# The BuildTools version used by the project
- build-tools-28.0.3
- android-28
- build-tools-29.0.2

# The SDK version used to compile the project
- android-29

sudo: false

Expand Down
6 changes: 3 additions & 3 deletions logback-android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ apply from: "${rootProject.projectDir}/gradle/docs.gradle"
apply from: "${rootProject.projectDir}/gradle/deploy.gradle"

android {
compileSdkVersion 28
compileSdkVersion 29

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_5
Expand All @@ -16,7 +16,7 @@ android {

defaultConfig {
minSdkVersion 9
targetSdkVersion 28
targetSdkVersion 29
versionCode VERSION_CODE.toInteger()
versionName VERSION_NAME
}
Expand Down Expand Up @@ -52,7 +52,7 @@ dependencies {
}

testImplementation 'org.hamcrest:hamcrest-junit:2.0.0.0'
testImplementation 'org.robolectric:robolectric:4.0.2'
testImplementation 'org.robolectric:robolectric:4.3.1'
testImplementation 'org.mockito:mockito-core:2.23.4'
testImplementation 'joda-time:joda-time:2.10.1'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void setupProperties(LoggerContext context) {
context.putProperties(props);
}

private static ContextWrapper getContext() {
protected static ContextWrapper getContext() {
try {
Class<?> c = Class.forName("android.app.AppGlobals");
Method method = c.getDeclaredMethod("getInitialApplication");
Expand Down Expand Up @@ -96,20 +96,41 @@ public String getMountedExternalStorageDirectoryPath() {
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED) ||
state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
path = absPath(Environment.getExternalStorageDirectory());
path = getExternalStorageDirectoryPath();
}
return path;
}

/**
* Gets the path to the external storage directory
*
* This API is available on SDK 8+. On API versions 29 onwards,
* this function uses the implementation in
* {@link android.content.Context#getExternalFilesDir(java.lang.String)}
* which is the proposed replacement for
* {@link android.os.Environment#getExternalStorageDirectory()}.
*
* @return the absolute path to the external storage directory
*/
@TargetApi(8)
@SuppressWarnings("deprecation")
public String getExternalStorageDirectoryPath() {
return Environment.getExternalStorageDirectory().getAbsolutePath();
if (Build.VERSION.SDK_INT >= 29) {
return getExternalFilesDirectoryPath();
} else {
return Environment.getExternalStorageDirectory().getAbsolutePath();
}
}

/**
* Gets the path to the external storage directory
*
* This API is available on SDK 8+. This function uses the implementation in
* {@link android.content.Context#getExternalFilesDir(java.lang.String)}.
*
* @return the absolute path to the external storage directory
*/
@TargetApi(8)
public String getExternalFilesDirectoryPath() {
return this.context != null
? absPath(this.context.getExternalFilesDir(null))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ public void before() {
public void getMountedExternalStorageDirectoryPathReturnsPathWhenMounted() {
ShadowEnvironment.setExternalStorageState(Environment.MEDIA_MOUNTED);
assertThat(contextUtil.getMountedExternalStorageDirectoryPath(),
is(Environment.getExternalStorageDirectory().getAbsolutePath()));
is(contextUtil.getContext().getExternalFilesDir(null).getAbsolutePath()));
}

@Test
public void getMountedExternalStorageDirectoryPathReturnsPathWhenMountedReadOnly() {
ShadowEnvironment.setExternalStorageState(Environment.MEDIA_MOUNTED_READ_ONLY);
assertThat(contextUtil.getMountedExternalStorageDirectoryPath(),
is(Environment.getExternalStorageDirectory().getAbsolutePath()));
is(contextUtil.getContext().getExternalFilesDir(null).getAbsolutePath()));
}

@Test
Expand Down Expand Up @@ -111,7 +111,7 @@ public void getMountedExternalStorageDirectoryPathReturnsNullWhenShared() {
@Test
public void getExternalStorageDirectoryPathIsNotEmpty() {
assertThat(contextUtil.getExternalStorageDirectoryPath(),
is(Environment.getExternalStorageDirectory().getAbsolutePath()));
is(contextUtil.getContext().getExternalFilesDir(null).getAbsolutePath()));
}

@Test
Expand Down

0 comments on commit e65d640

Please sign in to comment.