Skip to content

Commit

Permalink
feat: support island (aistra0528#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-math authored Oct 18, 2023
1 parent e8d3a23 commit 4fad5de
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
android:name="android.permission.PACKAGE_USAGE_STATS"
tools:ignore="ProtectedPermissions" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<uses-permission android:name="com.oasisfeng.island.permission.FREEZE_PACKAGE"/>
<uses-permission android:name="com.oasisfeng.island.permission.SUSPEND_PACKAGE"/>

<application
android:name=".HailApp"
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/kotlin/com/aistra/hail/app/AppManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.aistra.hail.app
import android.content.Intent
import com.aistra.hail.BuildConfig
import com.aistra.hail.utils.HDhizuku
import com.aistra.hail.utils.HIsland
import com.aistra.hail.utils.HPackages
import com.aistra.hail.utils.HPolicy
import com.aistra.hail.utils.HShell
Expand Down Expand Up @@ -42,6 +43,8 @@ object AppManager {
HailData.MODE_SHIZUKU_DISABLE -> HShizuku.setAppDisabled(packageName, frozen)
HailData.MODE_SHIZUKU_HIDE -> HShizuku.setAppHidden(packageName, frozen)
HailData.MODE_SHIZUKU_SUSPEND -> HShizuku.setAppSuspended(packageName, frozen)
HailData.MODE_ISLAND_HIDE -> HIsland.setAppHidden(packageName, frozen)
HailData.MODE_ISLAND_SUSPEND -> HIsland.setAppSuspended(packageName, frozen)
else -> false
}

Expand Down
3 changes: 3 additions & 0 deletions app/src/main/kotlin/com/aistra/hail/app/HailData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ object HailData {
const val DHIZUKU = "dhizuku_"
const val SU = "su_"
const val SHIZUKU = "shizuku_"
const val ISLAND = "island_"
const val DISABLE = "disable"
const val HIDE = "hide"
const val SUSPEND = "suspend"
Expand All @@ -45,6 +46,8 @@ object HailData {
const val MODE_SHIZUKU_DISABLE = SHIZUKU + DISABLE
const val MODE_SHIZUKU_HIDE = SHIZUKU + HIDE
const val MODE_SHIZUKU_SUSPEND = SHIZUKU + SUSPEND
const val MODE_ISLAND_HIDE = ISLAND + HIDE
const val MODE_ISLAND_SUSPEND = ISLAND + SUSPEND
private const val TILE_ACTION = "tile_action"
const val DYNAMIC_SHORTCUT_ACTION = "dynamic_shortcut_action"
const val ACTION_NONE = "none"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.os.Bundle
import android.provider.Settings
import android.view.*
import android.widget.FrameLayout
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.content.res.AppCompatResources
import androidx.core.app.NotificationManagerCompat
import androidx.core.view.MenuHost
Expand Down Expand Up @@ -42,6 +43,7 @@ import rikka.shizuku.Shizuku

class SettingsFragment : PreferenceFragmentCompat(), Preference.OnPreferenceChangeListener,
MenuProvider {
private val requestPermissionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()) {}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View {
Expand Down Expand Up @@ -292,6 +294,27 @@ class SettingsFragment : PreferenceFragmentCompat(), Preference.OnPreferenceChan
HUI.showToast(R.string.shizuku_missing)
false
}

mode.startsWith(HailData.ISLAND) -> return runCatching{
when{
mode == HailData.MODE_ISLAND_HIDE && HIsland.freezePermissionGranted() -> true
mode == HailData.MODE_ISLAND_SUSPEND && HIsland.suspendPermissionGranted() -> true
else -> {
lifecycleScope.launch {
requestPermissionLauncher.launch(if(mode == HailData.MODE_ISLAND_HIDE)
HIsland.PERMISSION_FREEZE_PACKAGE
else
HIsland.PERMISSION_SUSPEND_PACKAGE
)
}
false
}
}
}.getOrElse {
HLog.e(it)
HUI.showToast(R.string.permission_denied)
false
}
}

return true
Expand Down
77 changes: 77 additions & 0 deletions app/src/main/kotlin/com/aistra/hail/utils/HIsland.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.aistra.hail.utils

import android.app.Activity
import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Handler
import android.os.HandlerThread
import androidx.core.content.ContextCompat
import com.aistra.hail.HailApp.Companion.app
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeout

object HIsland {
const val PERMISSION_FREEZE_PACKAGE = "com.oasisfeng.island.permission.FREEZE_PACKAGE"
const val PERMISSION_SUSPEND_PACKAGE = "com.oasisfeng.island.permission.SUSPEND_PACKAGE"
private const val ACTION_SUSPEND = "com.oasisfeng.island.action.SUSPEND"
private const val ACTION_UNSUSPEND = "com.oasisfeng.island.action.UNSUSPEND"
private const val ACTION_FREEZE = "com.oasisfeng.island.action.FREEZE"
private const val ACTION_UNFREEZE = "com.oasisfeng.island.action.UNFREEZE"
private const val EXTRA_CALLER_ID = "caller"

private val thread by lazy { HandlerThread("HIsland").apply { start() } }
private val handler by lazy { Handler(thread.looper) }

fun freezePermissionGranted(): Boolean {
return ContextCompat.checkSelfPermission(
app, PERMISSION_FREEZE_PACKAGE
) == PackageManager.PERMISSION_GRANTED
}

fun suspendPermissionGranted(): Boolean {
return ContextCompat.checkSelfPermission(
app, PERMISSION_SUSPEND_PACKAGE
) == PackageManager.PERMISSION_GRANTED
}

private fun setAppFrozen(packageName: String, action: String): Boolean {
val intent = Intent(action).apply {
data = Uri.fromParts("package", packageName, null)
setPackage("com.oasisfeng.island")
addFlags(Intent.FLAG_RECEIVER_FOREGROUND)
putExtra(
EXTRA_CALLER_ID,
PendingIntent.getActivity(app, 0, Intent(), PendingIntent.FLAG_IMMUTABLE)
)
}
val result = CompletableDeferred<Boolean>()
app.sendOrderedBroadcast(
intent, null, object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (resultCode != Activity.RESULT_OK) {
HLog.i("HIsland", resultData)
}
result.complete(resultCode == Activity.RESULT_OK)
}
}, handler, Activity.RESULT_OK, null, null
)
return runBlocking {
runCatching {
withTimeout(500L) {
result.await()
}
}.getOrElse { false }
}
}

fun setAppHidden(packageName: String, hidden: Boolean): Boolean =
HTarget.N && setAppFrozen(packageName, if (hidden) ACTION_FREEZE else ACTION_UNFREEZE)

fun setAppSuspended(packageName: String, suspended: Boolean): Boolean =
HTarget.N && setAppFrozen(packageName, if (suspended) ACTION_SUSPEND else ACTION_UNSUSPEND)
}
2 changes: 2 additions & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@
<string name="action_terminal">终端</string>
<string name="mode_dhizuku_hide">Dhizuku - 隐藏</string>
<string name="mode_dhizuku_suspend">Dhizuku - 暂停</string>
<string name="mode_island_hide">炼妖壶 - 隐藏</string>
<string name="mode_island_suspend">炼妖壶 - 暂停</string>
<string name="mode_su_hide">超级用户 - 隐藏</string>
<string name="action_fdroid">F-Droid</string>
<string name="action_skip">跳过</string>
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
<item>@string/mode_dhizuku_suspend</item>
<item>@string/mode_owner_hide</item>
<item>@string/mode_owner_suspend</item>
<item>@string/mode_island_hide</item>
<item>@string/mode_island_suspend</item>
</string-array>
<string-array name="working_mode_values">
<item>default</item>
Expand All @@ -54,6 +56,8 @@
<item>dhizuku_suspend</item>
<item>owner_hide</item>
<item>owner_suspend</item>
<item>island_hide</item>
<item>island_suspend</item>
</string-array>
<string-array name="donate_payment_entries">
<item>@string/donate_alipay</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,6 @@
<string name="mode_su_hide">Superuser - Hide</string>
<string name="action_fdroid">F-Droid</string>
<string name="action_skip">Skip</string>
<string name="mode_island_hide">Island - Hide</string>
<string name="mode_island_suspend">Island - Suspend</string>
</resources>

0 comments on commit 4fad5de

Please sign in to comment.