blob: 8306731b7635d171e09cb8a9079e3b44e12cc85e [file] [log] [blame]
Winson Chungec1ef092017-10-25 16:22:34 -07001/*
2 * Copyright (C) 2017 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.server.am;
18
19import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
20import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
21
22import android.graphics.Bitmap;
23import android.os.Binder;
24import android.os.Bundle;
25import android.os.RemoteException;
26import android.util.Log;
27
28import com.android.internal.app.IAssistDataReceiver;
29import com.android.server.am.AssistDataRequester.AssistDataRequesterCallbacks;
30
31/**
32 * Proxies assist data to the given receiver, skipping all callbacks if the receiver dies.
33 */
34class AssistDataReceiverProxy implements AssistDataRequesterCallbacks,
35 Binder.DeathRecipient {
36
37 private static final String TAG = TAG_WITH_CLASS_NAME ? "AssistDataReceiverProxy" : TAG_AM;
38
39 private String mCallerPackage;
40 private boolean mBinderDied;
41 private IAssistDataReceiver mReceiver;
42
43 public AssistDataReceiverProxy(IAssistDataReceiver receiver, String callerPackage) {
44 try {
45 receiver.asBinder().linkToDeath(this, 0);
46 } catch (RemoteException e) {
47 Log.w(TAG, "Could not link to client death", e);
48 }
49 mReceiver = receiver;
50 mCallerPackage = callerPackage;
51 }
52
53 @Override
54 public boolean canHandleReceivedAssistDataLocked() {
55 // We are forwarding, so we can always receive this data
56 return true;
57 }
58
59 @Override
60 public void onAssistDataReceivedLocked(Bundle data, int activityIndex, int activityCount) {
61 if (!mBinderDied) {
62 try {
63 mReceiver.onHandleAssistData(data);
64 } catch (RemoteException e) {
65 Log.w(TAG, "Failed to proxy assist data to receiver in package="
66 + mCallerPackage, e);
67 }
68 }
69 }
70
71 @Override
72 public void onAssistScreenshotReceivedLocked(Bitmap screenshot) {
73 if (!mBinderDied) {
74 try {
75 mReceiver.onHandleAssistScreenshot(screenshot);
76 } catch (RemoteException e) {
77 Log.w(TAG, "Failed to proxy assist screenshot to receiver in package="
78 + mCallerPackage, e);
79 }
80 }
81 }
82
83 @Override
84 public void binderDied() {
85 mBinderDied = true;
86 }
87}