file:///scard/picture.jpg
36 | * file:///android_asset/picture.png
38 | * android.resource://com.example.app/drawable/picture
40 | *
41 | * @param context Application context
42 | * @param uri URI of the image
43 | * @param password
44 | * @param version
45 | * @return the decoded bitmap
46 | * @throws Exception if decoding fails.
47 | */
48 | @NonNull
49 | Bitmap decode(Context context, @NonNull Uri uri, char[] password, int version) throws Exception;
50 |
51 | }
--------------------------------------------------------------------------------
/app/src/main/java/se/arctosoft/vault/subsampling/decoder/ImageRegionDecoder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Valv-Android
3 | * Copyright (c) 2024 Arctosoft AB.
4 | *
5 | * This program is free software: you can redistribute it and/or modify
6 | * it under the terms of the GNU General Public License as published by
7 | * the Free Software Foundation, either version 3 of the License, or
8 | * (at your option) any later version.
9 | *
10 | * This program is distributed in the hope that it will be useful,
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 | * GNU General Public License for more details.
14 | *
15 | * You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/.
16 | */
17 |
18 | package se.arctosoft.vault.subsampling.decoder;
19 |
20 | import android.content.Context;
21 | import android.graphics.Bitmap;
22 | import android.graphics.Point;
23 | import android.graphics.Rect;
24 | import android.net.Uri;
25 |
26 | import androidx.annotation.NonNull;
27 |
28 | /**
29 | * Interface for image decoding classes, allowing the default {@link android.graphics.BitmapRegionDecoder}
30 | * based on the Skia library to be replaced with a custom class.
31 | */
32 | public interface ImageRegionDecoder {
33 |
34 | /**
35 | * Initialise the decoder. When possible, perform initial setup work once in this method. The
36 | * dimensions of the image must be returned. The URI can be in one of the following formats:
37 | * file:///scard/picture.jpg
39 | * file:///android_asset/picture.png
41 | * android.resource://com.example.app/drawable/picture
43 | *
44 | * @param context Application context. A reference may be held, but must be cleared on recycle.
45 | * @param uri URI of the image.
46 | * @param password
47 | * @param version
48 | * @return Dimensions of the image.
49 | * @throws Exception if initialisation fails.
50 | */
51 | @NonNull
52 | Point init(Context context, @NonNull Uri uri, char[] password, int version) throws Exception;
53 |
54 | /**
55 | *
56 | * Decode a region of the image with the given sample size. This method is called off the UI
57 | * thread so it can safely load the image on the current thread. It is called from
58 | * {@link android.os.AsyncTask}s running in an executor that may have multiple threads, so
59 | * implementations must be thread safe. Adding synchronized
to the method signature
60 | * is the simplest way to achieve this, but bear in mind the {@link #recycle()} method can be
61 | * called concurrently.
62 | *
63 | * See {@link SkiaImageRegionDecoder} and {@link SkiaPooledImageRegionDecoder} for examples of 64 | * internal locking and synchronization. 65 | *
66 | * 67 | * @param sRect Source image rectangle to decode. 68 | * @param sampleSize Sample size. 69 | * @return The decoded region. It is safe to return null if decoding fails. 70 | */ 71 | @NonNull 72 | Bitmap decodeRegion(@NonNull Rect sRect, int sampleSize); 73 | 74 | /** 75 | * Status check. Should return false before initialisation and after recycle. 76 | * 77 | * @return true if the decoder is ready to be used. 78 | */ 79 | boolean isReady(); 80 | 81 | /** 82 | * This method will be called when the decoder is no longer required. It should clean up any resources still in use. 83 | */ 84 | void recycle(); 85 | 86 | } 87 | -------------------------------------------------------------------------------- /app/src/main/java/se/arctosoft/vault/subsampling/decoder/SkiaImageDecoder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Valv-Android 3 | * Copyright (c) 2024 Arctosoft AB. 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/. 16 | */ 17 | 18 | package se.arctosoft.vault.subsampling.decoder; 19 | 20 | import android.content.ContentResolver; 21 | import android.content.Context; 22 | import android.graphics.Bitmap; 23 | import android.graphics.BitmapFactory; 24 | import android.net.Uri; 25 | 26 | import androidx.annotation.Keep; 27 | import androidx.annotation.NonNull; 28 | import androidx.annotation.Nullable; 29 | 30 | import se.arctosoft.vault.encryption.Encryption; 31 | import se.arctosoft.vault.subsampling.MySubsamplingScaleImageView; 32 | 33 | /** 34 | * Default implementation of {@link com.davemorrissey.labs.subscaleview.decoder.ImageDecoder} 35 | * using Android's {@link android.graphics.BitmapFactory}, based on the Skia library. This 36 | * works well in most circumstances and has reasonable performance, however it has some problems 37 | * with grayscale, indexed and CMYK images. 38 | */ 39 | public class SkiaImageDecoder implements ImageDecoder { 40 | private final Bitmap.Config bitmapConfig; 41 | 42 | @Keep 43 | @SuppressWarnings("unused") 44 | public SkiaImageDecoder() { 45 | this(null); 46 | } 47 | 48 | @SuppressWarnings({"WeakerAccess", "SameParameterValue"}) 49 | public SkiaImageDecoder(@Nullable Bitmap.Config bitmapConfig) { 50 | Bitmap.Config globalBitmapConfig = MySubsamplingScaleImageView.getPreferredBitmapConfig(); 51 | if (bitmapConfig != null) { 52 | this.bitmapConfig = bitmapConfig; 53 | } else if (globalBitmapConfig != null) { 54 | this.bitmapConfig = globalBitmapConfig; 55 | } else { 56 | this.bitmapConfig = Bitmap.Config.RGB_565; 57 | } 58 | } 59 | 60 | @Override 61 | @NonNull 62 | public Bitmap decode(Context context, @NonNull Uri uri, char[] password, int version) throws Exception { 63 | BitmapFactory.Options options = new BitmapFactory.Options(); 64 | Bitmap bitmap; 65 | options.inPreferredConfig = bitmapConfig; 66 | Encryption.Streams streams = null; 67 | try { 68 | ContentResolver contentResolver = context.getContentResolver(); 69 | streams = Encryption.getCipherInputStream(contentResolver.openInputStream(uri), password, false, version); 70 | bitmap = BitmapFactory.decodeStream(streams.getInputStream(), null, options); 71 | } finally { 72 | if (streams != null) { 73 | streams.close(); 74 | } 75 | } 76 | if (bitmap == null) { 77 | throw new RuntimeException("Skia image region decoder returned null bitmap - image format may not be supported"); 78 | } 79 | return bitmap; 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /app/src/main/java/se/arctosoft/vault/utils/Constants.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Valv-Android 3 | * Copyright (C) 2024 Arctosoft AB 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see https://www.gnu.org/licenses/. 17 | */ 18 | 19 | package se.arctosoft.vault.utils; 20 | 21 | public class Constants { 22 | public static final float FULL = 1f; 23 | public static final float HALF = 0.5f; 24 | } 25 | -------------------------------------------------------------------------------- /app/src/main/java/se/arctosoft/vault/utils/GlideStuff.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Valv-Android 3 | * Copyright (C) 2024 Arctosoft AB 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see https://www.gnu.org/licenses/. 17 | */ 18 | 19 | package se.arctosoft.vault.utils; 20 | 21 | import androidx.annotation.NonNull; 22 | 23 | import com.bumptech.glide.load.engine.DiskCacheStrategy; 24 | import com.bumptech.glide.request.RequestOptions; 25 | import com.bumptech.glide.signature.ObjectKey; 26 | 27 | import se.arctosoft.vault.MainActivity; 28 | 29 | public class GlideStuff { 30 | 31 | @NonNull 32 | public static RequestOptions getRequestOptions(boolean useDiskCache) { 33 | return new RequestOptions() 34 | .diskCacheStrategy(useDiskCache ? DiskCacheStrategy.AUTOMATIC : DiskCacheStrategy.NONE) 35 | .signature(new ObjectKey(MainActivity.GLIDE_KEY)); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /app/src/main/java/se/arctosoft/vault/utils/Pixels.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Valv-Android 3 | * Copyright (c) 2024 Arctosoft AB. 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/. 16 | */ 17 | 18 | package se.arctosoft.vault.utils; 19 | 20 | import android.content.Context; 21 | import android.util.DisplayMetrics; 22 | 23 | import androidx.annotation.NonNull; 24 | 25 | public class Pixels { 26 | 27 | public static int dpToPixel(float dp, @NonNull Context context) { 28 | return (int) (dp * ((float) context.getResources().getDisplayMetrics().densityDpi / DisplayMetrics.DENSITY_DEFAULT)); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /app/src/main/java/se/arctosoft/vault/utils/StringStuff.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Valv-Android 3 | * Copyright (C) 2024 Arctosoft AB 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see https://www.gnu.org/licenses/. 17 | */ 18 | 19 | package se.arctosoft.vault.utils; 20 | 21 | import android.icu.text.DecimalFormat; 22 | 23 | import androidx.annotation.NonNull; 24 | 25 | import java.util.Random; 26 | 27 | public class StringStuff { 28 | private static final String ALLOWED_CHARACTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; 29 | private static final int NAME_LENGTH = 32; 30 | 31 | public static String getRandomFileName() { 32 | return getRandomFileName(NAME_LENGTH); 33 | } 34 | 35 | @NonNull 36 | public static String getRandomFileName(int length) { 37 | final Random random = new Random(); 38 | final StringBuilder sb = new StringBuilder(length); 39 | for (int i = 0; i < length; ++i) { 40 | sb.append(ALLOWED_CHARACTERS.charAt(random.nextInt(ALLOWED_CHARACTERS.length()))); 41 | } 42 | return sb.toString(); 43 | } 44 | 45 | public static String bytesToReadableString(long bytes) { 46 | final DecimalFormat decimalFormat = new DecimalFormat("0.00"); 47 | if (bytes < 1000) { 48 | return decimalFormat.format(bytes + 0.0) + " B"; 49 | } else if (bytes < 1000000) { 50 | return decimalFormat.format(bytes / 1000.0) + " kB"; 51 | } else { 52 | return decimalFormat.format(bytes / 1000000.0) + " MB"; 53 | } 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /app/src/main/java/se/arctosoft/vault/utils/Toaster.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Valv-Android 3 | * Copyright (C) 2024 Arctosoft AB 4 | * 5 | * This program is free software: you can redistribute it and/or modify 6 | * it under the terms of the GNU General Public License as published by 7 | * the Free Software Foundation, either version 3 of the License, or 8 | * (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | * GNU General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU General Public License 16 | * along with this program. If not, see https://www.gnu.org/licenses/. 17 | */ 18 | 19 | package se.arctosoft.vault.utils; 20 | 21 | import android.content.Context; 22 | import android.widget.Toast; 23 | 24 | import androidx.annotation.NonNull; 25 | 26 | import java.lang.ref.WeakReference; 27 | 28 | public class Toaster { 29 | private static Toaster toaster; 30 | private static Toast toast; 31 | private final WeakReference