Felipe Leme | a4f39cd | 2019-02-19 15:08:59 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | package android.content; |
| 17 | |
| 18 | import android.annotation.NonNull; |
| 19 | import android.annotation.TestApi; |
| 20 | import android.app.ActivityThread; |
| 21 | import android.os.Parcel; |
| 22 | import android.os.Parcelable; |
| 23 | import android.util.Log; |
| 24 | import android.view.autofill.AutofillManager; |
| 25 | |
| 26 | import java.io.PrintWriter; |
| 27 | |
| 28 | /** |
| 29 | * Autofill options for a given package. |
| 30 | * |
| 31 | * <p>This object is created by the Autofill System Service and passed back to the app when the |
| 32 | * application is created. |
| 33 | * |
| 34 | * @hide |
| 35 | */ |
| 36 | @TestApi |
| 37 | public final class AutofillOptions implements Parcelable { |
| 38 | |
| 39 | private static final String TAG = AutofillOptions.class.getSimpleName(); |
| 40 | |
| 41 | /** |
| 42 | * Logging level for {@code logcat} statements. |
| 43 | */ |
| 44 | public final int loggingLevel; |
| 45 | |
| 46 | /** |
| 47 | * Whether compatibility mode is enabled for the package. |
| 48 | */ |
| 49 | public final boolean compatModeEnabled; |
| 50 | |
| 51 | /** |
| 52 | * Whether package is whitelisted for augmented autofill. |
| 53 | */ |
| 54 | public boolean augmentedEnabled; |
| 55 | // TODO(b/123100824): add (optional) list of activities |
| 56 | |
| 57 | public AutofillOptions(int loggingLevel, boolean compatModeEnabled) { |
| 58 | this.loggingLevel = loggingLevel; |
| 59 | this.compatModeEnabled = compatModeEnabled; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * @hide |
| 64 | */ |
| 65 | @TestApi |
| 66 | public static AutofillOptions forWhitelistingItself() { |
| 67 | final ActivityThread at = ActivityThread.currentActivityThread(); |
| 68 | if (at == null) { |
| 69 | throw new IllegalStateException("No ActivityThread"); |
| 70 | } |
| 71 | |
| 72 | final String packageName = at.getApplication().getPackageName(); |
| 73 | |
| 74 | if (!"android.autofillservice.cts".equals(packageName)) { |
| 75 | Log.e(TAG, "forWhitelistingItself(): called by " + packageName); |
| 76 | throw new SecurityException("Thou shall not pass!"); |
| 77 | } |
| 78 | |
| 79 | final AutofillOptions options = new AutofillOptions( |
| 80 | AutofillManager.FLAG_ADD_CLIENT_VERBOSE, /* compatModeAllowed= */ true); |
| 81 | options.augmentedEnabled = true; |
| 82 | // Always log, as it's used by test only |
| 83 | Log.i(TAG, "forWhitelistingItself(" + packageName + "): " + options); |
| 84 | |
| 85 | return options; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public String toString() { |
| 90 | return "AutofillOptions [loggingLevel=" + loggingLevel + ", compatMode=" |
| 91 | + compatModeEnabled + ", augmentedEnabled=" + augmentedEnabled + "]"; |
| 92 | } |
| 93 | |
| 94 | /** @hide */ |
| 95 | public void dumpShort(@NonNull PrintWriter pw) { |
| 96 | pw.print("logLvl="); pw.print(loggingLevel); |
| 97 | pw.print(", compatMode="); pw.print(compatModeEnabled); |
| 98 | pw.print(", augmented="); pw.print(augmentedEnabled); |
| 99 | } |
| 100 | |
| 101 | @Override |
| 102 | public int describeContents() { |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public void writeToParcel(Parcel parcel, int flags) { |
| 108 | parcel.writeInt(loggingLevel); |
| 109 | parcel.writeBoolean(compatModeEnabled); |
| 110 | parcel.writeBoolean(augmentedEnabled); |
| 111 | } |
| 112 | |
| 113 | public static final Parcelable.Creator<AutofillOptions> CREATOR = |
| 114 | new Parcelable.Creator<AutofillOptions>() { |
| 115 | |
| 116 | @Override |
| 117 | public AutofillOptions createFromParcel(Parcel parcel) { |
| 118 | final int loggingLevel = parcel.readInt(); |
| 119 | final boolean compatMode = parcel.readBoolean(); |
| 120 | final AutofillOptions options = new AutofillOptions(loggingLevel, compatMode); |
| 121 | options.augmentedEnabled = parcel.readBoolean(); |
| 122 | return options; |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public AutofillOptions[] newArray(int size) { |
| 127 | return new AutofillOptions[size]; |
| 128 | } |
| 129 | }; |
| 130 | } |