Skip to content
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
@@ -1,6 +1,7 @@
package io.github.aureliomo.prismlock;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
Expand Down Expand Up @@ -59,9 +60,8 @@ else if (!(new JSONObject(new String(bytes, StandardCharsets.UTF_8)).opt("layers
}
File destination = new File(app.getFilesDir(), "custom_" + slot + "." + kind);
if (!pending.renameTo(destination)) throw new IllegalStateException("Could not save this icon.");
android.content.SharedPreferences prefs = app.getSharedPreferences(MainActivity.PREFS, 0);
if (!prefs.edit().putString(slot + "_kind", kind)
.putLong("ver", SettingsStore.nextVersion(prefs)).commit()) {
SharedPreferences prefs = app.getSharedPreferences(MainActivity.PREFS, 0);
if (!SettingsStore.commitArtworkChange(prefs, editor -> editor.putString(slot + "_kind", kind))) {
throw new IllegalStateException("Could not save icon settings.");
}
} catch (Exception exception) {
Expand All @@ -74,6 +74,25 @@ else if (!(new JSONObject(new String(bytes, StandardCharsets.UTF_8)).opt("layers
});
}

static void clear(Context context, boolean resetSelection, Callback callback) {
Context app = context.getApplicationContext();
WORKER.execute(() -> {
SharedPreferences prefs = app.getSharedPreferences(MainActivity.PREFS, 0);
boolean saved = SettingsStore.commitArtworkChange(prefs, editor -> {
editor.putString("dark_kind", "").putString("light_kind", "");
if (resetSelection) {
editor.putInt("size", Icons.DEF_SIZE).putString("icon", "").putString("animation", "");
}
});
if (saved) {
for (String name : new String[]{"dark.png", "dark.json", "light.png", "light.json"}) {
new File(app.getFilesDir(), "custom_" + name).delete();
}
}
MAIN.post(() -> callback.complete(saved ? null : app.getString(R.string.settings_clear_error)));
});
}

private static byte[] preparePng(byte[] bytes) throws Exception {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Expand Down
94 changes: 50 additions & 44 deletions app/src/main/java/io/github/aureliomo/prismlock/ArtworkPreview.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,11 @@
import java.io.File;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

final class ArtworkPreview extends View {
private static final ThreadPoolExecutor LOADER = new ThreadPoolExecutor(1, 1,
30, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), task -> {
Thread thread = new Thread(task, "prismlock-preview");
thread.setDaemon(true);
return thread;
});
static { LOADER.allowCoreThreadTimeOut(true); }
private static final int MAX_RETAINED_BYTES = 12 * 1024 * 1024;
private static final ThreadPoolExecutor LOADER = AppWorkers.serial("prismlock-preview");
private static final android.os.Handler MAIN = new android.os.Handler(android.os.Looper.getMainLooper());
private Future<?> loading;
private volatile long generation;
private long started;
Expand Down Expand Up @@ -57,25 +51,17 @@ void loadCatalog() {
textPaint.setTextAlign(Paint.Align.CENTER);
textPaint.setTextSize(activity.dp(13));
textPaint.setTypeface(activity.catalogScreen.bodyTypeface());
boolean lightTheme = !Appearance.isDark(activity);
String tag = activity.preferences().getBoolean("enabled", true)
? activity.preferences().getString("icons".equals(category) ? "icon" : "animation", "") : "";
if (!tag.isEmpty()) {
try { tag = Catalog.resolve(activity, category, tag); }
catch (Exception ignored) { }
}
String slot = importSlot();
String file = tag.isEmpty() ? "" : category + "_" + tag + ".json";
if (!tag.isEmpty() && "icons".equals(category)) {
try {
boolean lightTheme = !Appearance.isDark(activity);
file = category + "_" + Catalog.iconTag(activity, tag, lightTheme ? "light" : "dark") + ".json";
} catch (Exception exception) {
file = category + "_" + tag + ".json";
}
String importKind = slot.isEmpty() ? "" : activity.importKind(slot);
String identity = slot.isEmpty() ? tag : slot + ":" + importKind + ":" + activity.preferences().getLong("ver", 0);
if (identity.equals(requested)) {
if (images != null) sizeToImages(images);
invalidate();
return;
}
final String previewFile = file;
String identity = slot.isEmpty() ? file : slot + ":" + activity.importKind(slot) + ":" + activity.preferences().getLong("ver", 0);
if (identity.equals(requested)) { invalidate(); return; }
requested = identity;
final long request = ++generation;
cancelLoading();
Expand All @@ -87,7 +73,7 @@ void loadCatalog() {
try {
PreviewImages next;
if (!slot.isEmpty()) {
if (!"png".equals(activity.importKind(slot))) {
if (!"png".equals(importKind)) {
if (request == generation && identity.equals(requested)) {
status = "Imported Lottie - preview unavailable";
postInvalidate();
Expand All @@ -100,7 +86,10 @@ void loadCatalog() {
next = new PreviewImages(decoded, new double[]{-bitmap.getWidth() / 2.0,
-bitmap.getHeight() / 2.0, bitmap.getWidth() / 2.0, bitmap.getHeight() / 2.0}, 30);
} else {
JSONObject document = new JSONObject(Catalog.read(activity, previewFile));
String resolved = "icons".equals(category)
? Catalog.iconTag(activity, tag, lightTheme ? "light" : "dark")
: Catalog.resolve(activity, category, tag);
JSONObject document = new JSONObject(Catalog.read(activity, category + "_" + resolved + ".json"));
org.json.JSONArray assets = document.getJSONArray("assets");
org.json.JSONArray bounds = document.getJSONArray("catalogBounds");
Bitmap[] frames = new Bitmap[assets.length()];
Expand All @@ -123,25 +112,14 @@ void loadCatalog() {
}
}
PreviewImages ready = next;
post(() -> {
MAIN.post(() -> {
if (request != generation || !identity.equals(requested) || !isAttachedToWindow()) {
recycleUnpublished(ready.frames);
return;
}
started = SystemClock.uptimeMillis();
images = ready;
double scale = CatalogScale.percent(category, activity.fingerprintSize(),
ready.bounds[2] - ready.bounds[0], ready.bounds[3] - ready.bounds[1]) / 100;
double extent = 288;
for (Bitmap bitmap : ready.frames) {
extent = Math.max(extent, Math.max(bitmap.getWidth() + Math.abs(ready.bounds[0] + ready.bounds[2]),
bitmap.getHeight() + Math.abs(ready.bounds[1] + ready.bounds[3])) * scale);
}
android.view.ViewGroup.LayoutParams layout = getLayoutParams();
if (layout != null) {
layout.height = activity.dp((int) Math.ceil(132 * extent / 288));
setLayoutParams(layout);
}
sizeToImages(ready);
status = "";
invalidate();
});
Expand All @@ -152,14 +130,35 @@ void loadCatalog() {
});
}

private void sizeToImages(PreviewImages ready) {
double scale = CatalogScale.percent(category, activity.fingerprintSize(),
ready.bounds[2] - ready.bounds[0], ready.bounds[3] - ready.bounds[1]) / 100;
double extent = 288;
for (Bitmap bitmap : ready.frames) {
extent = Math.max(extent, Math.max(bitmap.getWidth() + Math.abs(ready.bounds[0] + ready.bounds[2]),
bitmap.getHeight() + Math.abs(ready.bounds[1] + ready.bounds[3])) * scale);
}
android.view.ViewGroup.LayoutParams layout = getLayoutParams();
if (layout != null) {
int height = activity.dp((int) Math.ceil(132 * extent / 288));
if (layout.height != height) {
layout.height = height;
setLayoutParams(layout);
}
}
}

private static void recycleUnpublished(Bitmap[] frames) {
if (frames == null) return;
for (Bitmap frame : frames) if (frame != null && !frame.isRecycled()) frame.recycle();
}

private void cancelLoading() {
if (loading != null) loading.cancel(true);
LOADER.purge();
if (loading != null) {
loading.cancel(true);
loading = null;
LOADER.purge();
}
}

@Override protected void onWindowVisibilityChanged(int visibility) {
Expand All @@ -169,15 +168,18 @@ private void cancelLoading() {

@Override protected void onAttachedToWindow() {
super.onAttachedToWindow();
started = SystemClock.uptimeMillis();
loadCatalog();
}

@Override protected void onDetachedFromWindow() {
++generation;
cancelLoading();
requested = "";
images = null;
status = "";
if (images == null || images.bytes > MAX_RETAINED_BYTES) {
requested = "";
images = null;
status = "";
}
super.onDetachedFromWindow();
}

Expand Down Expand Up @@ -229,9 +231,13 @@ private static final class PreviewImages {
final double[] bounds;
final double fps;
final int[] opacity;
final long bytes;

PreviewImages(Bitmap[] frames, double[] bounds, double fps) {
this.frames = frames;
long size = 0;
for (Bitmap frame : frames) size += frame.getAllocationByteCount();
bytes = size;
this.bounds = bounds;
this.fps = fps;
this.opacity = new int[frames.length];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final class Catalog {
}
});

private static CatalogIndex index(Context context) throws Exception {
static CatalogIndex index(Context context) throws Exception {
CatalogIndex current = cachedIndex;
if (current != null) return current;
synchronized (Catalog.class) {
Expand Down
Loading
Loading