hxb
2022-11-22 b3513b1713bb979d0a69c5a8c4ddcd038f184e6e
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
package com.mm.android.deviceaddmodule.mobilecommon.base;
 
 
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
 
import com.mm.android.deviceaddmodule.R;
import com.mm.android.deviceaddmodule.mobilecommon.base.api.IProgressDialogControlView;
import com.mm.android.deviceaddmodule.mobilecommon.common.HandlerManager;
import com.mm.android.deviceaddmodule.mobilecommon.eventbus.event.BaseEvent;
import com.mm.android.deviceaddmodule.mobilecommon.utils.LogUtil;
 
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
 
import java.lang.reflect.Field;
 
/**
 * 基类,不加其他功能
 */
public class BaseFragment extends Fragment implements IProgressDialogControlView {
    private static String TAG = "BaseFragment";
    private ProgressDialog mProgressDialog;
    private Toast mToast;
    private Toast mToastInCenter;
    private Toast mToastWithImg;
    protected HandlerManager mHandlerManager;
 
    public BaseFragment() {
 
    }
 
    public boolean isCurrentPageView() {
        return false;
    }
 
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(BaseEvent event) {
    }
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        registerBroadCast();
        mProgressDialog = new ProgressDialog(getActivity(), R.style.mobile_common_custom_dialog);
        Window window = mProgressDialog.getWindow();
        if (window != null) {
            window.setWindowAnimations(R.style.mobile_common_dialog_anima);
        }
 
