Skip to content

Commit

Permalink
fix: support insular via island's open api (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-math authored Nov 21, 2023
1 parent 43e1725 commit 1ef752f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,10 @@ class SettingsFragment : PreferenceFragmentCompat(), Preference.OnPreferenceChan
HLog.e(it)
HUI.showToast(R.string.permission_denied)
false
}.also {
if (it) {
HIsland.checkOwnerApp()
}
}
}

Expand Down
26 changes: 25 additions & 1 deletion app/src/main/kotlin/com/aistra/hail/utils/HIsland.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ package com.aistra.hail.utils

import android.app.Activity
import android.app.PendingIntent
import android.app.admin.DevicePolicyManager
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 android.os.UserManager
import androidx.core.content.ContextCompat
import androidx.core.content.getSystemService
import com.aistra.hail.HailApp.Companion.app
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeout
import java.util.Optional

object HIsland {
const val PERMISSION_FREEZE_PACKAGE = "com.oasisfeng.island.permission.FREEZE_PACKAGE"
Expand All @@ -26,6 +30,23 @@ object HIsland {

private val thread by lazy { HandlerThread("HIsland").apply { start() } }
private val handler by lazy { Handler(thread.looper) }
private var ownerApp: Optional<String>? = null
get() {
field ?: checkOwnerApp()
return field!!
}

fun checkOwnerApp() {
val um = app.getSystemService<UserManager>()!!
val dpm = app.getSystemService<DevicePolicyManager>()!!
for (pkg in arrayOf("com.oasisfeng.island", "com.oasisfeng.island.fdroid")) {
if (dpm.isProfileOwnerApp(pkg) || (!um.isManagedProfile && dpm.isDeviceOwnerApp(pkg))) {
ownerApp = Optional.of(pkg)
return
}
}
ownerApp = Optional.empty()
}

fun freezePermissionGranted(): Boolean {
return ContextCompat.checkSelfPermission(
Expand All @@ -40,9 +61,12 @@ object HIsland {
}

private fun setAppFrozen(packageName: String, action: String): Boolean {
if (ownerApp!!.isEmpty) {
return false
}
val intent = Intent(action).apply {
data = Uri.fromParts("package", packageName, null)
setPackage("com.oasisfeng.island")
setPackage(ownerApp!!.get())
addFlags(Intent.FLAG_RECEIVER_FOREGROUND)
putExtra(
EXTRA_CALLER_ID,
Expand Down

0 comments on commit 1ef752f

Please sign in to comment.