This repository has been archived by the owner on Sep 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
LauncherAssistPlugin.java
125 lines (103 loc) · 4.44 KB
/
LauncherAssistPlugin.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package com.progur.launcherassist;
import android.app.WallpaperManager;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.Registrar;
/**
* LauncherAssistPlugin
*/
public class LauncherAssistPlugin implements MethodCallHandler {
private byte[] wallpaperData = null;
private PluginRegistry.Registrar registrar;
public LauncherAssistPlugin(Registrar registrar) {
this.registrar = registrar;
}
public static void registerWith(Registrar registrar) {
final MethodChannel channel = new MethodChannel(registrar.messenger(), "launcher_assist");
channel.setMethodCallHandler(new LauncherAssistPlugin(registrar));
}
@Override
public void onMethodCall(MethodCall methodCall, Result result) {
if(methodCall.method.equals("getAllApps")) {
getAllApps(result);
} else if(methodCall.method.equals("launchApp")) {
launchApp(methodCall.argument("packageName").toString());
} else if(methodCall.method.equals("getWallpaper")) {
getWallpaper(result);
}
}
private void getWallpaper(MethodChannel.Result result) {
if(wallpaperData != null) {
result.success(wallpaperData);
return;
}
WallpaperManager wallpaperManager = WallpaperManager.getInstance(registrar.context());
Drawable wallpaperDrawable = wallpaperManager.getDrawable();
if(wallpaperDrawable instanceof BitmapDrawable) {
wallpaperData = convertToBytes(((BitmapDrawable)wallpaperDrawable).getBitmap(),
Bitmap.CompressFormat.JPEG, 100);
result.success(wallpaperData);
}
}
private void launchApp(String packageName) {
Intent i = registrar.context().getPackageManager().getLaunchIntentForPackage(packageName);
if(i != null)
registrar.context().startActivity(i);
}
private Bitmap getBitmapFromDrawable(Drawable drawable) {
final Bitmap bmp = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bmp);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bmp;
}
private void getAllApps(MethodChannel.Result result) {
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
PackageManager manager = registrar.context().getPackageManager();
List<ResolveInfo> resList = manager.queryIntentActivities(intent, 0);
List<Map<String, Object>> _output = new ArrayList<>();
for (ResolveInfo resInfo : resList) {
try {
ApplicationInfo app = manager.getApplicationInfo(
resInfo.activityInfo.packageName, PackageManager.GET_META_DATA);
if (manager.getLaunchIntentForPackage(app.packageName) != null) {
byte[] iconData = convertToBytes(getBitmapFromDrawable(app.loadIcon(manager)),
Bitmap.CompressFormat.PNG, 100);
Map<String, Object> current = new HashMap<>();
current.put("label", app.loadLabel(manager).toString());
current.put("icon", iconData);
current.put("package", app.packageName);
_output.add(current);
}
} catch(Exception e) {
e.printStackTrace();
}
}
result.success(_output);
}
public static byte[] convertToBytes(Bitmap image, Bitmap.CompressFormat compressFormat, int quality) {
ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
image.compress(compressFormat, quality, byteArrayOS);
return byteArrayOS.toByteArray();
}
}