Skip to content

Commit

Permalink
Integrate shizuku[3]
Browse files Browse the repository at this point in the history
  • Loading branch information
Tornaco committed Jun 16, 2024
1 parent f5d5110 commit 6c991fe
Show file tree
Hide file tree
Showing 11 changed files with 337 additions and 76 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_select_activate_method">你想用哪种激活方式使用Thanox?</string>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_select_activate_method">Which activation method do you prefer?</string>
</resources>
7 changes: 3 additions & 4 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@

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

<meta-data
android:name="android.app.shortcuts"
android:resource="@xml/shortcuts" />
</activity>

<activity
Expand Down Expand Up @@ -260,6 +256,9 @@
<activity
android:name="now.fortuitous.thanos.launchother.LaunchOtherAppRuleActivity"
android:exported="false" />
<activity
android:name="now.fortuitous.thanos.main.ChooserActivity"
android:exported="false" />

<meta-data
android:name="xposedmodule"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* (C) Copyright 2022 Thanox
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package now.fortuitous.thanos.main

import android.content.Context
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import dagger.hilt.android.AndroidEntryPoint
import github.tornaco.android.thanos.module.compose.common.ComposeThemeActivity
import github.tornaco.android.thanos.util.ActivityUtils
import now.fortuitous.thanos.pref.AppPreference
import tornaco.apps.thanox.base.ui.LargeSpacer
import tornaco.apps.thanox.base.ui.NiaGradientBackground
import tornaco.apps.thanox.base.ui.theme.ThanosTheme

@AndroidEntryPoint
class ChooserActivity : ComposeThemeActivity() {

object Starter {
@JvmStatic
fun start(context: Context?) {
ActivityUtils.startActivity(context, ChooserActivity::class.java)
}
}

override fun isF(): Boolean {
return false
}

@Composable
override fun Content() {
AppChooser()
}

@Composable
private fun AppChooser() {
val context = LocalContext.current
ThanosTheme {
NiaGradientBackground(modifier = Modifier.fillMaxSize()) {
Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(
modifier = Modifier.fillMaxWidth(),
textAlign = TextAlign.Center,
text = stringResource(id = github.tornaco.android.thanos.res.R.string.app_select_activate_method),
style = MaterialTheme.typography.headlineSmall
)
repeat(4) {
LargeSpacer()
}

Button(modifier = Modifier.fillMaxWidth(0.68f), onClick = {
AppPreference.setAppType(context, "thanos")
NavActivity.Starter.start(context)
finish()
}) {
Text(text = "Shizuku")
}
LargeSpacer()
Button(modifier = Modifier.fillMaxWidth(0.68f), onClick = {
AppPreference.setAppType(context, "thanox")
NavActivity.Starter.start(context)
finish()
}) {
Text(text = "Xposed/Magisk")
}
}
}
}
}
}
101 changes: 96 additions & 5 deletions android/app/src/main/java/now/fortuitous/thanos/main/NavActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,25 @@
package now.fortuitous.thanos.main

import android.content.Context
import android.os.Build
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.preference.PreferenceManager
import dagger.hilt.android.AndroidEntryPoint
import github.tornaco.android.thanos.core.app.ThanosManager
import github.tornaco.android.thanos.core.shortcutManager
import github.tornaco.android.thanos.main.blockOnCreate
import github.tornaco.android.thanos.module.compose.common.ComposeThemeActivity
import github.tornaco.android.thanos.util.ActivityUtils
import now.fortuitous.thanos.onboarding.OnBoardingActivity
import now.fortuitous.thanos.pref.AppPreference
import tornaco.apps.thanox.MainGraph
import tornaco.apps.thanox.base.ui.theme.ThanosTheme
import tornaco.apps.thanox.subscribe.ThanosApp

