blob: c782dca4cad89a11fe6b21bc8b26e34cc2c031f2 [file] [log] [blame]
Sunny Goyal30ac97d2020-06-24 23:47:46 -07001/*
2 * Copyright (C) 2020 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 */
16package com.android.launcher3;
17
18import static android.content.Intent.EXTRA_COMPONENT_NAME;
19import static android.content.Intent.EXTRA_USER;
20
Sunny Goyal5c20fdd2022-04-05 20:58:03 -070021import static com.android.launcher3.AbstractFloatingView.TYPE_ICON_SURFACE;
22
Sunny Goyal30ac97d2020-06-24 23:47:46 -070023import android.annotation.TargetApi;
24import android.content.ComponentName;
25import android.content.Intent;
26import android.graphics.RectF;
27import android.os.Build;
28import android.os.Bundle;
Sunny Goyal5c20fdd2022-04-05 20:58:03 -070029import android.os.Handler;
30import android.os.Looper;
Sunny Goyal30ac97d2020-06-24 23:47:46 -070031import android.os.Message;
Sunny Goyal5c20fdd2022-04-05 20:58:03 -070032import android.os.Messenger;
Sunny Goyal30ac97d2020-06-24 23:47:46 -070033import android.os.RemoteException;
34import android.os.UserHandle;
35import android.util.Log;
36import android.view.SurfaceControl;
37
Sunny Goyal5c20fdd2022-04-05 20:58:03 -070038import androidx.annotation.NonNull;
Sunny Goyal30ac97d2020-06-24 23:47:46 -070039import androidx.annotation.Nullable;
40
Sunny Goyal5c20fdd2022-04-05 20:58:03 -070041import com.android.launcher3.views.ActivityContext;
42
43import java.lang.ref.WeakReference;
44
Sunny Goyal30ac97d2020-06-24 23:47:46 -070045/**
46 * Class to encapsulate the handshake protocol between Launcher and gestureNav.
47 */
48public class GestureNavContract {
49
50 private static final String TAG = "GestureNavContract";
51
52 public static final String EXTRA_GESTURE_CONTRACT = "gesture_nav_contract_v1";
53 public static final String EXTRA_ICON_POSITION = "gesture_nav_contract_icon_position";
54 public static final String EXTRA_ICON_SURFACE = "gesture_nav_contract_surface_control";
55 public static final String EXTRA_REMOTE_CALLBACK = "android.intent.extra.REMOTE_CALLBACK";
Sunny Goyal5c20fdd2022-04-05 20:58:03 -070056 public static final String EXTRA_ON_FINISH_CALLBACK = "gesture_nav_contract_finish_callback";
Sunny Goyal30ac97d2020-06-24 23:47:46 -070057
58 public final ComponentName componentName;
59 public final UserHandle user;
60
61 private final Message mCallback;
62
63 public GestureNavContract(ComponentName componentName, UserHandle user, Message callback) {
64 this.componentName = componentName;
65 this.user = user;
66 this.mCallback = callback;
67 }
68
69 /**
70 * Sends the position information to the receiver
71 */
72 @TargetApi(Build.VERSION_CODES.R)
Sunny Goyal5c20fdd2022-04-05 20:58:03 -070073 public void sendEndPosition(RectF position, ActivityContext context,
74 @Nullable SurfaceControl surfaceControl) {
Sunny Goyal30ac97d2020-06-24 23:47:46 -070075 Bundle result = new Bundle();
76 result.putParcelable(EXTRA_ICON_POSITION, position);
77 result.putParcelable(EXTRA_ICON_SURFACE, surfaceControl);
Sunny Goyal5c20fdd2022-04-05 20:58:03 -070078 if (sMessageReceiver == null) {
79 sMessageReceiver = new StaticMessageReceiver();
80 }
81 result.putParcelable(EXTRA_ON_FINISH_CALLBACK, sMessageReceiver.setCurrentContext(context));
Sunny Goyal30ac97d2020-06-24 23:47:46 -070082
83 Message callback = Message.obtain();
84 callback.copyFrom(mCallback);
85 callback.setData(result);
86
87 try {
88 callback.replyTo.send(callback);
89 } catch (RemoteException e) {
90 Log.e(TAG, "Error sending icon position", e);
91 }
92 }
93
94 /**
95 * Clears and returns the GestureNavContract if it was present in the intent.
96 */
97 public static GestureNavContract fromIntent(Intent intent) {
98 if (!Utilities.ATLEAST_R) {
99 return null;
100 }
101 Bundle extras = intent.getBundleExtra(EXTRA_GESTURE_CONTRACT);
102 if (extras == null) {
103 return null;
104 }
105 intent.removeExtra(EXTRA_GESTURE_CONTRACT);
106
107 ComponentName componentName = extras.getParcelable(EXTRA_COMPONENT_NAME);
108 UserHandle userHandle = extras.getParcelable(EXTRA_USER);
109 Message callback = extras.getParcelable(EXTRA_REMOTE_CALLBACK);
110
111 if (componentName != null && userHandle != null && callback != null
112 && callback.replyTo != null) {
113 return new GestureNavContract(componentName, userHandle, callback);
114 }
115 return null;
116 }
Sunny Goyal5c20fdd2022-04-05 20:58:03 -0700117
118 /**
119 * Message used for receiving gesture nav contract information. We use a static messenger to
120 * avoid leaking too make binders in case the receiving launcher does not handle the contract
121 * properly.
122 */
123 private static StaticMessageReceiver sMessageReceiver = null;
124
125 private static class StaticMessageReceiver implements Handler.Callback {
126
127 private static final int MSG_CLOSE_LAST_TARGET = 0;
128
129 private final Messenger mMessenger =
130 new Messenger(new Handler(Looper.getMainLooper(), this));
131
132 private WeakReference<ActivityContext> mLastTarget = new WeakReference<>(null);
133
134 public Message setCurrentContext(ActivityContext context) {
135 mLastTarget = new WeakReference<>(context);
136
137 Message msg = Message.obtain();
138 msg.replyTo = mMessenger;
139 msg.what = MSG_CLOSE_LAST_TARGET;
140 return msg;
141 }
142
143 @Override
144 public boolean handleMessage(@NonNull Message message) {
145 if (message.what == MSG_CLOSE_LAST_TARGET) {
146 ActivityContext lastContext = mLastTarget.get();
147 if (lastContext != null) {
148 AbstractFloatingView.closeOpenViews(lastContext, false, TYPE_ICON_SURFACE);
149 }
150 return true;
151 }
152 return false;
153 }
154 }
Sunny Goyal30ac97d2020-06-24 23:47:46 -0700155}