Skip to content

Commit 7977a3b

Browse files
committed
Add broadcast start freeform
1 parent cb483ed commit 7977a3b

7 files changed

Lines changed: 141 additions & 7 deletions

File tree

app/build.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ dependencies {
5454
implementation(libs.room.runtime)
5555
implementation(platform(libs.compose.bom))
5656
implementation(platform(libs.compose.bom))
57+
implementation(libs.hiddenapirefineruntime)
5758
androidTestImplementation(platform(libs.compose.bom))
5859
androidTestImplementation(platform(libs.compose.bom))
5960
ksp(libs.room.compiler)
@@ -68,6 +69,7 @@ dependencies {
6869
androidTestImplementation(platform(libs.compose.bom))
6970

7071
compileOnly(files("libs/XposedBridgeAPI-89.jar"))
72+
compileOnly(projects.hiddenApi)
7173

7274
testImplementation(libs.junit)
7375
androidTestImplementation(libs.androidx.test.ext.junit)

app/src/main/AndroidManifest.xml

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
android:name="android.permission.QUERY_ALL_PACKAGES"
77
tools:ignore="QueryAllPackagesPermission" />
88
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
9+
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
910

1011
<application
1112
android:name=".MiFreeform"
@@ -69,11 +70,31 @@
6970
<action android:name="android.service.quicksettings.action.QS_TILE" />
7071
</intent-filter>
7172
</service>
72-
7373
<service
7474
android:name=".service.FloatingService"
7575
android:enabled="true"
76-
android:exported="false"/>
76+
android:exported="false" />
77+
<service
78+
android:name=".service.SidebarService"
79+
android:enabled="true"
80+
android:exported="false" />
81+
82+
<receiver
83+
android:name=".receiver.BootReceiver"
84+
android:exported="true">
85+
<intent-filter>
86+
<action android:name="android.intent.action.BOOT_COMPLETED" />
87+
88+
<category android:name="android.intent.category.HOME" />
89+
</intent-filter>
90+
</receiver>
91+
<receiver
92+
android:name=".receiver.StartFreeformReceiver"
93+
android:exported="true">
94+
<intent-filter>
95+
<action android:name="com.sunshine.freeform.start_freeform" />
96+
</intent-filter>
97+
</receiver>
7798

7899
<meta-data
79100
android:name="xposedmodule"
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.sunshine.freeform.receiver
2+
3+
import android.content.BroadcastReceiver
4+
import android.content.Context
5+
import android.content.Intent
6+
import com.sunshine.freeform.MiFreeform
7+
import com.sunshine.freeform.service.SidebarService
8+
9+
/**
10+
* @author KindBrave
11+
* @since 2023/9/19
12+
*/
13+
class BootReceiver : BroadcastReceiver() {
14+
companion object {
15+
private const val BOOT = "android.intent.action.BOOT_COMPLETED"
16+
}
17+
override fun onReceive(context: Context, intent: Intent) {
18+
if (intent.action == BOOT) {
19+
context.startService(Intent(context, SidebarService::class.java))
20+
}
21+
}
22+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.sunshine.freeform.receiver
2+
3+
import android.content.BroadcastReceiver
4+
import android.content.Context
5+
import android.content.Intent
6+
import com.sunshine.freeform.MiFreeform
7+
import com.sunshine.freeform.MiFreeformServiceManager
8+
import kotlin.math.roundToInt
9+
10+
/**
11+
* @author KindBrave
12+
* @since 2023/9/19
13+
*/
14+
class StartFreeformReceiver : BroadcastReceiver() {
15+
companion object {
16+
private const val ACTION = "com.sunshine.freeform.start_freeform"
17+
}
18+
override fun onReceive(context: Context, intent: Intent) {
19+
if (intent.action == ACTION) {
20+
val packageName = intent.getStringExtra("packageName")
21+
val activityName = intent.getStringExtra("activityName")
22+
val userId = intent.getIntExtra("userId", 0)
23+
24+
if (packageName != null && activityName != null) {
25+
val sp = context.getSharedPreferences(MiFreeform.CONFIG, Context.MODE_PRIVATE)
26+
val screenWidth = context.resources.displayMetrics.widthPixels
27+
val screenHeight = context.resources.displayMetrics.heightPixels
28+
val screenDensityDpi = context.resources.displayMetrics.densityDpi
29+
MiFreeformServiceManager.createWindow(
30+
packageName,
31+
activityName,
32+
userId,
33+
sp.getInt("freeform_width", (screenWidth * 0.8).roundToInt()),
34+
sp.getInt("freeform_height", (screenHeight * 0.5).roundToInt()),
35+
sp.getInt("freeform_dpi", screenDensityDpi),
36+
)
37+
}
38+
}
39+
}
40+
}

app/src/main/java/com/sunshine/freeform/service/ServiceViewModel.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ class ServiceViewModel(private val application: Application): AndroidViewModel(a
124124
return sp.getInt(name, defaultValue)
125125
}
126126

127+
fun getBooleanSp(name: String, defaultValue: Boolean): Boolean {
128+
return sp.getBoolean(name, defaultValue)
129+
}
130+
127131
private inner class AppComparable : Comparator<AppInfo> {
128132
override fun compare(p0: AppInfo, p1: AppInfo): Int {
129133
return Collator.getInstance(Locale.CHINESE).compare(p0.label, p1.label)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.sunshine.freeform.service
2+
3+
import android.app.Service
4+
import android.content.Context
5+
import android.content.Intent
6+
import android.graphics.PixelFormat
7+
import android.os.IBinder
8+
import android.view.WindowManager
9+
import android.view.WindowManagerHidden
10+
11+
class SidebarService : Service() {
12+
13+
private lateinit var viewModel: ServiceViewModel
14+
private val layoutParams = WindowManagerHidden.LayoutParams()
15+
private lateinit var windowManager: WindowManager
16+
17+
override fun onBind(intent: Intent): IBinder? {
18+
return null
19+
}
20+
21+
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
22+
viewModel = ServiceViewModel(application)
23+
windowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager
24+
if (viewModel.getBooleanSp("sideline", false)) initSideLine()
25+
return START_STICKY
26+
}
27+
28+
/**
29+
* 启动侧边条
30+
*/
31+
private fun initSideLine() {
32+
// val screenWidth = resources.displayMetrics.widthPixels
33+
// val isLeft = viewModel.getBooleanSp()
34+
// layoutParams.apply {
35+
// type = 2026
36+
// width = 100
37+
// height = screenHeight / 5
38+
// x = -screenWidth / 2
39+
// y = -screenHeight / 6
40+
// flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or
41+
// WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or
42+
// WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS or
43+
// WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED
44+
// privateFlags = WindowManagerHidden.LayoutParams.SYSTEM_FLAG_SHOW_FOR_ALL_USERS or WindowManagerHidden.LayoutParams.PRIVATE_FLAG_IS_ROUNDED_CORNERS_OVERLAY or WindowManagerHidden.LayoutParams.PRIVATE_FLAG_USE_BLAST or WindowManagerHidden.LayoutParams.PRIVATE_FLAG_TRUSTED_OVERLAY
45+
// format = PixelFormat.RGBA_8888
46+
// }
47+
}
48+
}

freeform-server/src/main/java/io/sunshine0523/freeform/ui/sidebar/SideBarWindow.kt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,10 @@ import android.os.Handler
88
import android.view.Display
99
import android.view.IRotationWatcher
1010
import android.view.View
11-
import android.view.WindowInsetsHidden
1211
import android.view.WindowManager
1312
import android.view.WindowManagerHidden
1413
import io.sunshine0523.freeform.service.SystemServiceHolder
1514
import io.sunshine0523.freeform.util.MLog
16-
import java.lang.reflect.Field
17-
1815
/**
1916
* @author KindBrave
2017
* @since 2023/8/22
@@ -54,7 +51,7 @@ class SideBarWindow(
5451
rightView.setBackgroundColor(Color.TRANSPARENT)
5552
SideBarTouchListener(this)
5653
leftWindowParams.apply {
57-
type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
54+
type = 2026
5855
width = 100
5956
height = screenHeight / 5
6057
x = -screenWidth / 2
@@ -67,7 +64,7 @@ class SideBarWindow(
6764
format = PixelFormat.RGBA_8888
6865
}
6966
rightWindowParams.apply {
70-
type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
67+
type = 2026
7168
width = 100
7269
height = screenHeight / 5
7370
x = screenWidth / 2

0 commit comments

Comments
 (0)