Skip to content

Commit bc9927b

Browse files
authored
POC: load icon app via ksu://icon/[packageName] (#674)
* manager: load app icons from package name using AppIconUti Trying basic icon rendering from package via WebView ksu:// scheme. Includes cache and bitmap scaling. Still subject to refinement. * Update WebUIActivity.kt This proof-of-concept intercepts custom URLs of the form: ksu://icon/com.example.app It fetches the app icon using PackageManager via AppIconUtil, converts it to PNG, and returns it as a WebResourceResponse. Used inside shouldInterceptRequest() for early experimentation with dynamic WebView asset routing. Fallbacks to WebViewAssetLoader for all other requests. Notes: - Icon size currently fixed at 512px - No error icon or fallback image yet - No caching headers or mime sniffing implemented * POC: Handle ksu://icon/[packageName] to serve app icon via WebView This proof-of-concept intercepts custom URLs of the form: ksu://icon/com.example.app It fetches the app icon using PackageManager via AppIconUtil, converts it to PNG, and returns it as a WebResourceResponse. Used inside shouldInterceptRequest() for early experimentation with dynamic WebView asset routing. Fallbacks to WebViewAssetLoader for all other requests. Notes: - Icon size currently fixed at 512px - No error icon or fallback image yet - No caching headers or mime sniffing implemented
1 parent d4f4c0a commit bc9927b

2 files changed

Lines changed: 64 additions & 2 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.rifsxd.ksunext.ui.webui;
2+
3+
import android.content.Context;
4+
import android.content.pm.ApplicationInfo;
5+
import android.content.pm.PackageManager;
6+
import android.graphics.Bitmap;
7+
import android.graphics.Canvas;
8+
import android.graphics.drawable.BitmapDrawable;
9+
import android.graphics.drawable.Drawable;
10+
11+
import java.util.HashMap;
12+
import java.util.Map;
13+
14+
public class AppIconUtil {
15+
private static final Map<String, Bitmap> iconCache = new HashMap<>();
16+
17+
public static Bitmap loadAppIconSync(Context context, String packageName, int sizePx) {
18+
Bitmap cached = iconCache.get(packageName);
19+
if (cached != null) return cached;
20+
21+
try {
22+
PackageManager pm = context.getPackageManager();
23+
ApplicationInfo appInfo = pm.getApplicationInfo(packageName, 0);
24+
Drawable drawable = pm.getApplicationIcon(appInfo);
25+
Bitmap raw = drawableToBitmap(drawable, sizePx);
26+
Bitmap icon = Bitmap.createScaledBitmap(raw, sizePx, sizePx, true);
27+
iconCache.put(packageName, icon);
28+
return icon;
29+
} catch (Exception e) {
30+
return null;
31+
}
32+
}
33+
34+
private static Bitmap drawableToBitmap(Drawable drawable, int size) {
35+
if (drawable instanceof BitmapDrawable) return ((BitmapDrawable) drawable).getBitmap();
36+
37+
int width = drawable.getIntrinsicWidth() > 0 ? drawable.getIntrinsicWidth() : size;
38+
int height = drawable.getIntrinsicHeight() > 0 ? drawable.getIntrinsicHeight() : size;
39+
40+
Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
41+
Canvas canvas = new Canvas(bmp);
42+
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
43+
drawable.draw(canvas);
44+
return bmp;
45+
}
46+
}

manager/app/src/main/java/com/rifsxd/ksunext/ui/webui/WebUIActivity.kt

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,23 @@ class WebUIActivity : ComponentActivity() {
9696
view: WebView,
9797
request: WebResourceRequest
9898
): WebResourceResponse? {
99-
return webViewAssetLoader.shouldInterceptRequest(request.url)
99+
val url = request.url
100+
101+
//POC: Handle ksu://icon/[packageName] to serve app icon via WebView
102+
if (url.scheme.equals("ksu", ignoreCase = true) && url.host.equals("icon", ignoreCase = true)) {
103+
val packageName = url.path?.substring(1)
104+
if (!packageName.isNullOrEmpty()) {
105+
val icon = AppIconUtil.loadAppIconSync(this@WebUIActivity, packageName, 512)
106+
if (icon != null) {
107+
val stream = java.io.ByteArrayOutputStream()
108+
icon.compress(android.graphics.Bitmap.CompressFormat.PNG, 100, stream)
109+
val inputStream = java.io.ByteArrayInputStream(stream.toByteArray())
110+
return WebResourceResponse("image/png", null, inputStream)
111+
}
112+
}
113+
}
114+
115+
return webViewAssetLoader.shouldInterceptRequest(url)
100116
}
101117
override fun onPageFinished(view: WebView?, url: String?) {
102118
super.onPageFinished(view, url)
@@ -119,4 +135,4 @@ class WebUIActivity : ComponentActivity() {
119135
super.onDestroy()
120136
runCatching { rootShell?.close() }
121137
}
122-
}
138+
}

0 commit comments

Comments
 (0)