-
Notifications
You must be signed in to change notification settings - Fork 426
Expand file tree
/
Copy pathAppOpener.java
More file actions
254 lines (224 loc) · 10.9 KB
/
AppOpener.java
File metadata and controls
254 lines (224 loc) · 10.9 KB
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
package com.thirtydegreesray.openhub.util;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.PendingIntent;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.customtabs.CustomTabsIntent;
import android.widget.Toast;
import com.thirtydegreesray.openhub.R;
import com.thirtydegreesray.openhub.http.Downloader;
import com.thirtydegreesray.openhub.mvp.model.GitHubName;
import com.thirtydegreesray.openhub.service.CopyBroadcastReceiver;
import com.thirtydegreesray.openhub.service.ShareBroadcastReceiver;
import com.thirtydegreesray.openhub.ui.activity.*;
import es.dmoral.toasty.Toasty;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* Created by ThirtyDegreesRay on 2017/10/30 11:18:57
*/
public class AppOpener {
public static void openInCustomTabsOrBrowser(@NonNull Context context, @NonNull String url){
if(StringUtils.isBlank(url)){
Toasty.warning(context, context.getString(R.string.invalid_url), Toast.LENGTH_LONG).show();
return;
}
//check http prefix
if(!url.contains("//")){
url = "http://".concat(url);
}
String customTabsPackageName ;
if (PrefUtils.isCustomTabsEnable() &&
(customTabsPackageName = CustomTabsHelper.INSTANCE.getBestPackageName(context) ) != null) {
Bitmap backIconBitmap = ViewUtils.getBitmapFromResource(context, R.drawable.ic_arrow_back_title);
Intent shareIntent = new Intent(context.getApplicationContext(), ShareBroadcastReceiver.class);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent sharePendingIntent = PendingIntent.getBroadcast(
context.getApplicationContext(), 0, shareIntent, 0);
Intent copyIntent = new Intent(context.getApplicationContext(), CopyBroadcastReceiver.class);
copyIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent copyPendingIntent = PendingIntent.getBroadcast(
context.getApplicationContext(), 0, copyIntent, 0);
CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder()
.setToolbarColor(ViewUtils.getPrimaryColor(context))
.setCloseButtonIcon(backIconBitmap)
.setShowTitle(true)
.addMenuItem(context.getString(R.string.share), sharePendingIntent)
.addMenuItem(context.getString(R.string.copy_url), copyPendingIntent)
// .setStartAnimations(context, R.anim.slide_in_right, R.anim.slide_out_left)
// .setExitAnimations(context, android.R.anim.slide_in_left, android.R.anim.slide_out_right)
.build();
customTabsIntent.intent.setPackage(customTabsPackageName);
customTabsIntent.launchUrl(context, Uri.parse(url));
if(PrefUtils.isCustomTabsTipsEnable()){
Toasty.info(context, context.getString(R.string.use_custom_tabs_tips), Toast.LENGTH_LONG).show();
PrefUtils.set(PrefUtils.CUSTOM_TABS_TIPS_ENABLE, false);
}
} else {
openInBrowser(context,url);
}
}
public static void openInBrowser(@NonNull Context context, @NonNull String url){
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri).addCategory(Intent.CATEGORY_BROWSABLE);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent = createActivityChooserIntent(context, intent, uri, VIEW_IGNORE_PACKAGE);
if(intent != null){
context.startActivity(intent);
} else {
Toasty.warning(context, context.getString(R.string.no_browser_clients), Toast.LENGTH_LONG).show();
}
}
public static void startDownload(@NonNull Context context, @NonNull String url, String fileName) {
if(PrefUtils.isSystemDownloader()){
Downloader.create(context.getApplicationContext()).start(url, fileName);
}else{
openDownloader(context, url);
}
}
public static void openDownloader(@NonNull Context context, @NonNull String url) {
if(StringUtils.isBlank(url)){
Toasty.warning(context, context.getString(R.string.invalid_url), Toast.LENGTH_LONG).show();
return;
}
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri).addCategory(Intent.CATEGORY_BROWSABLE);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent = createActivityChooserIntent(context, intent, uri, VIEW_IGNORE_PACKAGE);
if(intent != null){
context.startActivity(intent);
} else {
Toasty.warning(context, context.getString(R.string.no_download_clients), Toast.LENGTH_LONG).show();
}
}
public static void shareText(@NonNull Context context, @NonNull String text) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, text);
shareIntent.setType("text/plain");
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try{
context.startActivity(Intent.createChooser(shareIntent, context.getString(R.string.share_to))
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
}catch (ActivityNotFoundException e){
Toasty.warning(context, context.getString(R.string.no_share_clients)).show();
}
}
public static void launchEmail(@NonNull Context context, @NonNull String email){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{email});
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try{
context.startActivity(Intent.createChooser(intent, context.getString(R.string.send_email))
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
}catch (ActivityNotFoundException e){
Toasty.warning(context, context.getString(R.string.no_email_clients)).show();
}
}
public static void openInMarket(@NonNull Context context){
Uri uri = Uri.parse("market://details?id=" + context.getPackageName());
Intent intent = new Intent(Intent.ACTION_VIEW,uri);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try{
context.startActivity(Intent.createChooser(intent, context.getString(R.string.open_in_market))
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
}catch (ActivityNotFoundException e){
Toasty.warning(context, context.getString(R.string.no_market_clients)).show();
}
}
public static void launchUrl(@NonNull Context context, @NonNull Uri uri){
if(StringUtils.isBlank(uri.toString())) return;
String url = uri.toString();
if(GitHubHelper.isImage(url)){
ViewerActivity.showImage(context, url);
return;
}
GitHubName gitHubName = GitHubName.fromUrl(url);
if(gitHubName == null){
openInCustomTabsOrBrowser(context, uri.toString());
return;
}
String userName = gitHubName.getUserName();
String repoName = gitHubName.getRepoName();
if (userName == null) {
context.startActivity(new Intent(context, SplashActivity.class));
} else if (GitHubHelper.isUserUrl(url)) {
ProfileActivity.show((Activity) context, userName);
} else if (GitHubHelper.isRepoUrl(url)) {
RepositoryActivity.show(context, userName, repoName);
} else if (GitHubHelper.isIssueUrl(url)) {
IssueDetailActivity.show((Activity) context, url);
} else if (GitHubHelper.isReleasesUrl(url)) {
ReleasesActivity.show((Activity) context, userName, repoName);
} else if (GitHubHelper.isReleaseTagUrl(url)) {
ReleaseInfoActivity.show((Activity) context, userName, repoName, gitHubName.getReleaseTagName());
} else if (GitHubHelper.isCommitUrl(url)) {
CommitDetailActivity.show((Activity) context, url);
} else {
openInCustomTabsOrBrowser(context, url);
}
}
public static boolean isAppAlive(Context context, String packageName){
ActivityManager activityManager =
(ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> processInfos
= activityManager.getRunningAppProcesses();
for(int i = 0; i < processInfos.size(); i++){
if(processInfos.get(i).processName.equals(packageName)){
return true;
}
}
return false;
}
private static Intent createActivityChooserIntent(Context context, Intent intent,
Uri uri, List<String> ignorPackageList) {
final PackageManager pm = context.getPackageManager();
final List<ResolveInfo> activities = pm.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
final ArrayList<Intent> chooserIntents = new ArrayList<>();
final String ourPackageName = context.getPackageName();
Collections.sort(activities, new ResolveInfo.DisplayNameComparator(pm));
for (ResolveInfo resInfo : activities) {
ActivityInfo info = resInfo.activityInfo;
if (!info.enabled || !info.exported) {
continue;
}
if (info.packageName.equals(ourPackageName)) {
continue;
}
if (ignorPackageList != null && ignorPackageList.contains(info.packageName)) {
continue;
}
Intent targetIntent = new Intent(intent);
targetIntent.setPackage(info.packageName);
targetIntent.setDataAndType(uri, intent.getType());
chooserIntents.add(targetIntent);
}
if (chooserIntents.isEmpty()) {
return null;
}
final Intent lastIntent = chooserIntents.remove(chooserIntents.size() - 1);
if (chooserIntents.isEmpty()) {
// there was only one, no need to showImage the chooser
return lastIntent;
}
Intent chooserIntent = Intent.createChooser(lastIntent, null);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
chooserIntents.toArray(new Intent[chooserIntents.size()]));
return chooserIntent;
}
private static final List<String> VIEW_IGNORE_PACKAGE = Arrays.asList(
"com.gh4a", "com.fastaccess", "com.taobao.taobao"
);
}