TimeZonePickerDialog.java revision a8dd2dfa92587e5fc11e6c24da91be9e7bf3afb7
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.app.Dialog;
20import android.app.DialogFragment;
21import android.os.Bundle;
22import android.view.LayoutInflater;
23import android.view.View;
24import android.view.ViewGroup;
25import android.view.Window;
26import android.view.WindowManager;
27
28public class TimeZonePickerDialog extends DialogFragment implements
29        TimeZonePickerView.OnTimeZoneSetListener {
30    public static final String BUNDLE_START_TIME_MILLIS = "bundle_event_start_time";
31    public static final String BUNDLE_TIME_ZONE = "bundle_event_time_zone";
32
33    private static final String KEY_HAS_RESULTS = "has_results";
34    private static final String KEY_LAST_FILTER_STRING = "last_filter_string";
35    private static final String KEY_LAST_FILTER_TYPE = "last_filter_type";
36    private static final String KEY_LAST_FILTER_TIME = "last_filter_time";
37    private static final String KEY_HIDE_FILTER_SEARCH = "hide_filter_search";
38
39    private OnTimeZoneSetListener mTimeZoneSetListener;
40    private TimeZonePickerView mView;
41    private boolean mHasCachedResults = false;
42
43    public interface OnTimeZoneSetListener {
44        void onTimeZoneSet(TimeZoneInfo tzi);
45    }
46
47    public void setOnTimeZoneSetListener(OnTimeZoneSetListener l) {
48        mTimeZoneSetListener = l;
49    }
50
51    public TimeZonePickerDialog() {
52        super();
53    }
54
55    @Override
56    public View onCreateView(LayoutInflater inflater, ViewGroup container,
57            Bundle savedInstanceState) {
58        long timeMillis = 0;
59        String timeZone = null;
60        Bundle b = getArguments();
61        if (b != null) {
62            timeMillis = b.getLong(BUNDLE_START_TIME_MILLIS);
63            timeZone = b.getString(BUNDLE_TIME_ZONE);
64        }
65        boolean hideFilterSearch = false;
66
67        if (savedInstanceState != null) {
68            hideFilterSearch = savedInstanceState.getBoolean(KEY_HIDE_FILTER_SEARCH);
69        }
70        mView = new TimeZonePickerView(getActivity(), null, timeZone, timeMillis, this,
71                hideFilterSearch);
72        if (savedInstanceState != null && savedInstanceState.getBoolean(KEY_HAS_RESULTS, false)) {
73            mView.showFilterResults(savedInstanceState.getInt(KEY_LAST_FILTER_TYPE),
74                                    savedInstanceState.getString(KEY_LAST_FILTER_STRING),
75                                    savedInstanceState.getInt(KEY_LAST_FILTER_TIME));
76        }
77        return mView;
78    }
79
80    @Override
81    public void onSaveInstanceState(Bundle outState) {
82        super.onSaveInstanceState(outState);
83        outState.putBoolean(KEY_HAS_RESULTS, mView != null && mView.hasResults());
84        if (mView != null) {
85            outState.putInt(KEY_LAST_FILTER_TYPE, mView.getLastFilterType());
86            outState.putString(KEY_LAST_FILTER_STRING, mView.getLastFilterString());
87            outState.putInt(KEY_LAST_FILTER_TIME, mView.getLastFilterTime());
88            outState.putBoolean(KEY_HIDE_FILTER_SEARCH, mView.getHideFilterSearchOnStart());
89        }
90    }
91
92    @Override
93    public Dialog onCreateDialog(Bundle savedInstanceState) {
94        Dialog dialog = super.onCreateDialog(savedInstanceState);
95        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
96        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
97        return dialog;
98    }
99
100    @Override
101    public void onTimeZoneSet(TimeZoneInfo tzi) {
102        if (mTimeZoneSetListener != null) {
103            mTimeZoneSetListener.onTimeZoneSet(tzi);
104        }
105        dismiss();
106    }
107}
108