/
ActivityUtils.java
569 lines (486 loc) · 18.7 KB
/
ActivityUtils.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
package me.shouheng.utils.app;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.os.Parcelable;
import androidx.annotation.AnimRes;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityOptionsCompat;
import androidx.fragment.app.Fragment;
import androidx.core.util.Pair;
import android.view.View;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import me.shouheng.utils.R;
import me.shouheng.utils.UtilsApp;
import me.shouheng.utils.constant.ActivityDirection;
/**
* ActivityUtils is a helper class used to start your activity defined in your manifest or
* start other activities by intent. You can easily prepare the values by the builder and connect
* all the methods like a chain, which is rather elegant than using the raw intent.
*
* @author shouh
* @version $Id: ActivityUtils, v 0.1 2018/11/17 17:29 shouh Exp$
*/
public final class ActivityUtils {
private static List<Activity> activities = new LinkedList<>();
/**
* Judge is given activity exists.
*
* @param pkg the package name
* @param cls the class name
* @return true if exists
*/
public static boolean isActivityExists(@NonNull final String pkg, @NonNull final String cls) {
Intent intent = new Intent();
intent.setClassName(pkg, cls);
PackageManager pm = UtilsApp.getApp().getPackageManager();
return pm.resolveActivity(intent, 0) != null
&& intent.resolveActivity(pm) != null
&& pm.queryIntentActivities(intent, 0).size() != 0;
}
/*---------------------------------- Starter --------------------------------------*/
/**
* Start given activity.
*
* @param context the context to start activity
* @param activity the destination activity
*/
public static void start(@NonNull Context context,
@NonNull Class<? extends Activity> activity) {
context.startActivity(new Intent(context, activity));
}
public static void start(@NonNull Context context,
@NonNull Class<? extends Activity> activity,
@ActivityDirection int direction) {
context.startActivity(new Intent(context, activity));
if (context instanceof Activity) {
overridePendingTransition((Activity) context, direction);
}
}
/**
* Start activity.
*
* @param activity starter activity
* @param activityClass target activity
* @param requestCode the request code
*/
public static void start(@NonNull Activity activity,
@NonNull Class<? extends Activity> activityClass,
int requestCode) {
activity.startActivityForResult(new Intent(activity, activityClass), requestCode);
}
public static void start(@NonNull Activity activity,
@NonNull Class<? extends Activity> activityClass,
int requestCode,
@ActivityDirection int direction) {
start(activity, activityClass, requestCode, direction, false);
}
public static void start(@NonNull Activity activity,
@NonNull Class<? extends Activity> activityClass,
int requestCode,
@ActivityDirection int direction,
boolean finishLast) {
activity.startActivityForResult(new Intent(activity, activityClass), requestCode);
if (finishLast) activity.finish();
overridePendingTransition(activity, direction);
}
/**
* Start activity.
*
* @param fragment starter fragment
* @param activityClass the target activity
* @param requestCode the request code
*/
public static void start(@NonNull Fragment fragment,
@NonNull Class<? extends Activity> activityClass,
int requestCode) {
fragment.startActivityForResult(new Intent(fragment.getContext(), activityClass), requestCode);
}
public static void start(@NonNull Fragment fragment,
@NonNull Class<? extends Activity> activityClass,
int requestCode,
@ActivityDirection int direction) {
fragment.startActivityForResult(new Intent(fragment.getContext(), activityClass), requestCode);
overridePendingTransition(fragment.getActivity(), direction);
}
/**
* Start home activity.
*
* @param context the context
*/
public static void startHomeActivity(Context context) {
Intent i = new Intent(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
context.startActivity(i);
}
public static void overridePendingTransition(Activity activity, @ActivityDirection int direction) {
if (direction == ActivityDirection.ANIMATE_FORWARD) {
activity.overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
} else if (direction == ActivityDirection.ANIMATE_BACK) {
activity.overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
} else if (direction == ActivityDirection.ANIMATE_EASE_IN_OUT) {
activity.overridePendingTransition(R.anim.ease_in, R.anim.ease_out);
} else if (direction == ActivityDirection.ANIMATE_SLIDE_TOP_FROM_BOTTOM) {
activity.overridePendingTransition(R.anim.slide_bottom_to_top, R.anim.slide_none_medium_time);
} else if (direction == ActivityDirection.ANIMATE_SLIDE_BOTTOM_FROM_TOP) {
activity.overridePendingTransition(R.anim.slide_none_medium_time, R.anim.slide_top_to_bottom);
} else if (direction == ActivityDirection.ANIMATE_SCALE_IN) {
activity.overridePendingTransition(R.anim.popup_scale_in, R.anim.slide_none);
} else if (direction == ActivityDirection.ANIMATE_SCALE_OUT) {
activity.overridePendingTransition(R.anim.slide_none, R.anim.popup_scale_out);
} else if (direction == ActivityDirection.ANIMATE_NO_ANIMATION) {
activity.overridePendingTransition(0, 0);
} else if (direction == ActivityDirection.ANIMATE_NONE) {
// do nothing
} else {
activity.overridePendingTransition(R.anim.magnify_fade_in, R.anim.fade_out);
}
}
/*---------------------------------- Finisher --------------------------------------*/
/**
* Finish given activity with default animation.
*
* @param activity the activity to finish
*/
public static void finishActivity(@NonNull Activity activity) {
activity.finish();
}
public static void finishActivity(@NonNull Activity activity, @ActivityDirection int direction) {
activity.finish();
overridePendingTransition(activity, direction);
}
/**
* Finish activity with given animation
*
* @param activity the activity to finish
* @param enterAnim the enter animation
* @param exitAnim the exit animation
*/
public static void finishActivity(@NonNull Activity activity,
@AnimRes final int enterAnim,
@AnimRes final int exitAnim) {
activity.finish();
activity.overridePendingTransition(enterAnim, exitAnim);
}
/*---------------------------------- Launcher --------------------------------------*/
/**
Get an {@link Builder} instance used to build the intent to start an activity.
*
* @param activity the destination activity
* @param <T> the activity type
* @return the builder
*/
public static <T extends Activity> Builder<T> open(Class<T> activity) {
return new Builder<>(activity);
}
/**
* Get an builder to build an intent to start an third part activity
*
* @return the builder used to build intent
*/
public static <T extends Activity> Builder<T> open() {
return new Builder<>();
}
/**
* A builder used to build intent to start activity
*
* @param <T> the activity type
*/
public static class Builder<T extends Activity> {
@AnimRes
private int enterAnim;
@AnimRes
private int exitAnim;
@ActivityDirection
private int direction;
private View[] sharedElements;
private Bundle options;
private Class<T> clz;
private final Intent intent = new Intent();
/**
* Builder with no params used to open the third part activity by intent.
*/
Builder() {}
/**
* Builder with given type activity used to open the activity defined in your manifest
*
* @param clz the activity to launch
*/
Builder(Class<T> clz) {
this.clz = clz;
}
public Builder<T> setClass(Context packageContext, Class<T> clz) {
intent.setClass(packageContext, clz);
return this;
}
public Builder<T> setAction(String action) {
intent.setAction(action);
return this;
}
public Builder<T> setData(Uri data) {
intent.setData(data);
return this;
}
public Builder<T> setFlags(int flags) {
intent.setFlags(flags);
return this;
}
public Builder<T> setType(String type) {
intent.setType(type);
return this;
}
public Builder<T> addCategory(String category) {
intent.addCategory(category);
return this;
}
public Builder<T> put(String key, byte b) {
intent.putExtra(key, b);
return this;
}
public Builder<T> put(String key, char c) {
intent.putExtra(key, c);
return this;
}
public Builder<T> put(String key, short s) {
intent.putExtra(key, s);
return this;
}
public Builder<T> put(String key, int i) {
intent.putExtra(key, i);
return this;
}
public Builder<T> put(String key, long l) {
intent.putExtra(key, l);
return this;
}
public Builder<T> put(String key, float f) {
intent.putExtra(key, f);
return this;
}
public Builder<T> put(String key, double d) {
intent.putExtra(key, d);
return this;
}
public Builder<T> put(String key, String s) {
intent.putExtra(key, s);
return this;
}
public Builder<T> put(String key, CharSequence cs) {
intent.putExtra(key, cs);
return this;
}
public Builder<T> put(String key, Serializable s) {
intent.putExtra(key, s);
return this;
}
public Builder<T> put(String key, Parcelable p) {
intent.putExtra(key, p);
return this;
}
public Builder<T> put(String key, boolean[] bs) {
intent.putExtra(key, bs);
return this;
}
public Builder<T> put(String key, byte[] bs) {
intent.putExtra(key, bs);
return this;
}
public Builder<T> put(String key, short[] ss) {
intent.putExtra(key, ss);
return this;
}
public Builder<T> put(String key, char[] cs) {
intent.putExtra(key, cs);
return this;
}
public Builder<T> put(String key, int[] is) {
intent.putExtra(key, is);
return this;
}
public Builder<T> put(String key, long[] ls) {
intent.putExtra(key, ls);
return this;
}
public Builder<T> put(String key, float[] fs) {
intent.putExtra(key, fs);
return this;
}
public Builder<T> put(String key, double[] ds) {
intent.putExtra(key, ds);
return this;
}
public Builder<T> put(String key, String[] ss) {
intent.putExtra(key, ss);
return this;
}
public Builder<T> put(String key, CharSequence[] css) {
intent.putExtra(key, css);
return this;
}
public Builder<T> put(String key, Bundle bundle) {
intent.putExtra(key, bundle);
return this;
}
public Builder<T> put(String key, Parcelable[] ps) {
intent.putExtra(key, ps);
return this;
}
public Builder<T> putParcelableList(String key, ArrayList<? extends Parcelable> pl) {
intent.putParcelableArrayListExtra(key, pl);
return this;
}
public Builder<T> putIntegerList(String key, ArrayList<Integer> il) {
intent.putIntegerArrayListExtra(key, il);
return this;
}
public Builder<T> putStringList(String key, ArrayList<String> sl) {
intent.putStringArrayListExtra(key, sl);
return this;
}
public Builder<T> putCharSequenceList(String key, ArrayList<CharSequence> csl) {
intent.putCharSequenceArrayListExtra(key, csl);
return this;
}
public Builder<T> withAnim(@AnimRes final int enterAnim, @AnimRes final int exitAnim) {
this.enterAnim = enterAnim;
this.exitAnim = exitAnim;
return this;
}
public Builder<T> withDirection(@ActivityDirection final int direction) {
this.direction = direction;
return this;
}
public Builder<T> wishSharedElements(View[] sharedElements) {
this.sharedElements = sharedElements;
return this;
}
public Builder<T> withOptions(Bundle options) {
this.options = options;
return this;
}
public Intent getIntent(Context context) {
if (clz != null) {
intent.setClass(context, clz);
}
return intent;
}
public void launch(@NonNull Context context) {
Intent i = getIntent(context);
if (context instanceof Activity) {
if (options == null && sharedElements != null) {
options = getOptionsBundle((Activity) context, sharedElements);
}
if (options != null && VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
context.startActivity(i, options);
} else {
context.startActivity(i);
}
if (enterAnim != 0 && exitAnim != 0) {
((Activity) context).overridePendingTransition(enterAnim, exitAnim);
}
if (direction != 0) {
overridePendingTransition((Activity) context, direction);
}
} else {
context.startActivity(i);
}
}
public void launch(@NonNull Activity activity, int requestCode) {
Intent i = getIntent(activity);
if (options == null && sharedElements != null) {
options = getOptionsBundle(activity, sharedElements);
}
if (options != null && VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
activity.startActivityForResult(i, requestCode, options);
} else {
activity.startActivityForResult(i, requestCode);
}
if (enterAnim != 0 && exitAnim != 0) {
activity.overridePendingTransition(enterAnim, exitAnim);
}
if (direction != 0) {
overridePendingTransition(activity, direction);
}
}
public void launch(@NonNull Fragment f, int requestCode) {
Activity a = f.getActivity();
Intent i = getIntent(a);
if (options == null && sharedElements != null) {
options = getOptionsBundle(a, sharedElements);
}
if (options != null && VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) {
f.startActivityForResult(i, requestCode, options);
} else {
f.startActivityForResult(i, requestCode);
}
if (enterAnim != 0 && exitAnim != 0 && a != null) {
a.overridePendingTransition(enterAnim, exitAnim);
}
if (direction != 0) {
overridePendingTransition(a, direction);
}
}
private Bundle getOptionsBundle(final Activity activity, final View[] sharedElements) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return null;
if (sharedElements == null) return null;
int len = sharedElements.length;
if (len <= 0) return null;
@SuppressWarnings("unchecked")
Pair<View, String>[] pairs = new Pair[len];
for (int i = 0; i < len; i++) {
pairs[i] = Pair.create(sharedElements[i], sharedElements[i].getTransitionName());
}
return ActivityOptionsCompat.makeSceneTransitionAnimation(activity, pairs).toBundle();
}
}
/*---------------------------------- Manager --------------------------------------*/
/**
* 将 Activity 添加到列表中,应该在 {@link Activity#onDestroy()} 方法中
* 调用 {@link #removeFromList(Activity)} 从列表中移除,防止内存泄漏
*
* @param activity 要添加的 Activity
*/
public static void addToList(Activity activity) {
activities.add(activity);
}
/**
* 将 Activity 从列表中移除
*
* @param activity 要移除的 Activity
*/
public static void removeFromList(Activity activity) {
int index = activities.indexOf(activity);
if (index != -1) {
activities.remove(activity);
}
}
/**
* 当前的 Activity,该 Activity 应当曾经调用过 {@link #addToList(Activity)}
*
* @return 当前 Activity
*/
public static Activity currentActivity() {
return activities.get(0);
}
/**
* 销毁列表中所有的 Activity
*/
public static void finishAll() {
for (Activity activity : activities) {
activity.finish();
}
}
/*---------------------------------- inner methods --------------------------------------*/
private ActivityUtils() {
throw new UnsupportedOperationException("u can't initialize me!");
}
}