| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.app; |
| 18 |
|
| 19 |
import android.annotation.MainThread; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.Nullable; |
| 22 |
import android.annotation.SystemApi; |
| 23 |
import android.content.Context; |
| 24 |
import android.content.Intent; |
| 25 |
import android.content.pm.InstantAppRequestInfo; |
| 26 |
import android.content.pm.InstantAppResolveInfo; |
| 27 |
import android.os.Build; |
| 28 |
import android.os.Bundle; |
| 29 |
import android.os.Handler; |
| 30 |
import android.os.IBinder; |
| 31 |
import android.os.IRemoteCallback; |
| 32 |
import android.os.Looper; |
| 33 |
import android.os.Message; |
| 34 |
import android.os.RemoteException; |
| 35 |
import android.os.UserHandle; |
| 36 |
import android.util.Log; |
| 37 |
import android.util.Slog; |
| 38 |
|
| 39 |
import com.android.internal.annotations.VisibleForTesting; |
| 40 |
import com.android.internal.os.SomeArgs; |
| 41 |
|
| 42 |
import java.util.Arrays; |
| 43 |
import java.util.Collections; |
| 44 |
import java.util.List; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
@SystemApi |
| 51 |
public abstract class InstantAppResolverService extends Service { |
| 52 |
private static final boolean DEBUG_INSTANT = Build.IS_DEBUGGABLE; |
| 53 |
private static final String TAG = "PackageManager"; |
| 54 |
|
| 55 |
|
| 56 |
public static final String EXTRA_RESOLVE_INFO = "android.app.extra.RESOLVE_INFO"; |
| 57 |
|
| 58 |
public static final String EXTRA_SEQUENCE = "android.app.extra.SEQUENCE"; |
| 59 |
Handler mHandler; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
@Deprecated |
| 70 |
public void onGetInstantAppResolveInfo(@Nullable int[] digestPrefix, @NonNull String token, |
| 71 |
@NonNull InstantAppResolutionCallback callback) { |
| 72 |
throw new IllegalStateException("Must define onGetInstantAppResolveInfo"); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
@Deprecated |
| 85 |
public void onGetInstantAppIntentFilter(@Nullable int[] digestPrefix, @NonNull String token, |
| 86 |
@NonNull InstantAppResolutionCallback callback) { |
| 87 |
throw new IllegalStateException("Must define onGetInstantAppIntentFilter"); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
@Deprecated |
| 115 |
public void onGetInstantAppResolveInfo(@NonNull Intent sanitizedIntent, |
| 116 |
@Nullable int[] hostDigestPrefix, @NonNull String token, |
| 117 |
@NonNull InstantAppResolutionCallback callback) { |
| 118 |
|
| 119 |
if (sanitizedIntent.isWebIntent()) { |
| 120 |
onGetInstantAppResolveInfo(hostDigestPrefix, token, callback); |
| 121 |
} else { |
| 122 |
callback.onInstantAppResolveInfo(Collections.emptyList()); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
@Deprecated |
| 146 |
public void onGetInstantAppIntentFilter(@NonNull Intent sanitizedIntent, |
| 147 |
@Nullable int[] hostDigestPrefix, |
| 148 |
@NonNull String token, @NonNull InstantAppResolutionCallback callback) { |
| 149 |
Log.e(TAG, "New onGetInstantAppIntentFilter is not overridden"); |
| 150 |
|
| 151 |
if (sanitizedIntent.isWebIntent()) { |
| 152 |
onGetInstantAppIntentFilter(hostDigestPrefix, token, callback); |
| 153 |
} else { |
| 154 |
callback.onInstantAppResolveInfo(Collections.emptyList()); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
@Deprecated |
| 183 |
public void onGetInstantAppResolveInfo(@NonNull Intent sanitizedIntent, |
| 184 |
@Nullable int[] hostDigestPrefix, @NonNull UserHandle userHandle, |
| 185 |
@NonNull String token, @NonNull InstantAppResolutionCallback callback) { |
| 186 |
|
| 187 |
onGetInstantAppResolveInfo(sanitizedIntent, hostDigestPrefix, token, callback); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
@Deprecated |
| 210 |
public void onGetInstantAppIntentFilter(@NonNull Intent sanitizedIntent, |
| 211 |
@Nullable int[] hostDigestPrefix, @NonNull UserHandle userHandle, |
| 212 |
@NonNull String token, @NonNull InstantAppResolutionCallback callback) { |
| 213 |
|
| 214 |
onGetInstantAppIntentFilter(sanitizedIntent, hostDigestPrefix, token, callback); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
@MainThread |
| 230 |
public void onGetInstantAppResolveInfo(@NonNull InstantAppRequestInfo request, |
| 231 |
@NonNull InstantAppResolutionCallback callback) { |
| 232 |
|
| 233 |
onGetInstantAppResolveInfo(request.getIntent(), request.getHostDigestPrefix(), |
| 234 |
request.getUserHandle(), request.getToken(), callback); |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
@MainThread |
| 247 |
public void onGetInstantAppIntentFilter(@NonNull InstantAppRequestInfo request, |
| 248 |
@NonNull InstantAppResolutionCallback callback) { |
| 249 |
|
| 250 |
onGetInstantAppIntentFilter(request.getIntent(), request.getHostDigestPrefix(), |
| 251 |
request.getUserHandle(), request.getToken(), callback); |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
Looper getLooper() { |
| 258 |
return getBaseContext().getMainLooper(); |
| 259 |
} |
| 260 |
|
| 261 |
@Override |
| 262 |
public final void attachBaseContext(Context base) { |
| 263 |
super.attachBaseContext(base); |
| 264 |
mHandler = new ServiceHandler(getLooper()); |
| 265 |
} |
| 266 |
|
| 267 |
@Override |
| 268 |
public final IBinder onBind(Intent intent) { |
| 269 |
return new IInstantAppResolver.Stub() { |
| 270 |
@Override |
| 271 |
public void getInstantAppResolveInfoList(InstantAppRequestInfo request, int sequence, |
| 272 |
IRemoteCallback callback) { |
| 273 |
if (DEBUG_INSTANT) { |
| 274 |
Slog.v(TAG, "[" + request.getToken() + "] Phase1 called; posting"); |
| 275 |
} |
| 276 |
final SomeArgs args = SomeArgs.obtain(); |
| 277 |
args.arg1 = request; |
| 278 |
args.arg2 = callback; |
| 279 |
mHandler.obtainMessage(ServiceHandler.MSG_GET_INSTANT_APP_RESOLVE_INFO, sequence, |
| 280 |
0, args).sendToTarget(); |
| 281 |
} |
| 282 |
|
| 283 |
@Override |
| 284 |
public void getInstantAppIntentFilterList(InstantAppRequestInfo request, |
| 285 |
IRemoteCallback callback) { |
| 286 |
if (DEBUG_INSTANT) { |
| 287 |
Slog.v(TAG, "[" + request.getToken() + "] Phase2 called; posting"); |
| 288 |
} |
| 289 |
final SomeArgs args = SomeArgs.obtain(); |
| 290 |
args.arg1 = request; |
| 291 |
args.arg2 = callback; |
| 292 |
mHandler.obtainMessage(ServiceHandler.MSG_GET_INSTANT_APP_INTENT_FILTER, args) |
| 293 |
.sendToTarget(); |
| 294 |
} |
| 295 |
}; |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
public static final class InstantAppResolutionCallback { |
| 302 |
private final IRemoteCallback mCallback; |
| 303 |
private final int mSequence; |
| 304 |
|
| 305 |
|
| 306 |
@VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) |
| 307 |
public InstantAppResolutionCallback(int sequence, IRemoteCallback callback) { |
| 308 |
mCallback = callback; |
| 309 |
mSequence = sequence; |
| 310 |
} |
| 311 |
|
| 312 |
public void onInstantAppResolveInfo(List<InstantAppResolveInfo> resolveInfo) { |
| 313 |
final Bundle data = new Bundle(); |
| 314 |
data.putParcelableList(EXTRA_RESOLVE_INFO, resolveInfo); |
| 315 |
data.putInt(EXTRA_SEQUENCE, mSequence); |
| 316 |
try { |
| 317 |
mCallback.sendResult(data); |
| 318 |
} catch (RemoteException e) { |
| 319 |
} |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
private final class ServiceHandler extends Handler { |
| 324 |
public static final int MSG_GET_INSTANT_APP_RESOLVE_INFO = 1; |
| 325 |
public static final int MSG_GET_INSTANT_APP_INTENT_FILTER = 2; |
| 326 |
public ServiceHandler(Looper looper) { |
| 327 |
super(looper, null , true ); |
| 328 |
} |
| 329 |
|
| 330 |
@Override |
| 331 |
@SuppressWarnings("unchecked") |
| 332 |
public void handleMessage(Message message) { |
| 333 |
final int action = message.what; |
| 334 |
switch (action) { |
| 335 |
case MSG_GET_INSTANT_APP_RESOLVE_INFO: { |
| 336 |
final SomeArgs args = (SomeArgs) message.obj; |
| 337 |
final InstantAppRequestInfo request = (InstantAppRequestInfo) args.arg1; |
| 338 |
final IRemoteCallback callback = (IRemoteCallback) args.arg2; |
| 339 |
args.recycle(); |
| 340 |
final int sequence = message.arg1; |
| 341 |
if (DEBUG_INSTANT) { |
| 342 |
Slog.d(TAG, "[" + request.getToken() + "] Phase1 request;" |
| 343 |
+ " prefix: " + Arrays.toString(request.getHostDigestPrefix()) |
| 344 |
+ ", userId: " + request.getUserHandle().getIdentifier()); |
| 345 |
} |
| 346 |
onGetInstantAppResolveInfo(request, |
| 347 |
new InstantAppResolutionCallback(sequence, callback)); |
| 348 |
} break; |
| 349 |
|
| 350 |
case MSG_GET_INSTANT_APP_INTENT_FILTER: { |
| 351 |
final SomeArgs args = (SomeArgs) message.obj; |
| 352 |
final InstantAppRequestInfo request = (InstantAppRequestInfo) args.arg1; |
| 353 |
final IRemoteCallback callback = (IRemoteCallback) args.arg2; |
| 354 |
args.recycle(); |
| 355 |
if (DEBUG_INSTANT) { |
| 356 |
Slog.d(TAG, "[" + request.getToken() + "] Phase2 request;" |
| 357 |
+ " prefix: " + Arrays.toString(request.getHostDigestPrefix()) |
| 358 |
+ ", userId: " + request.getUserHandle().getIdentifier()); |
| 359 |
} |
| 360 |
onGetInstantAppIntentFilter(request, |
| 361 |
new InstantAppResolutionCallback(-1 , callback)); |
| 362 |
} |
| 363 |
break; |
| 364 |
|
| 365 |
default: { |
| 366 |
throw new IllegalArgumentException("Unknown message: " + action); |
| 367 |
} |
| 368 |
} |
| 369 |
} |
| 370 |
} |
| 371 |
} |