@AndroidEntryPoint
class NavActivity : ComposeThemeActivity() {
Expand All @@ -41,19 +54,97 @@ class NavActivity : ComposeThemeActivity() {

@Composable
override fun Content() {
// Block
if (blockOnCreate(thisActivity())) {
// OnBoarding
if (!AppPreference.hasOnBoarding(thisActivity())) {
OnBoardingActivity.Starter.start(thisActivity())
finish()
return
}

// OnBoarding
if (!now.fortuitous.thanos.pref.AppPreference.hasOnBoarding(thisActivity())) {
OnBoardingActivity.Starter.start(thisActivity())
// Check if x is available
if (ThanosManager.from(thisActivity()).isServiceInstalled) {
AppPreference.setAppType(thisActivity(), "ask")
Thanox()
return
}

val sx = AppPreference.getAppType(thisActivity())
when (sx) {
"thanox" -> {
Thanox()
}

"thanos" -> {
Thanos()
}

else -> {
ChooserActivity.Starter.start(thisActivity())
finish()
}
}
}

@Composable
private fun Thanox() {
// Block
if (blockOnCreate(thisActivity())) {
finish()
return
}

LaunchedEffect(Unit) {
ShortcutInit(thisActivity()).initOnBootThanox()
}

LaunchedEffect(Unit) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
thisActivity().shortcutManager.disableShortcuts(
listOf(
"thanos_shortcut_smart_freeze",
"thanos_shortcut_process_manage"
)
)
thisActivity().shortcutManager.enableShortcuts(
listOf(
"shortcut_smart_freeze",
"shortcut_process_manage"
)
)
}
}

NavScreen()
}

@Composable
private fun Thanos() {
LaunchedEffect(Unit) {
ShortcutInit(thisActivity()).initOnBootThanos()
}

ThanosApp {
ThanosTheme {
var privacyAgreementAccept by remember {
mutableStateOf(false)
}
LaunchedEffect(Unit) {
privacyAgreementAccept =
PreferenceManager.getDefaultSharedPreferences(thisActivity())
.getBoolean(privacyAgreementKey, false)
}

if (!privacyAgreementAccept) {
PrivacyStatementDialog(onDismissRequest = {
PreferenceManager.getDefaultSharedPreferences(thisActivity()).edit()
.putBoolean(privacyAgreementKey, true).apply()
privacyAgreementAccept = true
})
}


MainGraph()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ private fun FrameworkErrorDialog(
}

@Composable
private fun PrivacyStatementDialog(
fun PrivacyStatementDialog(
onDismissRequest: () -> Unit,
) {
val context = LocalContext.current
Expand Down
112 changes: 112 additions & 0 deletions android/app/src/main/java/now/fortuitous/thanos/main/ShortcutInit.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package now.fortuitous.thanos.main

import android.content.Context
import android.content.Intent
import androidx.core.content.pm.ShortcutInfoCompat
import androidx.core.content.pm.ShortcutManagerCompat
import androidx.core.graphics.drawable.IconCompat
import androidx.core.net.toUri
import github.tornaco.android.thanos.R
import now.fortuitous.thanos.power.SmartFreezeActivity
import now.fortuitous.thanos.process.v2.ProcessManageActivityV2Delegate
import tornaco.apps.thanox.ThanosMainActivity

class ShortcutInit(val context: Context) {
companion object {
const val ID_THANOX_PM = "thanox_pm"
const val ID_THANOX_SF = "thanox_sf"
const val ID_THANOS_PM = "thanos_pm"
const val ID_THANOS_SF = "thanos_sf"
}

private fun installForThanox() {
if (!hasDynamicShortcut(ID_THANOX_SF)) {
addDynamicShortcut(
ID_THANOX_SF,
SmartFreezeActivity::class.java,
R.drawable.shortcut_smart_freeze,
context.getString(R.string.feature_title_smart_app_freeze)
)
}
if (!hasDynamicShortcut(ID_THANOX_PM)) {
addDynamicShortcut(
ID_THANOX_PM,
ProcessManageActivityV2Delegate::class.java,
R.drawable.ic_shortcut_process_manage,
context.getString(R.string.feature_title_process_manage)
)
}
}

private fun installForThanos() {
if (!hasDynamicShortcut(ID_THANOS_SF)) {
addDynamicShortcut(
ID_THANOS_SF,
ThanosMainActivity::class.java,
R.drawable.shortcut_smart_freeze,
context.getString(R.string.feature_title_smart_app_freeze),
"SF"
)
}

if (!hasDynamicShortcut(ID_THANOS_PM)) {
addDynamicShortcut(
ID_THANOS_PM,
ThanosMainActivity::class.java,
R.drawable.ic_shortcut_process_manage,
context.getString(R.string.feature_title_process_manage),
"PM"
)
}
}

fun initOnBootThanos() {
installForThanos()
removeDynamicShortcuts(listOf(ID_THANOX_PM, ID_THANOX_SF))
}

fun initOnBootThanox() {
installForThanox()
removeDynamicShortcuts(listOf(ID_THANOS_PM, ID_THANOS_SF))
}

fun addDynamicShortcut(
id: String,
clazz: Class<*>,
iconRes: Int,
title: String,
data: String? = null
): Boolean {
val shortcutInfoIntent = Intent(context, clazz).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
data?.let {
setData(it.toUri())
}
}
shortcutInfoIntent.action = Intent.ACTION_VIEW

val icon = IconCompat.createWithResource(
context,
iconRes
)

val shortcutInfo = ShortcutInfoCompat.Builder(context, id)
.setShortLabel(title)
.setLongLabel(title)
.setIcon(icon)
.setIntent(shortcutInfoIntent)
.build()

return ShortcutManagerCompat.pushDynamicShortcut(context, shortcutInfo)
}

fun removeDynamicShortcuts(id: List<String>) {
ShortcutManagerCompat.removeDynamicShortcuts(context, id)
}

fun hasDynamicShortcut(id: String): Boolean {
return ShortcutManagerCompat.getDynamicShortcuts(context).any {
it.id == id
}
}
}
Loading

0 comments on commit 6c991fe

Please sign in to comment.