Skip to content

Commit dda7295

Browse files
committed
添加全局toast
1 parent 4e42555 commit dda7295

File tree

7 files changed

+227
-1
lines changed

7 files changed

+227
-1
lines changed

app/src/main/java/com/wrbug/developerhelper/DeveloperApplication.kt

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,38 @@
11
package com.wrbug.developerhelper
22

3+
import android.os.Handler
4+
import android.os.Looper
35
import com.elvishew.xlog.LogLevel
46
import com.elvishew.xlog.XLog
57
import com.tencent.mmkv.MMKV
68
import com.wrbug.developerhelper.basecommon.BaseApp
7-
import com.wrbug.developerhelper.util.ShellUtils
89
import java.io.File
910
import java.io.FileOutputStream
1011
import kotlin.concurrent.thread
12+
import com.wrbug.developerhelper.ui.widget.flexibletoast.FlexibleToast
1113

1214

1315
class DeveloperApplication : BaseApp() {
16+
// 全局的 handler 对象
17+
private val appHandler = Handler()
18+
// 全局的 Toast 对象
19+
private val flexibleToast: FlexibleToast by lazy {
20+
FlexibleToast(this)
21+
}
22+
private val builder: FlexibleToast.Builder by lazy {
23+
FlexibleToast.Builder(this).setGravity(FlexibleToast.GRAVITY_BOTTOM)
24+
}
25+
26+
companion object {
27+
private lateinit var instance: DeveloperApplication
28+
fun getInstance(): DeveloperApplication {
29+
return instance
30+
}
31+
}
1432

1533
override fun onCreate() {
1634
super.onCreate()
35+
instance = this
1736
XLog.init(LogLevel.ALL)
1837
MMKV.initialize(this)
1938
releaseAssetsFile()
@@ -32,4 +51,18 @@ class DeveloperApplication : BaseApp() {
3251
fileOutputStream.close()
3352
}
3453
}
54+
55+
56+
fun showToast(builder: FlexibleToast.Builder) {
57+
if (Looper.myLooper() !== Looper.getMainLooper()) {
58+
appHandler.post { flexibleToast.toastShow(builder) }
59+
} else {
60+
flexibleToast.toastShow(builder)
61+
}
62+
}
63+
64+
fun showToast(msg: String) {
65+
builder.setSecondText(msg)
66+
showToast(builder)
67+
}
3568
}

