Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Commit

Permalink
Notify main process when settings changes + story fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kmod-midori committed Aug 4, 2017
1 parent 1e83dbc commit d7197ac
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 31 deletions.
3 changes: 2 additions & 1 deletion app/src/main/kotlin/moe/reimu/weiboxposed/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ package moe.reimu.weiboxposed

const val PREF_NAME = "prefs"
const val PROVIDER_AUTHORITY = "moe.reimu.weiboxposed.provider"
const val WB_PACKAGE_NAME = "com.sina.weibo"
const val WB_PACKAGE_NAME = "com.sina.weibo"
const val BROADCAST = "moe.reimu.weiboxposed.SETTINGS_CHANGED"
85 changes: 59 additions & 26 deletions app/src/main/kotlin/moe/reimu/weiboxposed/Module.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package moe.reimu.weiboxposed
import android.annotation.SuppressLint
import android.app.Activity
import android.app.AndroidAppHelper
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.app.Application
import android.content.*
import android.content.res.XResources
import android.graphics.Color
import android.net.Uri
import android.os.Bundle
import android.util.TypedValue
import android.view.View
import android.view.ViewGroup
import android.widget.ScrollView
import android.widget.TextView
Expand All @@ -22,6 +22,7 @@ import de.robv.android.xposed.callbacks.XC_InitPackageResources.InitPackageResou
import de.robv.android.xposed.callbacks.XC_LoadPackage

import de.robv.android.xposed.XposedHelpers.*
import de.robv.android.xposed.callbacks.XC_LayoutInflated

