lmw
2023-06-13 4b7d8d9a038f6522df46d0f14fa07eb940a1b34d
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
package cn.sinata.xldutils.activitys;
 
import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
 
 
import com.trello.rxlifecycle3.components.support.RxFragmentActivity;
 
import cn.sinata.xldutils.R;
import cn.sinata.xldutils.netstatus.NetChangeObserver;
import cn.sinata.xldutils.netstatus.NetStateReceiver;
import cn.sinata.xldutils.netstatus.NetUtils;
import cn.sinata.xldutils.utils.DensityUtil;
import cn.sinata.xldutils.utils.Toast;
import cn.sinata.xldutils.view.utils.ViewHolder;
import cn.sinata.xldutils.widget.ProgressDialog;
import io.reactivex.disposables.CompositeDisposable;
 
public abstract class BaseActivity extends RxFragmentActivity {
 
    private ProgressDialog dialog;
    public boolean isDestroy = false;
    protected ViewHolder mHolder;
    protected Context mContext;//上下文
    protected int mScreenWidth;//设备宽
    protected int mScreenHeight;//设备高
    public static final int REQUEST_NORMAL = 43214;
    //关闭应用广播action
    private String ACTION_CLOSE_ALL ;
    public CompositeDisposable compositeSubscription;
 
    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent!=null && TextUtils.equals(intent.getAction(),ACTION_CLOSE_ALL)){
                finish();
            }
        }
    };
 
    /**
     * network status
     */
    protected NetChangeObserver mNetChangeObserver = null;
 
    boolean isCanSop = true;//是否竖屏显示
 
    @SuppressLint("SourceLockedOrientationActivity")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        compositeSubscription = new CompositeDisposable();
 
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            //设置竖屏显示
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        } else {
            if (isCanSop) {
                //设置竖屏显示
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            }
        }
 
        if(!isTaskRoot()){
            Intent intent = getIntent();
            String action = intent.getAction();
            if(intent.hasCategory(Intent.CATEGORY_LAUNCHER) && action.equals(Intent.ACTION_MAIN)){
                finish();
                return;
            }
        }
 
        ACTION_CLOSE_ALL = String.format("cn.sinata.base.%s.all.close",getPackageName());
        //注册“关闭页面”广播监听器
        if (isRegisterCloseBroadReceiver()){
            registerReceiver(broadcastReceiver,new IntentFilter(ACTION_CLOSE_ALL));
        }
 
        mScreenHeight = DensityUtil.getDeviceHeight(this);
        mScreenWidth = DensityUtil.getDeviceWidth(this);
        mContext = this;
        isDestroy = false;
        mHolder = new ViewHolder(this);
 
        mNetChangeObserver = new NetChangeObserver() {
            @Override
            public void onNetConnected(NetUtils.NetType type) {
                super.onNetConnected(type);
                onNetworkConnected(type);
            }
 
            @Override
            public void onNetDisConnect() {
                super.onNetDisConnect();
                onNetworkDisConnected();
            }
        };
 
        //按需注册网络变化监听器
        if (shouldRegisterNetworkChangeReceiver()){
            NetStateReceiver.registerNetworkStateReceiver(this);
            NetStateReceiver.registerObserver(mNetChangeObserver);
        }
 
    }
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (shouldRegisterNetworkChangeReceiver()){
            NetStateReceiver.unRegisterNetworkStateReceiver(this);
            NetStateReceiver.removeRegisterObserver(mNetChangeObserver);
        }
        if (isRegisterCloseBroadReceiver()){
            try {
                unregisterReceiver(broadcastReceiver);
            } catch (Exception e) {
                e.printStackTrace();
            }
 
        }
 
        if (mHolder!=null){
            mHolder.unBind();
        }
        isDestroy = true;
        dismissDialog();
        if (!compositeSubscription.isDisposed()){
            compositeSubscription.dispose();
        }
    }
 
 
    /**
     * 设置是否可以竖屏显示
     * @param showSop
     */
    public void setShowSOP(boolean showSop) {
        isCanSop = showSop;
    }
 
    /**
     * 是否需要注册“关闭全部页面”广播
     * @return 默认false
     */
    protected boolean isRegisterCloseBroadReceiver(){
        return true;
    }
 
    /**
     * 是否注册网络变化监听器
     * @return 默认false
     */
    protected boolean shouldRegisterNetworkChangeReceiver(){
        return false;
    }
 
    /**
     * 显示toast
     * @param s 提示文字
     */
    protected void showToast(String s){
        Toast.create(this).show(s);
    }
 
    /**
     * 关闭所有页面
     */
    protected void closeAll(){
        Intent intent = new Intent(ACTION_CLOSE_ALL);
        sendBroadcast(intent);
    }
 
    /**
     * 必须先调用注册网络状态监听广播。否则没有任何反应
     * must call NetStateReceiver.registerNetworkStateReceiver(context) first ,if not,nothing change
     * @param type 网络类型
     */
    protected void onNetworkConnected(NetUtils.NetType type){}
 
    /**
     * 必须先调用注册网络状态监听广播。否则没有任何反应
     * must call NetStateReceiver.registerNetworkStateReceiver(context) first ,if not,nothing change
     */
    protected void onNetworkDisConnected(){}
 
    /**
     * 绑定视图,简化系统findViewById写法
     * @param resId 视图id
     * @param <T> 视图类型继承自系统的View类
     * @return 返回组件实例
     */
    @SuppressWarnings("unchecked")
    protected <T extends View> T bind(int resId){
        return  (T) mHolder.bind(resId);
    }
 
    /**
     * 显示加载提示窗
     */
    public void showDialog(){
        showDialog("加载中...");
    }
 
    /**
     * 显示加载提示窗
     * @param msg 提示文字
     */
    protected void showDialog(CharSequence msg){
        showDialog(msg,false);
    }
 
    /**
     * 显示加载提示窗
     * @param msg 提示文字
     * @param canCancel 是否可手动取消
     */
    protected void showDialog(CharSequence msg,boolean canCancel){
        if (isDestroy) {
            return;
        }
        if(dialog==null){
            dialog=new ProgressDialog(mContext,R.style.Theme_ProgressDialog);
        }
        dialog.setCanceledOnTouchOutside(canCancel);
        dialog.setMessage(msg);
        if (!dialog.isShowing()) {
            dialog.show();
        }
    }
 
    /**
     * 关闭加载窗
     */
    public void dismissDialog(){
        if (dialog!=null && dialog.isShowing()){
            dialog.dismiss();
        }
    }
}