app/src/main/java/com/wrbug/developerhelper/service/DeveloperHelperAccessibilityService.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import android.provider.Settings
1111
import android.text.TextUtils
1212
import android.view.accessibility.AccessibilityEvent
1313
import android.view.accessibility.AccessibilityNodeInfo
14+
import com.wrbug.developerhelper.DeveloperApplication
15+
import com.wrbug.developerhelper.R
1416
import com.wrbug.developerhelper.basecommon.BaseApp
1517
import com.wrbug.developerhelper.basecommon.showToast
1618
import com.wrbug.developerhelper.constant.ReceiverConstant
@@ -205,6 +207,7 @@ class DeveloperHelperAccessibilityService : AccessibilityService() {
205207

206208
inner class DeveloperHelperAccessibilityReceiver : BroadcastReceiver() {
207209
override fun onReceive(context: Context?, data: Intent?) {
210+
DeveloperApplication.getInstance().showToast(getString(R.string.geting_app_info))
208211
ShellManager.getTopActivity(object : Callback<TopActivityInfo?> {
209212

210213
override fun onSuccess(data: TopActivityInfo?) {

app/src/main/java/com/wrbug/developerhelper/ui/activity/main/MainActivity.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import android.view.MenuItem
99
import android.widget.CompoundButton
1010
import androidx.appcompat.app.AlertDialog
1111
import androidx.databinding.DataBindingUtil
12+
import com.wrbug.developerhelper.DeveloperApplication
1213
import com.wrbug.developerhelper.R
1314
import com.wrbug.developerhelper.basecommon.BaseVMActivity
1415
import com.wrbug.developerhelper.basecommon.obtainViewModel
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package com.wrbug.developerhelper.ui.widget.flexibletoast
2+
3+
import android.content.Context
4+
import android.view.Gravity
5+
import android.view.LayoutInflater
6+
import android.view.View
7+
import android.widget.ImageView
8+
import android.widget.TextView
9+
import android.widget.Toast
10+
import com.wrbug.developerhelper.R
11+
import com.wrbug.developerhelper.util.UiUtils
12+
13+
class FlexibleToast(private val mContext: Context) {
14+
15+
private val flexibleToast: Toast by lazy {
16+
Toast(mContext)
17+
}
18+
19+
fun toastShow(builder: Builder) {
20+
when {
21+
builder.mGravity == GRAVITY_CENTER -> flexibleToast.setGravity(
22+
Gravity.CENTER or Gravity.CENTER_VERTICAL,
23+
0,
24+
0
25+
)
26+
builder.mGravity == GRAVITY_TOP -> flexibleToast.setGravity(
27+
Gravity.TOP or Gravity.CENTER_VERTICAL,
28+
0,
29+
UiUtils.dp2px(mContext, 20F)
30+
)
31+
else -> flexibleToast.setGravity(
32+
Gravity.BOTTOM or Gravity.CENTER_VERTICAL,
33+
0,
34+
UiUtils.dp2px(mContext, 20F)
35+
)
36+
}
37+
if (builder.mDuration == TOAST_LONG) {
38+
flexibleToast.duration = Toast.LENGTH_LONG
39+
} else {
40+
flexibleToast.duration = Toast.LENGTH_SHORT
41+
}
42+
if (builder.hasCustomerView && builder.mCustomerView != null) {
43+
flexibleToast.view = builder.mCustomerView
44+
} else {
45+
flexibleToast.view = builder.mDefaultView
46+
}
47+
flexibleToast.show()
48+
}
49+
50+
/**
51+
* 控制Toast的显示样式
52+
*/
53+
class Builder(context: Context) {
54+
val mDefaultView: View = LayoutInflater.from(context)
55+
.inflate(R.layout.layout_toast_flexible, null)
56+
var mCustomerView: View? = null
57+
private val mIvImage: ImageView
58+
private val mTvFirst: TextView
59+
private val mTvSecond: TextView
60+
61+
private val dividerFirst: View
62+
private val dividerSecond: View
63+
64+
var mDuration = Toast.LENGTH_SHORT// 0 short, 1 long
65+
var mGravity = 0
66+
var hasCustomerView = false // 是否使用自定义layout
67+
68+
69+
init {
70+
mIvImage = mDefaultView.findViewById(R.id.imgIv)
71+
mTvFirst = mDefaultView.findViewById(R.id.firstTv)
72+
mTvSecond = mDefaultView.findViewById(R.id.secondTv)
73+
dividerFirst = mDefaultView.findViewById(R.id.firstDividerView)
74+
dividerSecond = mDefaultView.findViewById(R.id.secondDividerView)
75+
}
76+
77+
fun setImageResource(resId: Int): Builder {
78+
this.mIvImage.setImageResource(resId)
79+
this.mIvImage.visibility = View.VISIBLE
80+
this.dividerFirst.visibility = View.VISIBLE
81+
return this
82+
}
83+
84+
fun setFirstText(firstText: String): Builder {
85+
this.mTvFirst.text = firstText
86+
this.mTvFirst.visibility = View.VISIBLE
87+
this.dividerSecond.visibility = View.VISIBLE
88+
return this
89+
}
90+
91+
fun setSecondText(secondText: String): Builder {
92+
this.mTvSecond.text = secondText
93+
this.mTvSecond.visibility = View.VISIBLE
94+
return this
95+
}
96+
97+
fun setDuration(duration: Int): Builder {
98+
this.mDuration = duration
99+
return this
100+
}
101+
102+
fun setGravity(gravity: Int): Builder {
103+
this.mGravity = gravity
104+
return this
105+
}
106+
107+
/**
108+
* 为Toast指定自定义的layout,此时上面对ImageView和TextView的设置失效。
109+
* @param customerView
110+
* @return
111+
*/
112+
fun setCustomerView(customerView: View): Builder {
113+
this.mCustomerView = customerView
114+
this.hasCustomerView = true
115+
return this
116+
}
117+
}
118+
119+
companion object {
120+
121+
const val GRAVITY_BOTTOM = 0
122+
const val GRAVITY_CENTER = 1
123+
const val GRAVITY_TOP = 2
124+
const val TOAST_SHORT = 0
125+
const val TOAST_LONG = 1
126+
}
127+
128+
129+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<shape xmlns:android="http://schemas.android.com/apk/res/android">
3+
<solid android:color="#8A000000" />
4+
<corners android:topLeftRadius="8dp"
5+
android:topRightRadius="8dp"
6+
android:bottomRightRadius="8dp"
7+
android:bottomLeftRadius="8dp"/>
8+
</shape>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
android:layout_width="wrap_content"
5+
android:layout_height="wrap_content"
6+
android:layout_gravity="center"
7+
android:background="@drawable/toast_bg_circle_cornor_rect"
8+
android:gravity="center"
9+
android:orientation="vertical"
10+
android:padding="12dp">
11+
12+
<ImageView
13+
android:id="@+id/imgIv"
14+
android:layout_width="wrap_content"
15+
android:layout_height="wrap_content"
16+
android:scaleType="center"
17+
android:visibility="gone"/>
18+
19+
<View
20+
android:id="@+id/firstDividerView"
21+
android:layout_width="match_parent"
22+
android:layout_height="8dp"
23+
android:visibility="gone"
24+
/>
25+
26+
<TextView
27+
android:id="@+id/firstTv"
28+
android:layout_width="wrap_content"
29+
android:layout_height="wrap_content"
30+
android:gravity="center"
31+
android:textColor="#CCffffff"
32+
android:textSize="16sp"
33+
android:visibility="gone"/>
34+
35+
<View
36+
android:id="@+id/secondDividerView"
37+
android:layout_width="match_parent"
38+
android:layout_height="8dp"
39+
android:visibility="gone"
40+
/>
41+
42+
<TextView
43+
android:id="@+id/secondTv"
44+
android:layout_width="wrap_content"
45+
android:layout_height="wrap_content"
46+
android:gravity="center"
47+
android:textColor="#CCffffff"
48+
android:textSize="16sp"
49+
android:visibility="gone"/>
50+
51+
</LinearLayout>

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,5 @@
4949
<string name="analyzing">正在分析</string>
5050
<string name="getting_info">正在获取信息...</string>
5151
<string name="waiting">请稍后...</string>
52+
<string name="geting_app_info">正在获取应用信息</string>
5253
</resources>

0 commit comments

Comments
 (0)