Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf:重构浮窗基础实现,支持系统浮窗 #130

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(path: ':floatingx')
detektPlugins 'io.gitlab.arturbosch.detekt:detekt-formatting:1.22.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.2.0'
Expand All @@ -74,4 +75,6 @@ dependencies {
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.10'
// debugImplementation 'com.squareup.leakcanary:leakcanary-object-watcher-android-startup:2.10'
implementation isDev ? project(path: ':floatingx') : "io.github.petterpx:floatingx:$version_name"
debugImplementation "com.bytedance.tools.codelocator:codelocator-core:2.0.3"
implementation 'com.github.princekin-f:EasyFloat:2.0.4'
}
56 changes: 31 additions & 25 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,40 +1,46 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.petterp.floatingx.app">
xmlns:tools="http://schemas.android.com/tools"
package="com.petterp.floatingx.app">

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />

<application
android:name=".kotlin.CustomKtApplication"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.FloatingX">
android:name=".kotlin.CustomKtApplication"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.FloatingX">
<activity
android:name="com.petterp.floatingx.app.MainActivity"
android:configChanges="keyboard|orientation|keyboardHidden|screenSize|smallestScreenSize|screenLayout|locale|navigation|fontScale|mcc|mnc|uiMode"
android:exported="true"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
android:name="com.petterp.floatingx.app.MainActivity"
android:configChanges="keyboard|orientation|keyboardHidden|screenSize|smallestScreenSize|screenLayout|locale|navigation|fontScale|mcc|mnc|uiMode"
android:exported="true"
android:windowSoftInputMode="adjustPan"></activity>
<activity
android:name=".test.ImmersedActivity"
android:configChanges="keyboard|orientation|keyboardHidden|screenSize|smallestScreenSize|screenLayout|locale|navigation|fontScale|mcc|mnc|uiMode" />
android:name=".test.ImmersedActivity"
android:configChanges="keyboard|orientation|keyboardHidden|screenSize|smallestScreenSize|screenLayout|locale|navigation|fontScale|mcc|mnc|uiMode" />
<activity
android:name=".test.ScopeActivity"
android:configChanges="keyboard|orientation|keyboardHidden|screenSize|smallestScreenSize|screenLayout|locale|navigation|fontScale|mcc|mnc|uiMode" />
android:name=".test.ScopeActivity"
android:configChanges="keyboard|orientation|keyboardHidden|screenSize|smallestScreenSize|screenLayout|locale|navigation|fontScale|mcc|mnc|uiMode" />
<activity android:name=".test.BlackActivity" />
<activity android:name=".test.MultipleFxActivity" />

<activity android:name=".TestActivity" />
<activity
android:name=".test.SimpleRvActivity"
android:configChanges="keyboard|orientation|keyboardHidden|screenSize|smallestScreenSize|screenLayout|locale|navigation|fontScale|mcc|mnc|uiMode"
android:exported="true" />
android:name=".test.SystemActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".test.SimpleRvActivity"
android:configChanges="keyboard|orientation|keyboardHidden|screenSize|smallestScreenSize|screenLayout|locale|navigation|fontScale|mcc|mnc|uiMode"
android:exported="true" />
</application>

</manifest>
76 changes: 72 additions & 4 deletions app/src/main/java/com/petterp/floatingx/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import androidx.cardview.widget.CardView
import com.petterp.floatingx.FloatingX
import com.petterp.floatingx.app.simple.FxAnimationImpl
import com.petterp.floatingx.app.test.MultipleFxActivity
import com.petterp.floatingx.app.test.SystemActivity
import com.petterp.floatingx.util.createFx

