Skip to content

Commit

Permalink
Add broadcast start freeform
Browse files Browse the repository at this point in the history
  • Loading branch information
sunshine0523 committed Sep 19, 2023
1 parent cb483ed commit 7977a3b
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 7 deletions.
2 changes: 2 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ dependencies {
implementation(libs.room.runtime)
implementation(platform(libs.compose.bom))
implementation(platform(libs.compose.bom))
implementation(libs.hiddenapirefineruntime)
androidTestImplementation(platform(libs.compose.bom))
androidTestImplementation(platform(libs.compose.bom))
ksp(libs.room.compiler)
Expand All @@ -68,6 +69,7 @@ dependencies {
androidTestImplementation(platform(libs.compose.bom))

compileOnly(files("libs/XposedBridgeAPI-89.jar"))
compileOnly(projects.hiddenApi)

testImplementation(libs.junit)
androidTestImplementation(libs.androidx.test.ext.junit)
Expand Down
25 changes: 23 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
android:name="android.permission.QUERY_ALL_PACKAGES"
tools:ignore="QueryAllPackagesPermission" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<application
android:name=".MiFreeform"
Expand Down Expand Up @@ -69,11 +70,31 @@
<action android:name="android.service.quicksettings.action.QS_TILE" />
</intent-filter>
</service>

<service
android:name=".service.FloatingService"
android:enabled="true"
android:exported="false"/>
android:exported="false" />
<service
android:name=".service.SidebarService"
android:enabled="true"
android:exported="false" />

<receiver
android:name=".receiver.BootReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />

<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
<receiver
android:name=".receiver.StartFreeformReceiver"
android:exported="true">
<intent-filter>
<action android:name="com.sunshine.freeform.start_freeform" />
</intent-filter>
</receiver>

<meta-data
android:name="xposedmodule"
Expand Down
22 changes: 22 additions & 0 deletions app/src/main/java/com/sunshine/freeform/receiver/BootReceiver.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.sunshine.freeform.receiver

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.sunshine.freeform.MiFreeform
import com.sunshine.freeform.service.SidebarService

/**
* @author KindBrave
* @since 2023/9/19
*/
class BootReceiver : BroadcastReceiver() {
companion object {
private const val BOOT = "android.intent.action.BOOT_COMPLETED"
}
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == BOOT) {
context.startService(Intent(context, SidebarService::class.java))
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.sunshine.freeform.receiver

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.sunshine.freeform.MiFreeform
import com.sunshine.freeform.MiFreeformServiceManager
import kotlin.math.roundToInt

/**
* @author KindBrave
* @since 2023/9/19
*/
class StartFreeformReceiver : BroadcastReceiver() {
companion object {
private const val ACTION = "com.sunshine.freeform.start_freeform"
}
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == ACTION) {
val packageName = intent.getStringExtra("packageName")
val activityName = intent.getStringExtra("activityName")
val userId = intent.getIntExtra("userId", 0)

if (packageName != null && activityName != null) {
val sp = context.getSharedPreferences(MiFreeform.CONFIG, Context.MODE_PRIVATE)
val screenWidth = context.resources.displayMetrics.widthPixels
val screenHeight = context.resources.displayMetrics.heightPixels
val screenDensityDpi = context.resources.displayMetrics.densityDpi
MiFreeformServiceManager.createWindow(
packageName,
activityName,
userId,
sp.getInt("freeform_width", (screenWidth * 0.8).roundToInt()),
sp.getInt("freeform_height", (screenHeight * 0.5).roundToInt()),
sp.getInt("freeform_dpi", screenDensityDpi),
)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ class ServiceViewModel(private val application: Application): AndroidViewModel(a
return sp.getInt(name, defaultValue)
}

fun getBooleanSp(name: String, defaultValue: Boolean): Boolean {
return sp.getBoolean(name, defaultValue)
}

private inner class AppComparable : Comparator<AppInfo> {
override fun compare(p0: AppInfo, p1: AppInfo): Int {
return Collator.getInstance(Locale.CHINESE).compare(p0.label, p1.label)
Expand Down
48 changes: 48 additions & 0 deletions app/src/main/java/com/sunshine/freeform/service/SidebarService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.sunshine.freeform.service

import android.app.Service
import android.content.Context
import android.content.Intent
import android.graphics.PixelFormat
import android.os.IBinder
import android.view.WindowManager
import android.view.WindowManagerHidden

class SidebarService : Service() {

private lateinit var viewModel: ServiceViewModel
private val layoutParams = WindowManagerHidden.LayoutParams()
private lateinit var windowManager: WindowManager

override fun onBind(intent: Intent): IBinder? {
return null
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
viewModel = ServiceViewModel(application)
windowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager
if (viewModel.getBooleanSp("sideline", false)) initSideLine()
return START_STICKY
}

/**
* 启动侧边条
*/
private fun initSideLine() {
// val screenWidth = resources.displayMetrics.widthPixels
// val isLeft = viewModel.getBooleanSp()
// layoutParams.apply {
// type = 2026
// width = 100
// height = screenHeight / 5
// x = -screenWidth / 2
// y = -screenHeight / 6
// flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or
// WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or
// WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS or
// WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED
// 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
// format = PixelFormat.RGBA_8888
// }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@ import android.os.Handler
import android.view.Display
import android.view.IRotationWatcher
import android.view.View
import android.view.WindowInsetsHidden
import android.view.WindowManager
import android.view.WindowManagerHidden
import io.sunshine0523.freeform.service.SystemServiceHolder
import io.sunshine0523.freeform.util.MLog
import java.lang.reflect.Field

/**
* @author KindBrave
* @since 2023/8/22
Expand Down Expand Up @@ -54,7 +51,7 @@ class SideBarWindow(
rightView.setBackgroundColor(Color.TRANSPARENT)
SideBarTouchListener(this)
leftWindowParams.apply {
type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
type = 2026
width = 100
height = screenHeight / 5
x = -screenWidth / 2
Expand All @@ -67,7 +64,7 @@ class SideBarWindow(
format = PixelFormat.RGBA_8888
}
rightWindowParams.apply {
type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
type = 2026
width = 100
height = screenHeight / 5
x = screenWidth / 2
Expand Down

0 comments on commit 7977a3b

Please sign in to comment.