Skip to content

feature: support uri schema to exec api actions #310

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,22 @@ adb shell am start -a action -e key value

- `com.aistra.hail.action.LOCK_FREEZE`:冻结首页全部应用并锁定屏幕。无需`extra`。


或者通过 schema 执行API
```
hail://launch?package=xxx
hail://freeze?package=xxx
hail://unfreeze?package=xxx
hail://freeze_all
hail://unfreeze_all
hail://freeze_tag?tag=xxx
hail://unfreeze_tag?tag=xxx
hail://freeze_non_whitelisted
hail://freeze_auto
hail://lock
hail://lock_freeze
```

## 协助翻译

要将雹翻译成您的语言,或完善现有的翻译,请使用 [Weblate](https://hosted.weblate.org/engage/hail/)。
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@

<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>

<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="hail"/>
</intent-filter>
</activity>

<provider
Expand Down
56 changes: 56 additions & 0 deletions app/src/main/kotlin/com/aistra/hail/ui/api/ApiActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.aistra.hail.ui.api
import android.content.ActivityNotFoundException
import android.content.Intent
import android.content.pm.PackageManager.NameNotFoundException
import android.net.Uri
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
Expand Down Expand Up @@ -65,12 +66,67 @@ class ApiActivity : ComponentActivity() {
HailApi.ACTION_FREEZE_AUTO -> setAutoFreeze(false)
HailApi.ACTION_LOCK -> lockScreen(false)
HailApi.ACTION_LOCK_FREEZE -> lockScreen(true)
Intent.ACTION_VIEW -> handleSchema(intent.data)
else -> throw IllegalArgumentException("unknown action:\n${intent.action}")
}
finish()
}.onFailure(::setErrorDialog)
}

private fun requirePackageFromUri(uri: Uri): String{
return uri.getQueryParameter(HailData.KEY_PACKAGE)?.also {
HPackages.getApplicationInfoOrNull(it) ?: throw NameNotFoundException(getString(R.string.app_not_installed))
} ?: throw IllegalArgumentException("package must not be null")
}

private fun requireTagIdFromUri(uri: Uri): Int{
return uri.getQueryParameter(HailData.KEY_TAG)?.let {
HailData.tags.find { tag -> tag.first == it }?.second
?: throw IllegalStateException("tag unavailable:\n$it")
} ?: throw IllegalArgumentException("tag must not be null")
}

/**
* Handle schema actions
*
* hail://launch?package=xxx
* hail://freeze?package=xxx
* hail://unfreeze?package=xxx
* hail://freeze_all
* hail://unfreeze_all
* hail://freeze_tag?tag=xxx
* hail://unfreeze_tag?tag=xxx
* hail://freeze_non_whitelisted
* hail://freeze_auto
* hail://lock
* hail://lock_freeze
*/
private fun handleSchema(uri: Uri?) {
if (uri?.scheme != "hail"){
return
}
val action = uri.host
when (action) {
"show_app_info" -> {
val pkg = requirePackageFromUri(uri)
setContent { AppTheme { RedirectBottomSheet(pkg) } }
return
}

"launch" -> launchApp(requirePackageFromUri(uri), runCatching { requireTagIdFromUri(uri) }.getOrNull())
"freeze" -> setAppFrozen(requirePackageFromUri(uri), true)
"unfreeze" -> setAppFrozen(requirePackageFromUri(uri), false)
"freeze_tag" -> setListFrozen(true, HailData.checkedList.filter { it.tagId == requireTagIdFromUri(uri) }, true)
"unfreeze_tag" -> setListFrozen(false, HailData.checkedList.filter { it.tagId == requireTagIdFromUri(uri) })
"freeze_all" -> setListFrozen(true)
"unfreeze_all" -> setListFrozen(false)
"freeze_non_whitelisted" -> setListFrozen(true, skipWhitelisted = true)
"freeze_auto" -> setAutoFreeze(false)
"lock" -> lockScreen(false)
"lock_freeze" -> lockScreen(true)
}
}

private fun setErrorDialog(t: Throwable) = setContent { AppTheme { ErrorDialog(t) } }

@OptIn(ExperimentalMaterial3Api::class)
Expand Down