lmw
2023-06-06 7a563b559c48b9b339784c25fc5f0adc2ab5154e
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
package cn.sinata.xldutils.activity
 
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.ActivityInfo
import android.content.res.Configuration
import android.os.Build
import android.os.Bundle
import android.text.TextUtils
import android.view.MotionEvent
import android.view.View
import android.view.Window
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import cn.sinata.xldutils.R
import cn.sinata.xldutils.rxutils.RequestHelper
import cn.sinata.xldutils.utils.LanguageUtil
import cn.sinata.xldutils.utils.SPUtils
import cn.sinata.xldutils.utils.myToast
import cn.sinata.xldutils.widget.ProgressDialog
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import org.jetbrains.anko.AnkoLogger
 
/**
 * 基础activity,包含设置默认强制竖屏显示,广播方式实现关闭全部继承自该activity,并注册了关闭广播的子类
 *
 */
abstract class BaseActivity : AppCompatActivity(), AnkoLogger,RequestHelper {
 
    private lateinit var ACTION_CLOSE_ALL: String
    private val compositeDisposable = CompositeDisposable()
    //改用lazy初始,第一次使用时才会初始化
    private val dialog: ProgressDialog by lazy {
        ProgressDialog(this, R.style.Theme_ProgressDialog)
    }
 
    private val closeAllReceiver = object : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            if (intent != null && TextUtils.equals(intent.action, ACTION_CLOSE_ALL)) {
                finish()
            }
        }
    }
 
    private var isCanSop = true
 
    fun setShowSOP(showSop: Boolean) {
        isCanSop = showSop
    }
 
    override fun attachBaseContext(newBase: Context?) {
        val language = SPUtils.instance().getString("language")
        val country = SPUtils.instance().getString("country")//todo 特别标注:此处country代表地区而非国家,台湾省是中国的一部分!!(用于区分繁体中文和简体中文)
        super.attachBaseContext(LanguageUtil.attachBaseContext(newBase, language,country))
    }
 
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
            //设置竖屏显示
            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        } else {
            if (isCanSop) {
                //设置竖屏显示
                requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
            }
        }
        ACTION_CLOSE_ALL = "cn.sinata.base.%s.all.close".format(packageName)
        System.err.println(ACTION_CLOSE_ALL)
        if (isRegisterCloseBroadReceiver()) {
            registerReceiver(closeAllReceiver, IntentFilter(ACTION_CLOSE_ALL))
        }
    }
 
    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
    }
 
    protected open fun closeAll() {
        val intent = Intent(ACTION_CLOSE_ALL)
        sendBroadcast(intent)
    }
    protected var isTouchHide = true
 
    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        if (isTouchHide) {
            if (ev.action == MotionEvent.ACTION_DOWN) {
                val v = currentFocus
                if (isShouldHideInput(v, ev)) {
                    hideInputMethod()
                }
                return super.dispatchTouchEvent(ev)
            }
            // 必不可少,否则所有的组件都不会有TouchEvent了
            return if (window.superDispatchTouchEvent(ev)) {
                true
            } else onTouchEvent(ev)
        } else {
            return super.dispatchTouchEvent(ev)
        }
    }
    /**
     * 是否注册关闭全部的广播
     */
    protected fun isRegisterCloseBroadReceiver(): Boolean {
        return true
    }
    /**
     * 是否点击键盘以外的位置
     *
     * @param v
     * @param event
     * @return
     */
    fun isShouldHideInput(v: View?, event: MotionEvent): Boolean {
        if (v != null && v is EditText) {
            val leftTop = intArrayOf(0, 0)
            v.getLocationInWindow(leftTop)
            val left = leftTop[0]
            val top = leftTop[1]
            val bottom = top + v.height
            val right = left + v.width
            return !(event.x > left && event.x < right
                    && event.y > top && event.y < bottom)
        }
        return false
    }
 
    /**
     * 隐藏软键盘
     *
     * @param activity
     */
    fun hideInputMethod() {
        (getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager)
                .hideSoftInputFromWindow(this.currentFocus!!.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
    }
 
    override fun onDestroy() {
        super.onDestroy()
        if (!compositeDisposable.isDisposed) {
            compositeDisposable.dispose()
        }
 
        try {
            if (isRegisterCloseBroadReceiver()) {
                unregisterReceiver(closeAllReceiver)
            }
        } catch (e : Exception) {
            e.printStackTrace()
        }
 
        dismissDialog()
    }
 
    fun showDialog(msg: String = "加载中...", canCancel: Boolean = true) {
        if (!canCancel) {
            dialog.setOnCancelListener {
                if (this.finishWhenCancelDialog()) {
                    finish()
                }
            }
        } else {
            //这里设置如果是可以取消的监听器置null了。可以自己在页面上重新设置想要的操作。这里不知道具体需求。
            dialog.setOnCancelListener(null)
        }
        dialog.setCanceledOnTouchOutside(canCancel)
        dialog.setMessage(msg)
        if (!dialog.isShowing) {
            dialog.show()
        }
    }
 
    open fun dismissDialog() {
        if (dialog.isShowing) {
            dialog.dismiss()
        }
    }
 
    /**
     * 是否在取消progressDialog的时候关闭页面
     */
    protected open fun finishWhenCancelDialog() = true
 
    override fun onRequestFinish() {
        dismissDialog()
    }
 
    override fun onBindHelper(disposable: Disposable) {
        compositeDisposable.add(disposable)
    }
 
    override fun errorToast(msg: String?) {
        myToast(msg.toString())
    }
 
    open fun gotoLogin(){}
}