Home | History | Annotate | Download | only in deskclock
      1 /*
      2  * Copyright (C) 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.deskclock;
     18 
     19 import android.app.Activity;
     20 import android.app.DialogFragment;
     21 import android.os.Bundle;
     22 import android.text.Editable;
     23 import android.text.TextWatcher;
     24 import android.view.KeyEvent;
     25 import android.view.LayoutInflater;
     26 import android.view.View;
     27 import android.view.ViewGroup;
     28 import android.view.WindowManager;
     29 import android.view.inputmethod.EditorInfo;
     30 import android.widget.Button;
     31 import android.widget.EditText;
     32 import android.widget.TextView;
     33 import android.widget.TextView.OnEditorActionListener;
     34 
     35 import com.android.deskclock.timer.TimerObj;
     36 
     37 /**
     38  * DialogFragment to edit label.
     39  */
     40 public class LabelDialogFragment extends DialogFragment {
     41 
     42     private static final String KEY_LABEL = "label";
     43     private static final String KEY_ALARM = "alarm";
     44     private static final String KEY_TIMER = "timer";
     45     private static final String KEY_TAG = "tag";
     46 
     47     private EditText mLabelBox;
     48 
     49     public static LabelDialogFragment newInstance(Alarm alarm, String label) {
     50         final LabelDialogFragment frag = new LabelDialogFragment();
     51         Bundle args = new Bundle();
     52         args.putString(KEY_LABEL, label);
     53         args.putParcelable(KEY_ALARM, alarm);
     54         frag.setArguments(args);
     55         return frag;
     56     }
     57 
     58     public static LabelDialogFragment newInstance(TimerObj timer, String label, String tag) {
     59         final LabelDialogFragment frag = new LabelDialogFragment();
     60         Bundle args = new Bundle();
     61         args.putString(KEY_LABEL, label);
     62         args.putParcelable(KEY_TIMER, timer);
     63         args.putString(KEY_TAG, tag);
     64         frag.setArguments(args);
     65         return frag;
     66     }
     67 
     68     @Override
     69     public void onCreate(Bundle savedInstanceState) {
     70         super.onCreate(savedInstanceState);
     71         setStyle(DialogFragment.STYLE_NO_TITLE, 0);
     72     }
     73 
     74     @Override
     75     public View onCreateView(LayoutInflater inflater, ViewGroup container,
     76             Bundle savedInstanceState) {
     77         Bundle bundle = getArguments();
     78         final String label = bundle.getString(KEY_LABEL);
     79         final Alarm alarm = bundle.getParcelable(KEY_ALARM);
     80         final TimerObj timer = bundle.getParcelable(KEY_TIMER);
     81         final String tag = bundle.getString(KEY_TAG);
     82 
     83         final View view = inflater.inflate(R.layout.label_dialog, container, false);
     84 
     85         mLabelBox = (EditText) view.findViewById(R.id.labelBox);
     86         mLabelBox.setText(label);
     87         mLabelBox.setOnEditorActionListener(new OnEditorActionListener() {
     88             @Override
     89             public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
     90                 if (actionId == EditorInfo.IME_ACTION_DONE) {
     91                     set(alarm, timer, tag);
     92                     return true;
     93                 }
     94                 return false;
     95             }
     96         });
     97 
     98         final Button cancelButton = (Button) view.findViewById(R.id.cancelButton);
     99         cancelButton.setOnClickListener(new View.OnClickListener() {
    100             @Override
    101             public void onClick(View view) {
    102                 dismiss();
    103             }
    104         });
    105 
    106         final Button setButton = (Button) view.findViewById(R.id.setButton);
    107         setButton.setOnClickListener(new View.OnClickListener() {
    108             @Override
    109             public void onClick(View view) {
    110                 set(alarm, timer, tag);
    111             }
    112         });
    113 
    114         getDialog().getWindow().setSoftInputMode(
    115                 WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
    116 
    117         return view;
    118     }
    119 
    120     private void set(Alarm alarm, TimerObj timer, String tag) {
    121         String label = mLabelBox.getText().toString();
    122         if (label.trim().length() == 0) {
    123             // Don't allow user to input label with only whitespace.
    124             label = "";
    125         }
    126         final Activity activity = getActivity();
    127         if (activity instanceof AlarmLabelDialogHandler) {
    128             ((AlarmClock) getActivity()).onDialogLabelSet(alarm, label);
    129         } else if (activity instanceof TimerLabelDialogHandler){
    130             ((DeskClock) getActivity()).onDialogLabelSet(timer, label, tag);
    131         } else {
    132             Log.e("Error! Activities that use LabelDialogFragment must implement "
    133                     + "AlarmLabelDialogHandler or TimerLabelDialogHandler");
    134         }
    135         dismiss();
    136     }
    137 
    138     interface AlarmLabelDialogHandler {
    139         void onDialogLabelSet(Alarm alarm, String label);
    140     }
    141 
    142     interface TimerLabelDialogHandler {
    143         void onDialogLabelSet(TimerObj timer, String label, String tag);
    144     }
    145 }
    146