Helper.java revision 9a2e6058df51e94417dcedd4c7dd2a0cc380dcb1
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 17package com.android.server.autofill; 18 19import android.annotation.NonNull; 20import android.app.assist.AssistStructure; 21import android.app.assist.AssistStructure.ViewNode; 22import android.os.Bundle; 23import android.util.DebugUtils; 24import android.view.autofill.AutofillId; 25import android.view.autofill.AutofillManager; 26 27import java.util.Arrays; 28import java.util.Objects; 29import java.util.Set; 30 31public final class Helper { 32 33 /** 34 * Defines a logging flag that can be dynamically changed at runtime using 35 * {@code cmd autofill debug [on|off]}. 36 */ 37 public static boolean sDebug = false; 38 39 /** 40 * Defines a logging flag that can be dynamically changed at runtime using 41 * {@code cmd autofill verbose [on|off]}. 42 */ 43 public static boolean sVerbose = false; 44 45 private Helper() { 46 throw new UnsupportedOperationException("contains static members only"); 47 } 48 49 static void append(StringBuilder builder, Bundle bundle) { 50 if (bundle == null || !sVerbose) { 51 builder.append("null"); 52 return; 53 } 54 final Set<String> keySet = bundle.keySet(); 55 builder.append("[Bundle with ").append(keySet.size()).append(" extras:"); 56 for (String key : keySet) { 57 final Object value = bundle.get(key); 58 builder.append(' ').append(key).append('='); 59 builder.append((value instanceof Object[]) 60 ? Arrays.toString((Objects[]) value) : value); 61 } 62 builder.append(']'); 63 } 64 65 static String bundleToString(Bundle bundle) { 66 final StringBuilder builder = new StringBuilder(); 67 append(builder, bundle); 68 return builder.toString(); 69 } 70 71 static String getUpdateActionAsString(int action) { 72 return DebugUtils.flagsToString(AutofillManager.class, "ACTION_", action); 73 } 74 75 static ViewNode findViewNodeById(@NonNull AssistStructure structure, @NonNull AutofillId id) { 76 final int size = structure.getWindowNodeCount(); 77 for (int i = 0; i < size; i++) { 78 final AssistStructure.WindowNode window = structure.getWindowNodeAt(i); 79 final ViewNode root = window.getRootViewNode(); 80 if (id.equals(root.getAutofillId())) { 81 return root; 82 } 83 final ViewNode child = findViewNodeById(root, id); 84 if (child != null) { 85 return child; 86 } 87 } 88 return null; 89 } 90 91 static ViewNode findViewNodeById(@NonNull ViewNode parent, @NonNull AutofillId id) { 92 final int childrenSize = parent.getChildCount(); 93 if (childrenSize > 0) { 94 for (int i = 0; i < childrenSize; i++) { 95 final ViewNode child = parent.getChildAt(i); 96 if (id.equals(child.getAutofillId())) { 97 return child; 98 } 99 final ViewNode grandChild = findViewNodeById(child, id); 100 if (grandChild != null && id.equals(grandChild.getAutofillId())) { 101 return grandChild; 102 } 103 } 104 } 105 return null; 106 } 107} 108