| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.window; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.os.Bundle; |
| 22 |
import android.os.Handler; |
| 23 |
import android.os.Parcel; |
| 24 |
import android.os.Parcelable; |
| 25 |
import android.os.RemoteException; |
| 26 |
import android.os.ResultReceiver; |
| 27 |
import android.util.Log; |
| 28 |
import android.view.ViewRootImpl; |
| 29 |
|
| 30 |
import java.util.ArrayList; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public class ImeOnBackInvokedDispatcher implements OnBackInvokedDispatcher, Parcelable { |
| 44 |
|
| 45 |
private static final String TAG = "ImeBackDispatcher"; |
| 46 |
static final String RESULT_KEY_ID = "id"; |
| 47 |
static final String RESULT_KEY_CALLBACK = "callback"; |
| 48 |
static final String RESULT_KEY_PRIORITY = "priority"; |
| 49 |
static final int RESULT_CODE_REGISTER = 0; |
| 50 |
static final int RESULT_CODE_UNREGISTER = 1; |
| 51 |
@NonNull |
| 52 |
private final ResultReceiver mResultReceiver; |
| 53 |
|
| 54 |
public ImeOnBackInvokedDispatcher(Handler handler) { |
| 55 |
mResultReceiver = new ResultReceiver(handler) { |
| 56 |
@Override |
| 57 |
public void onReceiveResult(int resultCode, Bundle resultData) { |
| 58 |
WindowOnBackInvokedDispatcher dispatcher = getReceivingDispatcher(); |
| 59 |
if (dispatcher != null) { |
| 60 |
receive(resultCode, resultData, dispatcher); |
| 61 |
} |
| 62 |
} |
| 63 |
}; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
@Nullable |
| 71 |
protected WindowOnBackInvokedDispatcher getReceivingDispatcher() { |
| 72 |
return null; |
| 73 |
} |
| 74 |
|
| 75 |
ImeOnBackInvokedDispatcher(Parcel in) { |
| 76 |
mResultReceiver = in.readTypedObject(ResultReceiver.CREATOR); |
| 77 |
} |
| 78 |
|
| 79 |
@Override |
| 80 |
public void registerOnBackInvokedCallback( |
| 81 |
@OnBackInvokedDispatcher.Priority int priority, |
| 82 |
@NonNull OnBackInvokedCallback callback) { |
| 83 |
final Bundle bundle = new Bundle(); |
| 84 |
|
| 85 |
final IOnBackInvokedCallback iCallback = |
| 86 |
new WindowOnBackInvokedDispatcher.OnBackInvokedCallbackWrapper(callback); |
| 87 |
bundle.putBinder(RESULT_KEY_CALLBACK, iCallback.asBinder()); |
| 88 |
bundle.putInt(RESULT_KEY_PRIORITY, priority); |
| 89 |
bundle.putInt(RESULT_KEY_ID, callback.hashCode()); |
| 90 |
mResultReceiver.send(RESULT_CODE_REGISTER, bundle); |
| 91 |
} |
| 92 |
|
| 93 |
@Override |
| 94 |
public void unregisterOnBackInvokedCallback( |
| 95 |
@NonNull OnBackInvokedCallback callback) { |
| 96 |
Bundle bundle = new Bundle(); |
| 97 |
bundle.putInt(RESULT_KEY_ID, callback.hashCode()); |
| 98 |
mResultReceiver.send(RESULT_CODE_UNREGISTER, bundle); |
| 99 |
} |
| 100 |
|
| 101 |
@Override |
| 102 |
public int describeContents() { |
| 103 |
return 0; |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public void writeToParcel(@NonNull Parcel dest, int flags) { |
| 108 |
dest.writeTypedObject(mResultReceiver, flags); |
| 109 |
} |
| 110 |
|
| 111 |
@NonNull |
| 112 |
public static final Parcelable.Creator<ImeOnBackInvokedDispatcher> CREATOR = |
| 113 |
new Parcelable.Creator<ImeOnBackInvokedDispatcher>() { |
| 114 |
public ImeOnBackInvokedDispatcher createFromParcel(Parcel in) { |
| 115 |
return new ImeOnBackInvokedDispatcher(in); |
| 116 |
} |
| 117 |
public ImeOnBackInvokedDispatcher[] newArray(int size) { |
| 118 |
return new ImeOnBackInvokedDispatcher[size]; |
| 119 |
} |
| 120 |
}; |
| 121 |
|
| 122 |
private final ArrayList<ImeOnBackInvokedCallback> mImeCallbacks = new ArrayList<>(); |
| 123 |
|
| 124 |
private void receive( |
| 125 |
int resultCode, Bundle resultData, |
| 126 |
@NonNull WindowOnBackInvokedDispatcher receivingDispatcher) { |
| 127 |
final int callbackId = resultData.getInt(RESULT_KEY_ID); |
| 128 |
if (resultCode == RESULT_CODE_REGISTER) { |
| 129 |
int priority = resultData.getInt(RESULT_KEY_PRIORITY); |
| 130 |
final IOnBackInvokedCallback callback = IOnBackInvokedCallback.Stub.asInterface( |
| 131 |
resultData.getBinder(RESULT_KEY_CALLBACK)); |
| 132 |
registerReceivedCallback( |
| 133 |
callback, priority, callbackId, receivingDispatcher); |
| 134 |
} else if (resultCode == RESULT_CODE_UNREGISTER) { |
| 135 |
unregisterReceivedCallback(callbackId, receivingDispatcher); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
private void registerReceivedCallback( |
| 140 |
@NonNull IOnBackInvokedCallback iCallback, |
| 141 |
@OnBackInvokedDispatcher.Priority int priority, |
| 142 |
int callbackId, |
| 143 |
@NonNull WindowOnBackInvokedDispatcher receivingDispatcher) { |
| 144 |
final ImeOnBackInvokedCallback imeCallback = |
| 145 |
new ImeOnBackInvokedCallback(iCallback, callbackId, priority); |
| 146 |
mImeCallbacks.add(imeCallback); |
| 147 |
receivingDispatcher.registerOnBackInvokedCallbackUnchecked(imeCallback, priority); |
| 148 |
} |
| 149 |
|
| 150 |
private void unregisterReceivedCallback( |
| 151 |
int callbackId, OnBackInvokedDispatcher receivingDispatcher) { |
| 152 |
ImeOnBackInvokedCallback callback = null; |
| 153 |
for (ImeOnBackInvokedCallback imeCallback : mImeCallbacks) { |
| 154 |
if (imeCallback.getId() == callbackId) { |
| 155 |
callback = imeCallback; |
| 156 |
break; |
| 157 |
} |
| 158 |
} |
| 159 |
if (callback == null) { |
| 160 |
Log.e(TAG, "Ime callback not found. Ignoring unregisterReceivedCallback. " |
| 161 |
+ "callbackId: " + callbackId); |
| 162 |
return; |
| 163 |
} |
| 164 |
receivingDispatcher.unregisterOnBackInvokedCallback(callback); |
| 165 |
mImeCallbacks.remove(callback); |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
public void clear() { |
| 170 |
|
| 171 |
if (getReceivingDispatcher() != null) { |
| 172 |
for (ImeOnBackInvokedCallback callback : mImeCallbacks) { |
| 173 |
getReceivingDispatcher().unregisterOnBackInvokedCallback(callback); |
| 174 |
} |
| 175 |
} |
| 176 |
mImeCallbacks.clear(); |
| 177 |
} |
| 178 |
|
| 179 |
static class ImeOnBackInvokedCallback implements OnBackInvokedCallback { |
| 180 |
@NonNull |
| 181 |
private final IOnBackInvokedCallback mIOnBackInvokedCallback; |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
private final int mId; |
| 187 |
private final int mPriority; |
| 188 |
|
| 189 |
ImeOnBackInvokedCallback(@NonNull IOnBackInvokedCallback iCallback, int id, |
| 190 |
@Priority int priority) { |
| 191 |
mIOnBackInvokedCallback = iCallback; |
| 192 |
mId = id; |
| 193 |
mPriority = priority; |
| 194 |
} |
| 195 |
|
| 196 |
@Override |
| 197 |
public void onBackInvoked() { |
| 198 |
try { |
| 199 |
if (mIOnBackInvokedCallback != null) { |
| 200 |
mIOnBackInvokedCallback.onBackInvoked(); |
| 201 |
} |
| 202 |
} catch (RemoteException e) { |
| 203 |
Log.e(TAG, "Exception when invoking forwarded callback. e: ", e); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
private int getId() { |
| 208 |
return mId; |
| 209 |
} |
| 210 |
|
| 211 |
IOnBackInvokedCallback getIOnBackInvokedCallback() { |
| 212 |
return mIOnBackInvokedCallback; |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
public void switchRootView(ViewRootImpl previous, ViewRootImpl current) { |
| 224 |
for (ImeOnBackInvokedCallback imeCallback : mImeCallbacks) { |
| 225 |
if (previous != null) { |
| 226 |
previous.getOnBackInvokedDispatcher().unregisterOnBackInvokedCallback(imeCallback); |
| 227 |
} |
| 228 |
if (current != null) { |
| 229 |
current.getOnBackInvokedDispatcher().registerOnBackInvokedCallbackUnchecked( |
| 230 |
imeCallback, imeCallback.mPriority); |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
} |