Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #194: Update to latest libs containing the fix for 2 initial intents #327

Merged
merged 9 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
import androidx.annotation.Nullable;
import dagger.hilt.android.AndroidEntryPoint;

import net.twisterrob.android.contracts.CreateOpenableDocument;
import net.twisterrob.android.contracts.OpenOpenableDocument;
import net.twisterrob.android.utils.tools.DialogTools;
import net.twisterrob.inventory.android.Constants;
import net.twisterrob.inventory.android.backup.R;
import net.twisterrob.inventory.android.backup.concurrent.BackupService;
import net.twisterrob.inventory.android.content.CreateOpenableDocument;
import net.twisterrob.inventory.android.content.InventoryContract;
import net.twisterrob.inventory.android.content.OpenOpenableDocument;

@AndroidEntryPoint
public class BackupControllerFragment extends BaseFragment<BackupControllerFragment.BackupEvents> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,19 @@
package net.twisterrob.inventory.android.utils;

import java.io.File;
import java.util.regex.Pattern;
import android.graphics.Color;
import android.graphics.ColorMatrix;

import org.slf4j.*;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.*;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;

import static android.graphics.Color.*;

import androidx.annotation.*;

import net.twisterrob.android.content.ImageRequest;
import net.twisterrob.android.utils.tools.*;
import androidx.annotation.ColorInt;
import androidx.annotation.FloatRange;
import androidx.annotation.NonNull;

