註解繁體化

This commit is contained in:
Raymond Yang 2023-07-01 10:48:59 +08:00
parent 9ef79d0e5e
commit bbe99a1ca5
11 changed files with 108 additions and 78 deletions

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>

View File

@ -1,4 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="jbr-17" project-jdk-type="JavaSDK">

View File

@ -8,6 +8,7 @@ import android.os.Bundle
import android.provider.Settings
import android.view.*
import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
import android.widget.ImageView
import android.widget.Toast
import androidx.annotation.RequiresApi
import com.ray650128.floatwindowdemo.databinding.ActivityMainBinding
@ -15,7 +16,7 @@ import com.ray650128.floatwindowdemo.utils.Utils.REQUEST_FLOAT_CODE
import com.ray650128.floatwindowdemo.service.SuspendwindowService
import com.ray650128.floatwindowdemo.utils.ItemViewTouchListener
import com.ray650128.floatwindowdemo.utils.Utils
import com.ray650128.floatwindowdemo.utils.ViewModleMain
import com.ray650128.floatwindowdemo.utils.ViewModelMain
/**
* @功能: 悬浮窗口Demo
@ -54,18 +55,18 @@ class MainActivity : AppCompatActivity(), View.OnClickListener {
R.id.bt_02 -> {
Utils.checkSuspendedWindowPermission(this) {
isReceptionShow = false
ViewModleMain.isShowSuspendWindow.postValue(true)
ViewModelMain.isShowSuspendWindow.postValue(true)
}
}
R.id.bt_03 -> {
Utils.checkAccessibilityPermission(this) {
ViewModleMain.isShowWindow.postValue(true)
ViewModelMain.isShowWindow.postValue(true)
}
}
R.id.bt_04 -> {
Utils.checkSuspendedWindowPermission(this) {
isReceptionShow = true
ViewModleMain.isShowSuspendWindow.postValue(true)
ViewModelMain.isShowSuspendWindow.postValue(true)
}
}
R.id.bt_05 -> {
@ -83,13 +84,18 @@ class MainActivity : AppCompatActivity(), View.OnClickListener {
//设置大小 自适应
width = WRAP_CONTENT
height = WRAP_CONTENT
flags =
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
x = 10
y = 10
}
// 新建悬浮窗控件
floatRootView = LayoutInflater.from(this).inflate(R.layout.activity_float_item, null)
floatRootView?.findViewById<ImageView>(R.id.iv_close)?.setOnClickListener {
windowManager?.removeView(floatRootView)
}
//设置拖动事件
floatRootView?.setOnTouchListener(ItemViewTouchListener(layoutParam, windowManager))
floatRootView?.focusable = View.FOCUSABLE
// 将悬浮窗控件添加到WindowManager
windowManager.addView(floatRootView, layoutParam)
}
@ -106,8 +112,8 @@ class MainActivity : AppCompatActivity(), View.OnClickListener {
}
}
}
ViewModleMain.isShowSuspendWindow.postValue(false)
ViewModleMain.isShowWindow.postValue(false)
ViewModelMain.isShowSuspendWindow.postValue(false)
ViewModelMain.isShowWindow.postValue(false)
}
@ -124,7 +130,7 @@ class MainActivity : AppCompatActivity(), View.OnClickListener {
override fun onResume() {
super.onResume()
if (isReceptionShow) {
ViewModleMain.isVisible.postValue(true)
ViewModelMain.isVisible.postValue(true)
}
binding.textView.text = "onResume"
}
@ -137,7 +143,7 @@ class MainActivity : AppCompatActivity(), View.OnClickListener {
override fun onStop() {
super.onStop()
if (isReceptionShow) {
ViewModleMain.isVisible.postValue(false)
ViewModelMain.isVisible.postValue(false)
}
}

View File

@ -9,18 +9,18 @@ import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
import androidx.lifecycle.LifecycleService
import com.ray650128.floatwindowdemo.R
import com.ray650128.floatwindowdemo.utils.Utils
import com.ray650128.floatwindowdemo.utils.ViewModleMain
import com.ray650128.floatwindowdemo.utils.ViewModelMain
import com.ray650128.floatwindowdemo.utils.ItemViewTouchListener
/**
* @功能:应用外打开Service 有局限性 特殊界面无法显
* @功能:應用外打開Service 有侷限性 特殊界面無法顯
* @User Lmy
* @Creat 4/15/21 5:28 PM
* @Compony 远相信美好的事情即将发
* @Compony 遠相信美好的事情即將發
*/
class SuspendwindowService : LifecycleService() {
private lateinit var windowManager: WindowManager
private var floatRootView: View? = null//浮窗View
private var floatRootView: View? = null//浮窗View
override fun onCreate() {
super.onCreate()
@ -28,18 +28,18 @@ class SuspendwindowService : LifecycleService() {
}
/**
* 初始化订阅
* 初始化訂閱
*/
private fun initObserve() {
ViewModleMain.apply {
ViewModelMain.apply {
/**
* 悬浮窗按钮的显示和隐
* 懸浮窗按鈕的顯示和隱
*/
isVisible.observe(this@SuspendwindowService, {
floatRootView?.visibility = if (it) View.VISIBLE else View.GONE
})
/**
* 悬浮窗按钮的创建和移除
* 懸浮窗按鈕的建立和移除
*/
isShowSuspendWindow.observe(this@SuspendwindowService, {
if (it) {
@ -58,17 +58,17 @@ class SuspendwindowService : LifecycleService() {
}
/**
* 创建悬浮窗
* 建立懸浮窗
*/
@SuppressLint("ClickableViewAccessibility")
private fun showWindow() {
//取WindowManager
//取WindowManager
windowManager = getSystemService(WINDOW_SERVICE) as WindowManager
val outMetrics = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(outMetrics)
var layoutParam = WindowManager.LayoutParams().apply {
/**
* 设置type 这里进行了兼
* 設定type 這裡進行了相
*/
type = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
@ -77,18 +77,18 @@ class SuspendwindowService : LifecycleService() {
}
format = PixelFormat.RGBA_8888
flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
//位置大小设置
//位置大小設定
width = WRAP_CONTENT
height = WRAP_CONTENT
gravity = Gravity.LEFT or Gravity.TOP
//设置剧中屏幕显
//設定劇中螢幕顯
x = outMetrics.widthPixels / 2 - width / 2
y = outMetrics.heightPixels / 2 - height / 2
}
// 新建悬浮窗控
// 新建懸浮窗控制元
floatRootView = LayoutInflater.from(this).inflate(R.layout.activity_float_item, null)
floatRootView?.setOnTouchListener(ItemViewTouchListener(layoutParam, windowManager))
// 将悬浮窗控件添加到WindowManager
// 將懸浮窗控制元件新增到WindowManager
windowManager.addView(floatRootView, layoutParam)
}
}

View File

@ -14,17 +14,17 @@ import androidx.lifecycle.LifecycleRegistry
import com.ray650128.floatwindowdemo.R
import com.ray650128.floatwindowdemo.utils.ItemViewTouchListener
import com.ray650128.floatwindowdemo.utils.Utils.isNull
import com.ray650128.floatwindowdemo.utils.ViewModleMain
import com.ray650128.floatwindowdemo.utils.ViewModelMain
/**
* @功能:利用无障碍打开悬浮窗口 无局限性 任何界面可以显
* @功能:利用無障礙打開懸浮視窗 無侷限性 任何界面可以顯
* @User Lmy
* @Creat 4/15/21 5:57 PM
* @Compony 远相信美好的事情即将发
* @Compony 遠相信美好的事情即將發
*/
class WorkAccessibilityService : AccessibilityService(), LifecycleOwner {
private lateinit var windowManager: WindowManager
private var floatRootView: View? = null//浮窗View
private var floatRootView: View? = null//浮窗View
private val mLifecycleRegistry = LifecycleRegistry(this)
override fun onCreate() {
super.onCreate()
@ -33,10 +33,10 @@ class WorkAccessibilityService : AccessibilityService(), LifecycleOwner {
}
/**
* 开关闭的订阅
* 開關閉的訂閱
*/
private fun initObserve() {
ViewModleMain.isShowWindow.observe(this) {
ViewModelMain.isShowWindow.observe(this) {
if (it) {
showWindow()
} else {
@ -53,26 +53,21 @@ class WorkAccessibilityService : AccessibilityService(), LifecycleOwner {
@SuppressLint("ClickableViewAccessibility")
private fun showWindow() {
// 设置LayoutParam
// 获取WindowManager服务
// 設定LayoutParam
// 獲取WindowManager服務
windowManager = getSystemService(WINDOW_SERVICE) as WindowManager
val outMetrics = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(outMetrics)
var layoutParam = WindowManager.LayoutParams()
val layoutParam = WindowManager.LayoutParams()
layoutParam.apply {
//显示的位置
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
type = WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY
//刘海屏延伸到刘海里面
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
layoutInDisplayCutoutMode =
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
}
} else {
type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
//顯示的位置
type = WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY
//劉海屏延伸到劉海里面
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES
}
flags =
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
//flags = WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
width = WindowManager.LayoutParams.WRAP_CONTENT
height = WindowManager.LayoutParams.WRAP_CONTENT
format = PixelFormat.TRANSPARENT

View File

@ -5,10 +5,10 @@ import android.view.View
import android.view.WindowManager
/**
* @功能:处理悬浮窗拖动更新位置
* @功能:處理懸浮窗拖動更新位置
* @User Lmy
* @Creat 4/16/21 9:41 AM
* @Compony 远相信美好的事情即将发
* @Compony 遠相信美好的事情即將發
*/
class ItemViewTouchListener(val wl: WindowManager.LayoutParams, val windowManager: WindowManager) :
View.OnTouchListener {
@ -19,7 +19,6 @@ class ItemViewTouchListener(val wl: WindowManager.LayoutParams, val windowManage
MotionEvent.ACTION_DOWN -> {
x = motionEvent.rawX.toInt()
y = motionEvent.rawY.toInt()
}
MotionEvent.ACTION_MOVE -> {
val nowX = motionEvent.rawX.toInt()
@ -32,13 +31,13 @@ class ItemViewTouchListener(val wl: WindowManager.LayoutParams, val windowManage
x += movedX
y += movedY
}
//更新悬浮球控件位置
windowManager?.updateViewLayout(view, wl)
//更新懸浮球控制元件位置
windowManager.updateViewLayout(view, wl)
}
else -> {
}
}
return false
return true
}
}

View File

@ -14,18 +14,18 @@ import com.ray650128.floatwindowdemo.service.WorkAccessibilityService
import java.util.*
/**
* @功能: 工具
* @功能: 工具
* @User Lmy
* @Creat 4/16/21 8:33 AM
* @Compony 远相信美好的事情即将发
* @Compony 遠相信美好的事情即將發
*/
object Utils {
const val REQUEST_FLOAT_CODE=1001
/**
* 转到设置页面申请打开无障碍辅助功能
* 轉到設定頁面申請打開無障礙輔助功能
*/
private fun accessibilityToSettingPage(context: Context) {
//开启辅助功能页
private fun accessibilityToSettingPage(context: Context) {
//開啟輔助功能頁
try {
val intent = Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
@ -39,7 +39,7 @@ object Utils {
}
/**
* 断Service是否开启
* 斷Service是否開啟
*
*/
fun isServiceRunning(context: Context, ServiceName: String): Boolean {
@ -58,9 +58,9 @@ object Utils {
}
/**
* 断悬浮窗权限权限
* 斷懸浮窗許可權許可權
*/
private fun commonROMPermissionCheck(context: Context?): Boolean {
private fun commonROMPermissionCheck(context: Context?): Boolean {
var result = true
if (Build.VERSION.SDK_INT >= 23) {
try {
@ -76,13 +76,13 @@ object Utils {
}
/**
* 检查悬浮窗权限是否开启
* 檢查懸浮窗許可權是否開啟
*/
fun checkSuspendedWindowPermission(context: Activity, block: () -> Unit) {
if (commonROMPermissionCheck(context)) {
block()
} else {
Toast.makeText(context, "请开启悬浮窗权限", Toast.LENGTH_SHORT).show()
Toast.makeText(context, "請開啟懸浮窗許可權", Toast.LENGTH_SHORT).show()
context.startActivityForResult(Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION).apply {
data = Uri.parse("package:${context.packageName}")
}, REQUEST_FLOAT_CODE)
@ -90,7 +90,7 @@ object Utils {
}
/**
* 检查无障碍服务权限是否开启
* 檢查無障礙服務許可權是否開啟
*/
fun checkAccessibilityPermission(context: Activity, block: () -> Unit) {
if (isServiceRunning(context, WorkAccessibilityService::class.java.canonicalName)) {

View File

@ -1,23 +1,22 @@
package com.ray650128.floatwindowdemo.utils
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
/**
* @功能: 于和Service通信
* @功能: 於和Service通訊
* @User Lmy
* @Creat 4/16/21 8:37 AM
* @Compony 远相信美好的事情即将发
* @Compony 遠相信美好的事情即將發
*/
object ViewModleMain : ViewModel() {
//悬浮窗口创建 移除 基于无障碍服务
object ViewModelMain : ViewModel() {
//懸浮視窗建立 移除 基於無障礙服務
var isShowWindow = MutableLiveData<Boolean>()
//悬浮窗口创建 移除
//懸浮視窗建立 移除
var isShowSuspendWindow = MutableLiveData<Boolean>()
//悬浮窗口显示 隐
//懸浮視窗顯示 隱
var isVisible = MutableLiveData<Boolean>()
}

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:tint="#ffffff" android:height="24dp" android:width="24dp" android:viewportWidth="24" android:viewportHeight="24">
<path android:fillColor="@android:color/white" android:pathData="M12,2C6.47,2 2,6.47 2,12s4.47,10 10,10 10,-4.47 10,-10S17.53,2 12,2zM17,15.59L15.59,17 12,13.41 8.41,17 7,15.59 10.59,12 7,8.41 8.41,7 12,10.59 15.59,7 17,8.41 13.41,12 17,15.59z"/>
</vector>

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#77000000"/>
<corners android:radius="18dp"/>
</shape>

View File

@ -1,10 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
android:layout_height="wrap_content"
android:background="@drawable/realtime_clock_background">
<TextView
android:id="@+id/tv_time"
android:layout_width="140dp"
android:layout_height="38dp"
android:gravity="center"
android:letterSpacing="0.1"
android:paddingStart="15dp"
android:text="22:22:22"
android:textColor="@color/white"
android:textSize="16dp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/icon_logo" />
</RelativeLayout>
android:id="@+id/iv_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="12dp"
android:src="@drawable/ic_baseline_cancel_24"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@+id/tv_time"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>