Skip to content

Commit

Permalink
Current Activity 悬浮窗
Browse files Browse the repository at this point in the history
  • Loading branch information
lulululbj committed Jan 10, 2019
1 parent 669dd79 commit 6219899
Show file tree
Hide file tree
Showing 15 changed files with 349 additions and 19 deletions.
11 changes: 8 additions & 3 deletions Parser/src/main/java/luyao/parser/utils/Reader.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,14 @@ public void reset() throws IOException {
}

public static void log(String format, Object... params) {
if (showLog)
System.out.printf(format, params);
System.out.println();
try {
if (showLog)
System.out.printf(format, params);
System.out.println();
}catch (Exception e){
e.printStackTrace();
}

}

}
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.40'
implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.43-alpha1'
implementation 'androidx.recyclerview:recyclerview:1.1.0-alpha01'
implementation 'com.afollestad.material-dialogs:core:2.0.0-rc5'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.0'
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

<application
android:name=".App"
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/luyao/box/App.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package luyao.box

import android.app.Application
import android.content.Context
import kotlin.properties.Delegates

/**
* Created by luyao
* on 2018/12/29 13:33
*/
class App : Application() {

companion object {
var CONTEXT: Context by Delegates.notNull() }
override fun onCreate() {
super.onCreate()
CONTEXT=applicationContext
}
}
18 changes: 14 additions & 4 deletions app/src/main/java/luyao/box/Ext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@ package luyao.box
import android.content.Context
import android.widget.Toast

