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