Home | History | Annotate | Download | only in editor
      1 /*
      2  * Copyright 2012 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.notificationstudio.editor;
     18 
     19 import android.app.Activity;
     20 import android.app.DatePickerDialog;
     21 import android.app.DatePickerDialog.OnDateSetListener;
     22 import android.app.Dialog;
     23 import android.app.DialogFragment;
     24 import android.app.FragmentTransaction;
     25 import android.app.TimePickerDialog;
     26 import android.app.TimePickerDialog.OnTimeSetListener;
     27 import android.os.Bundle;
     28 import android.view.View;
     29 import android.view.View.OnClickListener;
     30 import android.widget.Button;
     31 import android.widget.DatePicker;
     32 import android.widget.TimePicker;
     33 
     34 import com.android.notificationstudio.R;
     35 import com.android.notificationstudio.editor.Editors.Editor;
     36 import com.android.notificationstudio.model.EditableItem;
     37 
     38 import java.text.SimpleDateFormat;
     39 import java.util.Date;
     40 
     41 public class DateTimeEditor implements Editor {
     42     private static final SimpleDateFormat YYYY_MM_DD = new SimpleDateFormat("yyyy/MM/dd");
     43     private static final SimpleDateFormat HH_MM_SS = new SimpleDateFormat("HH:mm:ss");
     44 
     45     @SuppressWarnings("deprecation")
     46     public Runnable bindEditor(View v, final EditableItem item, final Runnable afterChange) {
     47 
     48         final Button dateButton = (Button) v.findViewById(R.id.date_button);
     49         final Button timeButton = (Button) v.findViewById(R.id.time_button);
     50         final Button resetButton = (Button) v.findViewById(R.id.reset_button);
     51 
     52         int vPad = v.getResources().getDimensionPixelSize(R.dimen.editor_datetime_padding_v);
     53         int hPad = v.getResources().getDimensionPixelSize(R.dimen.editor_datetime_padding_h);
     54         for (Button b : new Button[] { dateButton, timeButton, resetButton }) {
     55             b.setVisibility(View.VISIBLE);
     56             b.setPadding(hPad, vPad, hPad, vPad);
     57         }
     58 
     59         final Runnable updateButtonText = new Runnable() {
     60             public void run() {
     61                 Date d = getDateTime(item);
     62                 String dateString = YYYY_MM_DD.format(d);
     63                 dateButton.setText(dateString);
     64                 String timeString = HH_MM_SS.format(d);
     65                 timeButton.setText(timeString);
     66             }};
     67         updateButtonText.run();
     68 
     69         // wire up date button
     70         DialogFragment datePickerFragment = new DialogFragment() {
     71             @Override
     72             public Dialog onCreateDialog(Bundle savedInstanceState) {
     73                 Date d = getDateTime(item);
     74                 OnDateSetListener onDateSet = new OnDateSetListener() {
     75                     public void onDateSet(DatePicker view, int year,
     76                             int monthOfYear, int dayOfMonth) {
     77                         Date d = getDateTime(item);
     78                         d.setYear(year - 1900);
     79                         d.setMonth(monthOfYear);
     80                         d.setDate(dayOfMonth);
     81                         item.setValue(d.getTime());
     82                         updateButtonText.run();
     83                         afterChange.run();
     84                     }
     85                 };
     86                 return new DatePickerDialog(getActivity(), onDateSet,
     87                         d.getYear() + 1900, d.getMonth(), d.getDate());
     88             }
     89         };
     90         Activity activity = (Activity) v.getContext();
     91         launchDialogOnClick(activity, "datePicker", dateButton, datePickerFragment);
     92 
     93         // wire up time button
     94         DialogFragment timePickerFragment = new DialogFragment() {
     95             @Override
     96             public Dialog onCreateDialog(Bundle savedInstanceState) {
     97                 Date d = getDateTime(item);
     98                 OnTimeSetListener onTimeSet = new OnTimeSetListener() {
     99                     public void onTimeSet(TimePicker view, int hourOfDay,
    100                             int minute) {
    101                         Date d = getDateTime(item);
    102                         d.setHours(hourOfDay);
    103                         d.setMinutes(minute);
    104                         item.setValue(d.getTime());
    105                         updateButtonText.run();
    106                         afterChange.run();
    107                     }
    108                 };
    109                 return new TimePickerDialog(getActivity(),
    110                         onTimeSet, d.getHours(), d.getMinutes(), true);
    111             }
    112         };
    113         launchDialogOnClick(activity, "timePicker", timeButton, timePickerFragment);
    114 
    115         // wire up reset button
    116         resetButton.setOnClickListener(new OnClickListener(){
    117             public void onClick(View v) {
    118                 item.setValue(null);
    119                 updateButtonText.run();
    120                 afterChange.run();
    121             }});
    122         return updateButtonText;
    123     }
    124 
    125     private static Date getDateTime(EditableItem item) {
    126         long value = item.hasValue() ? item.getValueLong() : System.currentTimeMillis();
    127         return new Date(value);
    128     }
    129 
    130     private static void launchDialogOnClick(final Activity activity,
    131             final String tag, Button button, final DialogFragment fragment) {
    132         button.setOnClickListener(new OnClickListener(){
    133             public void onClick(View v) {
    134                 FragmentTransaction ft = activity.getFragmentManager().beginTransaction();
    135                 fragment.show(ft, tag);
    136             }});
    137     }
    138 
    139 }
    140