1/*
2 * Copyright (C) 2015 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.content.Context;
20import android.graphics.Rect;
21import android.os.IBinder;
22import android.os.RemoteException;
23import android.util.EventLog;
24import android.util.Log;
25import android.util.SparseArray;
26
27import com.android.systemui.EventLogConstants;
28import com.android.systemui.EventLogTags;
29import com.android.systemui.recents.events.EventBus;
30import com.android.systemui.recents.events.activity.DockedTopTaskEvent;
31import com.android.systemui.recents.events.activity.RecentsActivityStartingEvent;
32import com.android.systemui.recents.events.ui.RecentsDrawnEvent;
33import com.android.systemui.recents.misc.ForegroundThread;
34
35/**
36 * An implementation of the system user's Recents interface to be called remotely by secondary
37 * users.
38 */
39public class RecentsSystemUser extends IRecentsSystemUserCallbacks.Stub {
40
41    private static final String TAG = "RecentsSystemUser";
42
43    private Context mContext;
44    private RecentsImpl mImpl;
45    private final SparseArray<IRecentsNonSystemUserCallbacks> mNonSystemUserRecents =
46            new SparseArray<>();
47
48    public RecentsSystemUser(Context context, RecentsImpl impl) {
49        mContext = context;
50        mImpl = impl;
51    }
52
53    @Override
54    public void registerNonSystemUserCallbacks(final IBinder nonSystemUserCallbacks,
55            final int userId) {
56        try {
57            final IRecentsNonSystemUserCallbacks callback =
58                    IRecentsNonSystemUserCallbacks.Stub.asInterface(nonSystemUserCallbacks);
59            nonSystemUserCallbacks.linkToDeath(new IBinder.DeathRecipient() {
60                @Override
61                public void binderDied() {
62                    mNonSystemUserRecents.removeAt(mNonSystemUserRecents.indexOfValue(callback));
63                    EventLog.writeEvent(EventLogTags.SYSUI_RECENTS_CONNECTION,
64                            EventLogConstants.SYSUI_RECENTS_CONNECTION_SYSTEM_UNREGISTER_USER,
65                            userId);
66                }
67            }, 0);
68            mNonSystemUserRecents.put(userId, callback);
69            EventLog.writeEvent(EventLogTags.SYSUI_RECENTS_CONNECTION,
70                    EventLogConstants.SYSUI_RECENTS_CONNECTION_SYSTEM_REGISTER_USER, userId);
71        } catch (RemoteException e) {
72            Log.e(TAG, "Failed to register NonSystemUserCallbacks", e);
73        }
74    }
75
76    public IRecentsNonSystemUserCallbacks getNonSystemUserRecentsForUser(int userId) {
77        return mNonSystemUserRecents.get(userId);
78    }
79
80    @Override
81    public void updateRecentsVisibility(boolean visible) {
82        ForegroundThread.getHandler().post(() -> {
83            mImpl.onVisibilityChanged(mContext, visible);
84        });
85    }
86
87    @Override
88    public void startScreenPinning(int taskId) {
89        ForegroundThread.getHandler().post(() -> {
90            mImpl.onStartScreenPinning(mContext, taskId);
91        });
92    }
93
94    @Override
95    public void sendRecentsDrawnEvent() {
96        EventBus.getDefault().post(new RecentsDrawnEvent());
97    }
98
99    @Override
100    public void sendDockingTopTaskEvent(int dragMode, Rect initialRect) throws RemoteException {
101        EventBus.getDefault().post(new DockedTopTaskEvent(dragMode, initialRect));
102    }
103
104    @Override
105    public void sendLaunchRecentsEvent() throws RemoteException {
106        EventBus.getDefault().post(new RecentsActivityStartingEvent());
107    }
108}
109