From 01e7fe702a38eb6ce093792ac3c517879f5fb746 Mon Sep 17 00:00:00 2001 From: rubikscraft Date: Sun, 12 Sep 2021 17:22:51 +0200 Subject: [PATCH] V1.1.0 --- app/build.gradle | 10 +-- app/src/main/AndroidManifest.xml | 88 ++++++++++++------- .../android/apps/photos/BlankActivity.java | 12 +++ .../android/apps/photos/MainActivity.java | 62 +++++-------- .../CameraAssistantService.java | 47 +--------- .../CameraAssistantServiceBind.java | 27 ------ .../ICameraAssistantServiceBind.java | 38 -------- build.gradle | 2 +- 8 files changed, 99 insertions(+), 187 deletions(-) create mode 100644 app/src/main/java/com/google/android/apps/photos/BlankActivity.java delete mode 100644 app/src/main/java/com/google/android/apps/photos/cameraassistant/CameraAssistantServiceBind.java delete mode 100644 app/src/main/java/com/google/android/apps/photos/cameraassistant/ICameraAssistantServiceBind.java diff --git a/app/build.gradle b/app/build.gradle index 82a0fa8..9b4a9b8 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,15 +1,15 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 29 - buildToolsVersion '29.0.2' + compileSdkVersion 31 + buildToolsVersion '31.0.0' defaultConfig { applicationId 'com.google.android.apps.photos' minSdkVersion 29 - targetSdkVersion 29 - versionCode 1 - versionName '1.0.0' + targetSdkVersion 31 + versionCode 2 + versionName '1.1.0' } /*signingConfigs { diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index e0cb49e..c6b205f 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,35 +1,59 @@ - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/google/android/apps/photos/BlankActivity.java b/app/src/main/java/com/google/android/apps/photos/BlankActivity.java new file mode 100644 index 0000000..6ec6735 --- /dev/null +++ b/app/src/main/java/com/google/android/apps/photos/BlankActivity.java @@ -0,0 +1,12 @@ +package com.google.android.apps.photos; + +import android.app.Activity; +import android.os.Bundle; + +public class BlankActivity extends Activity { + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + finish(); + } +} diff --git a/app/src/main/java/com/google/android/apps/photos/MainActivity.java b/app/src/main/java/com/google/android/apps/photos/MainActivity.java index 840cdfa..a7356eb 100644 --- a/app/src/main/java/com/google/android/apps/photos/MainActivity.java +++ b/app/src/main/java/com/google/android/apps/photos/MainActivity.java @@ -5,13 +5,7 @@ import android.content.Intent; import android.util.Log; import android.net.Uri; -import android.content.ContentResolver; -import java.io.InputStream; -import java.net.URI; -import android.content.ContentProviderClient; -import android.os.ParcelFileDescriptor; -import java.io.File; import android.database.Cursor; import android.provider.MediaStore; import android.content.Context; @@ -20,11 +14,12 @@ import android.content.pm.PackageManager; public class MainActivity extends Activity { - + // Polling settings for checking if the URI is finsihed yet private static final int waitMS = 100; private static final int maximumSeconds = 30000; private static final int iterations = maximumSeconds * 1000 / waitMS; + // Random integer to identify app in permission callback private static final int PERMISSIONS_INT = 755009201; private PendingIntent cameraRelaunchIntent; @@ -32,59 +27,48 @@ public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - // setContentView(R.layout.activity_main); Intent receivedIntent = getIntent(); + // Extract all necessary information from the intent + // We store the relaunch intent in this class so it can be used in callbacks + // (In this case that is just the permission callback) String SessionId = receivedIntent.getStringExtra("external_session_id"); - Uri uri = Uri.parse(receivedIntent.getData().toString() + "?requireOriginal=1"); + Uri uri = Uri.parse(receivedIntent.getData().toString()); String type = receivedIntent.getType(); - Uri processingURI = (Uri) receivedIntent.getParcelableExtra("processing_uri_intent_extra"); - this.cameraRelaunchIntent = (PendingIntent) receivedIntent.getParcelableExtra("CAMERA_RELAUNCH_INTENT_EXTRA"); + // Make sure there is even a session id, the call is probably invalid if this is gone if (SessionId == null) { finish(); return; } + // Make sure that we have the READ_EXTERNAL_STORAGE permission, + // we sadly need this permission to check if a URI is pending if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { Log.i("PhotosShim", "No permission, requesting them"); - requestPermissions(new String[] { Manifest.permission.READ_EXTERNAL_STORAGE }, PERMISSIONS_INT); + requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSIONS_INT); + quit(); return; } Log.i("PhotosShim", "SessionId: '" + SessionId + "', URI:'" + uri + "', type: '" + type + "', processingURI: '" + processingURI + "'"); + // Keep polling the URI to see if it has stopped being pending boolean success = false; - for (int i = 0; i < iterations; i++) { - String absolut = MainActivity.getRealPathFromURI(getApplicationContext(), uri); - if (absolut == "") - break; - - Log.i("PhotosShim", "URL: " + absolut); - - String name = ""; - try { - name = new File(absolut).getName(); - } catch (Exception e) { - Log.e("PhotosShim", "Error creating file obj", e); - break; - } - - if (name == "") - break; - - if (!name.startsWith(".pending")) { + if (!isUriPending(getApplicationContext(), uri)) { success = true; break; } + Log.i("PhotosShim", "Pending..."); + try { Thread.sleep(waitMS); } catch (Exception e) { @@ -98,6 +82,9 @@ protected void onCreate(Bundle savedInstanceState) { return; } + // If the URI is not pending anymore we send an implicit intent + // so you can now handle the image with any gallery you want + Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setDataAndType(uri, "image/*"); @@ -114,7 +101,6 @@ private void quit() { Log.e("PhotosShim", "Error sending intent", e); } finish(); - return; } @Override @@ -124,19 +110,19 @@ public void onRequestPermissionsResult(int requestCode, String[] permissions, in if (permissions.length == 0 || grantResults.length == 0) return; - if (permissions[0] == Manifest.permission.READ_EXTERNAL_STORAGE) + if (permissions[0].equals(Manifest.permission.READ_EXTERNAL_STORAGE)) quit(); } - public static String getRealPathFromURI(Context context, Uri contentUri) { + public static boolean isUriPending(Context context, Uri contentUri) { Cursor cursor = null; - String result = ""; + boolean result; try { - String[] proj = { MediaStore.Images.Media.DATA }; + String[] proj = {MediaStore.Images.Media.IS_PENDING}; cursor = context.getContentResolver().query(contentUri, proj, null, null, null); cursor.moveToFirst(); - int column_index = cursor.getColumnIndex(proj[0]); - result = cursor.getString(column_index); + int column_index_data = cursor.getColumnIndex(proj[0]); + result = cursor.getInt(column_index_data) != 0; } finally { if (cursor != null) cursor.close(); diff --git a/app/src/main/java/com/google/android/apps/photos/cameraassistant/CameraAssistantService.java b/app/src/main/java/com/google/android/apps/photos/cameraassistant/CameraAssistantService.java index 2c1f261..ad33bb5 100644 --- a/app/src/main/java/com/google/android/apps/photos/cameraassistant/CameraAssistantService.java +++ b/app/src/main/java/com/google/android/apps/photos/cameraassistant/CameraAssistantService.java @@ -7,55 +7,10 @@ import android.content.res.Configuration; public class CameraAssistantService extends Service { - - private final CameraAssistantServiceBind b = new CameraAssistantServiceBind(this); - - @Override - public void onCreate() { - super.onCreate(); - Log.i("DEBUGCAM", "DEBUGCAM: created"); - } - - @Override - public int onStartCommand(Intent intent, int flags, int startId) { - Log.i("DEBUGCAM", "DEBUGCAM: start flags " + flags + " id " + startId + " intent " + intent); - return Service.START_NOT_STICKY; - } - @Override public IBinder onBind(Intent intent) { Log.i("DEBUGCAM", "BOUND: " + intent + " with " + intent.getFlags()); - return b; - } - - @Override - public void onConfigurationChanged(Configuration config) { - Log.i("DEBUGCAM", "CONFIG CHANGE: " + config); - super.onConfigurationChanged(config); - } - - @Override - public void onStart(Intent intent, int id) { - Log.i("DEBUGCAM", "START: " + intent + " with " + id); - super.onStart(intent, id); - } - - @Override - public boolean onUnbind(Intent intent) { - - Log.i("DEBUGCAM", "UNBIND: " + intent); - return false; - } - - @Override - public void onTaskRemoved(Intent intent) { - Log.i("DEBUGCAM", "TASKREMOVE: " + intent); - super.onTaskRemoved(intent); - } - - @Override - public void onDestroy() { - Log.i("DEBUGCAM", "DEBUGCAM: destroyed"); + return null; } } diff --git a/app/src/main/java/com/google/android/apps/photos/cameraassistant/CameraAssistantServiceBind.java b/app/src/main/java/com/google/android/apps/photos/cameraassistant/CameraAssistantServiceBind.java deleted file mode 100644 index 6d0f59c..0000000 --- a/app/src/main/java/com/google/android/apps/photos/cameraassistant/CameraAssistantServiceBind.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.google.android.apps.photos.cameraassistant; - -import android.os.IBinder; -import android.os.Parcel; -import android.util.Log; - -public final class CameraAssistantServiceBind extends ICameraAssistantServiceBind { - final CameraAssistantService a; - - - public CameraAssistantServiceBind(CameraAssistantService cameraAssistantService) { - super("com.google.android.apps.photos.cameraassistant.ICameraAssistantService"); - this.a = cameraAssistantService; - } - - public CameraAssistantServiceBind() { - super("com.google.android.apps.photos.cameraassistant.ICameraAssistantService"); - this.a = null; - } - - @Override - public final boolean gh(int i, Parcel a, Parcel b) { - Log.i("DEBUGCAM", "Int: " + i + " Parcels: " + a + " " + b); - - return true; - } -} diff --git a/app/src/main/java/com/google/android/apps/photos/cameraassistant/ICameraAssistantServiceBind.java b/app/src/main/java/com/google/android/apps/photos/cameraassistant/ICameraAssistantServiceBind.java deleted file mode 100644 index b8f70ed..0000000 --- a/app/src/main/java/com/google/android/apps/photos/cameraassistant/ICameraAssistantServiceBind.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.google.android.apps.photos.cameraassistant; - -import android.os.IInterface; -import android.os.IBinder; -import android.os.Binder; -import android.os.Parcel; -import android.util.Log; - -public abstract class ICameraAssistantServiceBind extends Binder implements IInterface { - protected ICameraAssistantServiceBind(String str) { - attachInterface(this, str); - } - - public IBinder asBinder() { - return this; - } - - protected boolean gh(int i, Parcel a, Parcel b) { - return false; - } - - @Override - public boolean onTransact(int i1, Parcel a, Parcel b, int i2) { - Log.i("DEBUGCAM", "TRANSACTING"); - try { - if (i1 <= 16777215) { - a.enforceInterface(getInterfaceDescriptor()); - } else if (super.onTransact(i1, a, b, i2)) { - return true; - } - return gh(i1, a, b); - } catch (Exception e) { - Log.e("DEBUGCAM", e.toString()); - return false; - } - } - -} diff --git a/build.gradle b/build.gradle index 6f2942b..2f23a71 100644 --- a/build.gradle +++ b/build.gradle @@ -11,7 +11,7 @@ buildscript { google() } dependencies { - classpath 'com.android.tools.build:gradle:3.5.3' + classpath 'com.android.tools.build:gradle:3.5.4' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files