class MainActivity : AppCompatActivity() {
Expand All @@ -19,7 +20,7 @@ class MainActivity : AppCompatActivity() {

private val activityFx by createFx {
setLayout(R.layout.item_floating)
setEnableLog(true, "act")
setEnableLog(true, "activityFx")
build().toControl(this@MainActivity)
}

Expand All @@ -38,7 +39,7 @@ class MainActivity : AppCompatActivity() {
addNestedScrollView {
addLinearLayout {
addItemView("显示全局悬浮窗") {
FloatingX.control(MultipleFxActivity.TAG_1).show(this@MainActivity)
FloatingX.control(MultipleFxActivity.TAG_1).show()
}
addItemView("隐藏全局悬浮窗") {
FloatingX.control(MultipleFxActivity.TAG_1).hide()
Expand All @@ -49,7 +50,7 @@ class MainActivity : AppCompatActivity() {
this.updateViewContent {
it.setText(R.id.tvItemFx, "App")
}
}.show(this@MainActivity)
}.show()
}
addItemView("更新当前[全局浮窗]内容-(传递view方式)") {
FloatingX.control(MultipleFxActivity.TAG_1).apply {
Expand All @@ -62,7 +63,7 @@ class MainActivity : AppCompatActivity() {
setBackgroundColor(Color.GRAY)
}
}
show(this@MainActivity)
show()
}
}
addItemView("显示一个Activity悬浮窗-(展示与多指触摸)") {
Expand All @@ -80,9 +81,43 @@ class MainActivity : AppCompatActivity() {
.setCardBackgroundColor(Color.GREEN)
}
}
addItemView("显示windows级别悬浮窗") {
// val config =
// FxAppHelper.builder().setLayout(R.layout.item_floating)
// .setEnableLog(true, "windows")
// .setContext(applicationContext).build()
// val layoutParam = WindowManager.LayoutParams().apply {
// // 设置大小 自适应
// width = WindowManager.LayoutParams.WRAP_CONTENT
// height = WindowManager.LayoutParams.WRAP_CONTENT
// format = PixelFormat.TRANSPARENT
// /**
// * 注意,flag的值可以为:
// * 下面的flags属性的效果形同“锁定”。
// * 悬浮窗不可触摸,不接受任何事件,同时不影响后面的事件响应。
// * LayoutParams.FLAG_NOT_TOUCH_MODAL 不影响后面的事件
// * LayoutParams.FLAG_NOT_FOCUSABLE 不可聚焦
// * LayoutParams.FLAG_NOT_TOUCHABLE 不可触摸
// */
// flags =
// WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
// type = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
// } else {
// WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
// }
// }
// val fx = FxManagerView(applicationContext).init(config)
// fx.windowManager = windowManager
// windowManager.addView(fx, layoutParam)
windowManager
}
addItemView("进入测试页面") {
TestActivity::class.java.start(this@MainActivity)
}
addItemView("进入system浮窗测试页面") {
SystemActivity::class.java.start(this@MainActivity)
}
}
}
}
Expand All @@ -105,3 +140,36 @@ class MainActivity : AppCompatActivity() {
return viewGroup
}
}

// class ItemViewTouchListener(val wl: WindowManager.LayoutParams, val windowManager: WindowManager) :
// View.OnTouchListener {
// private var x = 0
// private var y = 0
// override fun onTouch(view: View, motionEvent: MotionEvent): Boolean {
// when (motionEvent.action) {
// MotionEvent.ACTION_DOWN -> {
// x = motionEvent.rawX.toInt()
// y = motionEvent.rawY.toInt()
// }
//
// MotionEvent.ACTION_MOVE -> {
// val nowX = motionEvent.rawX.toInt()
// val nowY = motionEvent.rawY.toInt()
// val movedX = nowX - x
// val movedY = nowY - y
// x = nowX
// y = nowY
// wl.apply {
// x += movedX
// y += movedY
// }
// // 更新悬浮球控件位置
// windowManager?.updateViewLayout(view, wl)
// }
//
// else -> {
// }
// }
// return false
// }
// }
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import com.petterp.floatingx.app.simple.FxAnimationImpl;
import com.petterp.floatingx.app.simple.FxConfigStorageToSpImpl;
import com.petterp.floatingx.assist.FxDisplayMode;
import com.petterp.floatingx.assist.helper.AppHelper;
import com.petterp.floatingx.assist.helper.ScopeHelper;
import com.petterp.floatingx.impl.lifecycle.FxTagActivityLifecycleImpl;
import com.petterp.floatingx.assist.helper.FxAppHelper;
import com.petterp.floatingx.assist.helper.FxScopeHelper;
import com.petterp.floatingx.impl.lifecycle.FxProxyTagLifecycleImp;

