1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License
15  */
16 
17 package com.android.systemui.recents;
18 
19 import android.graphics.Rect;
20 import android.os.Handler;
21 import android.os.Message;
22 import android.os.RemoteException;
23 
24 import com.android.internal.os.SomeArgs;
25 
26 /**
27  * A proxy class which directs all methods from {@link IRecentsNonSystemUserCallbacks} to
28  * {@link RecentsImpl} and makes sure they are called from the main thread.
29  */
30 public class RecentsImplProxy extends IRecentsNonSystemUserCallbacks.Stub {
31 
32     private static final int MSG_PRELOAD_RECENTS = 1;
33     private static final int MSG_CANCEL_PRELOADING_RECENTS = 2;
34     private static final int MSG_SHOW_RECENTS = 3;
35     private static final int MSG_HIDE_RECENTS = 4;
36     private static final int MSG_TOGGLE_RECENTS = 5;
37     private static final int MSG_ON_CONFIGURATION_CHANGED = 6;
38     private static final int MSG_DOCK_TOP_TASK = 7;
39     private static final int MSG_ON_DRAGGING_IN_RECENTS = 8;
40     private static final int MSG_ON_DRAGGING_IN_RECENTS_ENDED = 9;
41     private static final int MSG_SHOW_USER_TOAST = 10;
42 
43     private RecentsImpl mImpl;
44 
RecentsImplProxy(RecentsImpl recentsImpl)45     public RecentsImplProxy(RecentsImpl recentsImpl) {
46         mImpl = recentsImpl;
47     }
48 
49     @Override
preloadRecents()50     public void preloadRecents() throws RemoteException {
51         mHandler.sendEmptyMessage(MSG_PRELOAD_RECENTS);
52     }
53 
54     @Override
cancelPreloadingRecents()55     public void cancelPreloadingRecents() throws RemoteException {
56         mHandler.sendEmptyMessage(MSG_CANCEL_PRELOADING_RECENTS);
57     }
58 
59     @Override
showRecents(boolean triggeredFromAltTab, boolean draggingInRecents, boolean animate, boolean reloadTasks, boolean fromHome, int growTarget)60     public void showRecents(boolean triggeredFromAltTab, boolean draggingInRecents, boolean animate,
61             boolean reloadTasks, boolean fromHome, int growTarget)
62             throws RemoteException {
63         SomeArgs args = SomeArgs.obtain();
64         args.argi1 = triggeredFromAltTab ? 1 : 0;
65         args.argi2 = draggingInRecents ? 1 : 0;
66         args.argi3 = animate ? 1 : 0;
67         args.argi4 = reloadTasks ? 1 : 0;
68         args.argi5 = fromHome ? 1 : 0;
69         args.argi6 = growTarget;
70         mHandler.sendMessage(mHandler.obtainMessage(MSG_SHOW_RECENTS, args));
71     }
72 
73     @Override
hideRecents(boolean triggeredFromAltTab, boolean triggeredFromHomeKey)74     public void hideRecents(boolean triggeredFromAltTab, boolean triggeredFromHomeKey)
75             throws RemoteException {
76         mHandler.sendMessage(mHandler.obtainMessage(MSG_HIDE_RECENTS, triggeredFromAltTab ? 1 :0,
77                 triggeredFromHomeKey ? 1 : 0));
78     }
79 
80     @Override
toggleRecents(int growTarget)81     public void toggleRecents(int growTarget) throws RemoteException {
82         SomeArgs args = SomeArgs.obtain();
83         args.argi1 = growTarget;
84         mHandler.sendMessage(mHandler.obtainMessage(MSG_TOGGLE_RECENTS, args));
85     }
86 
87     @Override
onConfigurationChanged()88     public void onConfigurationChanged() throws RemoteException {
89         mHandler.sendEmptyMessage(MSG_ON_CONFIGURATION_CHANGED);
90     }
91 
92     @Override
dockTopTask(int topTaskId, int dragMode, int stackCreateMode, Rect initialBounds)93     public void dockTopTask(int topTaskId, int dragMode, int stackCreateMode,
94             Rect initialBounds) throws RemoteException {
95         SomeArgs args = SomeArgs.obtain();
96         args.argi1 = topTaskId;
97         args.argi2 = dragMode;
98         args.argi3 = stackCreateMode;
99         args.arg1 = initialBounds;
100         mHandler.sendMessage(mHandler.obtainMessage(MSG_DOCK_TOP_TASK, args));
101     }
102 
103     @Override
onDraggingInRecents(float distanceFromTop)104     public void onDraggingInRecents(float distanceFromTop) throws RemoteException {
105         mHandler.sendMessage(mHandler.obtainMessage(MSG_ON_DRAGGING_IN_RECENTS, distanceFromTop));
106     }
107 
108     @Override
onDraggingInRecentsEnded(float velocity)109     public void onDraggingInRecentsEnded(float velocity) throws RemoteException {
110         mHandler.sendMessage(mHandler.obtainMessage(MSG_ON_DRAGGING_IN_RECENTS_ENDED, velocity));
111     }
112 
113     @Override
showCurrentUserToast(int msgResId, int msgLength)114     public void showCurrentUserToast(int msgResId, int msgLength) {
115         mHandler.sendMessage(mHandler.obtainMessage(MSG_SHOW_USER_TOAST, msgResId, msgLength));
116     }
117 
118     private final Handler mHandler = new Handler() {
119 
120         @Override
121         public void handleMessage(Message msg) {
122             SomeArgs args;
123             switch (msg.what) {
124                 case MSG_PRELOAD_RECENTS:
125                     mImpl.preloadRecents();
126                     break;
127                 case MSG_CANCEL_PRELOADING_RECENTS:
128                     mImpl.cancelPreloadingRecents();
129                     break;
130                 case MSG_SHOW_RECENTS:
131                     args = (SomeArgs) msg.obj;
132                     mImpl.showRecents(args.argi1 != 0, args.argi2 != 0, args.argi3 != 0,
133                             args.argi4 != 0, args.argi5 != 0, args.argi6);
134                     break;
135                 case MSG_HIDE_RECENTS:
136                     mImpl.hideRecents(msg.arg1 != 0, msg.arg2 != 0);
137                     break;
138                 case MSG_TOGGLE_RECENTS:
139                     args = (SomeArgs) msg.obj;
140                     mImpl.toggleRecents(args.argi1);
141                     break;
142                 case MSG_ON_CONFIGURATION_CHANGED:
143                     mImpl.onConfigurationChanged();
144                     break;
145                 case MSG_DOCK_TOP_TASK:
146                     args = (SomeArgs) msg.obj;
147                     mImpl.dockTopTask(args.argi1, args.argi2, args.argi3 = 0,
148                             (Rect) args.arg1);
149                     break;
150                 case MSG_ON_DRAGGING_IN_RECENTS:
151                     mImpl.onDraggingInRecents((Float) msg.obj);
152                     break;
153                 case MSG_ON_DRAGGING_IN_RECENTS_ENDED:
154                     mImpl.onDraggingInRecentsEnded((Float) msg.obj);
155                     break;
156                 case MSG_SHOW_USER_TOAST:
157                     mImpl.onShowCurrentUserToast(msg.arg1, msg.arg2);
158                     break;
159                 default:
160                     super.handleMessage(msg);
161             }
162             super.handleMessage(msg);
163         }
164     };
165 }
166