fun Context.toast(text:String){
Toast.makeText(this,text,Toast.LENGTH_SHORT).show()
fun Context.toast(text: String) {
Toast.makeText(this, text, Toast.LENGTH_SHORT).show()
}

fun Context.toast(resId:Int){
Toast.makeText(this,resId,Toast.LENGTH_SHORT).show()
fun Context.toast(resId: Int) {
Toast.makeText(this, resId, Toast.LENGTH_SHORT).show()
}

fun Context.px2dp(pxValue: Int): Int {
val scale = resources.displayMetrics.density;
return (pxValue / scale + 0.5f).toInt()
}

fun Context.dp2px(dpValue: Int): Int {
val scale = resources.displayMetrics.density;
return (dpValue * scale + 0.5f).toInt()
}
25 changes: 25 additions & 0 deletions app/src/main/java/luyao/box/adapter/HistoryAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package luyao.box.adapter

import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import luyao.box.R
import luyao.box.bean.HistoryBean

/**
* Created by luyao
* on 2019/1/10 13:12
*/
class HistoryAdapter(layoutResId: Int = R.layout.item_history) :
BaseQuickAdapter<HistoryBean, BaseViewHolder>(layoutResId) {

override fun convert(helper: BaseViewHolder, item: HistoryBean) {
val position = helper.layoutPosition
if (position == 0 || getItem(position - 1)?.packageName != item.packageName) {
helper.setVisible(R.id.historyPackageName, true)
helper.setText(R.id.historyPackageName, item.packageName)
} else {
helper.setVisible(R.id.historyPackageName, false)
}
helper.setText(R.id.historyWindowName, item.windowName)
}
}
10 changes: 10 additions & 0 deletions app/src/main/java/luyao/box/bean/HistoryBean.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package luyao.box.bean

/**
* Created by luyao
* on 2019/1/10 13:18
*/
data class HistoryBean(
val packageName:String,
val windowName:String
)
14 changes: 11 additions & 3 deletions app/src/main/java/luyao/box/service/BoxAccessibilityService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,24 @@ package luyao.box.service
import android.accessibilityservice.AccessibilityService
import android.util.Log
import android.view.accessibility.AccessibilityEvent
import luyao.box.bean.HistoryBean
import luyao.box.util.FloatWindowManager
import luyao.box.util.Preference

class BoxAccessibilityService : AccessibilityService(){
class BoxAccessibilityService : AccessibilityService() {

private var showWindow by Preference(Preference.SHOW_WINDOW, false)
private val floatWindowManager by lazy { FloatWindowManager(this) }

override fun onInterrupt() {

}

override fun onAccessibilityEvent(event: AccessibilityEvent) {
if (event.eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED){
Log.e("box","${event.packageName}$\n${event.className}")
if (event.eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
Log.e("box", "${event.packageName}$\n${event.className}")
if (showWindow)
floatWindowManager.addItem(HistoryBean(event.packageName.toString(), event.className.toString()))
}
}
}
38 changes: 32 additions & 6 deletions app/src/main/java/luyao/box/ui/activity/CurrentActivity.kt
Original file line number Diff line number Diff line change
@@ -1,30 +1,56 @@
package luyao.box.ui.activity

import android.content.Intent
import android.net.Uri
import android.os.Build
import android.provider.Settings
import kotlinx.android.synthetic.main.activity_current.*
import kotlinx.android.synthetic.main.title_layout.*
import luyao.box.R
import luyao.box.common.base.BaseActivity
import luyao.box.service.BoxAccessibilityService
import luyao.box.util.AccessibilityUtils
import luyao.box.util.Preference

class CurrentActivity : BaseActivity() {

private var showWindow by Preference(Preference.SHOW_WINDOW, false)
private val isOpen by lazy {
AccessibilityUtils.checkBoxAccessibilityEnabled(this)
}

override fun getLayoutResId() = R.layout.activity_current

override fun initView() {
mToolbar.title=getString(R.string.current_activity)
mToolbar.title = getString(R.string.current_activity)
windowSwitch.isChecked=showWindow
windowSwitch.setOnCheckedChangeListener { _, isChecked -> updateWindow(isChecked) }
}

override fun initData() {
initAccessibilityService()

}

private fun initAccessibilityService(){
val isOpen=AccessibilityUtils.checkAccessibilityEnabled("$packageName/${BoxAccessibilityService::class.java.canonicalName.replace(packageName, "")}",this)
accessibilitySwitch.isChecked=isOpen
private fun initAccessibilityService() {
accessibilitySwitch.isChecked = isOpen
if (!isOpen)
AccessibilityUtils.goToAccessibilitySetting(this)
}

private fun updateWindow(isOpen: Boolean) {
if (AccessibilityUtils.checkBoxAccessibilityEnabled(this)) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1 && !Settings.canDrawOverlays(this)) {
windowSwitch.isChecked = false
showWindow = false
val intent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION)
intent.data = Uri.parse("package:$packageName")
startActivity(intent)
} else {
showWindow = isOpen
windowSwitch.isChecked = isOpen
}
} else {
showWindow = false
windowSwitch.isChecked = false
}
}
}
8 changes: 7 additions & 1 deletion app/src/main/java/luyao/box/util/AccessibilityUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ import android.content.Context
import android.content.Intent
import android.provider.Settings
import android.view.accessibility.AccessibilityManager
import luyao.box.service.BoxAccessibilityService

object AccessibilityUtils {

fun checkBoxAccessibilityEnabled(context: Context): Boolean = checkAccessibilityEnabled(
"${context.packageName}/${BoxAccessibilityService::class.java.canonicalName.replace(context.packageName, "")}",
context
)

fun checkAccessibilityEnabled(serviceName: String, context: Context): Boolean {

fun checkAccessibilityEnabled(serviceName: String, context: Context): Boolean {
val manager: AccessibilityManager =
context.getSystemService(Context.ACCESSIBILITY_SERVICE) as AccessibilityManager
val enabledAccessibilityServiceList =
Expand Down
58 changes: 58 additions & 0 deletions app/src/main/java/luyao/box/util/FloatWindowManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package luyao.box.util

import android.content.Context
import android.graphics.PixelFormat
import android.os.Build
import android.view.Gravity
import android.view.LayoutInflater
import android.view.WindowManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import luyao.box.R
import luyao.box.adapter.HistoryAdapter
import luyao.box.bean.HistoryBean
import luyao.box.dp2px

/**
* Created by luyao
* on 2019/1/10 14:37
*/
class FloatWindowManager(mContext: Context) {


private val windowManager by lazy { mContext.getSystemService(Context.WINDOW_SERVICE) as WindowManager }
private val windowAdapter by lazy { HistoryAdapter() }
private val view by lazy { LayoutInflater.from(mContext).inflate(R.layout.window_history, null) }
private val layoutParams: WindowManager.LayoutParams by lazy { WindowManager.LayoutParams() }
private val windowRecycleView by lazy { view.findViewById<RecyclerView>(R.id.windowRecycleView) }

init {
layoutParams.run {
gravity=Gravity.START and Gravity.TOP
width = mContext.dp2px(200)
height = mContext.dp2px(150)
format = PixelFormat.TRANSPARENT
flags = 0x18
type = if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.N_MR1)
WindowManager.LayoutParams.TYPE_TOAST
else
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
}


windowRecycleView.run {
layoutManager = LinearLayoutManager(mContext)
adapter = windowAdapter
}
windowManager.addView(view, layoutParams)
}

fun addItem(historyBean: HistoryBean) {
windowAdapter.addData(0, historyBean)
windowAdapter.notifyDataSetChanged()
windowRecycleView.scrollToPosition(0)
windowManager.updateViewLayout(view, layoutParams)
}


}
Loading

0 comments on commit 6219899

Please sign in to comment.