class Module : IXposedHookInitPackageResources, IXposedHookLoadPackage {
companion object {
Expand All @@ -30,20 +31,29 @@ class Module : IXposedHookInitPackageResources, IXposedHookLoadPackage {

private var remove_hot = false
private var debug_mode = false
private var force_browser = false
private var no_story = false
private val enabled_feature = arrayListOf("Night_Mode")
private val disabled_feature = arrayListOf<String>()
private var content_keyword = listOf<String>()
private var user_keyword = listOf<String>()
private var comment_filters = listOf<Int>()
private var receiver: BroadcastReceiver? = null


override fun handleInitPackageResources(resparam: InitPackageResourcesParam) {
if (resparam.packageName != WB_PACKAGE_NAME)
return
initPref()

// Disable Special BG touch event by setting width to 0
resparam.res.setReplacement(WB_PACKAGE_NAME, "dimen", "feed_title_specialbg_width",
XResources.DimensionReplacement(0f, TypedValue.COMPLEX_UNIT_PX))
resparam.res.hookLayout(WB_PACKAGE_NAME, "layout", "story_feed_horiz_photo_list", object : XC_LayoutInflated() {
override fun handleLayoutInflated(liparam: LayoutInflatedParam) {
if (no_story) liparam.view.visibility = View.GONE
}
})
}


Expand Down Expand Up @@ -122,9 +132,9 @@ class Module : IXposedHookInitPackageResources, IXposedHookLoadPackage {
if (debug_mode) XposedBridge.log("[WeiboXposed] " + text)
}

private val removeAD = object : XC_MethodHook() {
override fun afterHookedMethod(param: XC_MethodHook.MethodHookParam) {
val origResult = param.result as ArrayList<*>
private val removeAD = HookBuilder {
after {
val origResult = it.result as ArrayList<*>
val iterator = origResult.iterator()
while (iterator.hasNext()) {
val mblog = iterator.next()
Expand All @@ -135,14 +145,14 @@ class Module : IXposedHookInitPackageResources, IXposedHookLoadPackage {
}
}
}
}
}.build()

private val callbackCancel = XC_MethodReplacement.DO_NOTHING
private val callbackEmptyList = object : XC_MethodHook() {
override fun beforeHookedMethod(param: XC_MethodHook.MethodHookParam) {
param.result = arrayListOf<Any>()
private val callbackEmptyList = HookBuilder {
before {
it.result = arrayListOf<Any>()
}
}
}.build()

private fun hookAD(lpparam: XC_LoadPackage.LoadPackageParam) {
val LIST_BASE = "$WB_PACKAGE_NAME.models.MBlogListBaseObject"
Expand Down Expand Up @@ -179,6 +189,7 @@ class Module : IXposedHookInitPackageResources, IXposedHookLoadPackage {
Bundle::class.java)
.hook(object : XC_MethodHook() {
override fun beforeHookedMethod(param: XC_MethodHook.MethodHookParam) {
if (!force_browser) return
val intent = param.args[0] as Intent
if (intent.getBooleanExtra(MOD_PACKAGE_NAME, false)) {
logd("[Browser] Loop detected, skipping")
Expand Down Expand Up @@ -244,12 +255,6 @@ class Module : IXposedHookInitPackageResources, IXposedHookLoadPackage {
}
}

fun hookStory(lpparam: XC_LoadPackage.LoadPackageParam) {
lpparam.find("$WB_PACKAGE_NAME.story.common.bean.wrapper.StoryListWrapper")
.method("toList")
.hook(callbackEmptyList)
}

fun hookComment(lpparam: XC_LoadPackage.LoadPackageParam) {
lpparam.find("$WB_PACKAGE_NAME.models.JsonCommentMessageList")
.method("getCommentMessageList")
Expand Down Expand Up @@ -280,12 +285,34 @@ class Module : IXposedHookInitPackageResources, IXposedHookLoadPackage {
hookAD(lpparam)
hookGreyScale(lpparam)

if (Settings.force_browser) hookBrowser()
hookBrowser()
hookMoreItems(lpparam)

if (Settings.no_story) hookStory(lpparam)

hookComment(lpparam)
hookReload(lpparam)
}

private fun hookReload(lpparam: XC_LoadPackage.LoadPackageParam) {
val cls = lpparam.find("$WB_PACKAGE_NAME.MainTabActivity")
cls.method("onCreate", Bundle::class.java).hook {
after {
val activity = it.thisObject as Activity
receiver = object: BroadcastReceiver() {
override fun onReceive(p0: Context?, p1: Intent?) {
log("Got reload command")
reloadPrefs()
}
}
activity.registerReceiver(receiver, IntentFilter(BROADCAST))
}
}

cls.method("onDestroy").hook {
after {
val activity = it.thisObject as Activity
receiver ?: activity.unregisterReceiver(receiver)
}
}
}

object Settings : Preferences() {
Expand All @@ -305,14 +332,20 @@ class Module : IXposedHookInitPackageResources, IXposedHookLoadPackage {
val comment_filters by stringSetPref(null, setOf("2", "3", "4", "5", "6"))
}

private fun reloadPrefs(): Boolean {

val activityThread = XposedHelpers.callStaticMethod(
XposedHelpers.findClass("android.app.ActivityThread", null), "currentActivityThread")
val systemCtx = XposedHelpers.callMethod(activityThread, "getSystemContext") as Context
Preferences.init(systemCtx)
private fun initPref() {
if (!Preferences.checkInit()) {
val activityThread = XposedHelpers.callStaticMethod(
XposedHelpers.findClass("android.app.ActivityThread", null), "currentActivityThread")
val context = XposedHelpers.callMethod(activityThread, "getSystemContext") as Context
Preferences.init(context)
}
}

private fun reloadPrefs(): Boolean {
initPref()
debug_mode = Settings.debug_mode
force_browser = Settings.force_browser
no_story = Settings.no_story

if (!Settings.global_enabled) {
return false
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/kotlin/moe/reimu/weiboxposed/Preferences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ abstract class Preferences {
fun init(context: Context) {
this.context = context
}

fun checkInit() = this.context != null
}

private val prefs: RemotePreferences by lazy {
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/kotlin/moe/reimu/weiboxposed/SettingsActivity.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package moe.reimu.weiboxposed

import android.content.Intent
import android.content.SharedPreferences
import android.preference.PreferenceActivity
import android.os.Bundle
Expand All @@ -19,5 +20,6 @@ class SettingsActivity : PreferenceActivity(), SharedPreferences.OnSharedPrefere

override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
Toast.makeText(this, R.string.restart_weibo, Toast.LENGTH_SHORT).show()
sendBroadcast(Intent(BROADCAST))
}
}
8 changes: 4 additions & 4 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
<string name="title_activity_settings">设置</string>

<string name="pref_features_title">功能</string>
<string name="pref_title_global_enabled">启用模块</string>
<string name="pref_title_global_enabled">启用模块 *</string>
<string name="pref_title_remove_hot">去除已关注内容</string>
<string name="pref_description_remove_hot">包含来自已经关注用户的“粉丝头条”等推广内容</string>
<string name="pref_title_force_browser">强制使用外置浏览器</string>
<string name="pref_description_force_browser">与“去你大爷的内置浏览器”可能发生冲突,请禁用该模块的“微博”规则。</string>
<string name="pref_title_disable_new_message_flow">禁用新版通知界面</string>
<string name="pref_title_no_story">禁用首页上的“微博故事”推荐</string>
<string name="pref_title_disable_new_message_flow">禁用新版通知界面 *</string>
<string name="pref_title_no_story">禁用首页上的“微博故事”推荐 *</string>
<string name="pref_title_comment_filters">评论界面过滤</string>

<string name="pref_filtering_title">屏蔽(每行一个)</string>
Expand All @@ -21,6 +21,6 @@
<string name="pref_title_alipay">支付宝</string>
<string name="pref_title_debug_mode">调试模式</string>

<string name="restart_weibo">配置已保存,强行停止微博或重启设备后生效</string>
<string name="restart_weibo">配置已保存,除标注(*)外刷新即可生效</string>
<string name="module_not_enabled">无法正常调用Xposed框架的功能,请确认Xposed已经加载且此模块已启用。如果问题仍然存在,请尝试禁用并重新启用此模块。</string>
</resources>

0 comments on commit d7197ac

Please sign in to comment.