1e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com/*
2e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com * Copyright (C) 2014 Google Inc.
3e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com *
4e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com * Licensed under the Apache License, Version 2.0 (the "License");
5e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com * you may not use this file except in compliance with the License.
6e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com * You may obtain a copy of the License at
7e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com *
8e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com * http://www.apache.org/licenses/LICENSE-2.0
9e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com *
10e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com * Unless required by applicable law or agreed to in writing, software
11e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com * distributed under the License is distributed on an "AS IS" BASIS,
12e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com * See the License for the specific language governing permissions and
14e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com * limitations under the License.
15e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com */
16e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com
17e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.compackage com.android.i18n.addressinput;
18e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com
19e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.comimport android.app.ProgressDialog;
20e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.comimport android.content.Context;
21e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.comimport android.view.LayoutInflater;
22e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.comimport android.widget.ArrayAdapter;
23e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.comimport android.widget.EditText;
24e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.comimport android.widget.Spinner;
25e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.comimport android.widget.TextView;
26e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com
27e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.comimport com.android.i18n.addressinput.AddressField.WidthType;
28e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com
29e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com/**
30e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com * Base class for customizing widgets for address input.
31e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com *
32e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com * <p>
33e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com * Clients can optionally override this class and use
34e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com * {@link AddressWidget#setUiComponentProvider(AddressWidgetUiComponentProvider)} to set the the
35e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com * componentProvider field of the address widget, which will be invoked by the widget to create UI
36e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com * components that provide consistent look-and-feel with other UI components clients might use
37e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com * alongside the address widget.
38e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com */
39e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.compublic class AddressWidgetUiComponentProvider {
40e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    protected Context mContext;
41e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    protected LayoutInflater mInflater;
42e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com
43e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    public AddressWidgetUiComponentProvider(Context context) {
44e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        mContext = context;
45e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
46e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    }
47e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com
48e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    /**
49e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com     * Creates a label, e.g. "State", for an address input field.
50e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com     *
51e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com     * @param label the label of the address input field
52e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com     * @param widthType {@link WidthType} of the field
53e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com     * @return a custom {@link TextView} created for the field
54e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com     */
55e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    protected TextView createUiLabel(CharSequence label, WidthType widthType) {
56e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        TextView textView = (TextView) mInflater.inflate(R.layout.address_textview, null, false);
57e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        textView.setText(label);
58e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        return textView;
59e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    }
60e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com
61e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    /**
62e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com     * Creates a text input view for an address input field.
63e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com     *
64e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com     * @param widthType {@link WidthType} of the field
65e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com     * @return a custom {@link EditText} created for the field
66e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com     */
67e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    protected EditText createUiTextField(WidthType widthType) {
68e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        return (EditText) mInflater.inflate(R.layout.address_edittext, null, false);
69e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    }
70e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com
71e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    /**
72e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com     * Creates a {@link Spinner} for a input field that uses UI picker.
73e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com     *
74e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com     * @param widthType {@link WidthType} of the field
75e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com     * @return a custom {@link Spinner} created for the field
76e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com     */
77e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    protected Spinner createUiPickerSpinner(WidthType widthType) {
78e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        return (Spinner) mInflater.inflate(R.layout.address_spinner, null, false);
79e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    }
80e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com
81e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    /**
82e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com     * Creates an {@link ArrayAdapter} to work with the custom {@link Spinner} of a input field that
83e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com     * uses UI picker.
84e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com     *
85e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com     * @param widthType {@link WidthType} of the field
86e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com     * @return a custom {@link ArrayAdapter} for the field
87e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com     */
88e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    protected ArrayAdapter<String> createUiPickerAdapter(WidthType widthType) {
89e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        ArrayAdapter<String> adapter =
90e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com                new ArrayAdapter<String>(mContext, android.R.layout.simple_spinner_item);
91e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
92e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        return adapter;
93e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    }
94e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com
95e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    /** Gets an activity indicator to show that a task is in progress. */
96e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    protected ProgressDialog getUiActivityIndicatorView() {
97e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com        return new ProgressDialog(mContext);
98e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com    }
99e35bb3be4981ebf0f3d15645f6610681dfaf4627roubert@google.com}
100