1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
|
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.content.pm;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SystemApi;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Random;
/**
* Describes an externally resolvable instant application. There are three states that this class
* can represent: <p/>
* <ul>
* <li>
* The first, usable only for non http/s intents, implies that the resolver cannot
* immediately resolve this intent and would prefer that resolution be deferred to the
* instant app installer. Represent this state with {@link #InstantAppResolveInfo(Bundle)}.
* If the {@link android.content.Intent} has the scheme set to http/s and a set of digest
* prefixes were passed into one of the resolve methods in
* {@link android.app.InstantAppResolverService}, this state cannot be used.
* </li>
* <li>
* The second represents a partial match and is constructed with any of the other
* constructors. By setting one or more of the {@link Nullable}arguments to null, you
* communicate to the resolver in response to
* {@link android.app.InstantAppResolverService#onGetInstantAppResolveInfo(Intent, int[],
* String, InstantAppResolverService.InstantAppResolutionCallback)}
* that you need a 2nd round of resolution to complete the request.
* </li>
* <li>
* The third represents a complete match and is constructed with all @Nullable parameters
* populated.
* </li>
* </ul>
* @hide
*/
@SystemApi
public final class InstantAppResolveInfo implements Parcelable {
/** Algorithm that will be used to generate the domain digest */
private static final String SHA_ALGORITHM = "SHA-256";
private static final byte[] EMPTY_DIGEST = new byte[0];
private final InstantAppDigest mDigest;
private final String mPackageName;
/** The filters used to match domain */
private final List<InstantAppIntentFilter> mFilters;
/** The version code of the app that this class resolves to */
private final long mVersionCode;
/** Data about the app that should be passed along to the Instant App installer on resolve */
private final Bundle mExtras;
/**
* A flag that indicates that the resolver is aware that an app may match, but would prefer
* that the installer get the sanitized intent to decide.
*/
private final boolean mShouldLetInstallerDecide;
/** Constructor for intent-based InstantApp resolution results. */
public InstantAppResolveInfo(@NonNull InstantAppDigest digest, @Nullable String packageName,
@Nullable List<InstantAppIntentFilter> filters, int versionCode) {
this(digest, packageName, filters, (long) versionCode, null /* extras */);
}
/** Constructor for intent-based InstantApp resolution results with extras. */
public InstantAppResolveInfo(@NonNull InstantAppDigest digest, @Nullable String packageName,
@Nullable List<InstantAppIntentFilter> filters, long versionCode,
@Nullable Bundle extras) {
this(digest, packageName, filters, versionCode, extras, false);
}
/** Constructor for intent-based InstantApp resolution results by hostname. */
public InstantAppResolveInfo(@NonNull String hostName, @Nullable String packageName,
@Nullable List<InstantAppIntentFilter> filters) {
this(new InstantAppDigest(hostName), packageName, filters, -1 /*versionCode*/,
null /* extras */);
}
/**
* Constructor that indicates that resolution could be delegated to the installer when the
* sanitized intent contains enough information to resolve completely.
*/
public InstantAppResolveInfo(@Nullable Bundle extras) {
this(InstantAppDigest.UNDEFINED, null, null, -1, extras, true);
}
private InstantAppResolveInfo(@NonNull InstantAppDigest digest, @Nullable String packageName,
@Nullable List<InstantAppIntentFilter> filters, long versionCode,
@Nullable Bundle extras, boolean shouldLetInstallerDecide) {
// validate arguments
if ((packageName == null && (filters != null && filters.size() != 0))
|| (packageName != null && (filters == null || filters.size() == 0))) {
throw new IllegalArgumentException();
}
mDigest = digest;
if (filters != null) {
mFilters = new ArrayList<>(filters.size());
mFilters.addAll(filters);
} else {
mFilters = null;
}
mPackageName = packageName;
mVersionCode = versionCode;
mExtras = extras;
mShouldLetInstallerDecide = shouldLetInstallerDecide;
}
InstantAppResolveInfo(Parcel in) {
mShouldLetInstallerDecide = in.readBoolean();
mExtras = in.readBundle();
if (mShouldLetInstallerDecide) {
mDigest = InstantAppDigest.UNDEFINED;
mPackageName = null;
mFilters = Collections.emptyList();
mVersionCode = -1;
} else {
mDigest = in.readParcelable(null /*loader*/);
mPackageName = in.readString();
mFilters = new ArrayList<>();
in.readList(mFilters, null /*loader*/);
mVersionCode = in.readLong();
}
}
/**
* Returns true if the resolver is aware that an app may match, but would prefer
* that the installer get the sanitized intent to decide. This should not be true for
* resolutions that include a host and will be ignored in such cases.
*/
public boolean shouldLetInstallerDecide() {
return mShouldLetInstallerDecide;
}
public byte[] getDigestBytes() {
return mDigest.mDigestBytes.length > 0 ? mDigest.getDigestBytes()[0] : EMPTY_DIGEST;
}
public int getDigestPrefix() {
return mDigest.getDigestPrefix()[0];
}
public String getPackageName() {
return mPackageName;
}
public List<InstantAppIntentFilter> getIntentFilters() {
return mFilters;
}
/**
* @deprecated Use {@link #getLongVersionCode} instead.
*/
@Deprecated
public int getVersionCode() {
return (int) (mVersionCode & 0xffffffff);
}
public long getLongVersionCode() {
return mVersionCode;
}
@Nullable
public Bundle getExtras() {
return mExtras;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel out, int flags) {
out.writeBoolean(mShouldLetInstallerDecide);
out.writeBundle(mExtras);
if (mShouldLetInstallerDecide) {
return;
}
out.writeParcelable(mDigest, flags);
out.writeString(mPackageName);
out.writeList(mFilters);
out.writeLong(mVersionCode);
}
public static final @android.annotation.NonNull Parcelable.Creator<InstantAppResolveInfo> CREATOR
= new Parcelable.Creator<InstantAppResolveInfo>() {
public InstantAppResolveInfo createFromParcel(Parcel in) {
return new InstantAppResolveInfo(in);
}
public InstantAppResolveInfo[] newArray(int size) {
return new InstantAppResolveInfo[size];
}
};
/**
* Helper class to generate and store each of the digests and prefixes
* sent to the Instant App Resolver.
* <p>
* Since intent filters may want to handle multiple hosts within a
* domain [eg “*.google.com”], the resolver is presented with multiple
* hash prefixes. For example, "a.b.c.d.e" generates digests for
* "d.e", "c.d.e", "b.c.d.e" and "a.b.c.d.e".
*
* @hide
*/
@SystemApi
public static final class InstantAppDigest implements Parcelable {
static final int DIGEST_MASK = 0xfffff000;
/**
* A special instance that represents and undefined digest used for cases that a host was
* not provided or is irrelevant to the response.
*/
public static final InstantAppDigest UNDEFINED =
new InstantAppDigest(new byte[][]{}, new int[]{});
private static Random sRandom = null;
static {
try {
sRandom = SecureRandom.getInstance("SHA1PRNG");
} catch (NoSuchAlgorithmException e) {
// oh well
sRandom = new Random();
}
}
/** Full digest of the domain hashes */
private final byte[][] mDigestBytes;
/** The first 5 bytes of the domain hashes */
private final int[] mDigestPrefix;
/** The first 5 bytes of the domain hashes interspersed with random data */
private int[] mDigestPrefixSecure;
public InstantAppDigest(@NonNull String hostName) {
this(hostName, -1 /*maxDigests*/);
}
/** @hide */
public InstantAppDigest(@NonNull String hostName, int maxDigests) {
if (hostName == null) {
throw new IllegalArgumentException();
}
mDigestBytes = generateDigest(hostName.toLowerCase(Locale.ENGLISH), maxDigests);
mDigestPrefix = new int[mDigestBytes.length];
for (int i = 0; i < mDigestBytes.length; i++) {
mDigestPrefix[i] =
((mDigestBytes[i][0] & 0xFF) << 24
| (mDigestBytes[i][1] & 0xFF) << 16
| (mDigestBytes[i][2] & 0xFF) << 8
| (mDigestBytes[i][3] & 0xFF) << 0)
& DIGEST_MASK;
}
}
private InstantAppDigest(byte[][] digestBytes, int[] prefix) {
this.mDigestPrefix = prefix;
this.mDigestBytes = digestBytes;
}
private static byte[][] generateDigest(String hostName, int maxDigests) {
ArrayList<byte[]> digests = new ArrayList<>();
try {
final MessageDigest digest = MessageDigest.getInstance(SHA_ALGORITHM);
if (maxDigests <= 0) {
final byte[] hostBytes = hostName.getBytes();
digests.add(digest.digest(hostBytes));
} else {
int prevDot = hostName.lastIndexOf('.');
prevDot = hostName.lastIndexOf('.', prevDot - 1);
// shortcut for short URLs
if (prevDot < 0) {
digests.add(digest.digest(hostName.getBytes()));
} else {
byte[] hostBytes =
hostName.substring(prevDot + 1, hostName.length()).getBytes();
digests.add(digest.digest(hostBytes));
int digestCount = 1;
while (prevDot >= 0 && digestCount < maxDigests) {
prevDot = hostName.lastIndexOf('.', prevDot - 1);
hostBytes =
hostName.substring(prevDot + 1, hostName.length()).getBytes();
digests.add(digest.digest(hostBytes));
digestCount++;
}
}
}
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException("could not find digest algorithm");
}
return digests.toArray(new byte[digests.size()][]);
}
InstantAppDigest(Parcel in) {
final int digestCount = in.readInt();
if (digestCount == -1) {
mDigestBytes = null;
} else {
mDigestBytes = new byte[digestCount][];
for (int i = 0; i < digestCount; i++) {
mDigestBytes[i] = in.createByteArray();
}
}
mDigestPrefix = in.createIntArray();
mDigestPrefixSecure = in.createIntArray();
}
public byte[][] getDigestBytes() {
return mDigestBytes;
}
public int[] getDigestPrefix() {
return mDigestPrefix;
}
/**
* Returns a digest prefix with additional random prefixes interspersed.
* @hide
*/
public int[] getDigestPrefixSecure() {
if (this == InstantAppResolveInfo.InstantAppDigest.UNDEFINED) {
return getDigestPrefix();
} else if (mDigestPrefixSecure == null) {
// let's generate some random data to intersperse throughout the set of prefixes
final int realSize = getDigestPrefix().length;
final int manufacturedSize = realSize + 10 + sRandom.nextInt(10);
mDigestPrefixSecure = Arrays.copyOf(getDigestPrefix(), manufacturedSize);
for (int i = realSize; i < manufacturedSize; i++) {
mDigestPrefixSecure[i] = sRandom.nextInt() & DIGEST_MASK;
}
Arrays.sort(mDigestPrefixSecure);
}
return mDigestPrefixSecure;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel out, int flags) {
final boolean isUndefined = this == UNDEFINED;
out.writeBoolean(isUndefined);
if (isUndefined) {
return;
}
if (mDigestBytes == null) {
out.writeInt(-1);
} else {
out.writeInt(mDigestBytes.length);
for (int i = 0; i < mDigestBytes.length; i++) {
out.writeByteArray(mDigestBytes[i]);
}
}
out.writeIntArray(mDigestPrefix);
out.writeIntArray(mDigestPrefixSecure);
}
@SuppressWarnings("hiding")
public static final @android.annotation.NonNull Parcelable.Creator<InstantAppDigest> CREATOR =
new Parcelable.Creator<InstantAppDigest>() {
@Override
public InstantAppDigest createFromParcel(Parcel in) {
if (in.readBoolean() /* is undefined */) {
return UNDEFINED;
}
return new InstantAppDigest(in);
}
@Override
public InstantAppDigest[] newArray(int size) {
return new InstantAppDigest[size];
}
};
}
}
|