|
| 1 | +package com.setoskins.hook.activity |
| 2 | + |
| 3 | +import android.annotation.SuppressLint |
| 4 | +import android.os.Bundle |
| 5 | +import android.content.Context |
| 6 | +import android.content.Intent |
| 7 | +import android.content.SharedPreferences |
| 8 | +import android.os.Handler |
| 9 | +import android.os.Looper |
| 10 | +import android.util.Log |
| 11 | +import android.widget.Toast |
| 12 | +import com.setoskins.hook.activity.pages.MainPage |
| 13 | +import com.setoskins.hook.activity.pages.AboutPage |
| 14 | +import com.setoskins.hook.activity.pages.MenuPage |
| 15 | +import com.setoskins.hook.activity.pages.PowerkeeperPage |
| 16 | +import com.setoskins.hook.activity.pages.SoundPage |
| 17 | +import com.setoskins.hook.activity.pages.ThankList |
| 18 | +import com.setoskins.hook.utils.key.BackupUtils.CREATE_DOCUMENT_CODE |
| 19 | +import com.setoskins.hook.utils.key.BackupUtils.OPEN_DOCUMENT_CODE |
| 20 | +import com.setoskins.hook.utils.key.BackupUtils.handleCreateDocument |
| 21 | +import com.setoskins.hook.utils.key.BackupUtils.handleReadDocument |
| 22 | +import cn.fkj233.ui.activity.MIUIActivity |
| 23 | +import cn.fkj233.ui.dialog.MIUIDialog |
| 24 | +import com.setoskins.hook.BuildConfig |
| 25 | +import com.setoskins.hook.R |
| 26 | +import com.setoskins.hook.activity.pages.BmPage |
| 27 | +import com.setoskins.hook.activity.pages.OtherPage |
| 28 | +import com.setoskins.hook.activity.pages.SecuritycenterPage |
| 29 | +import com.setoskins.hook.activity.pages.UnlockSetting |
| 30 | +import java.io.IOException |
| 31 | + |
| 32 | + |
| 33 | +class MainActivity : MIUIActivity() { |
| 34 | + |
| 35 | + private lateinit var preferences: SharedPreferences |
| 36 | + private var showDialog = true // 初始化为 true,表示第一次打开应用时显示 |
| 37 | + companion object { |
| 38 | + @SuppressLint("StaticFieldLeak") |
| 39 | + lateinit var activity: MIUIActivity |
| 40 | + private val handler by lazy { Handler(Looper.getMainLooper()) } |
| 41 | + fun showToast(string: String) { |
| 42 | + Log.d("BlockMIUI", "Show Toast: $string") |
| 43 | + handler.post { |
| 44 | + Toast.makeText(activity, string, Toast.LENGTH_LONG).show() |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 50 | + |
| 51 | + |
| 52 | + super.onCreate(savedInstanceState) |
| 53 | + preferences = getSharedPreferences("MyAppPrefs", Context.MODE_PRIVATE) |
| 54 | + val showDialogFlag = preferences.getBoolean("showDialog", true) |
| 55 | + MIUIActivity.safeSP.putAny("SafeMode", "关闭") |
| 56 | + if (BuildConfig.DEBUG) { |
| 57 | + // Do something when running in debug mode |
| 58 | + } else { |
| 59 | + checkLSPosed() |
| 60 | + checkRootPermission() |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + if (showDialogFlag) { // 只在 showDialogFlag 为 true 时显示弹窗 |
| 66 | + abc() |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + private fun checkRootPermission() { |
| 73 | + try { |
| 74 | + val command = "su -c 'pm clear --cache-only com.miui.securitymanager'" |
| 75 | + val process = Runtime.getRuntime().exec(arrayOf("sh", "-c", command)) |
| 76 | + val exitCode = process.waitFor() |
| 77 | + |
| 78 | + if (exitCode == 0) { |
| 79 | + |
| 80 | + } else { |
| 81 | + // 执行失败 |
| 82 | + MIUIDialog(this) { |
| 83 | + setTitle(R.string.error) |
| 84 | + setMessage(R.string.root_warning) |
| 85 | + setCancelable(false) |
| 86 | + setRButton(R.string.done) { |
| 87 | + finishAffinity() |
| 88 | + } |
| 89 | + }.show() |
| 90 | + } |
| 91 | + } catch (e: IOException) { |
| 92 | + // 捕获 IO 异常 |
| 93 | + MIUIDialog(this) { |
| 94 | + setTitle(R.string.error) |
| 95 | + setMessage(R.string.root_warning) |
| 96 | + setCancelable(false) |
| 97 | + setRButton(R.string.done) { |
| 98 | + finishAffinity() |
| 99 | + } |
| 100 | + }.show() |
| 101 | + } |
| 102 | + } |
| 103 | + @SuppressLint("WorldReadableFiles") |
| 104 | + private fun checkLSPosed() { |
| 105 | + try { |
| 106 | + |
| 107 | + setSP(getSharedPreferences("config", MODE_WORLD_READABLE)) |
| 108 | + } catch (exception: SecurityException) { |
| 109 | + MIUIDialog(this) { |
| 110 | + setTitle(R.string.warning) |
| 111 | + setMessage(R.string.not_support) |
| 112 | + setCancelable(false) |
| 113 | + setRButton(R.string.done) { |
| 114 | + dismiss() |
| 115 | + } |
| 116 | + }.show() |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | + private fun abc() { |
| 124 | + showDialog = false // 设置为 false,确保之后不再显示弹窗 |
| 125 | + MIUIDialog(this) { |
| 126 | + |
| 127 | + setTitle(R.string.warning) |
| 128 | + setMessage(R.string.Splash_Tips) |
| 129 | + setCancelable(false) |
| 130 | + setLButton(R.string.Disagree) { |
| 131 | + System.exit(0) |
| 132 | + |
| 133 | + } |
| 134 | + setRButton(R.string.Agree) { |
| 135 | + preferences.edit().putBoolean("showDialog", false).apply() |
| 136 | + dismiss() |
| 137 | + val toast = Toast.makeText(activity, "记得把APP给予Root权限", Toast.LENGTH_LONG) |
| 138 | + toast.show() |
| 139 | + } |
| 140 | + }.show() |
| 141 | + } |
| 142 | + |
| 143 | + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { |
| 144 | + if (data != null && resultCode == RESULT_OK) { |
| 145 | + when (requestCode) { |
| 146 | + CREATE_DOCUMENT_CODE -> { |
| 147 | + handleCreateDocument(activity, data.data) |
| 148 | + } |
| 149 | + |
| 150 | + OPEN_DOCUMENT_CODE -> { |
| 151 | + handleReadDocument(activity, data.data) |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + init { |
| 158 | + activity = this |
| 159 | + registerPage(MainPage::class.java) |
| 160 | + registerPage(AboutPage::class.java) |
| 161 | + registerPage(ThankList::class.java) |
| 162 | + registerPage(SoundPage::class.java) |
| 163 | + registerPage(PowerkeeperPage::class.java) |
| 164 | + registerPage(MenuPage::class.java) |
| 165 | + registerPage(UnlockSetting::class.java) |
| 166 | + registerPage(SecuritycenterPage::class.java) |
| 167 | + registerPage(OtherPage::class.java) |
| 168 | + registerPage(BmPage::class.java) |
| 169 | + |
| 170 | + } |
| 171 | +} |
0 commit comments