From 5aeaa30e0da138b052e8bedc6b9f44dc2f500931 Mon Sep 17 00:00:00 2001 From: Leonardo Acevedo Date: Wed, 2 May 2018 19:51:54 -0300 Subject: [PATCH 1/7] #72 - Opens gallery instead of camera --- .../vansuita/pickimage/dialog/PickImageDialog.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/library/src/main/java/com/vansuita/pickimage/dialog/PickImageDialog.java b/library/src/main/java/com/vansuita/pickimage/dialog/PickImageDialog.java index 34e76ee..255cdb8 100644 --- a/library/src/main/java/com/vansuita/pickimage/dialog/PickImageDialog.java +++ b/library/src/main/java/com/vansuita/pickimage/dialog/PickImageDialog.java @@ -1,5 +1,6 @@ package com.vansuita.pickimage.dialog; +import android.Manifest; import android.content.Intent; import android.content.pm.PackageManager; import android.os.Bundle; @@ -114,7 +115,7 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis if (granted) { if (!launchSystemDialog()) { - if (grantResults.length == 1) { + if (hasCameraPermission(permissions, grantResults)) { launchGallery(); } else { launchCamera(); @@ -131,6 +132,15 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis } } + private boolean hasCameraPermission(String[] permissions, int[] grantResults) { + int cameraIndex = 0; + while(cameraIndex < permissions.length && !permissions[cameraIndex].equals(Manifest.permission.CAMERA)) { + cameraIndex++; + } + + return cameraIndex < permissions.length && permissions[cameraIndex].equals(Manifest.permission.CAMERA); + } + /* public static void forceDismiss(FragmentManager fm) { From d3a8249f14a44eb9e1b72478326dfe97bbff0f68 Mon Sep 17 00:00:00 2001 From: Leonardo Acevedo Date: Wed, 2 May 2018 20:11:33 -0300 Subject: [PATCH 2/7] Deal with automatic response to PR --- .../pickimage/dialog/PickImageDialog.java | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/library/src/main/java/com/vansuita/pickimage/dialog/PickImageDialog.java b/library/src/main/java/com/vansuita/pickimage/dialog/PickImageDialog.java index 255cdb8..c705c97 100644 --- a/library/src/main/java/com/vansuita/pickimage/dialog/PickImageDialog.java +++ b/library/src/main/java/com/vansuita/pickimage/dialog/PickImageDialog.java @@ -115,7 +115,16 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis if (granted) { if (!launchSystemDialog()) { - if (hasCameraPermission(permissions, grantResults)) { + // See if the CAMERA permission is among the granted ones + int cameraIndex = -1; + for (int i = 0; i < permissions.length; i++) { + if (permissions[cameraIndex].equals(Manifest.permission.CAMERA)) { + cameraIndex = i; + break; + } + } + + if (cameraIndex != -1) { launchGallery(); } else { launchCamera(); @@ -127,21 +136,9 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis if (grantResults.length > 1) Keep.with(getActivity()).askedForPermission(); } - - } } - private boolean hasCameraPermission(String[] permissions, int[] grantResults) { - int cameraIndex = 0; - while(cameraIndex < permissions.length && !permissions[cameraIndex].equals(Manifest.permission.CAMERA)) { - cameraIndex++; - } - - return cameraIndex < permissions.length && permissions[cameraIndex].equals(Manifest.permission.CAMERA); - } - - /* public static void forceDismiss(FragmentManager fm) { Fragment fragment = fm.findFragmentByTag(PickImageDialog.DIALOG_FRAGMENT_TAG); From 59496e163a918d95dd41feb08f3f25d118ff3034 Mon Sep 17 00:00:00 2001 From: Leonardo Acevedo Date: Thu, 3 May 2018 12:22:17 -0300 Subject: [PATCH 3/7] #76 - FileNotFoundException after camera takes picture with "Don't keep Activities" --- .../pickimage/dialog/PickImageBaseDialog.java | 19 +++++++++++--- .../pickimage/resolver/IntentResolver.java | 25 ++++++++++++++++++- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/library/src/main/java/com/vansuita/pickimage/dialog/PickImageBaseDialog.java b/library/src/main/java/com/vansuita/pickimage/dialog/PickImageBaseDialog.java index 67406fb..71ac5be 100644 --- a/library/src/main/java/com/vansuita/pickimage/dialog/PickImageBaseDialog.java +++ b/library/src/main/java/com/vansuita/pickimage/dialog/PickImageBaseDialog.java @@ -36,6 +36,7 @@ public abstract class PickImageBaseDialog extends DialogFragment implements IPickClick { protected static final String SETUP_TAG = "SETUP_TAG"; + protected static final String RESOLVER_STATE_TAG = "resolverState"; public static final String DIALOG_FRAGMENT_TAG = PickImageBaseDialog.class.getSimpleName(); private PickSetup setup; @@ -68,7 +69,7 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, View view = inflater.inflate(dialog, null, false); onAttaching(); - onInitialize(); + onInitialize(savedInstanceState); if (isValidProviders()) { onBindViewsHolders(view); @@ -86,6 +87,16 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, return view; } + @Override + public void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + + Bundle resolverState = new Bundle(); + this.resolver.onSaveInstanceState(resolverState); + + outState.putBundle(RESOLVER_STATE_TAG, resolverState); + } + private void onAttaching() { if (onClick == null) { if (getActivity() instanceof IPickClick) { @@ -100,14 +111,16 @@ private void onAttaching() { } - protected void onInitialize() { + protected void onInitialize(Bundle savedInstanceState) { if (getDialog().getWindow() != null) { getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); } this.setup = (PickSetup) getArguments().getSerializable(SETUP_TAG); - this.resolver = new IntentResolver(getActivity(), setup); + + Bundle resolverState = savedInstanceState.getBundle(RESOLVER_STATE_TAG); + this.resolver = new IntentResolver(getActivity(), setup, resolverState); } diff --git a/library/src/main/java/com/vansuita/pickimage/resolver/IntentResolver.java b/library/src/main/java/com/vansuita/pickimage/resolver/IntentResolver.java index 0de6e4b..8b96b64 100644 --- a/library/src/main/java/com/vansuita/pickimage/resolver/IntentResolver.java +++ b/library/src/main/java/com/vansuita/pickimage/resolver/IntentResolver.java @@ -8,9 +8,11 @@ import android.content.pm.ResolveInfo; import android.net.Uri; import android.os.Build; +import android.os.Bundle; import android.os.Environment; import android.os.Parcelable; import android.provider.MediaStore; +import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.support.v4.app.Fragment; import android.support.v4.content.ContextCompat; @@ -35,6 +37,8 @@ public class IntentResolver { public static final int REQUESTER = 99; + public static final String SAVE_FILE_PATH_TAG = "savePath"; + private Activity activity; private PickSetup setup; @@ -42,9 +46,14 @@ public class IntentResolver { private Intent cameraIntent; private File saveFile; - public IntentResolver(Activity activity, PickSetup setup) { + + public IntentResolver(Activity activity, PickSetup setup, Bundle savedInstanceState) { this.activity = activity; this.setup = setup; + + if (savedInstanceState != null) { + onRestoreInstanceState(savedInstanceState); + } } private Intent loadSystemPackages(Intent intent) { @@ -241,4 +250,18 @@ public boolean fromCamera(Intent data) { public Activity getActivity() { return activity; } + + private void onRestoreInstanceState(@NonNull Bundle savedInstanceState) { + final String saveFilePath = savedInstanceState.getString(SAVE_FILE_PATH_TAG); + + if (saveFilePath == null) { + throw new NullPointerException(); + } + + saveFile = new File(saveFilePath); + } + + public void onSaveInstanceState(@NonNull Bundle outState) { + outState.putString(SAVE_FILE_PATH_TAG, saveFile.getAbsolutePath()); + } } From ecd28cdf299e270ffe22d3f8ba5fbd90a5b8e272 Mon Sep 17 00:00:00 2001 From: Leonardo Acevedo Date: Thu, 3 May 2018 12:32:56 -0300 Subject: [PATCH 4/7] Fix test-time crashes --- .../vansuita/pickimage/dialog/PickImageBaseDialog.java | 5 ++++- .../vansuita/pickimage/resolver/IntentResolver.java | 10 +++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/library/src/main/java/com/vansuita/pickimage/dialog/PickImageBaseDialog.java b/library/src/main/java/com/vansuita/pickimage/dialog/PickImageBaseDialog.java index 71ac5be..ddd9954 100644 --- a/library/src/main/java/com/vansuita/pickimage/dialog/PickImageBaseDialog.java +++ b/library/src/main/java/com/vansuita/pickimage/dialog/PickImageBaseDialog.java @@ -119,7 +119,10 @@ protected void onInitialize(Bundle savedInstanceState) { this.setup = (PickSetup) getArguments().getSerializable(SETUP_TAG); - Bundle resolverState = savedInstanceState.getBundle(RESOLVER_STATE_TAG); + Bundle resolverState = null; + if (savedInstanceState != null) { + resolverState = savedInstanceState.getBundle(RESOLVER_STATE_TAG); + } this.resolver = new IntentResolver(getActivity(), setup, resolverState); } diff --git a/library/src/main/java/com/vansuita/pickimage/resolver/IntentResolver.java b/library/src/main/java/com/vansuita/pickimage/resolver/IntentResolver.java index 8b96b64..9c5b4c7 100644 --- a/library/src/main/java/com/vansuita/pickimage/resolver/IntentResolver.java +++ b/library/src/main/java/com/vansuita/pickimage/resolver/IntentResolver.java @@ -254,14 +254,14 @@ public Activity getActivity() { private void onRestoreInstanceState(@NonNull Bundle savedInstanceState) { final String saveFilePath = savedInstanceState.getString(SAVE_FILE_PATH_TAG); - if (saveFilePath == null) { - throw new NullPointerException(); + if (saveFilePath != null) { + saveFile = new File(saveFilePath); } - - saveFile = new File(saveFilePath); } public void onSaveInstanceState(@NonNull Bundle outState) { - outState.putString(SAVE_FILE_PATH_TAG, saveFile.getAbsolutePath()); + if (saveFile != null) { + outState.putString(SAVE_FILE_PATH_TAG, saveFile.getAbsolutePath()); + } } } From d85131520a07c9e313dec60fee82e97cc3ad97e9 Mon Sep 17 00:00:00 2001 From: Leonardo Acevedo Date: Thu, 3 May 2018 12:59:01 -0300 Subject: [PATCH 5/7] Add the whole .idea directory into .gitignore. --- .gitignore | 3 +- .idea/compiler.xml | 22 ---- .idea/copyright/profiles_settings.xml | 3 - .idea/gradle.xml | 19 --- .../markdown-navigator/profiles_settings.xml | 3 - .idea/misc.xml | 115 ------------------ .idea/modules.xml | 10 -- .idea/runConfigurations.xml | 12 -- .idea/vcs.xml | 6 - 9 files changed, 1 insertion(+), 192 deletions(-) delete mode 100644 .idea/compiler.xml delete mode 100644 .idea/copyright/profiles_settings.xml delete mode 100644 .idea/gradle.xml delete mode 100644 .idea/markdown-navigator/profiles_settings.xml delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/runConfigurations.xml delete mode 100644 .idea/vcs.xml diff --git a/.gitignore b/.gitignore index 39fb081..09b993d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,7 @@ *.iml .gradle /local.properties -/.idea/workspace.xml -/.idea/libraries +/.idea .DS_Store /build /captures diff --git a/.idea/compiler.xml b/.idea/compiler.xml deleted file mode 100644 index 96cc43e..0000000 --- a/.idea/compiler.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/copyright/profiles_settings.xml b/.idea/copyright/profiles_settings.xml deleted file mode 100644 index e7bedf3..0000000 --- a/.idea/copyright/profiles_settings.xml +++ /dev/null @@ -1,3 +0,0 @@ - - - \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml deleted file mode 100644 index 66e2f6f..0000000 --- a/.idea/gradle.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/markdown-navigator/profiles_settings.xml b/.idea/markdown-navigator/profiles_settings.xml deleted file mode 100644 index 57927c5..0000000 --- a/.idea/markdown-navigator/profiles_settings.xml +++ /dev/null @@ -1,3 +0,0 @@ - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index bfd3ad3..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,115 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index cad800c..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml deleted file mode 100644 index 7f68460..0000000 --- a/.idea/runConfigurations.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 35eb1dd..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file From a1844d9355a8dbdebe7568d84aa63096e38258ff Mon Sep 17 00:00:00 2001 From: Leonardo Acevedo Date: Thu, 3 May 2018 15:16:54 -0300 Subject: [PATCH 6/7] Fix crash when no cancel callback has been defined --- .../com/vansuita/pickimage/dialog/PickImageBaseDialog.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/library/src/main/java/com/vansuita/pickimage/dialog/PickImageBaseDialog.java b/library/src/main/java/com/vansuita/pickimage/dialog/PickImageBaseDialog.java index 67406fb..57fbfbc 100644 --- a/library/src/main/java/com/vansuita/pickimage/dialog/PickImageBaseDialog.java +++ b/library/src/main/java/com/vansuita/pickimage/dialog/PickImageBaseDialog.java @@ -160,7 +160,9 @@ private void onBindViewListeners() { @Override public void onClick(View view) { if (view.getId() == R.id.cancel) { - onPickCancel.onCancelClick(); + if (onPickCancel != null) { + onPickCancel.onCancelClick(); + } dismiss(); } else { if (view.getId() == R.id.camera) { From 909c6816909a15722477590a067820462f2eb14b Mon Sep 17 00:00:00 2001 From: Leonardo Acevedo Date: Thu, 3 May 2018 15:17:43 -0300 Subject: [PATCH 7/7] Allow the Activity to act as IPickCancel if nothing else was defined, just like with IPickResult --- .../com/vansuita/pickimage/dialog/PickImageBaseDialog.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/library/src/main/java/com/vansuita/pickimage/dialog/PickImageBaseDialog.java b/library/src/main/java/com/vansuita/pickimage/dialog/PickImageBaseDialog.java index 57fbfbc..1cec28a 100644 --- a/library/src/main/java/com/vansuita/pickimage/dialog/PickImageBaseDialog.java +++ b/library/src/main/java/com/vansuita/pickimage/dialog/PickImageBaseDialog.java @@ -97,6 +97,9 @@ private void onAttaching() { if (onPickResult == null && getActivity() instanceof IPickResult) onPickResult = (IPickResult) getActivity(); + + if (onPickCancel == null && getActivity() instanceof IPickCancel) + onPickCancel = (IPickCancel) getActivity(); }