3838 */
3939public class QuickBack extends BaseHC {
4040 private static final HashMap <String , Integer > mResMap = new HashMap <>();
41+ private static int [] values = null ;
4142
4243 @ Override
4344 protected void init () {
45+ try {
46+ Class <?> mReadyStateClass = findClass ("com.miui.home.recents.GestureBackArrowView$ReadyState" ).get ();
47+ Enum <?> READY_STATE_BACK = (Enum <?>) getStaticField (mReadyStateClass , "READY_STATE_BACK" );
48+ Enum <?> READY_STATE_RECENT = (Enum <?>) getStaticField (mReadyStateClass , "READY_STATE_RECENT" );
49+ Enum <?> READY_STATE_NONE = (Enum <?>) getStaticField (mReadyStateClass , "READY_STATE_NONE" );
50+ values = new int [((Object []) callStaticMethod (mReadyStateClass , "values" )).length ];
51+ values [READY_STATE_BACK .ordinal ()] = 1 ;
52+ values [READY_STATE_RECENT .ordinal ()] = 2 ;
53+ values [READY_STATE_NONE .ordinal ()] = 3 ;
54+ } catch (Throwable e ) {
55+ logE (TAG , e );
56+ }
57+
4458 hookMethod ("com.miui.home.recents.GestureStubView" ,
4559 "isDisableQuickSwitch" ,
4660 returnResult (false )
@@ -60,8 +74,16 @@ public void before() {
6074 Object getCurrentState = callMethod (mGestureBackArrowView , "getCurrentState" );
6175
6276 int ordinal = (int ) callMethod (getCurrentState , "ordinal" );
63- int [] mState = (int []) getStaticField ("com.miui.home.recents.GestureStubView$4" , "$SwitchMap$com$miui$home$recents$GestureBackArrowView$ReadyState" );
64- ordinal = mState [ordinal ];
77+ if (values == null ) {
78+ Class <?> mGestureStubView$X = existsClass ("com.miui.home.recents.GestureStubView$4" ) ?
79+ findClass ("com.miui.home.recents.GestureStubView$4" ).get ()
80+ : findClass ("com.miui.home.recents.GestureStubView$5" ).get ();
81+ int [] mState = (int []) getStaticField (mGestureStubView$X , "$SwitchMap$com$miui$home$recents$GestureBackArrowView$ReadyState" );
82+ ordinal = mState [ordinal ];
83+
84+ } else
85+ ordinal = values [ordinal ];
86+
6587 if (ordinal == 2 ) {
6688 callMethod (mGestureStubView , "onBackCancelled" );
6789 logI (TAG , "call onBackCancelled" );
@@ -84,69 +106,65 @@ public void before() {
84106 Object createLoadPlan = callMethod (taskLoader , "createLoadPlan" , context );
85107 callMethod (taskLoader , "preloadTasks" , createLoadPlan , -1 );
86108 Object taskStack = callMethod (createLoadPlan , "getTaskStack" );
87- ActivityOptions activityOptions = null ;
109+
88110 if (taskStack == null || (int ) callMethod (taskStack , "getTaskCount" ) == 0 ||
89111 (runningTask = (ActivityManager .RunningTaskInfo ) callMethod (recentsModel , "getRunningTask" )) == null ) {
90- setResult (null );
112+ setResult (null ); // 后台无其他任务
91113 return ;
92114 }
115+
93116 ArrayList <?> stackTasks = (ArrayList <?>) callMethod (taskStack , "getStackTasks" );
94- int size = stackTasks .size ();
95117 Object task = null ;
96- int i2 = 0 ;
97- while (true ) {
98- if (i2 >= size - 1 ) {
118+ for (int i = 0 ; i < stackTasks .size () - 1 ; i ++) {
119+ Object t = stackTasks .get (i );
120+ if ((int ) getField (getField (t , "key" ), "id" ) == runningTask .id ) {
121+ task = stackTasks .get (i + 1 );
99122 break ;
100- } else if ((int ) getField (getField (stackTasks .get (i2 ), "key" ), "id" ) == runningTask .id ) {
101- task = stackTasks .get (i2 + 1 );
102- break ;
103- } else {
104- i2 ++;
105123 }
106124 }
107- if (task == null && size >= 1 && "com.miui.home" .equals (runningTask .baseActivity .getPackageName ())) {
125+ if (task == null && ! stackTasks . isEmpty () && "com.miui.home" .equals (runningTask .baseActivity .getPackageName ())) {
108126 task = stackTasks .get (0 );
109127 }
128+
110129 if (task != null && getField (task , "icon" ) == null ) {
111- setField (task , "icon" , callMethod (taskLoader , "getAndUpdateActivityIcon" ,
112- getField (task , "key" ),
113- getField (task , "taskDescription" ),
114- context .getResources (), true
115- ));
130+ setField (task , "icon" ,
131+ callMethod (taskLoader , "getAndUpdateActivityIcon" ,
132+ getField (task , "key" ),
133+ getField (task , "taskDescription" ),
134+ context .getResources (),
135+ true
136+ )
137+ );
116138 }
117139 if (!(boolean ) getArgs (1 ) || task == null ) {
118140 setResult (task );
119141 return ;
120142 }
121- int i = (int ) getArgs (2 );
122- if (i == 0 ) {
143+
144+ ActivityOptions activityOptions = null ;
145+ int mGestureStubPos = (int ) getArgs (2 );
146+ if (mGestureStubPos == 0 ) {
123147 activityOptions = ActivityOptions .makeCustomAnimation (context ,
124148 getAnimId (context , "recents_quick_switch_left_enter" ),
125149 getAnimId (context , "recents_quick_switch_left_exit" ));
126- } else if (i == 1 ) {
150+ } else if (mGestureStubPos == 1 ) {
127151 activityOptions = ActivityOptions .makeCustomAnimation (context ,
128152 getAnimId (context , "recents_quick_switch_right_enter" ),
129153 getAnimId (context , "recents_quick_switch_right_exit" ));
130154 }
131155 Object iActivityManager = callStaticMethod ("android.app.ActivityManagerNative" , "getDefault" );
132156 if (iActivityManager != null ) {
133- try {
134- if ((int ) getField (getField (task , "key" ), "windowingMode" ) == 3 ) {
135- if (activityOptions == null ) {
136- activityOptions = ActivityOptions .makeBasic ();
137- }
138- activityOptions .getClass ().getMethod ("setLaunchWindowingMode" , Integer .class ).invoke (activityOptions , 4 );
157+ if ((int ) getField (getField (task , "key" ), "windowingMode" ) == 3 ) {
158+ if (activityOptions == null ) {
159+ activityOptions = ActivityOptions .makeBasic ();
139160 }
140- callMethod (iActivityManager , "startActivityFromRecents" ,
141- getField (getField (task , "key" ), "id" ),
142- activityOptions == null ? null : activityOptions .toBundle ());
143- setResult (task );
144- return ;
145- } catch (Exception e ) {
146- logE (TAG , "Error: " + e );
147- setResult (task );
148- return ;
161+ callMethod (activityOptions , "setLaunchWindowingMode" , 4 );
149162 }
163+ callMethod (iActivityManager , "startActivityFromRecents" ,
164+ getField (getField (task , "key" ), "id" ),
165+ activityOptions == null ? null : activityOptions .toBundle ());
166+ setResult (task );
167+ return ;
150168 }
151169 setResult (task );
152170 }
0 commit comments