回传图标包应用缩略图
This commit is contained in:
1 parent
b90660f5ec
commit
66381bc210
2 files changed
+38
-1
No files matched your search
+36
@@ -1,6 +1,11 @@
|
||||
package com.ttstd.dialer.activity.settings.iconpack;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Rect;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.util.Base64;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.ttstd.dialer.network.OkHttpManager;
|
||||
@@ -8,6 +13,7 @@ import com.ttstd.dialer.network.api.SnApi;
|
||||
import com.ttstd.dialer.utils.Logger;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -20,6 +26,9 @@ import okhttp3.RequestBody;
|
||||
public final class IconPackRemoteHandler {
|
||||
private static final String TAG = "IconPackRemoteHandler";
|
||||
private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
|
||||
private static final int ICON_SIZE = 48;
|
||||
private static final int MAX_ENCODED_ICON = 16 * 1024;
|
||||
private static final int MAX_ICON_PAYLOAD = 512 * 1024;
|
||||
|
||||
private IconPackRemoteHandler() {}
|
||||
|
||||
@@ -36,12 +45,20 @@ public final class IconPackRemoteHandler {
|
||||
|
||||
private static Map<String, Object> toRemoteState(IconPackRepository.State state) {
|
||||
List<Map<String, Object>> entries = new ArrayList<>();
|
||||
int remainingIconBytes = MAX_ICON_PAYLOAD;
|
||||
for (IconPackRepository.Entry entry : state.entries) {
|
||||
if (entries.size() == 200) break;
|
||||
Map<String, Object> item = new HashMap<>();
|
||||
item.put("packageName", entry.packageName);
|
||||
item.put("label", entry.label.substring(0, Math.min(256, entry.label.length())));
|
||||
item.put("available", entry.available);
|
||||
if (!entry.packageName.isEmpty() && entry.available && remainingIconBytes > 0) {
|
||||
String icon = encodeIcon(entry.icon);
|
||||
if (icon != null && icon.length() <= remainingIconBytes) {
|
||||
item.put("iconPngBase64", icon);
|
||||
remainingIconBytes -= icon.length();
|
||||
}
|
||||
}
|
||||
entries.add(item);
|
||||
}
|
||||
Map<String, Object> result = new HashMap<>();
|
||||
@@ -50,6 +67,25 @@ public final class IconPackRemoteHandler {
|
||||
return result;
|
||||
}
|
||||
|
||||
private static String encodeIcon(Drawable drawable) {
|
||||
if (drawable == null) return null;
|
||||
Bitmap bitmap = Bitmap.createBitmap(ICON_SIZE, ICON_SIZE, Bitmap.Config.ARGB_8888);
|
||||
Rect bounds = drawable.copyBounds();
|
||||
try {
|
||||
drawable.setBounds(0, 0, ICON_SIZE, ICON_SIZE);
|
||||
drawable.draw(new Canvas(bitmap));
|
||||
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
if (!bitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes)) return null;
|
||||
String encoded = Base64.encodeToString(bytes.toByteArray(), Base64.NO_WRAP);
|
||||
return encoded.length() <= MAX_ENCODED_ICON ? encoded : null;
|
||||
} catch (RuntimeException e) {
|
||||
return null;
|
||||
} finally {
|
||||
drawable.setBounds(bounds);
|
||||
bitmap.recycle();
|
||||
}
|
||||
}
|
||||
|
||||
private static void report(String requestId, Map<String, Object> state) {
|
||||
Map<String, Object> payload = new HashMap<>();
|
||||
payload.put("requestId", requestId);
|
||||
|
||||
+2
-1
@@ -60,7 +60,8 @@ final class IconPackRepository {
|
||||
if (!info.activityInfo.enabled || !info.activityInfo.applicationInfo.enabled) continue;
|
||||
String name = info.activityInfo.packageName;
|
||||
if (found.containsKey(name) || !hasAppFilter(pm, name)) continue;
|
||||
found.put(name, new Entry(name, info.loadLabel(pm).toString(), info.loadIcon(pm), true));
|
||||
found.put(name, new Entry(name, info.loadLabel(pm).toString(),
|
||||
info.activityInfo.applicationInfo.loadIcon(pm), true));
|
||||
}
|
||||
}
|
||||
List<Entry> entries = new ArrayList<>(found.values());
|
||||
|
||||
Reference in new issue
Block a user