Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
chr233 committed Aug 24, 2023
1 parent b3f9747 commit 83f303c
Show file tree
Hide file tree
Showing 14 changed files with 168 additions and 121 deletions.
1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion app/src/main/java/com/chrxw/purenga/Constant.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ package com.chrxw.purenga
object Constant {
const val NGA_PACKAGE_NAME = "gov.pianzong.androidnga"
const val NGA_MAIN_ACTIVITY_NAME = "com.donews.nga.activitys.MainActivity"
const val TAG = "PureNGA"
const val DNINFO = "dninfo"
const val REPO_URL="https://github.com/chr233/PureNGA"


/**
* 模块SharedPreferences名称
Expand Down
16 changes: 14 additions & 2 deletions app/src/main/java/com/chrxw/purenga/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.chrxw.purenga

import android.app.AlertDialog
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
Expand Down Expand Up @@ -40,15 +42,25 @@ class MainActivity : AppCompatActivity() {
}

override fun onPreferenceTreeClick(preference: Preference): Boolean {
if (preference.key == "hide_icon") {
val prefKey = preference.key

if (prefKey == "hide_icon") {
val ctx = context
if (ctx != null) {
hideAppIcon(ctx)
}
return true
} else if (prefKey == "plugin_setting") {
AlertDialog.Builder(activity).run {
setView(View.inflate(activity, R.layout.plugin_setting, null))
setNegativeButton("关闭", null)
show()
}
return true
}

val intent = when (preference.key) {

val intent = when (prefKey) {
"version" -> {
Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.github_url)))
}
Expand Down
12 changes: 10 additions & 2 deletions app/src/main/java/com/chrxw/purenga/XposedInit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.chrxw.purenga
import android.app.AndroidAppHelper
import android.app.Application
import android.app.Instrumentation
import android.content.pm.PackageInfo
import android.content.res.Resources
import android.content.res.XModuleResources
import com.chrxw.purenga.hook.IHook
Expand Down Expand Up @@ -54,10 +55,17 @@ class XposedInit : IXposedHookLoadPackage, IXposedHookZygoteInit {
if (Helper.init()) {
Hooks.initHooks(lpparam.classLoader)
if (!Helper.spPlugin.getBoolean(Constant.HIDE_HOOK_INFO, false)) {
Helper.toast("PureNGA 加载成功, 请到设置页面开启功能")
Helper.toast("PureNGA 加载成功, 请到【设置】>【PureNGA】开启功能")
}
} else {
Helper.toast("PureNGA 初始化失败,可能不支持当前版本 NGA: " + Helper.packageInfo.versionName)
val ngaVersion = try {
Helper.context.packageManager.getPackageInfo(
Constant.NGA_PACKAGE_NAME, PackageInfo.INSTALL_LOCATION_AUTO
).versionName
} catch (e: Throwable) {
"获取失败"
}
Helper.toast("PureNGA 初始化失败,可能不支持当前版本 NGA: $ngaVersion")
}
}
}
Expand Down
39 changes: 31 additions & 8 deletions app/src/main/java/com/chrxw/purenga/hook/AboutHook.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package com.chrxw.purenga.hook

import android.content.Intent
import android.content.pm.PackageInfo
import android.net.Uri
import android.view.View
import android.widget.Button
import android.widget.LinearLayout
import android.widget.TextView
import com.chrxw.purenga.BuildConfig
import com.chrxw.purenga.Constant
import com.chrxw.purenga.utils.Helper
import de.robv.android.xposed.XC_MethodHook
import de.robv.android.xposed.XposedHelpers
Expand All @@ -30,16 +35,34 @@ class AboutHook : IHook {
XposedHelpers.findAndHookMethod(clsAboutUsActivity, "initLayout", object : XC_MethodHook() {
override fun afterHookedMethod(param: MethodHookParam) {
val viewBinding = XposedHelpers.callMethod(param.thisObject, "getViewBinding")
val textView = XposedHelpers.getObjectField(viewBinding, "h") as TextView
val newText =
textView.text.toString() + " + PureNGA:" + BuildConfig.VERSION_NAME + " (点此检查插件更新)"
textView.text = newText
textView.setOnClickListener {
val intent = Intent(
Intent.ACTION_VIEW, Uri.parse("https://github.com/chr233/PureNGA")
val root = XposedHelpers.callMethod(viewBinding, "getRoot") as View
val viewId = Helper.getRId("tv_app_version")
val textView = root.findViewById<TextView>(viewId)

val pluginVersion = BuildConfig.VERSION_NAME
val ngaVersion = try {
Helper.context.packageManager.getPackageInfo(
Constant.NGA_PACKAGE_NAME, PackageInfo.INSTALL_LOCATION_AUTO
).versionName
} catch (e: Throwable) {
"获取失败"
}

textView.text =
"NGA 版本: $ngaVersion" + System.lineSeparator() + "PureNGA 版本: $pluginVersion"

val linearLayout = textView.parent as LinearLayout
val btn = Button(root.context)
btn.text = "检查插件更新"
btn.setOnClickListener {
root.context.startActivity(
Intent(
Intent.ACTION_VIEW, Uri.parse(Constant.REPO_URL)
)
)
Helper.context?.startActivity(intent)
}

linearLayout.addView(btn)
}
})
}
Expand Down
15 changes: 2 additions & 13 deletions app/src/main/java/com/chrxw/purenga/hook/MainHook.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.chrxw.purenga.hook

import android.content.Context
import com.chrxw.purenga.Constant
import com.chrxw.purenga.utils.Helper


Expand All @@ -15,20 +13,11 @@ class MainHook : IHook {
}

override fun init(classLoader: ClassLoader) {
// Helper.clsR = classLoader.loadClass("gov.pianzong.androidnga.R")
// Helper.clsRId = classLoader.loadClass("gov.pianzong.androidnga.R\$id")
// Helper.clsRColor = classLoader.loadClass("gov.pianzong.androidnga.R\$color")
// Helper.clsRDimen = classLoader.loadClass("gov.pianzong.androidnga.R\$dimen")
// Helper.clsRDrawable = classLoader.loadClass("gov.pianzong.androidnga.R\$drawable")
// Helper.clsRLayout = classLoader.loadClass("gov.pianzong.androidnga.R\$layout")

Helper.clsSPUtil = classLoader.loadClass("com.donews.nga.common.utils.SPUtil")
Helper.clsR = classLoader.loadClass("gov.pianzong.androidnga.R")
Helper.clsRId = classLoader.loadClass("gov.pianzong.androidnga.R\$id")
}


override fun hook() {
//设置SharedPreferences
Helper.spDoinfo = Helper.context.getSharedPreferences("dninfo", Context.MODE_PRIVATE)
Helper.spPlugin = Helper.context.getSharedPreferences(Constant.PLUGIN_PREFERENCE_NAME, Context.MODE_PRIVATE)
}
}
56 changes: 28 additions & 28 deletions app/src/main/java/com/chrxw/purenga/hook/PreferencesHook.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,22 @@ class PreferencesHook : IHook {

val context = root.context

val isDarkModel = Helper.isDarkModel()

btnPureNGASetting = Button(context).also { btn ->
btn.text = "PureNGA 设置"
btn.setOnClickListener {
val view = generateView(context)
val context = param.thisObject as Context
AlertDialog.Builder(context).setTitle("PureNGA 设置").setCancelable(false).setView(view)
.setNegativeButton("取消") { dialog, which ->
AlertDialog.Builder(context).run {
setTitle("PureNGA 设置")
setCancelable(false).setView(view)
setNegativeButton("取消") { dialog, which ->
Helper.toast("设置未保存")
}.setPositiveButton("确定") { dialog, which ->
}
setPositiveButton("确定") { dialog, which ->
saveSetting(view)
Helper.toast("设置已保存, 重启APP生效")
}.create().also { dialog ->
}
create().also { dialog ->
val params = dialog.window?.attributes
val metrics = DisplayMetrics()
dialog.window!!.windowManager.defaultDisplay.getMetrics(metrics)
Expand All @@ -89,8 +91,9 @@ class PreferencesHook : IHook {
dialog.window!!.attributes = params
dialog.show()
}
}
}
btn.setTextColor(Color.parseColor(if (isDarkModel) "#f8fae3" else "#3c3b39"))
btn.setTextColor(Color.parseColor(if (Helper.isDarkModel()) "#f8fae3" else "#3c3b39"))
btn.setBackgroundColor(0)
btn.setPadding(5, 5, 5, 5)
linearLayout.removeViewAt(linearLayout.childCount - 1)
Expand All @@ -105,8 +108,7 @@ class PreferencesHook : IHook {
Boolean::class.java,
object : XC_MethodHook() {
override fun afterHookedMethod(param: MethodHookParam) {
val isDarkModel = Helper.isDarkModel()
btnPureNGASetting?.setTextColor(Color.parseColor(if (isDarkModel) "#f8fae3" else "#3c3b39"))
btnPureNGASetting?.setTextColor(Color.parseColor(if (Helper.isDarkModel()) "#f8fae3" else "#3c3b39"))
}
})
}
Expand All @@ -123,30 +125,28 @@ class PreferencesHook : IHook {
}

private fun loadSetting(view: View) {
Helper.spPlugin.also { sp ->
view.findViewById<Switch>(R.id.pure_splash_ad).isChecked = sp.getBoolean(Constant.PURE_SPLASH_AD, false)
view.findViewById<Switch>(R.id.pure_post_ad).isChecked = sp.getBoolean(Constant.PURE_POST_AD, false)
view.findViewById<Switch>(R.id.crack_ad_task).isChecked = sp.getBoolean(Constant.CRACK_AD_TASK, false)
Helper.spPlugin.run {
view.findViewById<Switch>(R.id.pure_splash_ad).isChecked = getBoolean(Constant.PURE_SPLASH_AD, false)
view.findViewById<Switch>(R.id.pure_post_ad).isChecked = getBoolean(Constant.PURE_POST_AD, false)
view.findViewById<Switch>(R.id.crack_ad_task).isChecked = getBoolean(Constant.CRACK_AD_TASK, false)
view.findViewById<Switch>(R.id.use_external_browser).isChecked =
sp.getBoolean(Constant.USE_EXTERNAL_BROWSER, false)
view.findViewById<Switch>(R.id.kill_update_check).isChecked =
sp.getBoolean(Constant.KILL_UPDATE_CHECK, false)
view.findViewById<Switch>(R.id.kill_popup_dialog).isChecked =
sp.getBoolean(Constant.KILL_UPDATE_CHECK, false)
view.findViewById<Switch>(R.id.hide_hook_info).isChecked = sp.getBoolean(Constant.HIDE_HOOK_INFO, false)
getBoolean(Constant.USE_EXTERNAL_BROWSER, false)
view.findViewById<Switch>(R.id.kill_update_check).isChecked = getBoolean(Constant.KILL_UPDATE_CHECK, false)
view.findViewById<Switch>(R.id.kill_popup_dialog).isChecked = getBoolean(Constant.KILL_UPDATE_CHECK, false)
view.findViewById<Switch>(R.id.hide_hook_info).isChecked = getBoolean(Constant.HIDE_HOOK_INFO, false)
}
}

private fun saveSetting(view: View) {
Helper.spPlugin.edit().also { sp ->
sp.putBoolean(Constant.PURE_SPLASH_AD, view.findViewById<Switch>(R.id.pure_splash_ad).isChecked)
sp.putBoolean(Constant.PURE_POST_AD, view.findViewById<Switch>(R.id.pure_post_ad).isChecked)
sp.putBoolean(Constant.CRACK_AD_TASK, view.findViewById<Switch>(R.id.crack_ad_task).isChecked)
sp.putBoolean(Constant.USE_EXTERNAL_BROWSER, view.findViewById<Switch>(R.id.use_external_browser).isChecked)
sp.putBoolean(Constant.KILL_UPDATE_CHECK, view.findViewById<Switch>(R.id.kill_update_check).isChecked)
sp.putBoolean(Constant.KILL_UPDATE_CHECK, view.findViewById<Switch>(R.id.kill_popup_dialog).isChecked)
sp.putBoolean(Constant.HIDE_HOOK_INFO, view.findViewById<Switch>(R.id.hide_hook_info).isChecked)
sp.commit()
Helper.spPlugin.edit().run {
putBoolean(Constant.PURE_SPLASH_AD, view.findViewById<Switch>(R.id.pure_splash_ad).isChecked)
putBoolean(Constant.PURE_POST_AD, view.findViewById<Switch>(R.id.pure_post_ad).isChecked)
putBoolean(Constant.CRACK_AD_TASK, view.findViewById<Switch>(R.id.crack_ad_task).isChecked)
putBoolean(Constant.USE_EXTERNAL_BROWSER, view.findViewById<Switch>(R.id.use_external_browser).isChecked)
putBoolean(Constant.KILL_UPDATE_CHECK, view.findViewById<Switch>(R.id.kill_update_check).isChecked)
putBoolean(Constant.KILL_UPDATE_CHECK, view.findViewById<Switch>(R.id.kill_popup_dialog).isChecked)
putBoolean(Constant.HIDE_HOOK_INFO, view.findViewById<Switch>(R.id.hide_hook_info).isChecked)
commit()
}
}
}
Expand Down
57 changes: 16 additions & 41 deletions app/src/main/java/com/chrxw/purenga/utils/Helper.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.chrxw.purenga.utils

import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences
import android.content.pm.PackageInfo
import android.widget.Toast
import com.chrxw.purenga.Constant
import de.robv.android.xposed.XposedHelpers
Expand All @@ -13,17 +13,12 @@ import de.robv.android.xposed.XposedHelpers
*/
class Helper {
companion object {
@SuppressLint("StaticFieldLeak")
lateinit var context: Context
private lateinit var spDoinfo: SharedPreferences

lateinit var spDoinfo: SharedPreferences
lateinit var packageInfo: PackageInfo

// lateinit var clsR: Class<*>
// lateinit var clsRId: Class<*>
// lateinit var clsRColor: Class<*>
// lateinit var clsRDimen: Class<*>
// lateinit var clsRDrawable: Class<*>
// lateinit var clsRLayout: Class<*>
lateinit var clsR: Class<*>
lateinit var clsRId: Class<*>

lateinit var clsSPUtil: Class<*>
lateinit var spPlugin: SharedPreferences
Expand All @@ -33,11 +28,9 @@ class Helper {
*/
fun init(): Boolean {
return try {
packageInfo = context.packageManager.getPackageInfo(Constant.NGA_PACKAGE_NAME, 0)
Log.i(packageInfo)

// packageInfo = context.packageManager.getPackageInfo(BuildConfig.APPLICATION_ID, 0)
// Log.i(packageInfo)
//设置SharedPreferences
spDoinfo = context.getSharedPreferences(Constant.DNINFO, Context.MODE_PRIVATE)
spPlugin = context.getSharedPreferences(Constant.PLUGIN_PREFERENCE_NAME, Context.MODE_PRIVATE)

true
} catch (e: Exception) {
Expand All @@ -50,14 +43,14 @@ class Helper {
* 显示Toast
*/
fun toast(text: String, duration: Int = Toast.LENGTH_SHORT) {
if (context != null) {
try {
Toast.makeText(context, text, duration).show()
} else {
Log.d("AppContext 为 NULL")
} catch (e: Throwable) {
Log.d("toast 出错")
}
}

private inline fun getRes(cls: Class<*>?, key: String): Int {
private fun getRes(cls: Class<*>?, key: String): Int {
return try {
XposedHelpers.getStaticIntField(cls, key)
} catch (e: Throwable) {
Expand All @@ -67,30 +60,12 @@ class Helper {
}
}

// fun getRId(key: String): Int {
// return getRes(clsRId, key)
// }
//
// fun getRColor(key: String): Int {
// return getRes(clsRColor, key)
// }
//
// fun getRDimen(key: String): Int {
// return getRes(clsRDimen, key)
//
// }
//
// fun getRDrawable(key: String): Int {
// return getRes(clsRDrawable, key)
// }
//
// fun getRLayout(key: String): Int {
// return getRes(clsRLayout, key)
// }
fun getRId(key: String): Int {
return getRes(clsRId, key)
}

fun isDarkModel(): Boolean {
return spPlugin.getBoolean("DARK_MODEL", false)
return spDoinfo.getBoolean("DARK_MODEL", false)
}

}
}
Loading

0 comments on commit 83f303c

Please sign in to comment.