improved app icon fetch

This commit is contained in:
Thibault Deckers 2021-08-16 10:42:50 +09:00
parent 8d1352d3df
commit cb102cbc6b
7 changed files with 43 additions and 39 deletions

View file

@ -3,6 +3,7 @@ package deckers.thibault.aves.channel.calls
import android.content.* import android.content.*
import android.content.pm.ApplicationInfo import android.content.pm.ApplicationInfo
import android.content.res.Configuration import android.content.res.Configuration
import android.content.res.Resources
import android.net.Uri import android.net.Uri
import android.os.Handler import android.os.Handler
import android.os.Looper import android.os.Looper
@ -103,27 +104,29 @@ class AppAdapterHandler(private val context: Context) : MethodCallHandler {
var data: ByteArray? = null var data: ByteArray? = null
try { try {
val iconResourceId = context.packageManager.getApplicationInfo(packageName, 0).icon val iconResourceId = context.packageManager.getApplicationInfo(packageName, 0).icon
val uri = Uri.Builder() if (iconResourceId != Resources.ID_NULL) {
.scheme(ContentResolver.SCHEME_ANDROID_RESOURCE) val uri = Uri.Builder()
.authority(packageName) .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE)
.path(iconResourceId.toString()) .authority(packageName)
.build() .path(iconResourceId.toString())
.build()
val options = RequestOptions() val options = RequestOptions()
.format(DecodeFormat.PREFER_RGB_565) .format(DecodeFormat.PREFER_RGB_565)
.override(size, size) .override(size, size)
val target = Glide.with(context) val target = Glide.with(context)
.asBitmap() .asBitmap()
.apply(options) .apply(options)
.load(uri) .load(uri)
.submit(size, size) .submit(size, size)
try { try {
data = target.get()?.getBytes(canHaveAlpha = true, recycle = false) data = target.get()?.getBytes(canHaveAlpha = true, recycle = false)
} catch (e: Exception) { } catch (e: Exception) {
Log.w(LOG_TAG, "failed to decode app icon for packageName=$packageName", e) Log.w(LOG_TAG, "failed to decode app icon for packageName=$packageName", e)
}
Glide.with(context).clear(target)
} }
Glide.with(context).clear(target)
} catch (e: Exception) { } catch (e: Exception) {
Log.w(LOG_TAG, "failed to get app info for packageName=$packageName", e) Log.w(LOG_TAG, "failed to get app info for packageName=$packageName", e)
return return

View file

@ -4,6 +4,7 @@ import 'package:aves/services/android_app_service.dart';
import 'package:equatable/equatable.dart'; import 'package:equatable/equatable.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart'; import 'package:flutter/widgets.dart';
import 'package:transparent_image/transparent_image.dart';
class AppIconImage extends ImageProvider<AppIconImageKey> { class AppIconImage extends ImageProvider<AppIconImageKey> {
const AppIconImage({ const AppIconImage({
@ -39,10 +40,7 @@ class AppIconImage extends ImageProvider<AppIconImageKey> {
Future<ui.Codec> _loadAsync(AppIconImageKey key, DecoderCallback decode) async { Future<ui.Codec> _loadAsync(AppIconImageKey key, DecoderCallback decode) async {
try { try {
final bytes = await AndroidAppService.getAppIcon(key.packageName, key.size); final bytes = await AndroidAppService.getAppIcon(key.packageName, key.size);
if (bytes.isEmpty) { return await decode(bytes.isEmpty ? kTransparentImage : bytes);
throw StateError('$packageName app icon loading failed');
}
return await decode(bytes);
} catch (error) { } catch (error) {
debugPrint('$runtimeType _loadAsync failed with packageName=$packageName, error=$error'); debugPrint('$runtimeType _loadAsync failed with packageName=$packageName, error=$error');
throw StateError('$packageName app icon decoding failed'); throw StateError('$packageName app icon decoding failed');

View file

@ -34,8 +34,8 @@ class AndroidAppService {
'sizeDip': size, 'sizeDip': size,
}); });
if (result != null) return result as Uint8List; if (result != null) return result as Uint8List;
} on PlatformException catch (e, stack) { } on PlatformException catch (_, __) {
await reportService.recordError(e, stack); // ignore, as some packages legitimately do not have icons
} }
return Uint8List(0); return Uint8List(0);
} }

View file

@ -274,6 +274,11 @@ class Constants {
license: 'Apache 2.0', license: 'Apache 2.0',
sourceUrl: 'https://github.com/DavBfr/dart_pdf', sourceUrl: 'https://github.com/DavBfr/dart_pdf',
), ),
Dependency(
name: 'Transparent Image',
license: 'MIT',
sourceUrl: 'https://pub.dev/packages/transparent_image',
),
Dependency( Dependency(
name: 'Tuple', name: 'Tuple',
license: 'BSD 2-Clause', license: 'BSD 2-Clause',

View file

@ -168,23 +168,20 @@ class _OwnerPropState extends State<OwnerProp> {
text: context.l10n.viewerInfoLabelOwner, text: context.l10n.viewerInfoLabelOwner,
style: InfoRowGroup.keyStyle, style: InfoRowGroup.keyStyle,
), ),
// `com.android.shell` is the package reported WidgetSpan(
// for images copied to the device by ADB for Test Driver alignment: PlaceholderAlignment.middle,
if (ownerPackage != 'com.android.shell') child: Padding(
WidgetSpan( padding: const EdgeInsets.symmetric(horizontal: 4),
alignment: PlaceholderAlignment.middle, child: Image(
child: Padding( image: AppIconImage(
padding: const EdgeInsets.symmetric(horizontal: 4), packageName: ownerPackage,
child: Image( size: iconSize,
image: AppIconImage(
packageName: ownerPackage,
size: iconSize,
),
width: iconSize,
height: iconSize,
), ),
width: iconSize,
height: iconSize,
), ),
), ),
),
TextSpan( TextSpan(
text: appName, text: appName,
style: InfoRowGroup.baseStyle, style: InfoRowGroup.baseStyle,

View file

@ -981,7 +981,7 @@ packages:
source: hosted source: hosted
version: "0.3.19" version: "0.3.19"
transparent_image: transparent_image:
dependency: transitive dependency: "direct main"
description: description:
name: transparent_image name: transparent_image
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"

View file

@ -59,6 +59,7 @@ dependencies:
streams_channel: streams_channel:
git: git:
url: git://github.com/deckerst/aves_streams_channel.git url: git://github.com/deckerst/aves_streams_channel.git
transparent_image:
tuple: tuple:
url_launcher: url_launcher:
version: version: