Skip to content

Commit

Permalink
fix panel not show on some cases;remove unused code;make iobserver on…
Browse files Browse the repository at this point in the history
…eway
  • Loading branch information
ff19 committed Jan 10, 2023
1 parent 0e535a2 commit 96afda2
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 384 deletions.
4 changes: 2 additions & 2 deletions app/src/main/aidl/com/kaisar/xposed/godmode/IObserver.aidl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package com.kaisar.xposed.godmode;
import com.kaisar.xposed.godmode.rule.ActRules;

interface IObserver {
void onEditModeChanged(boolean enable);
void onViewRuleChanged(String packageName, in ActRules actRules);
oneway void onEditModeChanged(boolean enable);
oneway void onViewRuleChanged(String packageName, in ActRules actRules);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
Expand All @@ -22,13 +21,10 @@
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.RequiresApi;

import com.kaisar.xposed.godmode.R;
import com.kaisar.xposed.godmode.injection.bridge.GodModeManager;
import com.kaisar.xposed.godmode.injection.bridge.ManagerObserver;
import com.kaisar.xposed.godmode.injection.hook.ActivityLifecycleHook;
import com.kaisar.xposed.godmode.injection.hook.DispatchKeyEventHook;
import com.kaisar.xposed.godmode.injection.hook.DisplayPropertiesHook;
import com.kaisar.xposed.godmode.injection.hook.EventHandlerHook;
import com.kaisar.xposed.godmode.injection.hook.SystemPropertiesHook;
Expand All @@ -41,10 +37,8 @@
import com.kaisar.xservicemanager.XServiceManager;

import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Optional;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.IXposedHookZygoteInit;
Expand All @@ -64,7 +58,7 @@ public final class GodModeInjector implements IXposedHookLoadPackage, IXposedHoo
public final static Property<ActRules> actRuleProp = new Property<>();
public static XC_LoadPackage.LoadPackageParam loadPackageParam;
private static State state = State.UNKNOWN;
private static DispatchKeyEventHook dispatchKeyEventHook = new DispatchKeyEventHook();
private static ViewSelector viewSelector = new ViewSelector();

enum State {
UNKNOWN,
Expand All @@ -81,7 +75,7 @@ public static void notifyEditModeChanged(boolean enable) {
switchProp.set(enable);
}
// 显示侧栏
dispatchKeyEventHook.setdisplay(enable);
viewSelector.setPanel(enable);
}

public static void notifyViewRulesChanged(ActRules actRules) {
Expand Down Expand Up @@ -120,7 +114,7 @@ public void handleLoadPackage(XC_LoadPackage.LoadPackageParam loadPackageParam)
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
//Volume key select old
Activity activity = (Activity) param.thisObject;
dispatchKeyEventHook.setactivity(activity);
// onCreate的activity不一定就是当前显示的activity!
injectModuleResources(activity.getResources());
super.afterHookedMethod(param);
}
Expand Down Expand Up @@ -292,7 +286,7 @@ protected void beforeHookedMethod(MethodHookParam param) {
XposedHelpers.findAndHookMethod(View.class, "dispatchTouchEvent", MotionEvent.class, eventHandlerHook);
}

public static DispatchKeyEventHook getDispatchKeyEventHook(){
return dispatchKeyEventHook;
public static ViewSelector getViewSelector(){
return viewSelector;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@ public final class ViewController {
//
private final static SparseArray<Pair<WeakReference<View>, ViewProperty>> blockedViewCache = new SparseArray<>();
private final static Stack<ViewRule> editStack = new Stack<>();
private static boolean inBatch;

// 批量应用规则
public static void applyRuleBatch(Activity activity, List<ViewRule> rules) {
Logger.d(TAG, "[ApplyRuleBatch info start------------------------------------]");
inBatch = true;
for (ViewRule rule : new ArrayList<>(rules)) {
try {
Logger.d(TAG, "[Apply rule]:" + rule.toString());
Expand All @@ -53,7 +51,7 @@ public static void applyRuleBatch(Activity activity, List<ViewRule> rules) {
view = ViewHelper.findViewBestMatch(activity, rule);
Preconditions.checkNotNull(view, "apply rule fail not match any view");
}
boolean blocked = applyRule(view, rule);
boolean blocked = applyRule(view, rule,false);
if (blocked) {
Logger.i(TAG, String.format("[Success] %s#%s has been blocked", activity, view));
} else {
Expand All @@ -63,11 +61,10 @@ public static void applyRuleBatch(Activity activity, List<ViewRule> rules) {
Logger.w(TAG, String.format("[Failed] %s#%s block failed because %s", activity, rule.viewClass, e.getMessage()));
}
}
inBatch = false;
Logger.d(TAG, "[ApplyRuleBatch info end------------------------------------]");
}
// 根据Rule的规则设置view组建的可见性
public static boolean applyRule(View v, ViewRule viewRule) {
public static boolean applyRule(View v, ViewRule viewRule, boolean fromUser) {
int ruleHashCode = viewRule.hashCode();
Pair<WeakReference<View>, ViewProperty> viewInfo = blockedViewCache.get(ruleHashCode);
View blockedView = viewInfo != null ? viewInfo.first.get() : null;
Expand Down Expand Up @@ -96,7 +93,7 @@ public static boolean applyRule(View v, ViewRule viewRule) {
// 记录已经被block的view组件
blockedViewCache.put(ruleHashCode, Pair.create(new WeakReference<>(v), viewProperty));
// 单个操作的情况下
if(!inBatch){
if(fromUser){
editStack.push(viewRule);
}
Logger.d(TAG, String.format(Locale.getDefault(), "apply rule add view cache %d=%s", ruleHashCode, v));
Expand Down Expand Up @@ -162,6 +159,12 @@ public static void revokeRule(View v, ViewRule viewRule) {
}
}


public static void cleanStack(){
editStack.clear();
}


private static final class ViewProperty {

final float alpha;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.kaisar.xposed.godmode.injection.hook;
package com.kaisar.xposed.godmode.injection;

import static com.kaisar.xposed.godmode.GodModeApplication.TAG;
import static com.kaisar.xposed.godmode.injection.util.CommonUtils.recycleNullableBitmap;
Expand All @@ -8,7 +8,6 @@
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Rect;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.LayoutInflater;
Expand Down Expand Up @@ -42,7 +41,7 @@
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedHelpers;

public final class DispatchKeyEventHook extends XC_MethodHook implements Property.OnPropertyChangeListener<Boolean>, SeekBar.OnSeekBarChangeListener {
public final class ViewSelector implements Property.OnPropertyChangeListener<Boolean>, SeekBar.OnSeekBarChangeListener {

private static final int OVERLAY_COLOR = Color.argb(150, 255, 0, 0);
private final List<WeakReference<View>> mViewNodes = new ArrayList<>();
Expand All @@ -54,11 +53,14 @@ public final class DispatchKeyEventHook extends XC_MethodHook implements Propert
private SeekBar seekbar = null;
public static volatile boolean mKeySelecting = false;

public void setactivity(final Activity a) {

public void setTopActivity(final Activity a) {
Logger.d(TAG+".SelectPanel","set top activity: " + a);
activity = a;
ViewController.cleanStack();
}

public void setdisplay(Boolean display) {
public void setPanel(Boolean display) {
if (activity == null) return;
if (display) {
showNodeSelectPanel(activity);
Expand All @@ -67,30 +69,6 @@ public void setdisplay(Boolean display) {
}
}

@Override
protected void beforeHookedMethod(MethodHookParam param) {
if (GodModeInjector.switchProp.get() && !DispatchTouchEventHook.mDragging) {
Activity activity = (Activity) param.thisObject;
KeyEvent event = (KeyEvent) param.args[0];
param.setResult(dispatchKeyEvent(activity, event));
}
}
// 按键事件
private boolean dispatchKeyEvent(final Activity activity, KeyEvent keyEvent) {
Logger.d(TAG, keyEvent.toString());
int action = keyEvent.getAction();
int keyCode = keyEvent.getKeyCode();
if (action == KeyEvent.ACTION_UP &&
(keyCode == KeyEvent.KEYCODE_VOLUME_UP || keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)) {
if (!mKeySelecting) {
showNodeSelectPanel(activity);
} else {
//hide node select panel
dismissNodeSelectPanel();
}
}
return true;
}

private void showNodeSelectPanel(final Activity activity) {
mViewNodes.clear();
Expand All @@ -112,7 +90,7 @@ private void showNodeSelectPanel(final Activity activity) {
TooltipCompat.setTooltipText(btnBlock, GmResources.getText(R.string.accessibility_block));
btnBlock.setOnClickListener(v -> {
try {
mNodeSelectorPanel.setAlpha(0f);
// mNodeSelectorPanel.setAlpha(0f);
final View view = mViewNodes.get(mCurrentViewIndex).get();
Logger.d(TAG, "removed view = " + view);
if (view != null) {
Expand All @@ -127,19 +105,19 @@ private void showNodeSelectPanel(final Activity activity) {
@Override
public void onAnimationStart(View animView, Animator animation) {
viewRule.visibility = View.GONE;
ViewController.applyRule(view, viewRule);
ViewController.applyRule(view, viewRule,true);
GodModeManager.getDefault().writeRule(activity.getPackageName(), viewRule, snapshot);
}

@Override
public void onAnimationEnd(View animView, Animator animation) {
GodModeManager.getDefault().writeRule(activity.getPackageName(), viewRule, snapshot);
recycleNullableBitmap(snapshot);
particleView.detachFromContainer();
mNodeSelectorPanel.animate()
.alpha(1.0f)
.setInterpolator(new DecelerateInterpolator(1.0f))
.setDuration(300)
.start();
// mNodeSelectorPanel.animate()
// .alpha(1.0f)
// .setInterpolator(new DecelerateInterpolator(1.0f))
// .setDuration(300)
// .start();
}
});
particleView.boom(view);
Expand Down Expand Up @@ -185,20 +163,20 @@ public void onAnimationEnd(View animView, Animator animation) {
mKeySelecting = true;
XposedHelpers.findAndHookMethod(Activity.class, "dispatchKeyEvent", KeyEvent.class, new XC_MethodHook() {
protected void beforeHookedMethod(MethodHookParam param) {
if (GodModeInjector.switchProp.get() && !DispatchTouchEventHook.mDragging) {
if (GodModeInjector.switchProp.get()) {
KeyEvent event = (KeyEvent) param.args[0];
int action = event.getAction();
int keyCode = event.getKeyCode();
if (action == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
seekbarreduce();
}else if(action == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_VOLUME_UP){
seekbaradd();
}else if(action == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_VOLUME_UP){
seekbarreduce();
}
param.setResult(true);
}
}
});
} catch (Exception e) {
} catch (Throwable e) {
//god mode package uninstalled?
Logger.e(TAG, "showNodeSelectPanel fail", e);
mKeySelecting = false;
Expand Down Expand Up @@ -289,4 +267,11 @@ public void updateSelectedView(View v){
}
}


public void updateViewNodes(){
mViewNodes.clear();
mViewNodes.addAll(ViewHelper.buildViewNodes(activity.getWindow().getDecorView()));
seekbar.setMax(mViewNodes.size() - 1);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import android.app.Activity;
import android.text.TextUtils;
import android.util.Log;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import com.kaisar.xposed.godmode.injection.GodModeInjector;
import com.kaisar.xposed.godmode.injection.ViewController;
import com.kaisar.xposed.godmode.injection.ViewSelector;
import com.kaisar.xposed.godmode.injection.util.Logger;
import com.kaisar.xposed.godmode.injection.util.Property;
import com.kaisar.xposed.godmode.rule.ActRules;
Expand Down Expand Up @@ -43,6 +44,7 @@ protected void afterHookedMethod(MethodHookParam param) throws Throwable {
/*!!!这里有坑不要hook onCreate和onResume 因为getDecorView会执行installContentView的操作
所以在Activity的子类中有可能去requestFeature会导致异常所以尽量找一个很靠后的生命周期函数*/
if ("onPostResume".equals(methodName)) {
GodModeInjector.getViewSelector().setTopActivity(activity);
if (!sActivities.containsKey(activity)) {
OnLayoutChangeListener listener = new OnLayoutChangeListener(activity);
// 设置布局监听器
Expand Down Expand Up @@ -92,6 +94,9 @@ public void onPropertyChange(ActRules newActRules) {
}
}
}
if(ViewSelector.mKeySelecting){
GodModeInjector.getViewSelector().updateViewNodes();
}
}
// 布局监听器,在activity布局时应用已经设置好的规则
static final class OnLayoutChangeListener implements ViewTreeObserver.OnGlobalLayoutListener {
Expand Down
Loading

0 comments on commit 96afda2

Please sign in to comment.