/
IntentUtils.java
346 lines (304 loc) · 12 KB
/
IntentUtils.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package me.shouheng.utils.app;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.provider.Settings;
import androidx.annotation.RequiresPermission;
import java.util.ArrayList;
import java.util.List;
import me.shouheng.utils.UtilsApp;
import static android.Manifest.permission.CALL_PHONE;
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
/**
* @author Shouheng Wang (shouheng2020@gmail.com)
* @version 2019/5/7 23:09
*/
public final class IntentUtils {
/**
* 判断指定的意图是否有效(有没有 Activity 可以处理它)
*
* @param intent 意图
* @return TRUE 表示可以处理
*/
public static boolean isIntentAvailable(final Intent intent) {
return UtilsApp.getApp()
.getPackageManager()
.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
.size() > 0;
}
/**
* 获取匹配指定的意图的全部的 APP 的信息
*
* @param intent 意图
* @return 应用信息列表
*/
public static List<AppInfo> getMatchAppInfos(Intent intent) {
PackageManager pm = UtilsApp.getApp().getPackageManager();
List<ResolveInfo> infos = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
List<AppInfo> appInfoList = new ArrayList<>(infos.size());
for (ResolveInfo resolveInfo : infos) {
appInfoList.add(new AppInfo(resolveInfo.activityInfo.packageName,
resolveInfo.activityInfo.name,
resolveInfo.loadLabel(pm).toString(),
resolveInfo.loadIcon(pm)));
}
return appInfoList;
}
public static Intent getLaunchAppIntent(final String pkgName) {
return getLaunchAppIntent(pkgName, false);
}
/**
* 返回一个用来启动某个应用的意图
*
* @param pkgName 应用的包名
* @param isNewTask 是否作为 NEW TASK 启动指定的应用
* @return 意图
*/
public static Intent getLaunchAppIntent(final String pkgName, final boolean isNewTask) {
PackageManager pm = UtilsApp.getApp().getPackageManager();
Intent intent = pm.getLaunchIntentForPackage(pkgName);
return getIntent(intent, isNewTask);
}
public static Intent getLaunchSettingIntent(final String pkgName) {
return getLaunchSettingIntent(pkgName, false);
}
/**
* 返回一个用来打开某个应用的设置界面的意图
*
* @param pkgName 应用的包名
* @return 意图
*/
public static Intent getLaunchSettingIntent(final String pkgName, final boolean isNewTask) {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + pkgName));
return getIntent(intent, isNewTask);
}
public static Intent getShareTextIntent(final String content) {
return getShareTextIntent(content, false);
}
/**
* 返回一个用来分享文本的意图
*
* @param content 文本内容
* @param isNewTask 是否作为 NEW TASK 启动指定的应用
* @return 意图
*/
public static Intent getShareTextIntent(final String content, final boolean isNewTask) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, content);
return getIntent(intent, isNewTask);
}
public static Intent getDialIntent(final String phoneNumber) {
return getDialIntent(phoneNumber, false);
}
/**
* 返回一个用来拨打指定手机号码的意图
*
* @param phoneNumber 手机号码
* @param isNewTask 是否作为 NEW TASK 启动指定的应用
* @return 意图
*/
public static Intent getDialIntent(final String phoneNumber, final boolean isNewTask) {
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber));
return getIntent(intent, isNewTask);
}
@RequiresPermission(CALL_PHONE)
public static Intent getCallIntent(final String phoneNumber) {
return getCallIntent(phoneNumber, false);
}
/**
* 返回一个用来拨打指定手机号码的意图
*
* @param phoneNumber 手机号码
* @param isNewTask 是否作为 NEW TASK 启动指定的应用
* @return 意图
*/
@RequiresPermission(CALL_PHONE)
public static Intent getCallIntent(final String phoneNumber, final boolean isNewTask) {
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber));
return getIntent(intent, isNewTask);
}
public static Intent getSendSmsIntent(final String phoneNumber, final String content) {
return getSendSmsIntent(phoneNumber, content, false);
}
/**
* 返回一个用来发送短信的意图
*
* @param phoneNumber 手机号码
* @param content 内容
* @param isNewTask 是否作为 NEW TASK 启动指定的应用
* @return 意图
*/
public static Intent getSendSmsIntent(
final String phoneNumber, final String content, final boolean isNewTask) {
Uri uri = Uri.parse("smsto:" + phoneNumber);
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms_body", content);
return getIntent(intent, isNewTask);
}
public static Intent getSendEmailIntent(
final String emailAddress, final String subject, final String body) {
return getSendEmailIntent(emailAddress, subject, body, false);
}
/**
* 返回一个用来发送邮件的意图
*
* @param emailAddress 目标邮箱地址
* @param subject 邮件主题
* @param body 邮件正文
* @param isNewTask 是否作为 NEW TASK 启动指定的应用
* @return 意图
*/
public static Intent getSendEmailIntent(final String emailAddress, final String subject,
final String body, final boolean isNewTask) {
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("mailto:" + emailAddress));
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, body);
intent.putExtra(Intent.EXTRA_EMAIL, emailAddress);
return getIntent(intent, isNewTask);
}
public static Intent getOpenWebIntent(final String webUrl) {
return getOpenWebIntent(webUrl, false);
}
/**
* 返回一个用来打开指定的 url 的意图
*
* @param webUrl 网页的 url
* @param isNewTask 是否作为 NEW TASK 启动指定的应用
* @return 意图
*/
public static Intent getOpenWebIntent(final String webUrl, final boolean isNewTask) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(webUrl));
return getIntent(intent, isNewTask);
}
public static Intent getLaunchMarketIntent(final String pkgName) {
return getLaunchMarketIntent(pkgName, false);
}
/**
* 返回一个用来打开应用商店的意图:优先使用 market 打开,如果没有可用的应用,就使用浏览器打开
* Google Play 的地址
*
* @param pkgName 应用的包名
* @param isNewTask 是否作为 NEW TASK 启动指定的应用
* @return 意图
*/
public static Intent getLaunchMarketIntent(final String pkgName, final boolean isNewTask) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + pkgName));
if (!isIntentAvailable(intent)) {
intent = getOpenWebIntent("https://play.google.com/store/apps/details?id=" + pkgName);
}
return getIntent(intent, isNewTask);
}
public static Intent getCaptureIntent(final Uri outUri) {
return getCaptureIntent(outUri, false);
}
/**
* 返回一个用来拍摄图片的意图
*
* @param outUri 输出的 uri 的地址
* @param isNewTask 是否作为 NEW TASK 启动指定的应用
* @return 意图
*/
public static Intent getCaptureIntent(final Uri outUri, final boolean isNewTask) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outUri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
return getIntent(intent, isNewTask);
}
/**
* Get App notification setting intent.
*
* To check are notifications enabled, call
* {@link AppUtils#areNotificationsEnabled()} method.
*
* @param isNewTask is new task
* @return the intent
*/
public static Intent getNotificationSettingIntent(final boolean isNewTask) {
Context context = UtilsApp.getApp();
return getNotificationSettingIntent(context.getPackageName(), isNewTask);
}
/** 获取设置通知的意图 */
public static Intent getNotificationSettingIntent(final String pkgName, final boolean isNewTask) {
Intent intent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
intent = new Intent();
intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, pkgName);
} else {
intent = getLaunchSettingIntent(pkgName);
}
return getIntent(intent, isNewTask);
}
/**
* Get notification channel setting intent.
*
* To check is notification channel enabled, call
* {@link AppUtils#isNotificationChannelEnabled(String)}} method.
*
* @param channelId notification channel id
* @param isNewTask is new task
*/
public static Intent getNotificationChannelSettingIntent(String channelId, final boolean isNewTask) {
Context context = UtilsApp.getApp();
return getNotificationChannelSettingIntent(context.getPackageName(), channelId, isNewTask);
}
/**
* Get notification channel setting intent.
*
* @param pkgName package name
* @param channelId notification channel id
* @param isNewTask is new task
*/
public static Intent getNotificationChannelSettingIntent(
final String pkgName,
final String channelId,
final boolean isNewTask
) {
Intent intent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
intent = new Intent();
intent.setAction(Settings.ACTION_APP_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, pkgName);
intent.putExtra(Settings.EXTRA_CHANNEL_ID, channelId);
} else {
intent = getLaunchSettingIntent(pkgName);
}
return getIntent(intent, isNewTask);
}
public static class AppInfo {
public final String pkgName;
public final String launcherName;
public final String appName;
public final Drawable launcherIcon;
public AppInfo(String pkgName, String launcherName, String appName, Drawable launcherIcon) {
this.pkgName = pkgName;
this.launcherName = launcherName;
this.appName = appName;
this.launcherIcon = launcherIcon;
}
@Override
public String toString() {
return "AppInfo{" +
"pkgName='" + pkgName + '\'' +
", launcherName='" + launcherName + '\'' +
", appName='" + appName + '\'' +
", launcherIcon=" + launcherIcon +
'}';
}
}
/*----------------------------------inner methods--------------------------------------*/
private static Intent getIntent(final Intent intent, final boolean isNewTask) {
if (intent == null) return null;
return isNewTask ? intent.addFlags(FLAG_ACTIVITY_NEW_TASK) : intent;
}
private IntentUtils() {
throw new UnsupportedOperationException("u can't initialize me!");
}
}