Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.service.autofill; |
| 17 | |
Felipe Leme | 63c601a | 2017-09-27 17:40:30 -0700 | [diff] [blame] | 18 | import static android.view.autofill.Helper.sDebug; |
| 19 | |
Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 20 | import android.annotation.NonNull; |
Felipe Leme | 63c601a | 2017-09-27 17:40:30 -0700 | [diff] [blame] | 21 | import android.annotation.TestApi; |
Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 22 | import android.os.Parcelable; |
Felipe Leme | 63c601a | 2017-09-27 17:40:30 -0700 | [diff] [blame] | 23 | import android.util.Log; |
| 24 | import android.util.Pair; |
Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 25 | import android.widget.RemoteViews; |
| 26 | |
Felipe Leme | 63c601a | 2017-09-27 17:40:30 -0700 | [diff] [blame] | 27 | import java.util.ArrayList; |
| 28 | |
Philip P. Moltmann | 3858aa6 | 2017-07-11 15:22:14 -0700 | [diff] [blame] | 29 | /** |
| 30 | * Superclass of all transformation the system understands. As this is not public all |
| 31 | * subclasses have to implement {@link Transformation} again. |
| 32 | * |
| 33 | * @hide |
| 34 | */ |
Felipe Leme | 63c601a | 2017-09-27 17:40:30 -0700 | [diff] [blame] | 35 | @TestApi |
| 36 | public abstract class InternalTransformation implements Transformation, Parcelable { |
| 37 | |
| 38 | private static final String TAG = "InternalTransformation"; |
Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 39 | |
| 40 | /** |
Philip P. Moltmann | 3858aa6 | 2017-07-11 15:22:14 -0700 | [diff] [blame] | 41 | * Applies this transformation to a child view of a {@link android.widget.RemoteViews |
| 42 | * presentation template}. |
Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 43 | * |
| 44 | * @param finder object used to find the value of a field in the screen. |
| 45 | * @param template the {@link RemoteViews presentation template}. |
| 46 | * @param childViewId resource id of the child view inside the template. |
Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 47 | */ |
| 48 | abstract void apply(@NonNull ValueFinder finder, @NonNull RemoteViews template, |
Felipe Leme | 22101ca | 2017-07-18 08:58:50 -0700 | [diff] [blame] | 49 | int childViewId) throws Exception; |
Felipe Leme | 63c601a | 2017-09-27 17:40:30 -0700 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Applies multiple transformations to the children views of a |
| 53 | * {@link android.widget.RemoteViews presentation template}. |
| 54 | * |
| 55 | * @param finder object used to find the value of a field in the screen. |
| 56 | * @param template the {@link RemoteViews presentation template}. |
| 57 | * @param transformations map of resource id of the child view inside the template to |
| 58 | * transformation. |
Felipe Leme | 63c601a | 2017-09-27 17:40:30 -0700 | [diff] [blame] | 59 | */ |
| 60 | public static boolean batchApply(@NonNull ValueFinder finder, @NonNull RemoteViews template, |
| 61 | @NonNull ArrayList<Pair<Integer, InternalTransformation>> transformations) { |
| 62 | final int size = transformations.size(); |
| 63 | if (sDebug) Log.d(TAG, "getPresentation(): applying " + size + " transformations"); |
| 64 | for (int i = 0; i < size; i++) { |
| 65 | final Pair<Integer, InternalTransformation> pair = transformations.get(i); |
| 66 | final int id = pair.first; |
| 67 | final InternalTransformation transformation = pair.second; |
| 68 | if (sDebug) Log.d(TAG, "#" + i + ": " + transformation); |
| 69 | |
| 70 | try { |
| 71 | transformation.apply(finder, template, id); |
| 72 | } catch (Exception e) { |
| 73 | // Do not log full exception to avoid PII leaking |
| 74 | Log.e(TAG, "Could not apply transformation " + transformation + ": " |
| 75 | + e.getClass()); |
| 76 | return false; |
| 77 | } |
| 78 | } |
| 79 | return true; |
| 80 | } |
Felipe Leme | 979013d | 2017-06-22 10:59:23 -0700 | [diff] [blame] | 81 | } |