/**
* java 中的配置示例
Expand All @@ -25,7 +25,7 @@ public class CustomJavaApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
AppHelper helper = AppHelper.builder()
FxAppHelper helper = FxAppHelper.builder()
.setLayout(R.layout.item_floating)
// 设置启用日志,tag可以自定义,最终显示为FloatingX-xxx
.setEnableLog(true, "自定义的tag")
Expand Down Expand Up @@ -69,7 +69,7 @@ public void onCreate() {
// 设置浮窗展示类型,默认可移动可点击,无需配置
.setDisplayMode(FxDisplayMode.Normal)
//启用悬浮窗,即默认会插入到允许的activity中
.setTagActivityLifecycle(new FxTagActivityLifecycleImpl() {
.setTagActivityLifecycle(new FxProxyTagLifecycleImp() {
@Override
public void onCreated(@NonNull Activity activity, @Nullable Bundle bundle) {
// 允许插入的浮窗activity执行到onCreated时会回调相应方法
Expand All @@ -85,7 +85,7 @@ public void onCreated(@NonNull Activity activity, @Nullable Bundle bundle) {
* 创建一个局部悬浮窗
*/
public void createScopeFxSimple(Activity activity) {
ScopeHelper.builder()
FxScopeHelper.builder()
.setLayout(R.layout.item_floating)
.build()
.toControl(activity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.app.Activity
import android.app.Application
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.view.MotionEvent
import android.view.View
Expand All @@ -20,9 +19,10 @@ import com.petterp.floatingx.app.test.BlackActivity
import com.petterp.floatingx.app.test.MultipleFxActivity
import com.petterp.floatingx.assist.FxDisplayMode
import com.petterp.floatingx.assist.FxGravity
import com.petterp.floatingx.impl.FxScrollImpl
import com.petterp.floatingx.impl.lifecycle.FxTagActivityLifecycleImpl
import com.petterp.floatingx.assist.FxScopeType
import com.petterp.floatingx.impl.lifecycle.FxProxyTagLifecycleImp
import com.petterp.floatingx.listener.IFxViewLifecycle
import com.petterp.floatingx.util.FxScrollImpl

/** Kotlin-Application */
class CustomKtApplication : Application() {
Expand All @@ -44,13 +44,17 @@ class CustomKtApplication : Application() {
// }

installTag1(this)
installTag2(this)
}

companion object {

fun installTag1(context: Application) {
FloatingX.install {
setContext(context)
setSystemScope(FxScopeType.SYSTEM)
// 设置浮窗展示类型,默认可移动可点击,无需配置
setDisplayMode(FxDisplayMode.Normal)
setLayout(R.layout.item_floating)
// 传递自定义的View
// setLayoutView(
Expand All @@ -63,9 +67,9 @@ class CustomKtApplication : Application() {
// )

// 设置悬浮窗默认方向
setGravity(FxGravity.RIGHT_OR_BOTTOM)
setGravity(FxGravity.RIGHT_OR_TOP)
// 启用辅助方向,具体参加方法注释
setEnableAssistDirection(r = 100f)
setEnableAssistDirection(r = 100f, t = 100f)
// 设置启用边缘吸附,默认启用
setEnableEdgeAdsorption(true)
// 设置边缘偏移量
Expand Down Expand Up @@ -98,7 +102,7 @@ class CustomKtApplication : Application() {
Toast.makeText(context, "浮窗被点击", Toast.LENGTH_SHORT).show()
}
// 设置tag-Activity生命周期回调时的触发
setTagActivityLifecycle(object : FxTagActivityLifecycleImpl() {
setTagActivityLifecycle(object : FxProxyTagLifecycleImp() {
override fun onCreated(activity: Activity, bundle: Bundle?) {
// 允许插入的浮窗activity执行到onCreated时会回调相应方法
}
Expand All @@ -112,50 +116,46 @@ class CustomKtApplication : Application() {
setScrollListener(object : FxScrollImpl() {
override fun down() {
// 按下
Log.e("petterp", "down")
}

override fun up() {
// 释放
Log.e("petterp", "up")
}

override fun dragIng(event: MotionEvent, x: Float, y: Float) {
// 正在拖动
// Log.e("petterp", "dragIng-$x,$y")
}

override fun eventIng(event: MotionEvent) {
// 接收所有事件传递
Log.e("petterp", "eventIng,${event.x},${event.y}")
}
})
// 设置浮窗展示类型,默认可移动可点击,无需配置
setDisplayMode(FxDisplayMode.Normal)
// 设置是否启用日志
setEnableLog(BuildConfig.DEBUG)
// 设置浮窗tag
setTag(MultipleFxActivity.TAG_1)
// 只有调用了enableFx,默认才会启用fx,否则fx不会自动插入activity
// ps: 这里的只有调用了enableFx仅仅只是配置工具层的标记,后续使用control.show()也会默认启用
enableFx()
}
}.show()
}

fun installTag2(context: Application) {
FloatingX.install {
setContext(context)
setSystemScope(FxScopeType.APP_ACTIVITY)
setLayoutView(
CardView(context).apply {
setCardBackgroundColor(Color.GRAY)
radius = 30.dpF
addView(
TextView(this.context).apply {
layoutParams = ViewGroup.LayoutParams(
60.dp,
-2,
60.dp,
)
gravity = Gravity.CENTER
text = "浮窗2"
text = "浮窗2-act"
setTextColor(Color.WHITE)
textSize = 15f
},
Expand All @@ -164,6 +164,8 @@ class CustomKtApplication : Application() {
)
setTag(MultipleFxActivity.TAG_2)
setEnableLog(true)
setEdgeOffset(20f)
setEnableEdgeAdsorption(true)
enableFx()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class BlackActivity : AppCompatActivity() {
textSize = 50f
}
addItemView("显示全局悬浮窗") {
FloatingX.control(MultipleFxActivity.TAG_1).show(this@BlackActivity)
FloatingX.control(MultipleFxActivity.TAG_1).show()
}
}
}
Expand Down
Loading
Loading