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
17package com.android.systemui.recents;
18
19import android.graphics.Rect;
20import android.os.Handler;
21import android.os.Message;
22import android.os.RemoteException;
23
24import 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 */
30public 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
42    private RecentsImpl mImpl;
43
44    public RecentsImplProxy(RecentsImpl recentsImpl) {
45        mImpl = recentsImpl;
46    }
47
48    @Override
49    public void preloadRecents() throws RemoteException {
50        mHandler.sendEmptyMessage(MSG_PRELOAD_RECENTS);
51    }
52
53    @Override
54    public void cancelPreloadingRecents() throws RemoteException {
55        mHandler.sendEmptyMessage(MSG_CANCEL_PRELOADING_RECENTS);
56    }
57
58    @Override
59    public void showRecents(boolean triggeredFromAltTab, boolean draggingInRecents, boolean animate,
60            boolean reloadTasks, boolean fromHome, int growTarget)
61            throws RemoteException {
62        SomeArgs args = SomeArgs.obtain();
63        args.argi1 = triggeredFromAltTab ? 1 : 0;
64        args.argi2 = draggingInRecents ? 1 : 0;
65        args.argi3 = animate ? 1 : 0;
66        args.argi4 = reloadTasks ? 1 : 0;
67        args.argi5 = fromHome ? 1 : 0;
68        args.argi6 = growTarget;
69        mHandler.sendMessage(mHandler.obtainMessage(MSG_SHOW_RECENTS, args));
70    }
71
72    @Override
73    public void hideRecents(boolean triggeredFromAltTab, boolean triggeredFromHomeKey)
74            throws RemoteException {
75        mHandler.sendMessage(mHandler.obtainMessage(MSG_HIDE_RECENTS, triggeredFromAltTab ? 1 :0,
76                triggeredFromHomeKey ? 1 : 0));
77    }
78
79    @Override
80    public void toggleRecents(int growTarget) throws RemoteException {
81        SomeArgs args = SomeArgs.obtain();
82        args.argi1 = growTarget;
83        mHandler.sendMessage(mHandler.obtainMessage(MSG_TOGGLE_RECENTS, args));
84    }
85
86    @Override
87    public void onConfigurationChanged() throws RemoteException {
88        mHandler.sendEmptyMessage(MSG_ON_CONFIGURATION_CHANGED);
89    }
90
91    @Override
92    public void dockTopTask(int topTaskId, int dragMode, int stackCreateMode,
93            Rect initialBounds) throws RemoteException {
94        SomeArgs args = SomeArgs.obtain();
95        args.argi1 = topTaskId;
96        args.argi2 = dragMode;
97        args.argi3 = stackCreateMode;
98        args.arg1 = initialBounds;
99        mHandler.sendMessage(mHandler.obtainMessage(MSG_DOCK_TOP_TASK, args));
100    }
101
102    @Override
103    public void onDraggingInRecents(float distanceFromTop) throws RemoteException {
104        mHandler.sendMessage(mHandler.obtainMessage(MSG_ON_DRAGGING_IN_RECENTS, distanceFromTop));
105    }
106
107    @Override
108    public void onDraggingInRecentsEnded(float velocity) throws RemoteException {
109        mHandler.sendMessage(mHandler.obtainMessage(MSG_ON_DRAGGING_IN_RECENTS_ENDED, velocity));
110    }
111
112    private final Handler mHandler = new Handler() {
113
114        @Override
115        public void handleMessage(Message msg) {
116            SomeArgs args;
117            switch (msg.what) {
118                case MSG_PRELOAD_RECENTS:
119                    mImpl.preloadRecents();
120                    break;
121                case MSG_CANCEL_PRELOADING_RECENTS:
122                    mImpl.cancelPreloadingRecents();
123                    break;
124                case MSG_SHOW_RECENTS:
125                    args = (SomeArgs) msg.obj;
126                    mImpl.showRecents(args.argi1 != 0, args.argi2 != 0, args.argi3 != 0,
127                            args.argi4 != 0, args.argi5 != 0, args.argi6);
128                    break;
129                case MSG_HIDE_RECENTS:
130                    mImpl.hideRecents(msg.arg1 != 0, msg.arg2 != 0);
131                    break;
132                case MSG_TOGGLE_RECENTS:
133                    args = (SomeArgs) msg.obj;
134                    mImpl.toggleRecents(args.argi1);
135                    break;
136                case MSG_ON_CONFIGURATION_CHANGED:
137                    mImpl.onConfigurationChanged();
138                    break;
139                case MSG_DOCK_TOP_TASK:
140                    args = (SomeArgs) msg.obj;
141                    mImpl.dockTopTask(args.argi1, args.argi2, args.argi3 = 0,
142                            (Rect) args.arg1);
143                    break;
144                case MSG_ON_DRAGGING_IN_RECENTS:
145                    mImpl.onDraggingInRecents((Float) msg.obj);
146                    break;
147                case MSG_ON_DRAGGING_IN_RECENTS_ENDED:
148                    mImpl.onDraggingInRecentsEnded((Float) msg.obj);
149                    break;
150                default:
151                    super.handleMessage(msg);
152            }
153            super.handleMessage(msg);
154        }
155    };
156}
157