TimeZonePickerView.java revision b1b7080deea42aa533c3757b585cf765c6b76732
1/*
2 * Copyright (C) 2013 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.timezonepicker;
18
19import android.content.Context;
20import android.text.Editable;
21import android.text.TextWatcher;
22import android.util.AttributeSet;
23import android.view.LayoutInflater;
24import android.view.View;
25import android.widget.AdapterView;
26import android.widget.AdapterView.OnItemClickListener;
27import android.widget.AutoCompleteTextView;
28import android.widget.LinearLayout;
29import android.widget.ListView;
30
31public class TimeZonePickerView extends LinearLayout implements TextWatcher, OnItemClickListener {
32    private static final String TAG = "TimeZonePickerView";
33
34    private Context mContext;
35    private AutoCompleteTextView mAutoCompleteTextView;
36    private TimeZoneFilterTypeAdapter mFilterAdapter;
37    TimeZoneResultAdapter mResultAdapter;
38
39    public interface OnTimeZoneSetListener {
40        void onTimeZoneSet(TimeZoneInfo tzi);
41    }
42
43    public TimeZonePickerView(Context context, AttributeSet attrs,
44            String timeZone, long timeMillis, OnTimeZoneSetListener l) {
45        super(context, attrs);
46        mContext = context;
47        LayoutInflater inflater = (LayoutInflater) context.getSystemService(
48                Context.LAYOUT_INFLATER_SERVICE);
49        inflater.inflate(R.layout.timezonepickerview, this, true);
50
51        TimeZoneData tzd = new TimeZoneData(mContext, timeZone, timeMillis);
52
53        mResultAdapter = new TimeZoneResultAdapter(mContext, tzd, l);
54        ListView timeZoneList = (ListView) findViewById(R.id.timezonelist);
55        timeZoneList.setAdapter(mResultAdapter);
56
57        mAutoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.searchBox);
58        mFilterAdapter = new TimeZoneFilterTypeAdapter(mContext, tzd, mResultAdapter);
59        mAutoCompleteTextView.setAdapter(mFilterAdapter);
60        mAutoCompleteTextView.addTextChangedListener(this);
61        mAutoCompleteTextView.setOnItemClickListener(this);
62    }
63
64    // Implementation of TextWatcher
65    @Override
66    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
67    }
68
69    // Implementation of TextWatcher
70    @Override
71    public void onTextChanged(CharSequence s, int start, int before, int count) {
72        mFilterAdapter.getFilter().filter(s.toString());
73    }
74
75    // Implementation of TextWatcher
76    @Override
77    public void afterTextChanged(Editable s) {
78    }
79
80    @Override
81    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
82        // An onClickListener for the view item because I haven't figured out a
83        // way to update the AutoCompleteTextView without causing an infinite loop.
84        mFilterAdapter.onClick(view);
85    }
86}
87