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.TextUtils; 24 import android.text.TextWatcher; 25 import android.view.KeyEvent; 26 import android.view.LayoutInflater; 27 import android.view.View; 28 import android.view.ViewGroup; 29 import android.view.WindowManager; 30 import android.view.inputmethod.EditorInfo; 31 import android.widget.Button; 32 import android.widget.EditText; 33 import android.widget.TextView; 34 import android.widget.TextView.OnEditorActionListener; 35 36 import com.android.deskclock.provider.Alarm; 37 import com.android.deskclock.timer.TimerObj; 38 39 /** 40 * DialogFragment to edit label. 41 */ 42 public class LabelDialogFragment extends DialogFragment { 43 44 private static final String KEY_LABEL = "label"; 45 private static final String KEY_ALARM = "alarm"; 46 private static final String KEY_TIMER = "timer"; 47 private static final String KEY_TAG = "tag"; 48 49 private EditText mLabelBox; 50 51 public static LabelDialogFragment newInstance(Alarm alarm, String label, String tag) { 52 final LabelDialogFragment frag = new LabelDialogFragment(); 53 Bundle args = new Bundle(); 54 args.putString(KEY_LABEL, label); 55 args.putParcelable(KEY_ALARM, alarm); 56 args.putString(KEY_TAG, tag); 57 frag.setArguments(args); 58 return frag; 59 } 60 61 public static LabelDialogFragment newInstance(TimerObj timer, String label, String tag) { 62 final LabelDialogFragment frag = new LabelDialogFragment(); 63 Bundle args = new Bundle(); 64 args.putString(KEY_LABEL, label); 65 args.putParcelable(KEY_TIMER, timer); 66 args.putString(KEY_TAG, tag); 67 frag.setArguments(args); 68 return frag; 69 } 70 71 @Override 72 public void onCreate(Bundle savedInstanceState) { 73 super.onCreate(savedInstanceState); 74 setStyle(DialogFragment.STYLE_NO_TITLE, 0); 75 } 76 77 @Override 78 public View onCreateView(LayoutInflater inflater, ViewGroup container, 79 Bundle savedInstanceState) { 80 Bundle bundle = getArguments(); 81 final String label = bundle.getString(KEY_LABEL); 82 final Alarm alarm = bundle.getParcelable(KEY_ALARM); 83 final TimerObj timer = bundle.getParcelable(KEY_TIMER); 84 final String tag = bundle.getString(KEY_TAG); 85 86 final View view = inflater.inflate(R.layout.label_dialog, container, false); 87 88 mLabelBox = (EditText) view.findViewById(R.id.labelBox); 89 mLabelBox.setText(label); 90 mLabelBox.setOnEditorActionListener(new OnEditorActionListener() { 91 @Override 92 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 93 if (actionId == EditorInfo.IME_ACTION_DONE) { 94 set(alarm, timer, tag); 95 return true; 96 } 97 return false; 98 } 99 }); 100 mLabelBox.addTextChangedListener(new TextWatcher() { 101 @Override 102 public void beforeTextChanged(CharSequence s, int start, int count, int after) { 103 } 104 105 @Override 106 public void onTextChanged(CharSequence s, int start, int before, int count) { 107 setLabelBoxBackground(s == null || TextUtils.isEmpty(s)); 108 } 109 110 @Override 111 public void afterTextChanged(Editable editable) { 112 } 113 }); 114 setLabelBoxBackground(TextUtils.isEmpty(label)); 115 116 final Button cancelButton = (Button) view.findViewById(R.id.cancelButton); 117 cancelButton.setOnClickListener(new View.OnClickListener() { 118 @Override 119 public void onClick(View view) { 120 dismiss(); 121 } 122 }); 123 124 final Button setButton = (Button) view.findViewById(R.id.setButton); 125 setButton.setOnClickListener(new View.OnClickListener() { 126 @Override 127 public void onClick(View view) { 128 set(alarm, timer, tag); 129 } 130 }); 131 132 getDialog().getWindow().setSoftInputMode( 133 WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); 134 135 return view; 136 } 137 138 private void set(Alarm alarm, TimerObj timer, String tag) { 139 String label = mLabelBox.getText().toString(); 140 if (label.trim().length() == 0) { 141 // Don't allow user to input label with only whitespace. 142 label = ""; 143 } 144 145 if (alarm != null) { 146 set(alarm, tag, label); 147 } else if (timer != null) { 148 set(timer, tag, label); 149 } else { 150 LogUtils.e("No alarm or timer available."); 151 } 152 } 153 154 private void set(Alarm alarm, String tag, String label) { 155 final Activity activity = getActivity(); 156 // TODO just pass in a listener in newInstance() 157 if (activity instanceof AlarmLabelDialogHandler) { 158 ((DeskClock) getActivity()).onDialogLabelSet(alarm, label, tag); 159 } else { 160 LogUtils.e("Error! Activities that use LabelDialogFragment must implement " 161 + "AlarmLabelDialogHandler"); 162 } 163 dismiss(); 164 } 165 166 private void set(TimerObj timer, String tag, String label) { 167 final Activity activity = getActivity(); 168 // TODO just pass in a listener in newInstance() 169 if (activity instanceof TimerLabelDialogHandler){ 170 ((DeskClock) getActivity()).onDialogLabelSet(timer, label, tag); 171 } else { 172 LogUtils.e("Error! Activities that use LabelDialogFragment must implement " 173 + "AlarmLabelDialogHandler or TimerLabelDialogHandler"); 174 } 175 dismiss(); 176 } 177 178 private void setLabelBoxBackground(boolean emptyText) { 179 mLabelBox.setBackgroundResource(emptyText ? 180 R.drawable.bg_edittext_default : R.drawable.bg_edittext_activated); 181 } 182 183 interface AlarmLabelDialogHandler { 184 void onDialogLabelSet(Alarm alarm, String label, String tag); 185 } 186 187 interface TimerLabelDialogHandler { 188 void onDialogLabelSet(TimerObj timer, String label, String tag); 189 } 190 } 191