public abstract class PictureHelper {
private static final Logger LOG = LoggerFactory.getLogger(PictureHelper.class);

private static final String CROPPED_SUFFIX = "_crop";
private static final String EXTENSION_SEPARATOR = ".";
private static final String EXTRACT_PARTS = "(.*)" + Pattern.quote(EXTENSION_SEPARATOR) + "(.*)";
private static final float[] NEGATIVE = new float[] {
-1, 0, 0, 0, 255,
0, -1, 0, 0, 255,
0, 0, -1, 0, 255,
0, 0, 0, 1, 0
};
private @Nullable Bitmap thumb;
private Bitmap image;
private File file;
private File cropFile;
private final Activity activity;

public PictureHelper(Activity activity) {
this.activity = activity;
}

/**
* Given a transparent greyscale image this tints the blacks/greys with the given color, leaves white intact.
Expand All @@ -55,10 +28,10 @@ public PictureHelper(Activity activity) {
ColorMatrix matrix = new ColorMatrix();
matrix.postConcat(new ColorMatrix(NEGATIVE));
matrix.postConcat(new ColorMatrix(new float[] {
1 - red(color) / 255f, 0, 0, 0, 0,
0, 1 - green(color) / 255f, 0, 0, 0,
0, 0, 1 - blue(color) / 255f, 0, 0,
0, 0, 0, alpha(color) / 255f, 0
1 - Color.red(color) / 255f, 0, 0, 0, 0,
0, 1 - Color.green(color) / 255f, 0, 0, 0,
0, 0, 1 - Color.blue(color) / 255f, 0, 0,
0, 0, 0, Color.alpha(color) / 255f, 0
}));
matrix.postConcat(new ColorMatrix(NEGATIVE));
return matrix;
Expand All @@ -79,108 +52,18 @@ public PictureHelper(Activity activity) {
* @see <a href="http://stackoverflow.com/a/31217267/253468">SO</a>
* @see <a href="https://gist.github.com/ro-sharp/49fd46a071a267d9e5dd">Gist</a>
*/
@SuppressWarnings("UnusedAssignment")
public static int getColor(Object thing) {
public static @ColorInt int getColor(@NonNull Object thing) {
int seed = thing.hashCode();
// Math.sin jumps big enough even when adding 1, because argument is radian and period is ~3
int rand_r = (int)Math.abs(Math.sin(seed++) * 10000) & 0xFF;
int rand_g = (int)Math.abs(Math.sin(seed++) * 10000) & 0xFF;
int rand_b = (int)Math.abs(Math.sin(seed++) * 10000) & 0xFF;

int r = Math.round((160 + rand_r) / 2);
int g = Math.round((160 + rand_g) / 2);
int b = Math.round((160 + rand_b) / 2);
return argb(0xFF, r, g, b);
}

protected abstract File getTargetFile();

protected File getTargetFileCropped() {
File file = getTargetFile();
String name = file.getName().replaceFirst(EXTRACT_PARTS, "$1");
String ext = file.getName().replaceFirst(EXTRACT_PARTS, "$2");
return new File(file.getParentFile(), name + CROPPED_SUFFIX + EXTENSION_SEPARATOR + ext);
}

public Intent startCapture() {
file = getTargetFile();
return new ImageRequest.Builder(activity).addCameraIntents(file).build().getIntent();
}

/**
* Back from chooser: code = 0, data = null
* Camera take picture (file, uri): code = -1, data = null, uri= file:///storage/emulated/0/Android/data/net.twisterrob.inventory/files/Pictures/Item_5_20140717_173719.jpg
* Gallery pick picture: code = -1, data = (action=null, data=content://media/external/images/media/1173, extras)
* mimeType=image/*
* selectedCount=1
* selectedItems=content://media/external/images/media/1173
* Total commander: data=content://com.ghisler.android.TotalCommander.files/storage/emulated/0/DCIM/Facebook/IMG_85501072011957.jpeg
* @return <code>true</code> if capture was successful
*/
public boolean endCapture(int resultCode, Intent intent) {
LOG.trace("endCapture(resultCode={}, intent={}): {}\nextras={}", resultCode, intent, file,
intent != null? StringerTools.toShortString(intent.getExtras()) : null);
if (resultCode == Activity.RESULT_CANCELED) {
file = null;
return false;
}
file = processReceivedIntent(intent);
return true;
}

public File getFile() {
return cropFile != null? cropFile : file;
}

public Bitmap getBitmap() {
return image;
}

public Bitmap getThumbnail() {
return thumb;
}

public Intent startCrop() {
cropFile = getTargetFileCropped();
Intent intent = null;
if (file != null && cropFile != null) {
intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(Uri.fromFile(file), "image/*");
intent.putExtra("crop", "true");
intent.putExtra("scale", "true");
intent.putExtra("scaleUpIfNeeded", true);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 256);
intent.putExtra("outputY", 256);
intent.putExtra("return-data", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT, cropFile);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
if (PackageManagerTools.resolveActivity(activity.getPackageManager(), intent, PackageManager.MATCH_DEFAULT_ONLY) == null) {
//App.toastUser("Sorry, your device can't crop this image!");
intent = null;
}
}
return intent;
}

public boolean endCrop(int resultCode, Intent intent) {
LOG.trace("endCrop(resultCode={}, intent={}): {}\nextras={}", resultCode, intent, cropFile,
intent != null? StringerTools.toShortString(intent.getExtras()) : null);
if (resultCode == Activity.RESULT_CANCELED) {
cropFile = null;
return false;
}
cropFile = processReceivedIntent(intent);
return true;
}

private File processReceivedIntent(Intent intent) {
File file = null;
if (intent != null) {
file = ImageTools.getFile(activity, intent.getData());
thumb = IntentTools.getParcelableExtra(intent, "data", Bitmap.class);
}
return file;
// The `& 0xFF` will truncate the higher bits, so we end up with [0, 255] range.
int rand_r = (int)Math.abs(Math.sin(++seed) * 10000) & 0xFF;
int rand_g = (int)Math.abs(Math.sin(++seed) * 10000) & 0xFF;
int rand_b = (int)Math.abs(Math.sin(++seed) * 10000) & 0xFF;

// r, g, b in range of [(160 + 0) / 2 = 80, (160 + 255) / 2 = 207]
int r = (160 + rand_r) / 2;
int g = (160 + rand_g) / 2;
int b = (160 + rand_b) / 2;
return Color.argb(0xFF, r, g, b);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import android.net.Uri
import android.widget.Toast
import androidx.activity.result.ActivityResultCaller
import dagger.hilt.android.qualifiers.ActivityContext
import net.twisterrob.android.contracts.CreateOpenableDocument
import net.twisterrob.android.contracts.OpenOpenableDocument
import net.twisterrob.android.utils.tools.DialogTools
import net.twisterrob.android.utils.tools.DialogTools.PopupCallbacks
import net.twisterrob.inventory.android.arch.UiEffectHandler
import net.twisterrob.inventory.android.content.CreateOpenableDocument
import net.twisterrob.inventory.android.content.OpenOpenableDocument
import javax.inject.Inject

internal class ManageSpaceUiEffectHandler @Inject constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.uiautomator.UiObjectNotFoundException;

import net.twisterrob.android.contracts.CreateOpenableDocument;
import net.twisterrob.android.test.automators.DocumentsUiAutomator;
import net.twisterrob.android.utils.tools.IOTools;
import net.twisterrob.inventory.android.content.CreateOpenableDocument;

import static net.twisterrob.android.test.automators.UiAutomatorExtensions.clickOn;
import static net.twisterrob.android.test.automators.UiAutomatorExtensions.exists;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import androidx.annotation.*;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.view.menu.MenuBuilder;
import androidx.appcompat.widget.PopupMenu;
import androidx.appcompat.widget.SearchView;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentManager.*;
Expand Down Expand Up @@ -114,11 +114,10 @@ private BaseFragment<?> getFragment() {
welcome();
}

@SuppressLint("RestrictedApi") // MenuBuilder() and add() are used, but only for debugging
private void autoDebug() {
if (Constants.DISABLE) {
onOptionsItemSelected(new MenuBuilder(this)
.add(0, R.id.debug, 0, "Debug"));
Menu menu = new PopupMenu(this, getWindow().getDecorView()).getMenu();
onOptionsItemSelected(menu.add(R.id.debug, R.id.debug_capture, Menu.NONE, "Debug"));
}
}

Expand Down Expand Up @@ -357,11 +356,18 @@ public boolean onDebugOptionsItemSelected(MenuItem item) {
startActivity(ItemViewActivity.show(10010L));
return true;
} else if (id == R.id.debug_capture) {
File devFile =
new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "dev.jpg");
Uri target = Uri.fromFile(devFile);
startActivityForResult(CaptureImage.saveTo(this, devFile, target, 8192),
REQUEST_CODE_IMAGE);
Uri target;
StrictMode.ThreadPolicy originalPolicy = StrictMode.allowThreadDiskWrites();
try {
File devFile = Constants.Paths.getTempImage(this);
target = Constants.Paths.getShareUri(this, devFile);
} catch (IOException ex) {
throw new RuntimeException(ex);
} finally {
StrictMode.setThreadPolicy(originalPolicy);
}
Intent intent = CaptureImage.saveTo(this, target, 8192);
startActivityForResult(intent, REQUEST_CODE_IMAGE);
return true;
} else if (id == R.id.debug_testdb) {
resetToTestDatabase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
import dagger.hilt.android.AndroidEntryPoint;
import kotlin.io.FilesKt;

import net.twisterrob.android.contracts.CreateOpenableDocument;
import net.twisterrob.android.utils.concurrent.SimpleSafeAsyncTask;
import net.twisterrob.android.utils.tools.AndroidTools;
import net.twisterrob.inventory.android.*;
import net.twisterrob.inventory.android.Constants.Paths;
import net.twisterrob.inventory.android.activity.MainActivity;
import net.twisterrob.inventory.android.categories.cache.CategoryCache;
import net.twisterrob.inventory.android.content.CreateOpenableDocument;
import net.twisterrob.inventory.android.content.Intents;
import net.twisterrob.inventory.android.content.Intents.Extras;
import net.twisterrob.inventory.android.content.contract.Category;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ private void getPicture() {
try {
File file = Constants.Paths.getTempImage(requireContext());
Uri fileUri = Paths.getShareUri(requireContext(), file);
Intent intent = CaptureImage.saveTo(requireContext(), file, fileUri, 2048/*px*/);
Intent intent = CaptureImage.saveTo(requireContext(), fileUri, 2048/*px*/);
intent.putExtra(CaptureImage.EXTRA_FORMAT, CompressFormat.JPEG);
intent.putExtra(CaptureImage.EXTRA_QUALITY, 85/*%*/);
startActivityForResult(intent, REQUEST_CODE_GET_PICTURE);
Expand Down
2 changes: 1 addition & 1 deletion android/src/main/res/menu/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<item android:id="@+id/debug_testdb" android:title="Reset to Test DB" />
<item android:id="@+id/debug_showCategory" android:title="Show Category" />
<item android:id="@+id/debug_showItem" android:title="Show Item" />
<item android:id="@+id/debug_capture" android:title="Capture Image" />
<item android:id="@+id/debug_capture" android:title="Capture Image" />
<item android:id="@+id/debug_showImage" android:title="Show Image (InventoryProvider)" />
</group>
</menu>
Expand Down
2 changes: 1 addition & 1 deletion libs
Submodule libs updated 52 files
+2 −4 .github/workflows/CI.yml
+4 −15 gradle/libs.versions.toml
+0 −13 gradle/plugins/src/main/kotlin/net/twisterrob/libraries/build/allprojects.gradle.kts
+1 −0 ...e/plugins/src/main/kotlin/net/twisterrob/libraries/net.twisterrob.libraries.android.test-helpers.gradle.kts
+1 −6 internal/test/jvm_unit/build.gradle
+20 −0 twister-lib-android/basics/src/main/java/net/twisterrob/android/contracts/CreateOpenableDocument.kt
+27 −0 twister-lib-android/basics/src/main/java/net/twisterrob/android/contracts/OpenApplicationDetailsInSettings.kt
+18 −0 twister-lib-android/basics/src/main/java/net/twisterrob/android/contracts/OpenOpenableDocument.kt
+16 −0 twister-lib-android/basics/src/main/java/net/twisterrob/android/contracts/PickForMimeType.kt
+0 −34 twister-lib-android/basics/src/main/java/net/twisterrob/android/contracts/SettingsContracts.java
+16 −0 twister-lib-android/basics/src/main/java/net/twisterrob/android/utils/tools/IntentTools.java
+42 −0 twister-lib-android/basics/src/main/java/net/twisterrob/android/utils/tools/MenuTools.kt
+55 −0 twister-lib-android/basics/src/main/java/net/twisterrob/android/utils/tools/StrictModeTools.kt
+26 −14 twister-lib-android/basics/src/test/java/net/twisterrob/android/utils/tools/DatabaseToolsTest.java
+200 −0 twister-lib-android/basics/src/test/java/net/twisterrob/android/utils/tools/MenuToolsTest_asIterable.kt
+10 −0 twister-lib-android/capture_image/src/androidTest/AndroidManifest.xml
+2 −1 ...ndroid/capture_image/src/androidTest/java/net/twisterrob/android/activity/CaptureImageActivityTestRule.java
+47 −24 ...b-android/capture_image/src/androidTest/java/net/twisterrob/android/activity/CaptureImageTest_External.java
+27 −8 twister-lib-android/capture_image/src/main/AndroidManifest.xml
+137 −178 twister-lib-android/capture_image/src/main/java/net/twisterrob/android/activity/CaptureImage.java
+66 −0 twister-lib-android/capture_image/src/main/java/net/twisterrob/android/content/CaptureImageFileProvider.kt
+10 −0 twister-lib-android/capture_image/src/main/java/net/twisterrob/android/content/FileUriExposedException.kt
+0 −250 twister-lib-android/capture_image/src/main/java/net/twisterrob/android/content/ImageRequest.java
+55 −0 twister-lib-android/capture_image/src/main/java/net/twisterrob/android/utils/tools/CameraTools.java
+127 −0 twister-lib-android/capture_image/src/main/java/net/twisterrob/android/utils/tools/CropTools.java
+298 −0 twister-lib-android/capture_image/src/main/java/net/twisterrob/android/view/ExternalPicker.kt
+14 −9 twister-lib-android/capture_image/src/main/java/net/twisterrob/android/view/SelectionView.java
+8 −6 twister-lib-android/capture_image/src/main/res/layout/activity_camera.xml
+49 −0 twister-lib-android/capture_image/src/main/res/menu/image__choose_external.xml
+1 −0 twister-lib-android/capture_image/src/main/res/values-land/activity_camera_values.xml
+0 −9 twister-lib-android/capture_image/src/main/res/values-v29/strings.xml
+1 −0 twister-lib-android/capture_image/src/main/res/values/activity_camera_values.xml
+12 −0 twister-lib-android/capture_image/src/main/res/values/dimens.xml
+14 −6 twister-lib-android/capture_image/src/main/res/values/strings.xml
+12 −0 twister-lib-android/capture_image/src/main/res/xml/image__capture_paths.xml
+1 −0 twister-lib-android/capture_image/test_helpers/build.gradle
+0 −13 ...r-lib-android/capture_image/test_helpers/src/main/java/net/twisterrob/android/activity/PickDialogActor.java
+93 −0 ...ter-lib-android/capture_image/test_helpers/src/main/java/net/twisterrob/android/activity/PickDialogActor.kt
+1 −8 twister-lib-android/config/lint/lint.xml
+11 −0 twister-lib-android/espresso/src/main/java/net/twisterrob/android/test/espresso/DialogMatchers.java
+68 −0 twister-lib-android/espresso/src/main/java/net/twisterrob/android/test/espresso/ViewActions.java
+54 −0 ...spresso/src/main/java/net/twisterrob/android/test/espresso/actions/ScreenTranslatedCoordinatesProvider.java
+6 −0 twister-lib-android/monolith/src/main/res/values/consts.xml
+4 −4 twister-lib-android/orbit/src/test/kotlin/net/twisterrob/orbit/logging/LoggingContainerDecoratorTest.kt
+2 −2 ...er-lib-android/permissions/src/main/java/net/twisterrob/android/permissions/PermissionDenialRemediator.java
+7 −3 twister-lib-android/uiautomator/src/main/java/net/twisterrob/android/test/automators/GoogleDriveAutomator.java
+0 −3 twister-lib-java/config/lint/lint.xml
+8 −2 twister-lib-java/lib/general/src/test/java/net/twisterrob/java/text/SuggesterTest.java
+20 −17 twister-lib-java/lib/hamcrest/src/test/java/net/twisterrob/test/hamcrest/WrappingMatcherTest.java
+1 −1 twister-lib-java/lib/mockito/build.gradle
+2 −1 twister-lib-java/lib/mockito/src/test/java/net/twisterrob/test/mockito/SameFileTest.java
+12 −15 twister-lib-java/utils/stringer/src/test/java/net/twisterrob/java/utils/tostring/StringerRepoTest.java
Loading