-
Notifications
You must be signed in to change notification settings - Fork 74
/
AppQueryUtil.java
260 lines (230 loc) · 10.2 KB
/
AppQueryUtil.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package com.zhou.android.common;
import android.app.usage.StorageStats;
import android.app.usage.StorageStatsManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.IPackageStatsObserver;
import android.content.pm.PackageManager;
import android.content.pm.PackageStats;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.RemoteException;
import android.os.UserHandle;
import android.os.storage.StorageManager;
import android.provider.Settings;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* 应用大小查询
*/
public class AppQueryUtil {
private static String TAG = "app_util";
public final static int WITHOUT_PERMISSION = 0;
public final static int OTHER_ERROR = 1;
private static int count = 0;
public static void queryAppSize(Context context, Callback callback) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
queryAfterTarget26(context, callback);
} else {
queryBeforeTarget26(context, callback);
}
}
@RequiresApi(api = Build.VERSION_CODES.O)
private static void queryAfterTarget26(final Context context, final Callback callback) {
Log.i(TAG, "use queryAfterTarget26");
new Thread(new Runnable() {
@Override
public void run() {
final List<Item> data = new ArrayList<>();
Item item;
int error = -1;
StorageStatsManager statsManager = (StorageStatsManager) context.getSystemService(Context.STORAGE_STATS_SERVICE);
UserHandle handler = UserHandle.getUserHandleForUid(-2);
PackageManager pm = context.getPackageManager();
List<ApplicationInfo> apps = pm.getInstalledApplications(0);
boolean hasPermission = true;
for (ApplicationInfo info : apps) {
item = new Item();
item.packageName = info.packageName;
item.type = (info.flags & ApplicationInfo.FLAG_SYSTEM) <= 0 ? 0 : 1;
try {
item.icon = pm.getApplicationIcon(info.packageName);
} catch (PackageManager.NameNotFoundException e) {
//
}
if (statsManager != null) {
try {
//permission.PACKAGE_USAGE_STATS
//info.storageUuid
if (!hasPermission && !context.getPackageName().equals(info.packageName)) {
//没有权限,不是当前应用,不申请
data.add(item);
continue;
}
StorageStats stats = statsManager.queryStatsForPackage(StorageManager.UUID_DEFAULT, info.packageName, handler);
item.codeBytes = stats.getAppBytes();
item.dataBytes = stats.getDataBytes();
item.cacheBytes = stats.getCacheBytes();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SecurityException e) {
//这里说明没有权限,没有权限只能查询自身应用大小
if (hasPermission) {
hasPermission = false;
error = WITHOUT_PERMISSION;
e.printStackTrace();
}
}
} else {
error = OTHER_ERROR;
}
data.add(item);
}
if (callback != null) {
final int _e = error;
Handler ui = new Handler(Looper.getMainLooper());
ui.post(new Runnable() {
@Override
public void run() {
if (_e != -1) {
callback.onError(data, _e);
} else {
callback.onSuccess(data);
}
}
});
}
}
}).start();
}
private static void queryBeforeTarget26(final Context context, final Callback callback) {
Log.i(TAG, "use queryBeforeTarget26");
new Thread(new Runnable() {
@Override
public void run() {
final HashMap<String, Item> map = new HashMap<>();
Item item;
int error = -1;
PackageManager pm = context.getPackageManager();
List<ApplicationInfo> apps = pm.getInstalledApplications(0);
Method method = null;
try {
method = PackageManager.class.getMethod("getPackageSizeInfo", String.class, IPackageStatsObserver.class);
} catch (NoSuchMethodException e) {
error = OTHER_ERROR;
}
count = 0;
IPackageStatsObserver packageStatsObserver = new IPackageStatsObserver.Stub() {
@Override
public void onGetStatsCompleted(PackageStats pStats, boolean succeeded) throws RemoteException {
count++;
Item item = map.get(pStats.packageName);
if (item != null) {
item.codeBytes = pStats.codeSize;
item.dataBytes = pStats.dataSize;
item.cacheBytes = pStats.cacheSize;
}
if (count == map.size()) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
List<Item> data = new ArrayList<>(map.values());
callback.onSuccess(data);
}
});
}
}
};
for (ApplicationInfo info : apps) {
item = new Item();
item.packageName = info.packageName;
item.type = (info.flags & ApplicationInfo.FLAG_SYSTEM) <= 0 ? 0 : 1;
try {
item.icon = pm.getApplicationIcon(info.packageName);
} catch (PackageManager.NameNotFoundException e) {
//
}
if (method != null && error == -1) {
try {
method.invoke(pm, info.packageName, packageStatsObserver);
} catch (IllegalAccessException e) {
e.printStackTrace();
error = OTHER_ERROR;
} catch (InvocationTargetException e) {
e.printStackTrace();
error = OTHER_ERROR;
}
}
map.put(info.packageName, item);
}
if (error != -1) {
final int _e = error;
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
List<Item> data = new ArrayList<>(map.values());
callback.onError(data, _e);
}
});
}
}
}).start();
}
/**
* 请求 PACKAGE_USAGE_STATS 权限,使用自带的权限查询,有很大可能是显示无权限
*/
public static void requestPermission(final Context context) {
AlertDialog ad = new AlertDialog.Builder(context)
.setTitle("警告")
.setMessage("缺少权限:permission.PACKAGE_USAGE_STATS\n需要在\"设置>安全\"中给应用提供权限")
.setPositiveButton("设置", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
context.startActivity(intent);
}
})
.setNegativeButton("拒绝", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
ToastUtils.show(context, "已拒绝,请手动开启");
}
})
.create();
ad.show();
}
public static class Item {
public String packageName;
public long codeBytes, dataBytes, cacheBytes;
public Drawable icon;
public int type;
public String getAppSizeString() {
return "code: " + Tools.getUnit(codeBytes)
+ " data: " + Tools.getUnit(dataBytes)
+ " cache: " + Tools.getUnit(cacheBytes);
}
public long getAppSize() {
return codeBytes + dataBytes + cacheBytes;
}
}
public interface Callback {
void onSuccess(List<Item> data);
void onError(List<Item> data, int error);
}
}