From 57713d5c5dcd80f5ca41bc20de7abc5d38ab8d3f Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 20 Nov 2022 14:53:13 +1100 Subject: [PATCH] Still use file:// urls for devices earlier than android 6.0 --- android/app/src/main/AndroidManifest.xml | 2 +- .../main/java/com/classicube/CCFileProvider.java | 16 ++++++++++------ .../main/java/com/classicube/MainActivity.java | 12 +++++++++++- misc/buildbot.sh | 7 +++++-- 4 files changed, 27 insertions(+), 10 deletions(-) diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 8ace3ed22..0f75fcf75 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -13,7 +13,7 @@ diff --git a/android/app/src/main/java/com/classicube/CCFileProvider.java b/android/app/src/main/java/com/classicube/CCFileProvider.java index 3fb00ced8..a6d011de0 100644 --- a/android/app/src/main/java/com/classicube/CCFileProvider.java +++ b/android/app/src/main/java/com/classicube/CCFileProvider.java @@ -12,11 +12,12 @@ import android.database.Cursor; import android.database.MatrixCursor; import android.net.Uri; import android.os.ParcelFileDescriptor; +import android.provider.MediaStore; import android.provider.OpenableColumns; public class CCFileProvider extends ContentProvider { - final static String[] DEFAULT_COLUMNS = { OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE }; + final static String[] DEFAULT_COLUMNS = { OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE, MediaStore.MediaColumns.DATA }; File root; @Override @@ -33,11 +34,11 @@ public class CCFileProvider extends ContentProvider @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { File file = getFileForUri(uri); - // can be null when caller is requesting all supported columns + // can be null when caller is requesting all columns if (projection == null) projection = DEFAULT_COLUMNS; - ArrayList cols = new ArrayList(2); - ArrayList vals = new ArrayList(2); + ArrayList cols = new ArrayList(3); + ArrayList vals = new ArrayList(3); for (String column : projection) { if (column.equals(OpenableColumns.DISPLAY_NAME)) { @@ -46,6 +47,9 @@ public class CCFileProvider extends ContentProvider } else if (column.equals(OpenableColumns.SIZE)) { cols.add(OpenableColumns.SIZE); vals.add(file.length()); + } else if (column.equals(MediaStore.MediaColumns.DATA)) { + cols.add(MediaStore.MediaColumns.DATA); + vals.add(file.getAbsolutePath()); } } @@ -92,7 +96,7 @@ public class CCFileProvider extends ContentProvider // See AndroidManifest.xml for authority return new Uri.Builder() .scheme("content") - .authority("com.classicube.android.client.ccfiles") + .authority("com.classicube.android.client.provider") .encodedPath(Uri.encode(path, "/")) .build(); } @@ -100,7 +104,7 @@ public class CCFileProvider extends ContentProvider File getFileForUri(Uri uri) { String path = uri.getPath(); File file = new File(root, path); - + file = file.getAbsoluteFile(); // security validation check if (!file.getPath().startsWith(root.getPath())) { diff --git a/android/app/src/main/java/com/classicube/MainActivity.java b/android/app/src/main/java/com/classicube/MainActivity.java index dcf9d8faf..f3a0a068e 100644 --- a/android/app/src/main/java/com/classicube/MainActivity.java +++ b/android/app/src/main/java/com/classicube/MainActivity.java @@ -794,7 +794,17 @@ public class MainActivity extends Activity public String shareScreenshot(String path) { try { - Uri uri = CCFileProvider.getUriForFile("screenshots", path); + Uri uri; + if (android.os.Build.VERSION.SDK_INT >= 23){ // android 6.0 + uri = CCFileProvider.getUriForFile("screenshots/" + path); + } else { + // when trying to use content:// URIs on my android 4.0.3 test device + // - 1 app crashed + // - 1 app wouldn't show image previews + // so fallback to file:// on older devices as they seem to reliably work + File file = new File(getGameDataDirectory() + "/screenshots/" + path); + uri = Uri.fromFile(file); + } Intent intent = new Intent(); intent.setAction(Intent.ACTION_SEND); diff --git a/misc/buildbot.sh b/misc/buildbot.sh index 9f8d05bfe..92087f35b 100644 --- a/misc/buildbot.sh +++ b/misc/buildbot.sh @@ -179,9 +179,12 @@ build_android() { # https://github.com/skanti/Android-Manual-Build-Command-Line/blob/master/hello-jni/Makefile # https://github.com/skanti/Android-Manual-Build-Command-Line/blob/master/hello-jni/CMakeLists.txt - # compile interop java file into its multiple .class files - javac java/com/classicube/MainActivity.java -d ./obj -classpath $SDK_ROOT/android.jar + # compile java files into multiple .class files + cd $ROOT_DIR/android/app/src/main/java/com/classicube + javac *.java -d $ROOT_DIR/android/app/src/main/obj -classpath $SDK_ROOT/android.jar if [ $? -ne 0 ]; then echo "Failed to compile Android Java" >> "$ERRS_FILE"; fi + + cd $ROOT_DIR/android/app/src/main # compile the multiple .class files into one .dex file $TOOLS_ROOT/dx --dex --output=obj/classes.dex ./obj # create initial .apk with packaged version of resources