コード例 #1
0
        public override void Autofill(SparseArray values)
        {
            Context context = Context;

            // User has just selected a Dataset from the list of autofill suggestions.
            // The Dataset is comprised of a list of AutofillValues, with each AutofillValue meant
            // to fill a specific autofillable view. Now we have to update the UI based on the
            // AutofillValues in the list, but first we make sure all autofilled values belong to the
            // same partition
            if (DEBUG)
            {
                Log.Debug(TAG, "autofill(): " + values);
            }

            // First get the name of all partitions in the values
            ArraySet partitions = new ArraySet();

            for (int i = 0; i < values.Size(); i++)
            {
                int id        = values.KeyAt(i);
                var partition = mPartitionsByAutofillId.Get(id);
                if (partition == null)
                {
                    ShowError(context.GetString(Resource.String.message_autofill_no_partitions, id,
                                                mPartitionsByAutofillId));
                    return;
                }

                partitions.Add(partition.mName);
            }

            // Then make sure they follow the Highlander rule (There can be only one)
            if (partitions.Size() != 1)
            {
                ShowError(context.GetString(Resource.String.message_autofill_blocked, partitions));
                return;
            }

            // Finally, autofill it.
            var df = Android.Text.Format.DateFormat.GetDateFormat(context);

            for (int i = 0; i < values.Size(); i++)
            {
                int id    = values.KeyAt(i);
                var value = (AutofillValue)values.ValueAt(i);
                var item  = mVirtualViews.Get(id);

                if (item == null)
                {
                    Log.Warn(TAG, "No item for id " + id);
                    continue;
                }

                if (!item.editable)
                {
                    ShowError(context.GetString(Resource.String.message_autofill_readonly, item.text.ToString()));
                    continue;
                }

                // Check if the type was properly set by the autofill service
                if (DEBUG)
                {
                    Log.Debug(TAG, "Validating " + i
                              + ": expectedType=" + CommonUtil.GetTypeAsString(item.type)
                              + "(" + item.type + "), value=" + value);
                }

                bool valid = false;
                if (value.IsText && item.type == AutofillType.Text)
                {
                    item.text = value.TextValue;
                    valid     = true;
                }
                else if (value.IsDate && item.type == AutofillType.Date)
                {
                    item.text = df.Format(new Date(value.DateValue));
                    valid     = true;
                }
                else
                {
                    Log.Warn(TAG, "Unsupported type: " + value);
                }

                if (!valid)
                {
                    item.text = context.GetString(Resource.String.message_autofill_invalid);
                }
            }

            PostInvalidate();
            ShowMessage(context.GetString(Resource.String.message_autofill_ok, partitions.ValueAt(0)));
        }