Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 package org.chromium.android_webview; | |
| 2 | |
| 3 import android.annotation.SuppressLint; | |
| 4 import android.content.Context; | |
| 5 import android.graphics.Rect; | |
| 6 import android.util.Pair; | |
| 7 import android.util.SparseArray; | |
| 8 import android.view.View; | |
| 9 import android.view.ViewGroup; | |
| 10 import android.view.ViewStructure; | |
| 11 import android.view.autofill.AutofillValue; | |
| 12 | |
| 13 import org.chromium.base.Log; | |
| 14 import org.chromium.base.ThreadUtils; | |
| 15 import org.chromium.components.autofill.AutofillProvider; | |
| 16 import org.chromium.components.autofill.FormData; | |
| 17 import org.chromium.components.autofill.FormFieldData; | |
| 18 | |
| 19 import java.util.List; | |
| 20 | |
| 21 // All methods are supposed to be called in UI thread. | |
| 22 @SuppressLint("NewApi") // Removed once SDK roll to O | |
| 23 public class AwAutofillProvider extends AutofillProvider { | |
| 24 | |
| 25 private static class AutofillRequest { | |
| 26 public final static short OUT_OF_FOCUS = -1; | |
| 27 private static int sClientId; | |
| 28 public final int mClientId; | |
| 29 private FormData mFormData; | |
| 30 private short mFocusField; | |
| 31 | |
| 32 public AutofillRequest(FormData formData, short focus) { | |
| 33 mClientId = getNextClientId(); | |
| 34 mFormData = formData; | |
| 35 mFocusField = focus; | |
| 36 } | |
| 37 | |
| 38 public void fillViewStructure(ViewStructure structure) { | |
|
svetoslavganov
2017/04/11 22:47:17
This children are not properly added which I suspe
| |
| 39 Log.e(TAG, "fillViewStructure " + mFormData.mFields.size()); | |
| 40 structure.setClassName(mFormData.mName); | |
| 41 structure.setUrl(mFormData.mOrigin); | |
| 42 int index = structure.addChildCount(mFormData.mFields.size()); | |
| 43 short fieldIndex = 0; | |
| 44 for (FormFieldData field : mFormData.mFields) { | |
| 45 ViewStructure child = structure.newChild(index); | |
| 46 child.setAutofillId(structure, toVirtualId(mClientId, fieldIndex ++)); | |
| 47 // This is just for testing purpose, we should set ViewStructure | |
| 48 // according new API. | |
| 49 child.setAutofillValue(AutofillValue.forText(field.mName)); | |
| 50 } | |
| 51 } | |
| 52 | |
| 53 public boolean autofill(final SparseArray<AutofillValue> values) { | |
| 54 for (int i = 0; i < values.size(); ++i) { | |
| 55 int id = values.keyAt(i); | |
| 56 AutofillValue value = values.get(id); | |
| 57 if (toClientId(id) != mClientId) | |
| 58 return false; | |
| 59 if (value == null) continue; | |
| 60 short index = toIndex(id); | |
| 61 if (index < 0 || index >= mFormData.mFields.size()) | |
| 62 return false; | |
| 63 FormFieldData field = mFormData.mFields.get(index); | |
| 64 if (field == null) | |
| 65 return false; | |
| 66 field.updataValue((String) value.getTextValue()); | |
| 67 } | |
| 68 return true; | |
| 69 } | |
| 70 | |
| 71 public void setFocusField(short focusField) { | |
| 72 mFocusField = focusField; | |
| 73 } | |
| 74 | |
| 75 public short getFocusField() { | |
| 76 return mFocusField; | |
| 77 } | |
| 78 | |
| 79 public AutofillValue getFieldNewValue(int index) { | |
| 80 FormFieldData field = mFormData.mFields.get(index); | |
| 81 if (field == null) | |
| 82 return null; | |
| 83 String value = field.getValue(); | |
| 84 return AutofillValue.forText(value); | |
| 85 } | |
| 86 | |
| 87 public int getVirtualId(short index) { | |
| 88 return toVirtualId(mClientId, index); | |
| 89 } | |
| 90 | |
| 91 private static int getNextClientId() { | |
| 92 ThreadUtils.assertOnUiThread(); | |
| 93 if (sClientId == 0xffff) sClientId = 0; | |
| 94 return sClientId++; | |
| 95 } | |
| 96 | |
| 97 private static int toClientId(int virtualId) { | |
| 98 return (virtualId & 0xffff0000) >> 16; | |
| 99 } | |
| 100 | |
| 101 private static short toIndex(int virtualId) { | |
| 102 return (short)(virtualId & 0xffff); | |
| 103 } | |
| 104 | |
| 105 private static int toVirtualId(int clientId, short index) { | |
| 106 return (clientId << 16) | index; | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 private static final String TAG = "bt:"; | |
| 111 private AwAutofillManager mAutoFillManager; | |
| 112 private final ViewGroup mContainerView; | |
| 113 | |
| 114 private AutofillRequest mRequest; | |
| 115 | |
| 116 public AwAutofillProvider(Context context, ViewGroup containerView, | |
| 117 AwAutofillManager manager) { | |
| 118 mAutoFillManager = manager; | |
| 119 mContainerView = containerView; | |
| 120 } | |
| 121 | |
| 122 public void onProvideAutoFillVirtualStructure(ViewStructure structure, int f lags) { | |
| 123 mRequest.fillViewStructure(structure); | |
| 124 } | |
| 125 | |
| 126 public void autofill(final SparseArray<AutofillValue> values) { | |
| 127 if (mRequest.autofill(values)) { | |
| 128 autofill(mRequest.mFormData); | |
| 129 } | |
| 130 } | |
| 131 | |
| 132 @Override | |
| 133 public void queryFormFieldAutofill(FormData formData, int focus, int x, int y, int width, | |
| 134 int height) { | |
| 135 Log.e(TAG, "queryFormFieldAutofill"); | |
| 136 // Check focusField inside short value? | |
| 137 // If query for credit card we need check if connection is secure. | |
| 138 if (mRequest != null) { | |
| 139 mAutoFillManager.cancel(); | |
| 140 } | |
| 141 Rect bounds = new Rect(x, y, x + width, y + height); | |
| 142 mRequest = new AutofillRequest(formData, (short) focus); | |
| 143 int virtualId = mRequest.getVirtualId((short) focus); | |
| 144 mAutoFillManager.notifyVirtualViewEntered(mContainerView, virtualId, bou nds); | |
| 145 } | |
| 146 | |
| 147 @Override | |
| 148 public void onTextFieldDidChange(int index) { | |
| 149 // Check index inside short value? | |
| 150 AutofillValue autofillValue = mRequest.getFieldNewValue(index); | |
| 151 mAutoFillManager.notifyVirtualValueChanged(mContainerView, | |
| 152 mRequest.getVirtualId((short) index), | |
| 153 autofillValue); | |
| 154 } | |
| 155 | |
| 156 @Override | |
| 157 public void OnWillSubmitForm() { | |
| 158 mAutoFillManager.commit(); | |
| 159 mRequest = null; | |
| 160 } | |
| 161 | |
| 162 @Override | |
| 163 public void OnFocusChanged(boolean focusOnForm, int focusField, int x, int y , int width, | |
| 164 int height) { | |
| 165 // Check focusField inside short value? | |
| 166 short prev = mRequest.getFocusField(); | |
| 167 if (focusOnForm) { | |
| 168 if (prev == focusField) return; | |
| 169 // Notify focus changed. | |
| 170 if (prev != AutofillRequest.OUT_OF_FOCUS) { | |
| 171 mAutoFillManager.notifyVirtualViewExited(mContainerView, | |
| 172 mRequest.getVirtualId(prev)); | |
| 173 } | |
| 174 mAutoFillManager.notifyVirtualViewEntered(mContainerView, | |
| 175 mRequest.getVirtualId((short) focusField), | |
| 176 new Rect(x, y, x + width, y + height)); | |
| 177 mRequest.setFocusField((short)focusField); | |
| 178 } else { | |
| 179 if (prev == AutofillRequest.OUT_OF_FOCUS) return; | |
| 180 // Notify focus changed. | |
| 181 mAutoFillManager.notifyVirtualViewExited(mContainerView, | |
| 182 mRequest.getVirtualId(prev)); | |
| 183 mRequest.setFocusField(AutofillRequest.OUT_OF_FOCUS); | |
| 184 } | |
| 185 } | |
| 186 } | |
| OLD | NEW |