        mProgressDialog.setCanceledOnTouchOutside(false);
        if (!EventBus.getDefault().isRegistered(this)) EventBus.getDefault().register(this);
        mHandlerManager = new HandlerManager();
    }
 
    @Override
    public void showProgressDialog(int layoutId) {
        if (isDialogEnable() && !mProgressDialog.isShowing()) {
            mProgressDialog.show();
            mProgressDialog.setContentView(layoutId);
        }
    }
 
    @Override
    public void dissmissProgressDialog() {
        if (isDialogEnable() && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }
 
    @Override
    public void cancleProgressDialog() {
        if (isDialogEnable() && mProgressDialog.isShowing()) {
            mProgressDialog.cancel();
        }
    }
 
    @Override
    public void setProgressDialogCancelable(boolean flag) {
        if (isDialogEnable()) {
            mProgressDialog.setCancelable(flag);
        }
    }
 
    @Override
    public void setProgressDialogCancelListener(DialogInterface.OnCancelListener cancelListener) {
        if (isDialogEnable()) {
            mProgressDialog.setOnCancelListener(cancelListener);
        }
    }
 
    private boolean isDialogEnable() {
        return getActivity() != null && !getActivity().isFinishing() && mProgressDialog != null;
    }
 
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (getActivity() instanceof BaseFragmentActivity) {
            ((BaseFragmentActivity) getActivity()).addBaseFragment(this);
        }
    }
 
    @Override
    public void onDetach() {
        if (getActivity() instanceof BaseFragmentActivity) {
            ((BaseFragmentActivity) getActivity()).removeBaseFragment(this);
        }
        super.onDetach();
        try {
            Field childFragment = Fragment.class.getDeclaredField("mChildFragmentManager");
            childFragment.setAccessible(true);
            childFragment.set(this, null);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
 
    @Override
    public void onDestroyView() {
        dissmissProgressDialog();
        mProgressDialog = null;
        super.onDestroyView();
    }
 
    @Override
    public void onDestroy() {
        super.onDestroy();
        unRegisterBroadCast();
        dissmissProgressDialog();
        mProgressDialog = null;
        if (EventBus.getDefault().isRegistered(this)) EventBus.getDefault().unregister(this);
        mHandlerManager.clearHandlers();
    }
 
    protected void toast(int res) {
        if (getActivity() != null && !getActivity().isFinishing()) {
            String content = "";
            try {
                content = getActivity().getString(res);
            } catch (Resources.NotFoundException e) {
                LogUtil.debugLog("toast", "resource id not found!!!");
            }
            toast(content);
        }
    }
 
    protected void toast(String content) {
        if (!isAdded()) {
            return;
        }
        if (getActivity() != null && !getActivity().isFinishing()) {
            //系统版本大于等于android 9.0之后的版本
            if (Build.VERSION_CODES.O_MR1 < Build.VERSION.SDK_INT) {
                Toast.makeText(getActivity(), content, Toast.LENGTH_SHORT).show();
            } else {
                if (mToast == null) {
                    mToast = Toast.makeText(getActivity(), content, Toast.LENGTH_SHORT);
                } else {
                    mToast.setText(content);
                    mToast.setDuration(Toast.LENGTH_SHORT);
                }
                mToast.show();
            }
        }
    }
 
    protected void toastInCenter(int res) {
        if (getActivity() != null && !getActivity().isFinishing()) {
            String content = "";
            try {
                content = getActivity().getString(res);
            } catch (Resources.NotFoundException e) {
                LogUtil.debugLog("toast", "resource id not found!!!");
            }
            toastInCenter(content);
        }
    }
 
    protected void toastInCenter(String content) {
        if (!isAdded()) {
            return;
        }
        if (getActivity() != null && !getActivity().isFinishing()) {
            Toast toastInCenter = null;
            //系统版本大于等于android 9.0之后的版本
            if (Build.VERSION_CODES.O_MR1 < Build.VERSION.SDK_INT) {
                toastInCenter = Toast.makeText(getActivity(), content, Toast.LENGTH_SHORT);
            } else {
                if (mToastInCenter == null) {
                    mToastInCenter = Toast.makeText(getActivity(), content, Toast.LENGTH_SHORT);
                } else {
                    mToastInCenter.setText(content);
                    mToastInCenter.setDuration(Toast.LENGTH_SHORT);
                }
                toastInCenter = mToastInCenter;
            }
            if (toastInCenter == null) return;
            ViewGroup toastView = (ViewGroup) toastInCenter.getView();
            View imageCodeProject = toastView.getChildAt(0);
            if (imageCodeProject != null && (imageCodeProject instanceof ImageView)) {
                ((ImageView) imageCodeProject).setImageResource(0);
            }
            toastInCenter.show();
        }
    }
 
    protected void toastWithImg(int strResId, int imgId) {
        if (getActivity() != null && !getActivity().isFinishing()) {
            String content = "";
            try {
                content = getActivity().getString(strResId);
            } catch (Resources.NotFoundException e) {
                LogUtil.debugLog("toast", "resource id not found!!!");
            }
            toastWithImg(content, imgId);
        }
    }
 
    protected void toastWithImg(String content, int imgId) {
        if (!isAdded()) {
            return;
        }
        if (getActivity() != null && !getActivity().isFinishing()) {
            Toast toastInCenter = null;
            //系统版本大于等于android 9.0之后的版本
            if (Build.VERSION_CODES.O_MR1 < Build.VERSION.SDK_INT) {
                toastInCenter = Toast.makeText(getActivity(), content, Toast.LENGTH_SHORT);
            } else {
                if (mToastWithImg == null) {
                    mToastWithImg = Toast.makeText(getActivity(), content, Toast.LENGTH_SHORT);
                } else {
                    mToastWithImg.setText(content);
                    mToastWithImg.setDuration(Toast.LENGTH_SHORT);
                }
                toastInCenter = mToastWithImg;
            }
            if (toastInCenter == null) return;
 
            toastInCenter.setGravity(Gravity.CENTER, 0, 0);
            TextView tv = toastInCenter.getView().findViewById(android.R.id.message);
            tv.setGravity(Gravity.CENTER);
            tv.setTextColor(Color.WHITE);
            tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
            tv.setLineSpacing(0, 1.2f);
            tv.setBackgroundResource(0);
            // set padding
            int paddingHorizontal = (int) getResources().getDimension(R.dimen.mobile_common_dp_10);
            int paddingVertical = (int) getResources().getDimension(R.dimen.mobile_common_dp_10);
            ViewGroup toastView = (ViewGroup) toastInCenter.getView();
            toastView.setPadding(paddingHorizontal, paddingVertical, paddingHorizontal, paddingVertical);
            toastView.setBackgroundResource(R.drawable.mobile_common_shape_round_bg);
            if (imgId > 0) {
                View imageCodeProject = toastView.getChildAt(0);
                if (imageCodeProject == null || !(imageCodeProject instanceof ImageView)) {
                    imageCodeProject = new ImageView(getActivity());
                    toastView.addView(imageCodeProject, 0);
                }
                ((ImageView) imageCodeProject).setImageResource(imgId);
            } else {
                View imageCodeProject = toastView.getChildAt(0);
                if (imageCodeProject == null || !(imageCodeProject instanceof ImageView)) {
                    imageCodeProject = new ImageView(getActivity());
                    toastView.addView(imageCodeProject, 0);
                }
                ((ImageView) imageCodeProject).setImageResource(0);
            }
            toastInCenter.show();
        }
 
    }
 
 
    public boolean onBackPressed() {
        return false;
    }
 
    /********************** 注册广播 ****************************/
    private BroadcastReceiver broadcastReceiver = null;
 
    private void registerBroadCast() {
        IntentFilter mIntentFilter = createBroadCast();
        if (mIntentFilter != null && mIntentFilter.countActions() > 0) {
            broadcastReceiver = new BaseBroadcast();
            if (getActivity() != null)
                getActivity().registerReceiver(broadcastReceiver, mIntentFilter);
        }
    }
 
    protected void unRegisterBroadCast() {
 
        if (broadcastReceiver != null) {
            if (getActivity() != null) getActivity().unregisterReceiver(broadcastReceiver);
            broadcastReceiver = null;
 
        }
    }
 
    protected IntentFilter createBroadCast() {
        return null;
    }
 
    protected void onReceive(Context context, Intent intent) {
 
    }
 
    private class BaseBroadcast extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            BaseFragment.this.onReceive(context, intent);
        }
    }
 
 
    protected final <E extends View> E getView(View rootView, int id) {
        try {
            return (E) rootView.findViewById(id);
        } catch (ClassCastException e) {
            LogUtil.errorLog("getView", "Cloud not cast view to concrete class.", e);
            throw e;
        } catch (NullPointerException e) {
            LogUtil.errorLog("getView", "rootView is null.", e);
            throw e;
        }
    }
 
    protected Handler addHandler(Handler handler) {
        return mHandlerManager.addHandler(handler);
    }
 
    protected <V> V inflateViewStub(ViewStub viewStub, Class<V> clz) {
        V view;
        if (viewStub.getParent() != null) {
            view = (V) viewStub.inflate();
            viewStub.setTag(view);
        } else {
            view = (V) viewStub.getTag();
        }
        return view;
    }
 
    /**
     * 是否已经inflate
     *
     * @param viewStub
     * @return true表示已经inflate
     */
    protected boolean isViewStubInflate(ViewStub viewStub) {
        return viewStub.getParent() == null;
    }
 
    /**
     * 更新ViewStub是否可见
     * @param viewStub
     * @param visible
     */
    protected void updateViewStubVisibility(ViewStub viewStub, int visible) {
        switch (visible) {
            case View.VISIBLE:
                inflateViewStub(viewStub, View.class).setVisibility(View.VISIBLE);
                break;
            default:
                if (isViewStubInflate(viewStub)) {
                    inflateViewStub(viewStub, View.class).setVisibility(visible);
                }
                break;
        }
    }
 
}