BaseActivity.java
20.4 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
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
/*
* Copyright (c) Wondertek Technologies Co., Ltd. 2019-2022. All rights reserved.
*/
package com.wd.common.base;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.TypedArray;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.Observer;
import com.wd.base.log.Logger;
import com.wd.capability.network.constant.NetConstant;
import com.wd.capability.router.data.ActionBean;
import com.wd.capability.router.data.RouterParameter;
import com.wd.foundation.wdkit.constant.EventConstants;
import com.wd.foundation.wdkit.utils.DialogUtils;
import com.wd.common.dialog.FlowTooBigDialog;
import com.wd.common.permissions.IPmsCallBack;
import com.wd.common.permissions.PermissionsUtils;
import com.wd.common.utils.NotificationUtils;
import com.wd.common.utils.ProcessUtils;
import com.wd.common.utils.ToolsUtil;
import com.wd.foundation.wdkit.view.CustomTitleBar;
import com.wd.foundation.wdkit.utils.StatusBarUtil;
import com.wd.foundation.wdkit.view.DefaultView;
import com.wd.fastcoding.base.R;
import com.wd.foundation.bean.livedate.NetStateMessage;
import com.wd.foundation.wdkit.mvvm.base.BaseMvvmActivity;
import com.wd.foundation.wdkit.statusbar.StatusBarCompat;
import com.wd.foundation.wdkit.statusbar.StatusBarStyleEnum;
import com.wd.foundation.wdkit.utils.ActivityController;
import com.wd.foundation.wdkit.utils.SpUtils;
import com.wd.foundation.wdkitcore.livedata.LiveDataBus;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* Activity的基类<BR>
*
* @author zhangbo
* @version [V1.0.0, 2020/10/24]
* @since V1.0.0
*/
public abstract class BaseActivity extends BaseMvvmActivity implements CustomTitleBar.TitleBarClickListener, NotificationUtils.NotificationListener, StatusBarCompat.NavigationBarInterface {
private NotificationUtils notificationUtils;
protected static final int SEL_TYPE_PIC = 0;
protected static final int SEL_TYPE_VIDEO = 1;
protected static final int SEL_TYPE_ALL = 2;
/**
* 记录开启页面时间
*/
private long startTime;
/**
* 浏览时长
*/
public long duration;
/**
* 是否第一次加载
*/
protected boolean isFirstLoad = true;
/**
* tag
*/
private static final String TAG = "BaseActivity";
/**
* 导航栏控件
*/
protected CustomTitleBar titleBar;
/**
* 导航栏标题内容
*/
protected String titleContent;
/**
* 拍照保存路径
*/
private String mPhotoPath;
private Dialog mLoadingDialog;
private boolean mIsShowTitleBar = true;
private boolean mNetStateObserver;
private Object actionBeanObject;
/**
* 缺省页
*/
private DefaultView baseDefaultView;
/**
* 重试按钮点击监听
*/
private DefaultView.RetryClickListener retryClickListener;
private FlowTooBigDialog flowTooBigDialog;
public int statusHeight;
/*
Cannot add the same observer with different lifecycles异常
* 占位、规避 当liveData多次调用observe时, 如果Observer使用lambda, 并且lamba中没有引用非静态变量或方法时, 可能就会出现crash! (为什么是可能, 可以看 后续)
* */
public int forLiveDataBus;
/**
* 是否显示音频播放浮窗
*/
private boolean canShowMusicPlay;
public Activity mBaseActivity;
/**
* 夜间模式是否需要保存原有状态
*/
private boolean isNightKeep = false;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
try {
parseExtrasKeyData();
} catch (Exception e) {
Logger.t(TAG).e(e.getMessage());
}
statusHeight = StatusBarCompat.getStatusBarHeight(this);
super.onCreate(savedInstanceState);
mBaseActivity = this;
if (savedInstanceState != null) {
// 重新启动App -> 杀进程
ProcessUtils.startWelcomeActivity();
android.os.Process.killProcess(android.os.Process.myPid());
return;
}
if (getTag() != null) {
Logger.t(TAG).d(this.getClass().getName() + "//");
}
ActivityController.addActivity(this);
isNightKeep = setNightKeep();
initStatusBar();
setTopMargin(statusHeight);
initDataBus();
canShowMusicPlay = canShowMusicPlay();
notificationUtils = new NotificationUtils(this, this);
}
private void initDataBus() {
// 接口是否流量超标
LiveDataBus.getInstance().with(EventConstants.FLOW_TOO_LARGE, Boolean.class).observe(this, isok -> {
Log.e("!!!!", "isok " + isok);
if (isok && NetConstant.isFlowTooLarge) {
NetConstant.isFlowTooLarge = false;
if (flowTooBigDialog == null) {
flowTooBigDialog = new FlowTooBigDialog(this, new FlowTooBigDialog.OnCloseClickListener() {
@Override
public void back() {
/*if (isTaskRoot()) {
flowTooBigDialog.dismiss();
} else {
finish();
}*/
flowTooBigDialog.dismiss();
}
});
}
flowTooBigDialog.setIsflowtoobig(true);
if (!flowTooBigDialog.isShowing()) {
flowTooBigDialog.show();
}
} else {
if (flowTooBigDialog == null) {
return;
}
flowTooBigDialog.setIsflowtoobig(false);
}
});
}
@Override
protected void onStop() {
super.onStop();
}
/**
* 解析额外key 数据
*/
private void parseExtrasKeyData() {
if (getIntent() == null || getIntent().getExtras() == null || getTag() == null) {
return;
}
actionBeanObject = getIntent().getExtras().getSerializable(RouterParameter.ACTION_KEY);
if (actionBeanObject == null) {
Logger.t(getTag()).w("extras no data");
return;
}
if (!(actionBeanObject instanceof ActionBean)) {
Logger.t(getTag()).w("action paramType error");
return;
}
}
/**
* 得到额外对象
*
* @return Object
*/
public Object getExtrasSerializableObject() {
return actionBeanObject;
}
@Override
protected void onDestroy() {
//浏览时长
isFirstLoad = true;
long endTime = System.currentTimeMillis();
duration = (int) (endTime - startTime)/1000 ;
releaseDialog();
if (mLoadingDialog != null) {
mLoadingDialog.cancel();
mLoadingDialog = null;
}
super.onDestroy();
// remove activity
ActivityController.removeActivity(this, true);
// try {
// UMShareAPI.get(this).release();
// } catch (Exception e) {
// e.printStackTrace();
// }
notificationUtils.unregisterReceiver();
}
/**
* 释放拥堵弹框
*/
private void releaseDialog() {
if (flowTooBigDialog != null) {
flowTooBigDialog.cancel();
flowTooBigDialog = null;
}
}
private void initStatusBar() {
StatusBarStyleEnum statusBarStyle = getStatusBarStyle();
if (statusBarStyle == null || statusBarStyle == StatusBarStyleEnum.NONE) {
return;
}
setStatusBarStyle(statusBarStyle);
}
/**
* 获取状态栏类型
*
* @return 状态栏类型
*/
protected StatusBarStyleEnum getStatusBarStyle() {
return SpUtils.isNightMode()&& !isNightKeep ? StatusBarStyleEnum.NORMAL_161827_LIGHT_ENUM : StatusBarStyleEnum.NORMAL_WHITE_DARK_ENUM;
}
public void setStatusBarStyle(@NonNull StatusBarStyleEnum statusBarStyle) {
StatusBarUtil.setStatusBarStyle(statusBarStyle, this);
}
/**
* 底部导航栏背景
* @return 颜色值
*/
public int getNavigationBarBgColor(){
return ContextCompat.getColor(getBaseContext(),isNightKeep ?
R.color.res_color_common_C8_keep : R.color.res_color_common_C8);
}
/**
* 切换状态栏背景
*
* @param labelIsBlack 状态文字是否黑色,即light还是dark
* @param color 状态栏背景颜色
*/
protected final void changeStatusBarStyle(boolean labelIsBlack, String color) {
// 修改状态栏背景色,非全屏
int state = labelIsBlack ? View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR : View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR;
StatusBarCompat.setStatusBar(this, state, color);
}
/**
* 拍照得到的 图片
*
* @return mPhotoPath
*/
public String getPhotoPath() {
return mPhotoPath;
}
@Override
protected void onResume() {
super.onResume();
if (mIsShowTitleBar) {
initTitleBar();
}
obserNetState();
if (isFirstLoad) {
startTime = System.currentTimeMillis();
isFirstLoad = false;
}
// //短视频 竖屏直播直播 音频
// if (getClass().getSimpleName().contains("LiveDetailActivityVertical") ||
// getClass().getSimpleName().contains("ShortVideoActivity")) {
// sendHiddenAudio();
// return;
// }
// if (canShowMusicPlay && FloatWindow.get() != null && !FloatWindow.get().getColseed()) {
// FloatWindow.get().show();
// } else if (FloatWindow.get() != null) {
//// PlayerManagerUtils.pauseAudio();
// FloatWindow.get().hide();
// FloatWindow.get().setAudioFloatingIsShow(false);
// }
}
/**
* 隐藏音频悬浮窗方法
*/
public void sendHiddenAudio() {
LiveDataBus.getInstance().with(EventConstants.FLOW_AUDIO_FREQUENCY, Integer.class).postValue(1);
}
/**
* 子类控制是否设置titlebar
*
* @param isShowTitleBar
*/
protected void setShowTitleBar(boolean isShowTitleBar) {
mIsShowTitleBar = isShowTitleBar;
}
/**
* 初始化titlebar
*/
public void initTitleBar() {
if (titleBar == null) {
View view = findViewById(R.id.title_bar);
if (view != null) {
titleBar = (CustomTitleBar) view;
titleBar.setTitleBarClickListener(this);
if (!TextUtils.isEmpty(titleContent)) {
titleBar.setTitle(titleContent);
}
}
}
}
/**
* 创建dialog 对象
*
* @param allowCancel
*/
private void createLoadingDialog(boolean allowCancel, String interactCode) {
mLoadingDialog = DialogUtils.createRequestDialog(this, allowCancel, interactCode);
}
/**
* loading
*/
public void startLoading() {
startLoading(true);
}
public void startLoading(boolean allowCancel) {
startLoading(allowCancel, "refreshing_common_loading.json");
}
protected void startLoading(boolean allowCancel, String interactCode) {
if (isDestroyed() || isFinishing()) {
return;
}
if (mLoadingDialog == null) {
createLoadingDialog(allowCancel, interactCode);
}
if (mLoadingDialog != null && !mLoadingDialog.isShowing()) {
mLoadingDialog.show();
}
}
/**
* loading
*/
public void stopLoading() {
if (isDestroyed() || isFinishing()) {
return;
}
if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
mLoadingDialog.dismiss();
}
}
/**
* 导航栏栏点击左侧按钮
*/
@Override
public void onLeftClick() {
finish();
}
/**
* 导航栏栏点击右侧按钮
*/
@Override
public void onRightClick() {
}
/**
* 展示缺省页
*
* @param defaultView
* @param type
* @return
*/
protected View showDefaultView(DefaultView defaultView, int type) {
if (defaultView != null) {
baseDefaultView = defaultView;
} else {
baseDefaultView = new DefaultView(this, null, 0);
}
retryClickListener = new DefaultView.RetryClickListener() {
@Override
public void onRetryClick() {
retryBtnClickListener();
}
};
baseDefaultView.setRetryBtnClickListener(retryClickListener);
baseDefaultView.show(type);
return baseDefaultView;
}
/**
* 关闭缺省页面
*/
protected void hideDefaultView() {
if (baseDefaultView != null) {
baseDefaultView.hide();
}
}
/**
* 回调提供
*/
public void retryBtnClickListener() {
}
/**
* 子类设置网络是否监听方法
*
* @param isNetObserver
*/
protected void setNetStateObserver(boolean isNetObserver) {
mNetStateObserver = isNetObserver;
obserNetState();
}
/**
* 网络监听方法
*/
private void obserNetState() {
if (mNetStateObserver) {
LiveDataBus.getInstance()
.with(EventConstants.NETWORK_STATE_CHANGE, NetStateMessage.class)
.observe(this, new Observer<NetStateMessage>() {
@Override
public void onChanged(@Nullable NetStateMessage mEventMessage) {
Logger.t(TAG).d("Network status" + mEventMessage.type);
netStateData(mEventMessage);
}
});
}
}
/**
* 子类可以实现这个方法获取网络状态
*
* @param msg
*/
protected void netStateData(NetStateMessage msg) {
if (msg == null) {
return;
}
if (msg.type.equals(NetConstant.NET_WIFI_TYPE)) {
Logger.d("net is wifi type");
} else if (msg.type.equals(NetConstant.NET_CELL_TYPE)) {
Logger.d("net is cell type");
} else if (msg.type.equals(NetConstant.NET_ERROR_TYPE)) {
Logger.d("net is error type");
}
}
/**
* 夜间模式保持原样
*/
public boolean setNightKeep(){
return false;
}
/**
* 设置沉浸式头部的间距
*
* @param statusHeight
*/
public void setTopMargin(int statusHeight) {
}
/**
* 进入到改页面是否显示音频播放浮窗
*
* @return
*/
public boolean canShowMusicPlay() {
return true;
}
public void setTitleContent(String titleContent) {
this.titleContent = titleContent;
}
/**
* 拍照
*
* @param requestCode 请求码
*/
protected void openCamera(final int requestCode) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
PermissionsUtils.getCameraAndSdPermission(this, new IPmsCallBack() {
@Override
public void granted() {
goCamera(requestCode);
}
@Override
public void notGranted() {
// ToastUtils.showShort(getResources().getString(R.string.permission_refuse));
}
});
} else {
goCamera(requestCode);
}
}
/**
* 拍照,在onResult 取 mPhotoPath
*
* @param requestCode 请求码
*/
protected void goCamera(int requestCode) {
mPhotoPath = ToolsUtil.openCamera(this, requestCode);
}
/**
* 选择相册
*
* @param maxNum 选择张数
* @param requestCode 请求码
* @param mItemList 存储相册数据
* @param type 0 图片,1 视频,2 图片和视频
*/
// protected void openAlbum(final int maxNum, final int type, final List<Item> mItemList, final int requestCode) {
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// PermissionsUtils.getSDPermission(this, new IPmsCallBack() {
// @Override
// public void granted() {
// selectPics(maxNum, type, mItemList, requestCode);
// }
//
// @Override
// public void notGranted() {
// // ToastUtils.showShort(getResources().getString(R.string.permission_refuse));
// }
// });
// } else {
// selectPics(maxNum, type, mItemList, requestCode);
// }
// }
/**
* 选择相册,在onResult
*
* @param maxNum 选取最大个数
* @param type 文件类型
* @param mItemList 存储相册数据
* @param requestCode 请求码
*/
// protected void selectPics(int maxNum, int type, List<Item> mItemList, int requestCode) {
//
// ToolsUtil.setMaxNum(maxNum);
// if (type == SEL_TYPE_PIC) {
// ToolsUtil.selectPhotos(this, mItemList, requestCode,-1);
// return;
// }
// if (type == SEL_TYPE_VIDEO) {
// ToolsUtil.selectVideos(this, mItemList, requestCode,-1,-1);
// return;
// }
// if (type == SEL_TYPE_ALL) {
// ToolsUtil.selectPicsAndVideo(this, mItemList, requestCode,-1,-1,-1);
// }
// }
/**
* 友盟分享使用,不可以删除
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// try {
// UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
// } catch (Exception e) {
// e.printStackTrace();
// }
}
// @Override
// protected void attachBaseContext(Context newBase) {
// super.attachBaseContext(FoldingScreenUtils.attachBaseContext(newBase));
// }
// @Override
// public void onConfigurationChanged(@NonNull Configuration newConfig) {
// FoldingScreenUtils.onConfigurationChanged(this, newConfig);
// super.onConfigurationChanged(newConfig);
// }
public void sendNotification(String message) {
notificationUtils.sendNotification(message);
}
@Override
public void onNotificationReceived(String message) {
// 处理接收到的通知消息
Log.d("Notification", "Received message: " + message);
}
/**
* 判断当前activity是否销毁
* @return
*/
public boolean activityIsDestroy() {
if(mBaseActivity == null || mBaseActivity.isFinishing() || mBaseActivity.isDestroyed())
{
return true;
}
return false;
}
/**
* hook反射方向检查
**/
public void fixOrientation(Activity activity) {
try {
Class activityClass = Activity.class;
Field mActivityInfoField = activityClass.getDeclaredField("mActivityInfo");
mActivityInfoField.setAccessible(true);
ActivityInfo activityInfo = (ActivityInfo) mActivityInfoField.get(activity);
// 设置屏幕不固定
activityInfo.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* hook反射检查是否透明色或者悬浮
**/
public boolean isTranslucentOrFloating() {
boolean isTranslucentOrFloating = false;
try {
int[] styleableRes = (int[]) Class.forName("com.android.internal.R$styleable").getField("Window").get(null);
final TypedArray typedArray = obtainStyledAttributes(styleableRes);
Method method = ActivityInfo.class.getMethod("isTranslucentOrFloating", TypedArray.class);
method.setAccessible(true);
isTranslucentOrFloating = (boolean) method.invoke(null, typedArray);
method.setAccessible(false);
} catch (Exception e) {
e.printStackTrace();
}
return isTranslucentOrFloating;
}
}