1/* 2 * 3 * Copyright 2001-2011 Texas Instruments, Inc. - http://www.ti.com/ 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package com.ti.fmrxapp; 19 20import android.app.Activity; 21import android.app.AlertDialog; 22import android.content.Intent; 23import android.os.Bundle; 24import android.util.Log; 25import android.view.KeyEvent; 26import android.view.View; 27import android.view.View.OnKeyListener; 28import android.widget.Button; 29import android.widget.EditText; 30import android.widget.TextView; 31import com.ti.fm.IFmConstants; 32 33/** 34 * FmRxFreqInput asks the user to enter a valid frequency for tuning the radio. 35 * It is an activity that appears as a dialog. 36 */ 37 38public class FmRxFreqInput extends Activity implements OnKeyListener, 39 View.OnClickListener, IFmConstants, FmRxAppConstants { 40 41 public static final String TAG = "ManualFreqInput"; 42 private EditText mUserText; 43 private TextView mBandRange; 44 private Button btnCancel, btnOk; 45 46 /** Called when the activity is first created. */ 47 48 public void onCreate(Bundle savedInstanceState) { 49 super.onCreate(savedInstanceState); 50 setContentView(R.layout.fmrxfreq); 51 initControls(); 52 } 53 54 private void initControls() { 55 /* listening to key click or a key input into the exit text box */ 56 mUserText = (EditText) findViewById(R.id.txtFrequency); 57 /* wait for key inputs or mouse clicks in the edit box */ 58 mUserText.setOnKeyListener(this); 59 btnCancel = (Button) findViewById(R.id.btnCancel); 60 btnOk = (Button) findViewById(R.id.btnOk); 61 btnOk.setOnClickListener(this); 62 btnCancel.setOnClickListener(this); 63 mBandRange = (TextView) findViewById(R.id.freqRange); 64 if (FmRxApp.sBand == FM_BAND_EUROPE_US) { 65 mBandRange.setText(getText(R.string.freqRangeEurope)); 66 } else { 67 mBandRange.setText(getText(R.string.freqRangeJapan)); 68 69 } 70 71 } 72 73 private void writeFrequency() { 74 // get the text entered in edit box 75 String text = mUserText.getText().toString(); 76 try { 77 float iFreq = Float.parseFloat(text); 78 Float validFreq = UpdateFrequency(iFreq); 79 if (validFreq != 0) { 80 // reset the text in edit box for the next entry 81 mUserText.setText(null); 82 83 Bundle bundle = new Bundle(); 84 bundle.putFloat(FREQ_VALUE, validFreq); 85 Intent result = new Intent(); 86 result.putExtras(bundle); 87 setResult(RESULT_OK, result); 88 finish(); 89 90 } else { 91 new AlertDialog.Builder(this).setIcon( 92 android.R.drawable.ic_dialog_alert).setMessage( 93 "Enter valid frequency!!").setNegativeButton( 94 android.R.string.ok, null).show(); 95 mUserText.setText(null); 96 } 97 } catch (NumberFormatException nfe) { 98 Log.d(TAG, "NumberFormatException:" + nfe.getMessage()); 99 new AlertDialog.Builder(this).setIcon( 100 android.R.drawable.ic_dialog_alert).setMessage( 101 "Enter valid number!!").setNegativeButton( 102 android.R.string.ok, null).show(); 103 mUserText.setText(null); 104 } 105 106 } 107 108 /* This is a method implementation of OnKeyListener */ 109 public boolean onKey(View v, int keyCode, KeyEvent event) { 110 if (event.getAction() == KeyEvent.ACTION_DOWN) { 111 switch (keyCode) { 112 case KeyEvent.KEYCODE_DPAD_CENTER: 113 case KeyEvent.KEYCODE_ENTER: 114 writeFrequency(); 115 return true; 116 } 117 } 118 return false; 119 } 120 121 public void onClick(View v) { 122 int id = v.getId(); 123 switch (id) { 124 case R.id.btnOk: 125 writeFrequency(); 126 break; 127 case R.id.btnCancel: 128 finish(); 129 break; 130 131 } 132 } 133 134 static float BaseFreq() { 135 return FmRxApp.sBand == FM_BAND_JAPAN ? APP_FM_FIRST_FREQ_JAPAN_KHZ 136 : APP_FM_FIRST_FREQ_US_EUROPE_KHZ; 137 } 138 139 static float LastFreq() { 140 return FmRxApp.sBand == FM_BAND_JAPAN ? APP_FM_LAST_FREQ_JAPAN_KHZ 141 : APP_FM_LAST_FREQ_US_EUROPE_KHZ; 142 } 143 144 // Update the Frequency label with the given value 145 float UpdateFrequency(float freq) { 146 Log.d(TAG, "FM App: UpdateFrequency %d." + freq); 147 if (freq < BaseFreq() || freq > LastFreq()) { 148 freq = 0; 149 } 150 return (float) freq; 151 } 152 153} 154