1/*
2 *
3 * Copyright 2001-2010 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 java.io.FileInputStream;
21import java.io.FileOutputStream;
22import java.io.IOException;
23import java.io.ObjectInputStream;
24import java.io.ObjectOutputStream;
25import java.util.ArrayList;
26import java.util.HashMap;
27import java.util.Iterator;
28
29import android.app.Activity;
30import android.app.ProgressDialog;
31import android.content.BroadcastReceiver;
32import android.content.Context;
33import android.content.Intent;
34import android.content.IntentFilter;
35import android.os.Bundle;
36import android.os.Handler;
37import android.os.Message;
38import android.util.Log;
39import android.view.KeyEvent;
40import android.view.Menu;
41import android.view.MenuItem;
42import android.view.OrientationListener;
43import android.view.View;
44import android.widget.Button;
45import android.widget.ImageButton;
46import android.widget.ImageView;
47import android.widget.TextView;
48import com.ti.fm.FmRadio;
49import com.ti.fm.FmRadioIntent;
50import com.ti.fm.IFmConstants;
51import android.app.AlertDialog;
52import android.os.Bundle;
53import android.content.SharedPreferences;
54import android.content.res.Configuration;
55import android.database.Cursor;
56import android.database.SQLException;
57import android.app.Notification;
58import android.app.NotificationManager;
59import android.app.PendingIntent;
60import android.view.Window;
61import android.media.AudioManager;
62import android.widget.Toast;
63import android.view.Gravity;
64import android.view.LayoutInflater;
65import android.view.ViewGroup;
66
67/*
68 FM Boot up Sequence:
69
70 FM APIS NON Blocking:
71
72 sFmRadio.rxEnable() is called from FM application, when the user selects FM radio icon from main menu.
73
74 Once  the callback for sFmRadio.rxEnable() is received (EVENT_FM_ENABLED),
75 the default configurations which have been saved in the preference will be loaded using loadDefaultConfiguration() function .
76 After this  sFmRadio.rxSetVolume() with default volume will be called.
77
78 Once the callback for  sFmRadio.rxSetVolume() is received(EVENT_VOLUME_CHANGE),
79 sFmRadio.rxSetBand() with default band will be called.
80
81 Once the callback for sFmRadio.rxSetBand() is received(EVENT_BAND_CHANGE), sFmRadio.rxTune_nb()
82 with default frequency will be called.
83
84 Once the callback for sFmRadio.rxTune_nb()is received (EVENT_TUNE_COMPLETE)
85 sFmRadio.rxEnableAudioRouting()  will be called to enable audio.
86
87 After these sequences user can hear the FM audio.
88
89
90 The rest of the APIS will be called based on the user actions like when user presses seek up or down
91 sFmRadio.rxSeek_nb() will be called and the callback for the same will be EVENT_SEEK_STARTED.
92
93 To increase decrease the volume  sFmRadio.rxSetVolume() will be called and the callback for
94 the same will be EVENT_VOLUME_CHANGE.
95
96 To mute /unmute, sFmRadio.rxSetMuteMode() will be called and the callback
97 for the same will be EVENT_MUTE_CHANGE.
98
99
100  FM APIS  Blocking:
101
102In case of blocking FM APIS, the above sequence holds good. The difference will be the FM Events will not posted
103as intents and the usage of FM APIS will be sequential.
104 */
105
106public class FmRxApp extends Activity implements View.OnClickListener,
107        IFmConstants, FmRxAppConstants , FmRadio.ServiceListener {
108    public static final String TAG = "FmRxApp";
109    private static final boolean DBG = false;
110
111    /********************************************
112     * Widgets
113     ********************************************/
114
115    private ImageView imgFmPower, imgFmMode, imgFmVolume, imgFmAudiopath;
116    private ImageButton imgFmSeekUp, imgFmSeekDown;
117    private TextView txtStatusMsg, txtRadioText;
118    private TextView txtPsText;
119    static TextView txtStationName;
120    private static Button btnStation1, btnStation2, btnStation3;
121    private static Button btnStation4, btnStation5, btnStation6;
122    private ProgressDialog pd = null, configPd;
123
124    /********************************************
125     * Menu Constants
126     ********************************************/
127    public static final int MENU_CONFIGURE = Menu.FIRST + 1;
128    public static final int MENU_EXIT = Menu.FIRST + 2;
129    public static final int MENU_ABOUT = Menu.FIRST + 3;
130    public static final int MENU_PRESET = Menu.FIRST + 4;
131    public static final int MENU_SETFREQ = Menu.FIRST + 5;
132
133       public boolean mPreset = false;
134
135    /********************************************
136     * private variables
137     ********************************************/
138    private int mToggleMode = 0; // To toggle between the mono/stereo
139    private int mToggleAudio = 1; // To toggle between the speaker/headset
140    private boolean mToggleMute = false; // To toggle between the mute/unmute
141
142    private boolean mRdsState = false;
143    /* Default values */
144    private int mVolume = DEF_VOLUME;
145    private int mMode = DEFAULT_MODE;
146    private boolean mRds = DEFAULT_RDS;
147    private boolean mRdsAf = DEFAULT_RDS_AF;
148    private int mRdsSystem = INITIAL_VAL;
149    private int mDeEmpFilter = INITIAL_VAL;
150    private int mRssi = INITIAL_RSSI;
151    // Seek up/down direction
152    private int mDirection = FM_SEEK_UP;
153
154    /* State values */
155
156    // variable to make sure that the next configuration change happens after
157    // the current configuration request has been completed.
158    private int configurationState = CONFIGURATION_STATE_IDLE;
159    // variable to make sure that the next volume change happens after the
160    // current volume request has been completed.
161    private boolean mVolState = VOL_REQ_STATE_IDLE;
162    // variable to make sure that the next seek happens after the current seek
163    // request has been completed.
164    private boolean mSeekState = SEEK_REQ_STATE_IDLE;
165
166    private boolean mStatus;
167    private int mIndex = 0;
168    private int mStationIndex;
169
170
171    /*
172     * Variable to identify whether we need to do the default setting when
173     * entering the FM application. Based on this variable,the default
174     * configurations for the FM will be done for the first time
175     */
176
177    private static boolean sdefaultSettingOn = false;
178
179    private static boolean mIsDbPresent = false;
180
181    private NotificationManager mNotificationManager;
182
183    static final String FM_INTERRUPTED_KEY = "fm_interrupted";
184    static final String FM_STATE_KEY = "fm_state";
185    /* Flag to know whether FM App was interrupted due to orientation change */
186    boolean mFmInterrupted = false;
187
188    /*Flag to check if service is connected*/
189    boolean mFmServiceConnected = false;
190    /********************************************
191     * public variables
192     ********************************************/
193    public static int sBand = DEFAULT_BAND;
194    public static int sChannelSpace = DEFAULT_CHANNELSPACE;
195
196    public static Float lastTunedFrequency = (float) DEFAULT_FREQ_EUROPE;
197    public static FmRadio sFmRadio;
198
199    /** Arraylist of stations */
200    public static ArrayList<HashMap<String, String>> stations = new ArrayList<HashMap<String, String>>(
201            6);
202    public static TextView txtFmRxTunedFreq;
203    private OrientationListener mOrientationListener;
204
205    Context mContext;
206
207    DBAdapter db;
208
209    /** Called when the activity is first created. */
210
211    public void onCreate(Bundle savedInstanceState) {
212
213        super.onCreate(savedInstanceState);
214        mContext = this;
215        /* Retrieve the fm_state and find out whether FM App was interrupted */
216
217        if (savedInstanceState != null) {
218            Bundle fmState = savedInstanceState.getBundle(FM_STATE_KEY);
219            if (fmState != null) {
220                mFmInterrupted = fmState.getBoolean(FM_INTERRUPTED_KEY, false);
221
222            }
223        }
224
225        // Create orientation listenter.
226
227
228        //requestWindowFeature(Window.FEATURE_NO_TITLE);
229        setVolumeControlStream(AudioManager.STREAM_MUSIC);
230
231        // Register for FM intent broadcasts.
232        IntentFilter intentFilter = new IntentFilter();
233        intentFilter.addAction(FmRadioIntent.FM_ENABLED_ACTION);
234        intentFilter.addAction(FmRadioIntent.FM_DISABLED_ACTION);
235        intentFilter.addAction(FmRadioIntent.GET_FREQUENCY_ACTION);
236        intentFilter.addAction(FmRadioIntent.SEEK_ACTION);
237        intentFilter.addAction(FmRadioIntent.BAND_CHANGE_ACTION);
238        intentFilter.addAction(FmRadioIntent.GET_CHANNEL_SPACE_ACTION);
239        intentFilter.addAction(FmRadioIntent.SET_MODE_MONO_STEREO_ACTION);
240        intentFilter.addAction(FmRadioIntent.VOLUME_CHANGED_ACTION);
241        intentFilter.addAction(FmRadioIntent.RDS_TEXT_CHANGED_ACTION);
242        intentFilter.addAction(FmRadioIntent.PS_CHANGED_ACTION);
243        intentFilter.addAction(FmRadioIntent.AUDIO_PATH_CHANGED_ACTION);
244        intentFilter.addAction(FmRadioIntent.TUNE_COMPLETE_ACTION);
245        intentFilter.addAction(FmRadioIntent.SEEK_STOP_ACTION);
246        intentFilter.addAction(FmRadioIntent.MUTE_CHANGE_ACTION);
247        intentFilter.addAction(FmRadioIntent.DISPLAY_MODE_MONO_STEREO_ACTION);
248        intentFilter.addAction(FmRadioIntent.ENABLE_RDS_ACTION);
249        intentFilter.addAction(FmRadioIntent.DISABLE_RDS_ACTION);
250        intentFilter.addAction(FmRadioIntent.SET_RDS_AF_ACTION);
251        intentFilter.addAction(FmRadioIntent.SET_RDS_SYSTEM_ACTION);
252        intentFilter.addAction(FmRadioIntent.SET_DEEMP_FILTER_ACTION);
253        intentFilter.addAction(FmRadioIntent.SET_RSSI_THRESHHOLD_ACTION);
254        intentFilter.addAction(FmRadioIntent.SET_RF_DEPENDENT_MUTE_ACTION);
255        intentFilter.addAction(FmRadioIntent.PI_CODE_CHANGED_ACTION);
256        intentFilter.addAction(FmRadioIntent.MASTER_VOLUME_CHANGED_ACTION);
257        intentFilter.addAction(FmRadioIntent.CHANNEL_SPACING_CHANGED_ACTION);
258        intentFilter.addAction(FmRadioIntent.COMPLETE_SCAN_DONE_ACTION);
259        intentFilter.addAction(FmRadioIntent.COMPLETE_SCAN_STOP_ACTION);
260        intentFilter.addAction(FmRadioIntent.GET_BAND_ACTION);
261        intentFilter.addAction(FmRadioIntent.GET_MONO_STEREO_MODE_ACTION);
262        intentFilter.addAction(FmRadioIntent.GET_MUTE_MODE_ACTION);
263        intentFilter.addAction(FmRadioIntent.GET_RF_MUTE_MODE_ACTION);
264        intentFilter.addAction(FmRadioIntent.GET_RSSI_THRESHHOLD_ACTION);
265        intentFilter.addAction(FmRadioIntent.GET_DEEMPHASIS_FILTER_ACTION);
266        intentFilter.addAction(FmRadioIntent.GET_VOLUME_ACTION);
267        intentFilter.addAction(FmRadioIntent.GET_RDS_SYSTEM_ACTION);
268        intentFilter.addAction(FmRadioIntent.GET_RDS_GROUPMASK_ACTION);
269        intentFilter.addAction(FmRadioIntent.GET_RDS_AF_SWITCH_MODE_ACTION);
270        intentFilter.addAction(FmRadioIntent.GET_RSSI_ACTION);
271        intentFilter.addAction(FmRadioIntent.COMPLETE_SCAN_PROGRESS_ACTION);
272        intentFilter.addAction(FmRadioIntent.SET_RDS_AF_ACTION);
273        intentFilter.addAction(FmRadioIntent.SET_RF_DEPENDENT_MUTE_ACTION);
274        intentFilter.addAction(FmRadioIntent.SET_CHANNEL_SPACE_ACTION);
275
276        registerReceiver(mReceiver, intentFilter);
277
278       db = new DBAdapter(this);
279       createEmptyList();
280        /*
281         * Need to enable the FM if it is not enabled earlier
282         */
283
284        sFmRadio = new FmRadio(this, this);
285}
286    private void setButtonLabels() {
287
288       //Log.d(TAG, "setButtonLabels");
289        Iterator<HashMap<String, String>> stationIterator = stations.iterator();
290       while (stationIterator.hasNext()) {
291          HashMap<String, String> item = (HashMap<String, String>) stationIterator.next();
292
293          //Log.d(TAG, "setButtonLabels item " + item);
294          String freq = item.get(ITEM_VALUE);
295          int iButtonNo = Integer.parseInt(item.get(ITEM_KEY));
296          switch (iButtonNo) {
297             case 1:
298                if (freq.equals(""))
299                    btnStation1.setEnabled(false);
300                else {
301                    btnStation1.setEnabled(true);
302                    btnStation1.setText(freq);
303                }
304                break;
305             case 2:
306                if (freq.equals(""))
307                    btnStation2.setEnabled(false);
308                else {
309                    btnStation2.setEnabled(true);
310                    btnStation2.setText(freq);
311                }
312                break;
313             case 3:
314                if (freq.equals(""))
315                    btnStation3.setEnabled(false);
316                else {
317                    btnStation3.setEnabled(true);
318                    btnStation3.setText(freq);
319                }
320                break;
321             case 4:
322                if (freq.equals(""))
323                    btnStation4.setEnabled(false);
324                else {
325                    btnStation4.setEnabled(true);
326                    btnStation4.setText(freq);
327                }
328                break;
329             case 5:
330                if (freq.equals(""))
331                    btnStation5.setEnabled(false);
332                else {
333                    btnStation5.setEnabled(true);
334                    btnStation5.setText(freq);
335                }
336                break;
337             case 6:
338                if (freq.equals(""))
339                    btnStation6.setEnabled(false);
340                else {
341                    btnStation6.setEnabled(true);
342                    btnStation6.setText(freq);
343                }
344                break;
345
346          }
347
348       }
349
350    }
351private void startup() {
352        //Log.i(TAG, "startup");
353
354
355        switch (sFmRadio.rxGetFMState()) {
356        /* FM is alreary enabled. Just update the UI. */
357        case FmRadio.STATE_ENABLED:
358            Log.i(TAG, "FmRadio.STATE_ENABLED ");
359            // Fm app is on and we are entering from the other screens
360                    loadDefaultConfiguration();
361                    setContentView(R.layout.fmrxmain);
362                    initControls();
363                   setButtonLabels();
364                   if (sFmRadio.rxIsFMPaused()) {
365                         Log.i(TAG, "FmRadio.STATE_PAUSE ");
366                    mStatus = sFmRadio.resumeFm();
367                  if (mStatus == false) {
368                    showAlert(this, "FmRadio", "Cannot resume Radio!!!!");
369                      }
370            }
371
372            break;
373
374        case FmRadio.STATE_DISABLED:
375            Log.i(TAG, "FmRadio.STATE_DISABLED ");
376            sdefaultSettingOn = false;
377
378            mStatus = sFmRadio.rxEnable();
379            if (mStatus == false) {
380                showAlert(this, "FmRadio", "Cannot enable Radio!!!!");
381
382            }
383
384            else { /* Display the dialog till FM is enabled */
385                pd = ProgressDialog.show(this, "Please wait..",
386                        "Powering on Radio", true, false);
387            }
388
389            break;
390        /* FM has not been started. Start the FM */
391        case FmRadio.STATE_DEFAULT:
392
393            Log.i(TAG, "FmRadio.STATE_DEFAULT ");
394            sdefaultSettingOn = false;
395            /*
396             * Make sure not to start the FM_Enable() again, if it has been
397             * already called before orientation change
398             */
399            if (mFmInterrupted == false) {
400
401                mStatus = sFmRadio.rxEnable();
402                if (mStatus == false) {
403                    showAlert(this, "FmRadio", "Cannot enable Radio!!!!");
404
405                }
406
407                else { /* Display the dialog till FM is enabled */
408                    pd = ProgressDialog.show(this, "Please wait..",
409                            "Powering on Radio", true, false);
410                }
411            } else {
412                Log.w(TAG, "mFmInterrupted is true dont  call enable");
413
414            }
415            break;
416        }
417    }
418
419    public void onServiceConnected() {
420        //Log.i(TAG, "onServiceConnected");
421        mFmServiceConnected = true;
422        startup();
423    }
424
425    public void onServiceDisconnected() {
426        //Log.d(TAG, "Lost connection to service");
427        mFmServiceConnected = false ;
428        sFmRadio = null;
429    }
430
431
432    /*
433     * Set the arraylist with the selected station name and display the name on
434     * main screen
435     */
436    public static void UpdateRenameStation(int index, String name) {
437        txtStationName.setText(name.toString());
438        // Store the name in the selected index in Arraylist
439        SetStation(index, "", name);
440
441    }
442
443    /*
444     * Set the arraylist and the buttons with the selected station and make the
445     * button enabled so that user can select it to select the stored stations
446     */
447    public static void updateSetStation(int index, String freq, String name) {
448        switch (index) {
449        case 0:
450            if (freq.equals(""))
451                btnStation1.setEnabled(false);
452            else {
453                btnStation1.setEnabled(true);
454                btnStation1.setText(freq);
455            }
456
457            break;
458        case 1:
459            if (freq.equals(""))
460                btnStation2.setEnabled(false);
461            else {
462                btnStation2.setEnabled(true);
463                btnStation2.setText(freq);
464            }
465
466            break;
467        case 2:
468            if (freq.equals(""))
469                btnStation3.setEnabled(false);
470            else {
471                btnStation3.setEnabled(true);
472                btnStation3.setText(freq);
473            }
474            break;
475        case 3:
476            if (freq.equals(""))
477                btnStation4.setEnabled(false);
478            else {
479                btnStation4.setEnabled(true);
480                btnStation4.setText(freq);
481            }
482
483            break;
484        case 4:
485            if (freq.equals(""))
486                btnStation5.setEnabled(false);
487            else {
488                btnStation5.setEnabled(true);
489                btnStation5.setText(freq);
490            }
491
492            break;
493        case 5:
494            if (freq.equals(""))
495                btnStation6.setEnabled(false);
496            else {
497                btnStation6.setEnabled(true);
498                btnStation6.setText(freq);
499            }
500            break;
501        }
502        // Store the freq in the selected index in Arraylist
503        SetStation(index, freq, "");
504
505    }
506
507    /*
508     * Unset the arraylist and the button with the empty station and make the
509     * button disabled so that user cannot select it
510     */
511
512    public static void updateUnSetStation(int index) {
513        switch (index) {
514        case 0:
515            btnStation1.setEnabled(false);
516            btnStation1.setText("1");
517            break;
518        case 1:
519            btnStation2.setEnabled(false);
520            btnStation2.setText("2");
521            break;
522        case 2:
523            btnStation3.setEnabled(false);
524            btnStation3.setText("3");
525            break;
526        case 3:
527            btnStation4.setEnabled(false);
528            btnStation4.setText("4");
529            break;
530        case 4:
531
532            btnStation5.setEnabled(false);
533            btnStation5.setText("4");
534            break;
535        case 5:
536            btnStation6.setEnabled(false);
537            btnStation6.setText("6");
538            break;
539        }
540
541        UnSetStation(index, "", "");
542
543    }
544
545    /* Set particular station name or frequency in the stations arraylist */
546    public static void SetStation(Integer index, String freq, String name) {
547        Log.i(TAG, "SetStation");
548        Integer pos = new Integer(index.intValue() + 1);
549        try {
550            HashMap<String, String> item = stations.get(index.intValue());
551            item.put(ITEM_KEY, pos.toString());
552            // Update the name only
553            if ((freq.equals(""))) {
554                item.put(ITEM_NAME, name);
555
556                // Empty name string is passed. Update the name
557                if ((name.equals("")))
558                    item.put(ITEM_NAME, name);
559            }
560
561            // Update the frequency only
562            else if ((name.equals(""))) {
563                item.put(ITEM_VALUE, freq);
564
565            }
566            stations.set(index.intValue(), item);
567        } catch (NullPointerException e) {
568            Log.e(TAG, "Tried to add null value");
569        }
570    }
571
572    /* UnSet particular station name and frequency in the stations arraylist */
573    public static void UnSetStation(Integer index, String freq, String name) {
574       Log.i(TAG, "UnSetStation");
575       Integer pos = new Integer(index.intValue() + 1);
576       try {
577          HashMap<String, String> item = stations.get(index.intValue());
578          item.put(ITEM_KEY, pos.toString());
579          item.put(ITEM_VALUE, freq);
580          item.put(ITEM_NAME, name);
581          stations.set(index.intValue(), item);
582       } catch (NullPointerException e) {
583          Log.e(TAG, "Tried to add null value");
584       }
585    }
586
587    /* Get particular station from the stations arraylist */
588
589    public static String GetStation(Integer index) {
590        Log.i(TAG, "GetStation");
591        Integer pos = new Integer(index.intValue() + 1);
592        try {
593            HashMap<String, String> item = stations.get(index.intValue());
594            Object freq = item.get(ITEM_VALUE);
595            Object name = item.get(ITEM_NAME);
596            if (name != null) {
597                txtStationName.setText(name.toString());
598            } else {
599                Log.w(TAG, "There is no name in the HashMap.");
600            }
601
602            if (freq != null) {
603                return freq.toString();
604            } else {
605                Log.w(TAG, "There is no key in the HashMap.");
606            }
607        } catch (NullPointerException e) {
608            Log.e(TAG, "Tried to add null value");
609        }
610        return null;
611    }
612
613    /*
614     * Handler for all the FM related events. The events will be handled only
615     * when we are in the FM application main menu
616     */
617
618    private Handler mHandler = new Handler() {
619    StringBuilder sb ;
620    StringBuilder sbPs ;
621
622        public void handleMessage(Message msg) {
623
624            switch (msg.what) {
625
626            /*
627             * After FM is enabled dismiss the progress dialog and display the
628             * FM main screen. Set the default volume.
629             */
630
631            case EVENT_FM_ENABLED:
632
633                Log.i(TAG, "enter handleMessage ----EVENT_FM_ENABLED");
634                if (pd != null)
635                    pd.dismiss();
636
637                loadDefaultConfiguration();
638                setContentView(R.layout.fmrxmain);
639                /* At Power up, FM should be always unmuted. */
640                mToggleMute = false;
641                //Log.i(TAG, " handleMessage  init mToggleMute" + mToggleMute);
642                initControls();
643                setButtonLabels();
644                imgFmPower.setImageResource(R.drawable.poweron);
645
646                break;
647
648             /*
649              * Update the icon on main screen with appropriate mono/stereo
650              * icon
651              */
652            case EVENT_MONO_STEREO_CHANGE:
653
654                Log.i(TAG, "enter handleMessage ---EVENT_MONO_STEREO_CHANGE");
655                if (mMode == 0) {
656                    imgFmMode.setImageResource(R.drawable.fm_stereo);
657                } else {
658                    imgFmMode.setImageResource(R.drawable.fm_mono);
659                }
660                break;
661
662             /*
663              * Update the icon on main screen with appropriate mono/stereo
664              * icon
665              */
666            case EVENT_MONO_STEREO_DISPLAY:
667
668                Log.i(TAG, "enter handleMessage ---EVENT_MONO_STEREO_DISPLAY");
669
670                Integer mode = (Integer) msg.obj;
671                //Log.i(TAG, "enter handleMessage ---mode" + mode.intValue());
672                if (mode.intValue() == 0) {
673                    imgFmMode.setImageResource(R.drawable.fm_stereo);
674                } else {
675                    imgFmMode.setImageResource(R.drawable.fm_mono);
676                }
677                break;
678
679            /*
680             * Update the icon on main screen with appropriate mute/unmute icon
681             */
682            case EVENT_MUTE_CHANGE:
683                Log.i(TAG,"enter handleMessage ---EVENT_MUTE_CHANGE  ");
684
685                break;
686
687            case EVENT_SEEK_STOPPED:
688
689                Log.i(TAG, "enter handleMessage ---EVENT_SEEK_STOPPED");
690                Integer seekFreq = (Integer) msg.obj;
691                          //Log.i(TAG, "enter handleMessage ----EVENT_SEEK_STOPPED seekFreq" + seekFreq);
692                lastTunedFrequency = (float) seekFreq / 1000;
693                txtStatusMsg.setText(R.string.playing);
694                txtFmRxTunedFreq.setText(lastTunedFrequency.toString());
695
696                break;
697
698            case EVENT_FM_DISABLED:
699
700                Log.i(TAG, "enter handleMessage ----EVENT_FM_DISABLED");
701
702                /*
703                 * we have exited the FM App. Set the sdefaultSettingOn flag to
704                 * false Save the default configuration in the preference
705                 */
706                sdefaultSettingOn = false;
707                saveDefaultConfiguration();
708                finish();
709                break;
710
711            case EVENT_SEEK_STARTED:
712
713                Integer freq = (Integer) msg.obj;
714                          //Log.i(TAG, "enter handleMessage ----EVENT_SEEK_STARTED freq" + freq);
715                lastTunedFrequency = (float) freq / 1000;
716                txtStatusMsg.setText(R.string.playing);
717                txtFmRxTunedFreq.setText(lastTunedFrequency.toString());
718                // clear the RDS text
719                txtRadioText.setText(null);
720
721                // clear the PS text
722                txtPsText.setText(null);
723
724                /*
725                 * Seek up/down will be completed here. So set the state to
726                 * idle, so that user can seek to other frequency.
727                 */
728
729                mSeekState = SEEK_REQ_STATE_IDLE;
730
731                break;
732
733            /*
734             * Set the default band , if the fm app is getting started first
735             * time
736             */
737            case EVENT_VOLUME_CHANGE:
738                Log.i(TAG, "enter handleMessage ----EVENT_VOLUME_CHANGE");
739
740                /*
741                 * volume change will be completed here. So set the state to
742                 * idle, so that user can set other volume.
743                 */
744                mVolState = VOL_REQ_STATE_IDLE;
745                /*
746                 * Setting the default band after the volume change when FM app
747                 * is started for the first time
748                 */
749                if (sdefaultSettingOn == false) {
750                     /* Set the default band */
751                        if (MAKE_FM_APIS_BLOCKING == true) {
752                                 // Code for blocking call
753                               mStatus = sFmRadio.rxSetBand(sBand);
754                                if (mStatus == false) {
755                            showAlert(getParent(), "FmRadio","Not able to setband!!!!");
756                           } else {
757                                lastTunedFrequency = (float) lastTunedFrequency ;
758                                 mStatus = sFmRadio.rxTune_nb((int)(lastTunedFrequency.floatValue()* 1000));
759                            if (mStatus == false) {
760                                   showAlert(getParent(), "FmRadio","Not able to tune!!!!");
761                            }
762                        }
763
764                   } else {
765                       // Code for non blocking call
766                      mStatus = sFmRadio.rxSetBand_nb(sBand);
767                      if (mStatus == false) {
768                           showAlert(getParent(), "FmRadio","Not able to setband!!!!");
769                          }
770                   }
771
772
773
774                }
775                break;
776
777                case EVENT_COMPLETE_SCAN_PROGRESS:
778                    Log.i(TAG, "enter handleMessage ----EVENT_COMPLETE_SCAN_PROGRESS");
779
780                    Integer progress = (Integer) msg.obj;
781                      // Log.i(TAG,"enter handleMessage ----EVENT_COMPLETE_SCAN_PROGRESS progress"+ progress);
782                    break;
783
784            /*
785             * enable audio routing , if the fm app is getting started first
786             * time
787             */
788            case EVENT_TUNE_COMPLETE:
789                Integer tuneFreq = (Integer) msg.obj;
790                Log.i(TAG,"enter handleMessage ----EVENT_TUNE_COMPLETE tuneFreq"
791                                + tuneFreq);
792                lastTunedFrequency = (float) tuneFreq / 1000;
793                txtStatusMsg.setText(R.string.playing);
794                txtFmRxTunedFreq.setText(lastTunedFrequency.toString());
795                // clear the RDS text
796                txtRadioText.setText(null);
797                // clear the PS text
798                txtPsText.setText(null);
799
800                //Log.d(TAG, "sdefaultSettingOn" + sdefaultSettingOn);
801
802                /*
803                 * Enable the Audio routing after the tune complete , when FM
804                 * app is started for the first time or after reentering the Fm
805                 * app
806                 */
807                if (sdefaultSettingOn == false) {
808/*
809                mStatus = sFmRadio.rxEnableAudioRouting();
810                    if (mStatus == false) {
811                        showAlert(getParent(), "FmRadio",
812                                "Not able to enable audio!!!!");
813                    }
814*/
815                    /*
816                     * The default setting for the FMApp are completed here. If
817                     * the user selects back and goes out of the FM App, when he
818                     * reenters we dont have to do the default configurations
819                     */
820                    sdefaultSettingOn = true;
821                }
822
823                // clear the RDS text
824                txtRadioText.setText(null);
825
826                // clear the PS text
827                txtPsText.setText(null);
828
829                break;
830
831            /* Display the RDS text on UI */
832            case EVENT_RDS_TEXT:
833                Log.i(TAG, "enter handleMessage ----EVENT_RDS_TEXT");
834                if (FM_SEND_RDS_IN_BYTEARRAY == true) {
835                    byte[] rdsText = (byte[]) msg.obj;
836
837                    for (int i = 0; i < 4; i++)
838                        Log.i(TAG, "rdsText" + rdsText[i]);
839                } else {
840                       String rds = (String) msg.obj;
841                    Log.i(TAG, "enter handleMessage ----EVENT_RDS_TEXT rds"    + rds);
842                       sb = new StringBuilder("[RDS] ");
843                       sb.append(rds);
844                    Log.i(TAG, "enter handleMessage ----EVENT_RDS_TEXT sb.toString()"+ sb.toString());
845                       txtRadioText.setText(sb.toString());
846                }
847                break;
848
849            /* Display the RDS text on UI */
850            case EVENT_PI_CODE:
851                String pi = (String) msg.obj;
852                Log.i(TAG, "enter handleMessage ----EVENT_PI_CODE rds" + pi);
853
854                break;
855
856            case EVENT_SET_CHANNELSPACE:
857                Log.i(TAG, "enter handleMessage ----EVENT_SET_CHANNELSPACE");
858                break;
859
860
861            case EVENT_GET_CHANNEL_SPACE_CHANGE:
862                Log.i(TAG, "enter handleMessage ----EVENT_GET_CHANNEL_SPACE_CHANGE");
863                Long gChSpace = (Long) msg.obj;
864                //Log.d(TAG, "enter handleMessage ----gChSpace" + gChSpace);
865                    break;
866
867            /* tune to default frequency after the band change callback . */
868
869            case EVENT_BAND_CHANGE:
870                Log.i(TAG, "enter handleMessage ----EVENT_BAND_CHANGE");
871                /*
872                 * Tune to the last stored frequency at the
873                 * enable/re-enable,else tune to the default frequency when band
874                 * changes
875                 */
876
877                if (sdefaultSettingOn == true) {
878                    /* Set the default frequency */
879                    if (sBand == FM_BAND_EUROPE_US)
880                        lastTunedFrequency = (float) DEFAULT_FREQ_EUROPE;
881                    else
882                        lastTunedFrequency = (float) DEFAULT_FREQ_JAPAN;
883                }
884
885                mStatus = sFmRadio.rxTune_nb((int)(lastTunedFrequency.floatValue()*1000));
886                if (mStatus == false) {
887                    showAlert(getParent(), "FmRadio", "Not able to tune!!!!");
888                }
889
890                break;
891
892            /* Enable RDS system after enale RDS callback . */
893
894            case EVENT_ENABLE_RDS:
895                Log.i(TAG, "enter handleMessage ----EVENT_ENABLE_RDS");
896                break;
897
898            /* Set RSSI after SET_RDS_AF callback */
899            case EVENT_SET_RDS_AF:
900                Log.i(TAG, "enter handleMessage ----EVENT_SET_RDS_AF");
901                break;
902            /* Set RDS AF after SET_RDS_SYSTEM callback */
903            case EVENT_SET_RDS_SYSTEM:
904                Log.i(TAG, "enter handleMessage ----EVENT_SET_RDS_SYSTEM");
905                break;
906            /* Set RSSI after disable RDS callback */
907            case EVENT_DISABLE_RDS:
908                Log.i(TAG, "enter handleMessage ----EVENT_DISABLE_RDS");
909                txtPsText.setText(null);
910                txtRadioText.setText(null);
911                break;
912
913            case EVENT_SET_DEEMP_FILTER:
914                Log.i(TAG, "enter handleMessage ----EVENT_SET_DEEMP_FILTER");
915                break;
916
917            /* Display the PS text on UI */
918            case EVENT_PS_CHANGED:
919                Log.i(TAG, "enter handleMessage ----EVENT_PS_CHANGED");
920
921                if (FM_SEND_RDS_IN_BYTEARRAY == true) {
922                    byte[] psName = (byte[]) msg.obj;
923
924                    for (int i = 0; i < 4; i++)
925                        Log.i(TAG, "psName" + psName[i]);
926                } else {
927                   String ps = (String) msg.obj;
928                   Log.i(TAG, "ps  String is " + ps);
929                   sbPs = new StringBuilder("[PS] ");
930                   sbPs.append(ps);
931                   //Log.i(TAG, "enter handleMessage ----EVENT_PS_CHANGED sbPs.toString()"+ sbPs.toString());
932                   txtPsText.setText(sbPs.toString());
933                }
934
935                break;
936
937            case EVENT_SET_RSSI_THRESHHOLD:
938                Log.i(TAG, "enter handleMessage ----EVENT_SET_RSSI_THRESHHOLD");
939                /*
940                 * All the configurations will be completed here. So set the
941                 * state to idle, so that user can configure again
942                 */
943                configurationState = CONFIGURATION_STATE_IDLE;
944                break;
945            case EVENT_SET_RF_DEPENDENT_MUTE:
946                Log.i(TAG,"enter handleMessage ----EVENT_SET_RF_DEPENDENT_MUTE");
947                break;
948
949            case EVENT_COMPLETE_SCAN_STOP:
950                Log.i(TAG, "enter handleMessage ----EVENT_COMPLETE_SCAN_STOP");
951                break;
952
953            case EVENT_COMPLETE_SCAN_DONE:
954                Log.i(TAG, "enter handleMessage ----EVENT_COMPLETE_SCAN_DONE");
955
956                int[] channelList = (int[]) msg.obj;
957                int noOfChannels = (int) msg.arg2;
958
959                Log.i(TAG, "noOfChannels" + noOfChannels);
960
961                for (int i = 0; i < noOfChannels; i++)
962                    Log.i(TAG, "channelList" + channelList[i]);
963
964                break;
965
966            case EVENT_GET_BAND :
967
968                Long gBand = (Long) msg.obj;
969                Log.i(TAG,"enter handleMessage ----EVENT_GET_BAND");
970                Log.d(TAG, "enter handleMessage ----gBand" + gBand);
971                   break;
972
973            case EVENT_GET_FREQUENCY :
974
975                Integer gFreq = (Integer) msg.obj;
976                Log.i(TAG,"enter handleMessage ----EVENT_GET_FREQUENCY");
977                //Log.d(TAG, "enter handleMessage ----gFreq" + gFreq);
978                   break;
979            case EVENT_GET_VOLUME :
980                Long gVol = (Long) msg.obj;
981                Log.i(TAG,"enter handleMessage ----EVENT_GET_VOLUME");
982                Log.d(TAG, "enter handleMessage ----gVol" + gVol);
983                   break;
984            case EVENT_GET_MODE :
985                Long gMode = (Long) msg.obj;
986                Log.i(TAG,"enter handleMessage ----EVENT_GET_MODE");
987                Log.d(TAG, "enter handleMessage ----gMode" + gMode);
988                   break;
989            case EVENT_GET_MUTE_MODE :
990
991                Long gMuteMode = (Long) msg.obj;
992
993                if(gMuteMode ==(long) FM_UNMUTE)
994                    imgFmVolume.setImageResource(R.drawable.fm_volume);
995                else if (gMuteMode ==(long) FM_MUTE)
996                    imgFmVolume.setImageResource(R.drawable.fm_volume_mute);
997
998                Log.i(TAG,"enter handleMessage ----EVENT_GET_MUTE_MODE");
999                //Log.d(TAG, "enter handleMessage ----gMuteMode" + gMuteMode);
1000                     break;
1001            case EVENT_GET_RF_MUTE_MODE :
1002
1003                Long gRfMuteMode = (Long) msg.obj;
1004                Log.i(TAG,"enter handleMessage ----EVENT_GET_RF_MUTE_MODE");
1005                //Log.d(TAG, "enter handleMessage ----gRfMuteMode" + gRfMuteMode);
1006                   break;
1007            case EVENT_GET_RSSI_THRESHHOLD :
1008                Long gRssi = (Long) msg.obj;
1009                Log.i(TAG,"enter handleMessage ----EVENT_GET_RSSI_THRESHHOLD");
1010                Log.d(TAG, "enter handleMessage ----gRssi" + gRssi);
1011                   break;
1012
1013            case EVENT_GET_RSSI :
1014                   Integer rssi = (Integer) msg.obj;
1015                   Log.i(TAG,"enter handleMessage ----EVENT_GET_RSSI");
1016                   Log.d(TAG, "enter handleMessage ----rssi" + rssi);
1017                   break;
1018            case EVENT_GET_DEEMPHASIS_FILTER :
1019                Long gFilter= (Long) msg.obj;
1020                Log.i(TAG,"enter handleMessage ----EVENT_GET_DEEMPHASIS_FILTER");
1021                Log.d(TAG, "enter handleMessage ----gFilter" + gFilter);
1022                           break;
1023
1024            case EVENT_GET_RDS_SYSTEM :
1025                        Long gRdsSys= (Long) msg.obj;
1026                Log.i(TAG, "enter handleMessage ----EVENT_GET_RDS_SYSTEM");
1027                Log.d(TAG, "enter handleMessage ----gRdsSys" + gRdsSys);
1028            break;
1029            case EVENT_GET_RDS_GROUPMASK :
1030                Long gRdsMask= (Long) msg.obj;
1031                Log.i(TAG, "enter handleMessage ----EVENT_GET_RDS_GROUPMASK");
1032                Log.d(TAG, "enter handleMessage ----gRdsMask" + gRdsMask);
1033            break;
1034
1035            case EVENT_MASTER_VOLUME_CHANGED:
1036                Integer vol = (Integer) msg.obj;
1037                mVolume = vol;
1038                Log.i(TAG, "enter handleMessage ----EVENT_MASTER_VOLUME_CHANGED");
1039                Log.d(TAG, "enter handleMessage ----mVolume" + vol);
1040            }
1041        }
1042    };
1043
1044    /* Display alert dialog */
1045    public void showAlert(Context context, String title, String msg) {
1046
1047        new AlertDialog.Builder(context).setTitle(title).setIcon(
1048                android.R.drawable.ic_dialog_alert).setMessage(msg)
1049                .setNegativeButton(android.R.string.cancel, null).show();
1050
1051    }
1052
1053
1054    private void setRdsConfig() {
1055        Log.i(TAG, "setRdsConfig()-entered");
1056        configurationState = CONFIGURATION_STATE_PENDING;
1057        SharedPreferences fmConfigPreferences = getSharedPreferences(
1058                "fmConfigPreferences", MODE_PRIVATE);
1059
1060            // Set Band
1061            int band = fmConfigPreferences.getInt(BAND, DEFAULT_BAND);
1062            Log.i(TAG, "setRdsConfig()--- band= " + band);
1063            if (band != sBand) // If Band is same as the one set already donot set
1064            // it again
1065            {
1066
1067            if (MAKE_FM_APIS_BLOCKING == true) {
1068                  // Code for blocking call
1069                mStatus = sFmRadio.rxSetBand(band);
1070                if (mStatus == false) {
1071                    Log.e(TAG, "setRdsConfig()-- setBand ->Erorr");
1072                    showAlert(this, "FmRadio",
1073                            "Cannot  setBand to selected Value!!!!");
1074                } else {
1075                    sBand = band;
1076                    if (sdefaultSettingOn == true) {
1077                        /* Set the default frequency */
1078                        if (sBand == FM_BAND_EUROPE_US)
1079                            lastTunedFrequency = (float) DEFAULT_FREQ_EUROPE;
1080                        else
1081                            lastTunedFrequency = (float) DEFAULT_FREQ_JAPAN;
1082                    }
1083
1084                    lastTunedFrequency = (float) lastTunedFrequency ;
1085                    mStatus = sFmRadio.rxTune_nb((int)(lastTunedFrequency.floatValue()*1000));
1086                    if (mStatus == false) {
1087                        showAlert(getParent(), "FmRadio", "Not able to tune!!!!");
1088                    }
1089
1090                }
1091
1092            }
1093
1094            else
1095                {
1096
1097                    mStatus = sFmRadio.rxSetBand_nb(band);
1098                            if (mStatus == false) {
1099                    Log.e(TAG, "setRdsConfig()-- setBand ->Erorr");
1100                    showAlert(this, "FmRadio",
1101                            "Cannot  setBand to selected Value!!!!");
1102                } else {
1103                    sBand = band;
1104                    if (sdefaultSettingOn == true) {
1105                        /* Set the default frequency */
1106                        if (sBand == FM_BAND_EUROPE_US)
1107                            lastTunedFrequency = (float) DEFAULT_FREQ_EUROPE;
1108                        else
1109                            lastTunedFrequency = (float) DEFAULT_FREQ_JAPAN;
1110                    }
1111
1112                }
1113    }
1114}
1115
1116
1117        // Set De-emp Filter
1118        int deEmp = fmConfigPreferences.getInt(DEEMP, DEFAULT_DEEMP);
1119           if (mDeEmpFilter != deEmp)// If De-Emp filter is same as the one set
1120        // already donot set it again
1121        {
1122            if (MAKE_FM_APIS_BLOCKING == true)
1123            mStatus = sFmRadio.rxSetDeEmphasisFilter(deEmp);
1124            else
1125            mStatus = sFmRadio.rxSetDeEmphasisFilter_nb(deEmp);
1126
1127            Log.i(TAG, "setRdsConfig()--- DeEmp= " + deEmp);
1128
1129            if (mStatus == false) {
1130                Log.e(TAG, "setRdsConfig()-- setDeEmphasisFilter ->Erorr");
1131                showAlert(this, "FmRadio",
1132                        "Cannot set De-Emp Fileter to selected Value!!!!");
1133
1134            }
1135            mDeEmpFilter = deEmp;
1136
1137        }
1138
1139
1140                // Set Mode
1141        int mode = fmConfigPreferences.getInt(MODE, DEFAULT_MODE);
1142        if (mMode != mode)// If Mode is same as the one set already donot set it
1143        // again
1144        {
1145
1146            if (MAKE_FM_APIS_BLOCKING == true)
1147            mStatus = sFmRadio.rxSetMonoStereoMode(mode);
1148            else
1149            mStatus = sFmRadio.rxSetMonoStereoMode_nb(mode);
1150            if (mStatus == false) {
1151                showAlert(this, "FmRadio", "Not able to setmode!!!!");
1152            } else {
1153                mMode = mode;
1154                if (mMode == 0) {
1155                    imgFmMode.setImageResource(R.drawable.fm_stereo);
1156                } else {
1157                    imgFmMode.setImageResource(R.drawable.fm_mono);
1158                }
1159            }
1160
1161        }
1162
1163
1164        /** Set channel spacing to the one selected by the user */
1165        int channelSpace = fmConfigPreferences.getInt(CHANNELSPACE,
1166                DEFAULT_CHANNELSPACE);
1167        Log.i(TAG, "setChannelSpacing()--- channelSpace= " + channelSpace);
1168        if (channelSpace != sChannelSpace) // If channelSpace is same as the one
1169        // set already donot set
1170        // it again
1171        {
1172            if (MAKE_FM_APIS_BLOCKING == true)
1173                        mStatus = sFmRadio.rxSetChannelSpacing(channelSpace);
1174                    else
1175                        mStatus = sFmRadio.rxSetChannelSpacing_nb(channelSpace);
1176
1177            if (mStatus == false) {
1178                Log.e(TAG, "setChannelSpacing()-- setChannelSpacing ->Erorr");
1179                showAlert(this, "FmRadio",
1180                        "Cannot  setChannelSpacing to selected Value!!!!");
1181            }
1182            sChannelSpace = channelSpace;
1183        }
1184
1185
1186        // set RDS related configuration
1187        boolean rdsEnable = fmConfigPreferences.getBoolean(RDS, DEFAULT_RDS);
1188        Log.i(TAG, "setRDS()--- rdsEnable= " + rdsEnable);
1189        if (mRds != rdsEnable) {
1190
1191            if (rdsEnable) {
1192                    if (MAKE_FM_APIS_BLOCKING == true)
1193                        mStatus = sFmRadio.rxEnableRds();
1194                    else
1195                        mStatus = sFmRadio.rxEnableRds_nb();
1196                if (mStatus == false) {
1197                    Log.e(TAG, "setRDS()-- enableRds() ->Erorr");
1198                    showAlert(this, "FmRadio", "Cannot enable RDS!!!!");
1199                }
1200
1201            } else {
1202                if (MAKE_FM_APIS_BLOCKING == true)
1203                    mStatus = sFmRadio.rxDisableRds();
1204                else
1205                    mStatus = sFmRadio.rxDisableRds_nb();
1206
1207                if (mStatus == false) {
1208                    Log.e(TAG, "setRDS()-- disableRds() ->Erorr");
1209                    showAlert(this, "FmRadio", "Cannot disable RDS!!!!");
1210                } else {
1211                    Log.e(TAG, "setRDS()-- disableRds() ->success");
1212                    /* clear the PS and RDS text */
1213                    txtPsText.setText(null);
1214                    txtRadioText.setText(null);
1215                }
1216            }
1217            mRds = rdsEnable;
1218        }
1219
1220        // setRdssystem
1221        int rdsSystem = fmConfigPreferences.getInt(RDSSYSTEM,
1222                DEFAULT_RDS_SYSTEM);
1223    if (DBG)
1224        Log.d(TAG, "setRdsSystem()--- rdsSystem= " + rdsSystem);
1225        if (mRdsSystem != rdsSystem) {
1226            // Set RDS-SYSTEM if a new choice is made by the user
1227
1228            if (MAKE_FM_APIS_BLOCKING == true)
1229            mStatus = sFmRadio.rxSetRdsSystem(fmConfigPreferences.getInt(
1230                    RDSSYSTEM, DEFAULT_RDS_SYSTEM));
1231            else
1232            mStatus = sFmRadio.rxSetRdsSystem_nb(fmConfigPreferences.getInt(
1233                    RDSSYSTEM, DEFAULT_RDS_SYSTEM));
1234
1235            if (mStatus == false) {
1236                Log.e(TAG, " setRdsSystem()-- setRdsSystem ->Erorr");
1237                showAlert(this, "FmRadio",
1238                        "Cannot set Rds System to selected Value!!!!");
1239            }
1240            mRdsSystem = rdsSystem;
1241        }
1242
1243        boolean rdsAfSwitch = fmConfigPreferences.getBoolean(RDSAF,
1244                    DEFAULT_RDS_AF);
1245            int rdsAf = 0;
1246            rdsAf = rdsAfSwitch ? 1 : 0;
1247            if (DBG)
1248            Log.d(TAG, "setRdsAf()--- rdsAfSwitch= " + rdsAf);
1249            if (mRdsAf != rdsAfSwitch) {
1250                // Set RDS-AF if a new choice is made by the user
1251
1252                if (MAKE_FM_APIS_BLOCKING == true)
1253                mStatus = sFmRadio.rxSetRdsAfSwitchMode(rdsAf);
1254                else
1255                mStatus = sFmRadio.rxSetRdsAfSwitchMode_nb(rdsAf);
1256                if (mStatus == false) {
1257                    Log.e(TAG, "setRdsAf()-- setRdsAfSwitchMode(1) ->Erorr");
1258                    showAlert(this, "FmRadio", "Cannot set RDS AF Mode ON!!!!");
1259                }
1260                mRdsAf = rdsAfSwitch;
1261            }
1262        // Set Rssi
1263        int rssiThreshHold = fmConfigPreferences.getInt(RSSI, DEFAULT_RSSI);
1264        Log.i(TAG, "setRssi()-ENTER --- rssiThreshHold= " + rssiThreshHold);
1265
1266            // Set RSSI if a new value is entered by the user
1267
1268            if (MAKE_FM_APIS_BLOCKING == true)
1269                        mStatus = sFmRadio.rxSetRssiThreshold(rssiThreshHold);
1270                    else
1271                        mStatus = sFmRadio.rxSetRssiThreshold_nb(rssiThreshHold);
1272
1273            if (mStatus == false) {
1274                showAlert(this, "FmRadio", "Not able to setRssiThreshold!!!!");
1275            }
1276
1277            mRssi = rssiThreshHold;
1278
1279
1280            Log.i(TAG, "setRdsConfig()-exit");
1281
1282    }
1283
1284    /* Load the Default values from the preference when the application starts */
1285    private void loadDefaultConfiguration() {
1286
1287       Log.i(TAG, "loadDefaultConfiguration()-entered");
1288       SharedPreferences fmConfigPreferences = getSharedPreferences("fmConfigPreferences",
1289             MODE_PRIVATE);
1290
1291         if (mPreset)
1292            saveObject();
1293
1294       /* Read the stations stored in DB and update the UI */
1295       try {
1296          db.open();
1297          Cursor c = db.getStation(1);
1298          if (c != null && c.moveToFirst())
1299             mIsDbPresent = true;
1300          else
1301             mIsDbPresent = false;
1302          c.close();
1303          db.close();
1304       } catch (SQLException e) {
1305          mIsDbPresent = false;
1306       } catch (Exception ex) {
1307          mIsDbPresent = false;
1308       }
1309
1310       if (mIsDbPresent == false) {
1311
1312          Log.d(TAG, " mIsDbPresent writeobject" + mIsDbPresent);
1313          writeObject();
1314          mIsDbPresent = true;
1315       } else {
1316          Log.d(TAG, " mIsDbPresent readobject" + mIsDbPresent);
1317          readObject();
1318       }
1319
1320       Log.d(TAG, " mIsDbPresent " + mIsDbPresent);
1321        sBand = fmConfigPreferences.getInt(BAND, DEFAULT_BAND);
1322        lastTunedFrequency = fmConfigPreferences.getFloat(FREQUENCY,
1323                (sBand == FM_BAND_EUROPE_US ? DEFAULT_FREQ_EUROPE
1324                        : DEFAULT_FREQ_JAPAN));
1325        mMode = fmConfigPreferences.getInt(MODE, DEFAULT_MODE);
1326        mToggleMute = fmConfigPreferences.getBoolean(MUTE, false);
1327       mRdsState = fmConfigPreferences.getBoolean(RDS, false);
1328
1329       if (DBG)
1330          Log.d(TAG, " Load default band " + sBand + "default volume" + mVolume + "last fre"
1331                + lastTunedFrequency + "mode" + mMode + "mToggleMute" + mToggleMute +"mRdsState"+mRdsState);
1332
1333    }
1334
1335    private void createEmptyList() {
1336
1337       Log.d(TAG, " createEmptyList ");
1338       stations = new ArrayList<HashMap<String, String>>(6);
1339       for (int i = 1; i < 7; i++) {
1340          HashMap<String, String> stationsMap = new HashMap<String, String>();
1341
1342          stationsMap.put(ITEM_KEY, String.valueOf(i));
1343          stationsMap.put(ITEM_VALUE, "");
1344          stationsMap.put(ITEM_NAME, "");
1345          stations.add(i - 1, stationsMap);
1346       }
1347
1348    }
1349    /* Save the Default values to the preference when the application exits */
1350    private void saveDefaultConfiguration() {
1351        Log.i(TAG, "saveDefaultConfiguration()-Entered");
1352
1353        SharedPreferences fmConfigPreferences = getSharedPreferences(
1354                "fmConfigPreferences", MODE_PRIVATE);
1355        SharedPreferences.Editor editor = fmConfigPreferences.edit();
1356        editor.putInt(BAND, sBand);
1357        editor.putBoolean(MUTE, mToggleMute);
1358        editor.putFloat(FREQUENCY, lastTunedFrequency);
1359             if (DBG)
1360            Log.d(TAG, " save default band " + sBand + "default volume" + mVolume
1361                + "last fre" + lastTunedFrequency+"mToggleMute"+mToggleMute);
1362        /* Write the stations stored to text file */
1363             saveObject();
1364        editor.commit();
1365    }
1366
1367    /* Read the Stations from the text file in file system */
1368    void readObject() {
1369       Log.i(TAG, "readObject ");
1370
1371       db.open();
1372       Cursor c = db.getAllStations();
1373       int count = c.getCount();
1374
1375       Log.i(TAG, "readObject cursor count " + count);
1376       int iCount = 0;
1377       if (c.moveToFirst()) {
1378          do {
1379
1380             HashMap<String, String> stations1 = new HashMap<String, String>();
1381
1382             stations1.put(ITEM_KEY, c.getString(1));
1383             stations1.put(ITEM_VALUE, c.getString(2));
1384             stations1.put(ITEM_NAME, c.getString(3));
1385             stations.set(iCount, stations1);
1386             iCount++;
1387
1388          } while (c.moveToNext());
1389
1390       } else
1391          Toast.makeText(this, "No station found", Toast.LENGTH_LONG).show();
1392       c.close();
1393       db.close();
1394
1395    }
1396
1397    /* Write the Stations to the text file in file system */
1398    void writeObject() {
1399        Log.i(TAG, "writeObject ");
1400
1401       db.open();
1402       long id;
1403       id = db.insertStation("1", "", "");
1404       id = db.insertStation("2", "", " ");
1405       id = db.insertStation("3", "", "");
1406       id = db.insertStation("4", "", " ");
1407       id = db.insertStation("5", "", " ");
1408       id = db.insertStation("6", "", " ");
1409       db.close();
1410
1411    }
1412
1413    void saveObject() {
1414
1415       Log.i(TAG, "enter saveObject");
1416       db.open();
1417       Iterator iterator = stations.iterator();
1418
1419       while (iterator.hasNext()) {
1420          HashMap<String, String> item = (HashMap<String, String>) iterator.next();
1421          Object v = item.get(ITEM_VALUE);
1422          Object n = item.get(ITEM_NAME);
1423          Object i = item.get(ITEM_KEY);
1424
1425          try {
1426             mIndex = Integer.parseInt(i.toString());
1427             updateSetStation((mIndex - 1), v.toString(), n.toString());
1428             db.updateStation(mIndex, i.toString(), v.toString(), n.toString());
1429          } catch (Exception e) {
1430             Log.e(TAG, "Exception");
1431             e.printStackTrace();
1432          }
1433
1434       }
1435
1436       db.close();
1437
1438    }
1439    /* Initialise all the widgets */
1440    private void initControls() {
1441        /**
1442         * Start FM RX
1443         */
1444        Log.i(TAG, "enter initControls");
1445
1446        imgFmPower = (ImageView) findViewById(R.id.imgPower);
1447        imgFmPower.setOnClickListener(this);
1448
1449        imgFmMode = (ImageView) findViewById(R.id.imgMode);
1450        if (mMode == 0) {
1451        Log.i(TAG, " setting stereo icon" +mMode);
1452        imgFmMode.setImageResource(R.drawable.fm_stereo);
1453        } else {
1454        Log.i(TAG, " setting mono icon" +mMode);
1455        imgFmMode.setImageResource(R.drawable.fm_mono);
1456        }
1457
1458        imgFmVolume = (ImageView) findViewById(R.id.imgMute);
1459        imgFmVolume.setOnClickListener(this);
1460
1461        if (mToggleMute == true) {
1462        imgFmVolume.setImageResource(R.drawable.fm_volume_mute);
1463        Log.i(TAG, " initControls  mute" +mToggleMute);
1464        } else {
1465        imgFmVolume.setImageResource(R.drawable.fm_volume);
1466        Log.i(TAG, " initControls  mute" +mToggleMute);
1467        }
1468
1469
1470        imgFmSeekUp = (ImageButton) findViewById(R.id.imgseekup);
1471        imgFmSeekUp.setOnClickListener(this);
1472
1473        imgFmSeekDown = (ImageButton) findViewById(R.id.imgseekdown);
1474        imgFmSeekDown.setOnClickListener(this);
1475
1476        txtFmRxTunedFreq = (TextView) findViewById(R.id.txtRxFreq);
1477        txtFmRxTunedFreq.setText(lastTunedFrequency.toString());
1478
1479        txtStatusMsg = (TextView) findViewById(R.id.txtStatusMsg);
1480        txtRadioText = (TextView) findViewById(R.id.txtRadioText);
1481        txtPsText = (TextView) findViewById(R.id.txtPsText);
1482
1483        txtStationName = (TextView) findViewById(R.id.txtStationName);
1484        txtStationName.setText(null);
1485
1486        btnStation1 = (Button) findViewById(R.id.station1);
1487        btnStation1.setOnClickListener(this);
1488        btnStation1.setEnabled(false);
1489
1490        btnStation2 = (Button) findViewById(R.id.station2);
1491        btnStation2.setOnClickListener(this);
1492        btnStation2.setEnabled(false);
1493
1494        btnStation3 = (Button) findViewById(R.id.station3);
1495        btnStation3.setOnClickListener(this);
1496        btnStation3.setEnabled(false);
1497
1498        btnStation4 = (Button) findViewById(R.id.station4);
1499        btnStation4.setOnClickListener(this);
1500        btnStation4.setEnabled(false);
1501
1502        btnStation5 = (Button) findViewById(R.id.station5);
1503        btnStation5.setOnClickListener(this);
1504        btnStation5.setEnabled(false);
1505
1506        btnStation6 = (Button) findViewById(R.id.station6);
1507        btnStation6.setOnClickListener(this);
1508        btnStation6.setEnabled(false);
1509
1510        // Get the notification manager service.
1511        mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
1512
1513    }
1514
1515    /** Adds Delay of 3 seconds */
1516    private void insertDelayThread() {
1517
1518        new Thread() {
1519            public void run() {
1520                try {
1521                    // Add some delay to make sure all configuration has been
1522                    // completed.
1523                    sleep(3000);
1524                } catch (Exception e) {
1525                    Log.e(TAG, "InsertDelayThread()-- Exception !!");
1526                }
1527                // Dismiss the Dialog
1528                configPd.dismiss();
1529            }
1530        }.start();
1531
1532    }
1533
1534    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
1535        super.onActivityResult(requestCode, resultCode, data);
1536        Log.i(TAG, "onActivityResult");
1537        switch (requestCode) {
1538          case (ACTIVITY_PRESET): {
1539             if (resultCode == Activity.RESULT_OK) {
1540                Log.i(TAG, "Prests saved");
1541                saveObject();
1542
1543             }
1544          }
1545             break;
1546        case (ACTIVITY_TUNE): {
1547            if (resultCode == Activity.RESULT_OK && data != null) {
1548
1549                Bundle extras = data.getExtras();
1550                if (extras != null) {
1551
1552                    lastTunedFrequency = (float) extras.getFloat(FREQ_VALUE, 0);
1553                    txtFmRxTunedFreq.setText(lastTunedFrequency.toString());
1554                    mStatus = sFmRadio.rxTune_nb((int)(lastTunedFrequency.floatValue()*1000));
1555                    if (mStatus == false) {
1556                        showAlert(this, "FmRadio", "Not able to tune!!!!");
1557                    }
1558                }
1559            }
1560        }
1561            break;
1562
1563        case (ACTIVITY_CONFIG): {
1564            if (resultCode == Activity.RESULT_OK) {
1565                Log.i(TAG, "ActivityFmRdsConfig configurationState "
1566                        + configurationState);
1567            if(configurationState == CONFIGURATION_STATE_IDLE)
1568            {
1569
1570
1571                    setRdsConfig();
1572                    configPd = ProgressDialog.show(this, "Please wait..",
1573                            "Applying new Configuration", true, false);
1574                            // The delay is inserted to make sure all the configurations
1575                    // have been completed.
1576                    insertDelayThread();
1577                }
1578            }
1579
1580        }
1581            break;
1582
1583        }
1584    }
1585
1586    public boolean onKeyDown(int keyCode, KeyEvent event) {
1587
1588        switch (keyCode) {
1589        case KeyEvent.KEYCODE_DPAD_DOWN:
1590
1591            return true;
1592        case KeyEvent.KEYCODE_DPAD_UP:
1593
1594            return true;
1595        case KeyEvent.KEYCODE_BACK:
1596
1597            saveDefaultConfiguration();
1598            finish();
1599            return true;
1600            /* Keys A to L are mapped to different get APIs for Testing */
1601        case KeyEvent.KEYCODE_A:
1602
1603            if (MAKE_FM_APIS_BLOCKING == true) {
1604                      // Code for blocking call
1605                                  Log.i(TAG, "Testing getVolume()  returned volume = "
1606                    + sFmRadio.rxGetVolume());
1607
1608                    } else {
1609                      // Code for non blocking call
1610                                  Log.i(TAG, "Testing getVolume_nb()  returned volume = "
1611                    + sFmRadio.rxGetVolume_nb());
1612
1613                    }
1614
1615
1616            return true;
1617
1618        case KeyEvent.KEYCODE_B:
1619
1620            if (MAKE_FM_APIS_BLOCKING == true) {
1621          // Code for blocking call
1622                      Log.i(TAG, "Testing getTunedFrequency()  returned Tuned Freq = "
1623                    + sFmRadio.rxGetTunedFrequency());
1624
1625        } else {
1626          // Code for non blocking call
1627                      Log.i(TAG, "Testing getTunedFrequency_nb()  returned Tuned Freq = "
1628                    + sFmRadio.rxGetTunedFrequency_nb());
1629
1630        }
1631
1632            return true;
1633
1634        case KeyEvent.KEYCODE_C:
1635            if (MAKE_FM_APIS_BLOCKING == true) {
1636          // Code for blocking call
1637                    Log.i(TAG, "Testing getRssiThreshold()    returned RSSI thrshld = "
1638                    + sFmRadio.rxGetRssiThreshold());
1639        } else {
1640          // Code for non blocking call
1641                    Log.i(TAG, "Testing getRssiThreshold_nb()    returned RSSI thrshld = "
1642                    + sFmRadio.rxGetRssiThreshold_nb());
1643        }
1644
1645            return true;
1646
1647        case KeyEvent.KEYCODE_D:
1648            if (MAKE_FM_APIS_BLOCKING == true) {
1649          // Code for blocking call
1650                    Log.i(TAG, "Testing getBand() returned Band  = "
1651                    + sFmRadio.rxGetBand());
1652        } else {
1653          // Code for non blocking call
1654                    Log.i(TAG, "Testing getBand_nb() returned Band  = "
1655                    + sFmRadio.rxGetBand_nb());
1656        }
1657
1658            return true;
1659
1660        case KeyEvent.KEYCODE_E:
1661            if (MAKE_FM_APIS_BLOCKING == true) {
1662          // Code for blocking call
1663                    Log.i(TAG, "Testing getDeEmphasisFilter()    returned De-emp  = "
1664                    + sFmRadio.rxGetDeEmphasisFilter());
1665        } else {
1666          // Code for non blocking call
1667                    Log.i(TAG, "Testing getDeEmphasisFilter_nb()    returned De-emp  = "
1668                    + sFmRadio.rxGetDeEmphasisFilter_nb());
1669        }
1670
1671            return true;
1672
1673        case KeyEvent.KEYCODE_F:
1674            if (MAKE_FM_APIS_BLOCKING == true) {
1675          // Code for blocking call
1676                      Log.i(TAG, "Testing getMonoStereoMode() returned MonoStereo = "
1677                    + sFmRadio.rxGetMonoStereoMode());
1678
1679        } else {
1680          // Code for non blocking call
1681                      Log.i(TAG, "Testing getMonoStereoMode_nb() returned MonoStereo = "
1682                    + sFmRadio.rxGetMonoStereoMode_nb());
1683
1684        }
1685
1686            return true;
1687
1688        case KeyEvent.KEYCODE_G:
1689            if (MAKE_FM_APIS_BLOCKING == true) {
1690          // Code for blocking call
1691                    Log.i(TAG, "Testing getMuteMode()  returned MuteMode = "
1692                    + sFmRadio.rxGetMuteMode());
1693        } else {
1694          // Code for non blocking call
1695                    Log.i(TAG, "Testing getMuteMode_nb()  returned MuteMode = "
1696                    + sFmRadio.rxGetMuteMode_nb());
1697        }
1698
1699            return true;
1700
1701        case KeyEvent.KEYCODE_H:
1702            if (MAKE_FM_APIS_BLOCKING == true) {
1703          // Code for blocking call
1704                    Log.i(TAG,
1705                    "Testing getRdsAfSwitchMode()    returned RdsAfSwitchMode = "
1706                            + sFmRadio.rxGetRdsAfSwitchMode());
1707        } else {
1708          // Code for non blocking call
1709                    Log.i(TAG,
1710                    "Testing getRdsAfSwitchMode_nb()    returned RdsAfSwitchMode = "
1711                            + sFmRadio.rxGetRdsAfSwitchMode_nb());
1712        }
1713
1714            return true;
1715
1716        case KeyEvent.KEYCODE_I:
1717            if (MAKE_FM_APIS_BLOCKING == true) {
1718          // Code for blocking call
1719                    Log.i(TAG, "Testing getRdsGroupMask() returned RdsGrpMask = "
1720                    + sFmRadio.rxGetRdsGroupMask());
1721        } else {
1722          // Code for non blocking call
1723                    Log.i(TAG, "Testing getRdsGroupMask_nb() returned RdsGrpMask = "
1724                    + sFmRadio.rxGetRdsGroupMask_nb());
1725        }
1726
1727            return true;
1728
1729        case KeyEvent.KEYCODE_J:
1730            if (MAKE_FM_APIS_BLOCKING == true) {
1731          // Code for blocking call
1732                    Log.i(TAG, "Testing getRdsSystem() returned Rds System = "
1733                    + sFmRadio.rxGetRdsSystem());
1734        } else {
1735          // Code for non blocking call
1736                    Log.i(TAG, "Testing getRdsSystem_nb() returned Rds System = "
1737                    + sFmRadio.rxGetRdsSystem_nb());
1738        }
1739
1740            return true;
1741
1742        case KeyEvent.KEYCODE_K:
1743            if (MAKE_FM_APIS_BLOCKING == true) {
1744          // Code for blocking call
1745                    Log.i(TAG,
1746                    "Testing getRfDependentMuteMode()    returned RfDepndtMuteMode = "
1747                            + sFmRadio.rxGetRfDependentMuteMode());
1748        } else {
1749          // Code for non blocking call
1750                    Log.i(TAG,
1751                    "Testing getRfDependentMuteMode_nb()    returned RfDepndtMuteMode = "
1752                            + sFmRadio.rxGetRfDependentMuteMode_nb());
1753        }
1754
1755            return true;
1756
1757        case KeyEvent.KEYCODE_L:
1758
1759            if (MAKE_FM_APIS_BLOCKING == true) {
1760
1761            LayoutInflater inflater = getLayoutInflater();
1762            View layout = inflater.inflate(R.layout.toast,
1763                    (ViewGroup) findViewById(R.id.toast_layout));
1764            TextView text = (TextView) layout.findViewById(R.id.text);
1765            text.setText("The current Rssi    " + sFmRadio.rxGetRssi());
1766
1767            Toast toast = new Toast(getApplicationContext());
1768            toast.setGravity(Gravity.BOTTOM | Gravity.CENTER_VERTICAL, 0, 0);
1769            toast.setDuration(Toast.LENGTH_LONG);
1770            toast.setView(layout);
1771            toast.show();
1772                }
1773            else
1774                {
1775                Log.i(TAG,
1776                    "Testing rxGetRssi_nb()    returned  = "
1777                            + sFmRadio.rxGetRssi_nb());
1778                }
1779
1780            return true;
1781
1782        case KeyEvent.KEYCODE_M:
1783            Log.i(TAG, "Testing isValidChannel()    returned isValidChannel = "
1784                    + sFmRadio.rxIsValidChannel());
1785
1786            return true;
1787
1788        case KeyEvent.KEYCODE_N:
1789            Log.i(TAG, "Testing getFwVersion()    returned getFwVersion = "
1790                    + sFmRadio.rxGetFwVersion());
1791
1792            return true;
1793
1794        case KeyEvent.KEYCODE_O:
1795            if (MAKE_FM_APIS_BLOCKING == true) {
1796          // Code for blocking call
1797                    Log.i(TAG,
1798                    "Testing getChannelSpacing()    returned getChannelSpacing = "
1799                            + sFmRadio.rxGetChannelSpacing());
1800        } else {
1801          // Code for non blocking call
1802                    Log.i(TAG,
1803                    "Testing getChannelSpacing_nb()    returned getChannelSpacing = "
1804                            + sFmRadio.rxGetChannelSpacing_nb());
1805        }
1806
1807
1808            return true;
1809
1810        case KeyEvent.KEYCODE_P:
1811            Log.i(TAG, "Testing completescan()");
1812            sFmRadio.rxCompleteScan_nb();
1813            return true;
1814
1815        case KeyEvent.KEYCODE_Q:
1816
1817            if (MAKE_FM_APIS_BLOCKING == true) {
1818            Log.i(TAG,
1819                    "Testing getCompleteScanProgress()    returned scan progress = "
1820                            + sFmRadio.rxGetCompleteScanProgress());
1821                }
1822            else
1823                {
1824                            Log.i(TAG,
1825                    "Testing getCompleteScanProgress()    returned scan progress = "
1826                            + sFmRadio.rxGetCompleteScanProgress_nb());
1827                }
1828
1829            return true;
1830
1831        case KeyEvent.KEYCODE_R:
1832            if (MAKE_FM_APIS_BLOCKING == true) {
1833            Log.i(TAG, "Testing stopCompleteScan()    returned status = "
1834                    + sFmRadio.rxStopCompleteScan());
1835                }
1836            else
1837                {
1838                            Log.i(TAG, "Testing stopCompleteScan()    returned status = "
1839                    + sFmRadio.rxStopCompleteScan_nb());
1840                }
1841
1842            return true;
1843
1844        case KeyEvent.KEYCODE_S:
1845
1846if (MAKE_FM_APIS_BLOCKING == true) {
1847          // Code for blocking call
1848            Log.i(TAG,
1849                    "Testing setRfDependentMuteMode()    returned RfDepndtMuteMode = "
1850                            + sFmRadio.rxSetRfDependentMuteMode(1));
1851        } else {
1852          // Code for non blocking call
1853    Log.i(TAG,
1854                    "Testing setRfDependentMuteMode()    returned RfDepndtMuteMode = "
1855                            + sFmRadio.rxSetRfDependentMuteMode_nb(1));
1856        }
1857
1858            return true;
1859
1860        }
1861
1862        return false;
1863    }
1864
1865    /* Get the stored frequency from the arraylist and tune to that frequency */
1866    void tuneStationFrequency(String text) {
1867        try {
1868            float iFreq = Float.parseFloat(text);
1869            if (iFreq != 0) {
1870                lastTunedFrequency = (float) iFreq ;
1871                if (DBG)
1872                Log.d(TAG, "lastTunedFrequency" + lastTunedFrequency);
1873                mStatus = sFmRadio.rxTune_nb((int)(lastTunedFrequency.floatValue()*1000));
1874                if (mStatus == false) {
1875                    showAlert(getParent(), "FmRadio", "Not able to tune!!!!");
1876                }
1877            } else {
1878
1879                new AlertDialog.Builder(this).setIcon(
1880                        android.R.drawable.ic_dialog_alert).setMessage(
1881                        "Enter valid frequency!!").setNegativeButton(
1882                        android.R.string.ok, null).show();
1883
1884            }
1885        } catch (NumberFormatException nfe) {
1886            Log.e(TAG, "nfe");
1887        }
1888    }
1889
1890    public void onClick(View v) {
1891        int id = v.getId();
1892
1893        switch (id) {
1894        case R.id.imgPower:
1895
1896            /*
1897             * The exit from the FM application happens here The audio will be
1898             * disabled and when he callback for this is recived, FM will be
1899             * disabled
1900             */
1901
1902//            mStatus = sFmRadio.rxDisableAudioRouting();
1903//            Log.d(TAG, "mStatus is : " + mStatus );
1904
1905//            try{
1906//            if (mStatus == false) {
1907//                showAlert(getParent(), "FmRadio",
1908//                        "Not able to disable audio!!!!");
1909//            }
1910//            }catch (NumberFormatException e) {
1911//            Log.e(TAG, "showAlert - disableAudioRouting exception  (" + e.toString() + ")");
1912//            }
1913
1914            mStatus = sFmRadio.rxDisable();
1915                    mPreset = false;
1916            break;
1917        case R.id.imgMode:
1918            break;
1919        //case R.id.imgAudiopath:
1920
1921            // TODO
1922            //break;
1923        case R.id.imgMute:
1924
1925
1926            Log.d(TAG, " imgMute mToggleMute " + mToggleMute );
1927            int muteValue =0;
1928            if(mToggleMute ==true)
1929            muteValue =1 ;
1930            else
1931            muteValue =0;
1932
1933            if (MAKE_FM_APIS_BLOCKING == true) {
1934                      // Code for blocking call
1935                        mStatus = sFmRadio.rxSetMuteMode(muteValue);
1936            } else {
1937                      // Code for non blocking call
1938                        mStatus = sFmRadio.rxSetMuteMode_nb(muteValue);
1939            }
1940
1941            if (mStatus == false) {
1942                showAlert(this, "FmRadio", "Not able to setmute!!!!");
1943            } else {
1944
1945                mToggleMute = !mToggleMute ;
1946                if (mToggleMute == true) {
1947                    imgFmVolume.setImageResource(R.drawable.fm_volume_mute);
1948                } else {
1949                    imgFmVolume.setImageResource(R.drawable.fm_volume);
1950                }
1951            }
1952
1953            break;
1954
1955        case R.id.imgseekdown:
1956            mDirection = FM_SEEK_DOWN;
1957            // FM seek down
1958
1959            if (mSeekState == SEEK_REQ_STATE_IDLE) {
1960                txtStationName.setText(null); // set the station name to null
1961                mStatus = sFmRadio.rxSeek_nb(mDirection);
1962                if (mStatus == false) {
1963                    showAlert(this, "FmRadio", "Not able to seek down!!!!");
1964                } else {
1965                    mSeekState = SEEK_REQ_STATE_PENDING;
1966                    txtStatusMsg.setText(R.string.seeking);
1967                }
1968
1969            }
1970
1971            break;
1972        case R.id.imgseekup:
1973            mDirection = FM_SEEK_UP;
1974            // FM seek up
1975            if (mSeekState == SEEK_REQ_STATE_IDLE) {
1976                txtStationName.setText(null); // set the station name to null
1977                mStatus = sFmRadio.rxSeek_nb(mDirection);
1978                if (mStatus == false) {
1979                    showAlert(this, "FmRadio", "Not able to seek up!!!!");
1980
1981                } else {
1982                    mSeekState = SEEK_REQ_STATE_PENDING;
1983                    txtStatusMsg.setText(R.string.seeking);
1984                }
1985            }
1986
1987            break;
1988        case R.id.station1:
1989            mStationIndex = 0;
1990            updateStationDisplay(mStationIndex);
1991
1992            break;
1993        case R.id.station2:
1994            mStationIndex = 1;
1995            updateStationDisplay(mStationIndex);
1996
1997            break;
1998        case R.id.station3:
1999            mStationIndex = 2;
2000            updateStationDisplay(mStationIndex);
2001
2002            break;
2003        case R.id.station4:
2004            mStationIndex = 3;
2005            updateStationDisplay(mStationIndex);
2006            break;
2007
2008        case R.id.station5:
2009            mStationIndex = 4;
2010            updateStationDisplay(mStationIndex);
2011
2012            break;
2013        case R.id.station6:
2014            mStationIndex = 5;
2015            updateStationDisplay(mStationIndex);
2016            break;
2017        }
2018
2019    }
2020
2021    /* Gets the stored frequency and tunes to it. */
2022    void updateStationDisplay(int index) {
2023        String tunedFreq = null;
2024        tunedFreq = GetStation(index);
2025        tuneStationFrequency(tunedFreq);
2026        txtFmRxTunedFreq.setText(tunedFreq.toString());
2027
2028    }
2029
2030    /* Creates the menu items */
2031    public boolean onCreateOptionsMenu(Menu menu) {
2032
2033        super.onCreateOptionsMenu(menu);
2034        MenuItem item;
2035
2036        item = menu.add(0, MENU_CONFIGURE, 0, R.string.configure);
2037        item.setIcon(R.drawable.configure);
2038
2039        item = menu.add(0, MENU_ABOUT, 0, R.string.about);
2040        item.setIcon(R.drawable.fm_menu_help);
2041
2042        item = menu.add(0, MENU_EXIT, 0, R.string.exit);
2043        item.setIcon(R.drawable.icon);
2044
2045        item = menu.add(0, MENU_PRESET, 0, R.string.preset);
2046        item.setIcon(R.drawable.fm_menu_preferences);
2047
2048        item = menu.add(0, MENU_SETFREQ, 0, R.string.setfreq);
2049        item.setIcon(R.drawable.fm_menu_manage);
2050
2051        return true;
2052    }
2053
2054    /* Handles item selections */
2055    public boolean onOptionsItemSelected(MenuItem item) {
2056
2057        switch (item.getItemId()) {
2058        case MENU_CONFIGURE:
2059            /* Start the configuration window */
2060            Intent irds = new Intent(INTENT_RDS_CONFIG);
2061            startActivityForResult(irds, ACTIVITY_CONFIG);
2062            break;
2063        case MENU_PRESET:
2064            /* Start the Presets window */
2065            Intent i = new Intent(INTENT_PRESET);
2066                    mPreset = true;
2067            startActivity(i);
2068            break;
2069        case MENU_EXIT:
2070            /*
2071             * The exit from the FM application happens here The audio will be
2072             * disabled and when he callback for this is recived, FM will be
2073             * disabled
2074             */
2075//            mStatus = sFmRadio.rxDisableAudioRouting();
2076//            if (mStatus == false) {
2077//                showAlert(getParent(), "FmRadio",
2078//                        "Not able to disable audio!!!!");
2079//            }
2080//            Log.d(TAG, "MENU_EXIT ------------------- 2");
2081
2082            mStatus = sFmRadio.rxDisable();
2083                    mPreset = false;
2084
2085            break;
2086        case MENU_ABOUT:
2087            /* Start the help window */
2088            Intent iTxHelp = new Intent(INTENT_RXHELP);
2089            startActivity(iTxHelp);
2090            break;
2091
2092        case MENU_SETFREQ:
2093            /* Start the Manual frequency input window */
2094            Intent iRxTune = new Intent(INTENT_RXTUNE);
2095            startActivityForResult(iRxTune, ACTIVITY_TUNE);
2096            break;
2097
2098        }
2099        return super.onOptionsItemSelected(item);
2100    }
2101
2102    protected void onSaveInstanceState(Bundle icicle) {
2103        super.onSaveInstanceState(icicle);
2104        Log.i(TAG, "onSaveInstanceState");
2105        /* save the fm state into bundle for the activity restart */
2106        mFmInterrupted = true;
2107        Bundle fmState = new Bundle();
2108        fmState.putBoolean(FM_INTERRUPTED_KEY, mFmInterrupted);
2109
2110        icicle.putBundle(FM_STATE_KEY, fmState);
2111    }
2112
2113    public void onStart() {
2114        Log.i(TAG, "onStart");
2115        super.onStart();
2116    }
2117
2118    public void onPause() {
2119        super.onPause();
2120        Log.i(TAG, "onPause");
2121
2122        if (pd != null)
2123            pd.dismiss();
2124
2125        saveDefaultConfiguration();
2126
2127    }
2128
2129    public void onConfigurationChanged(Configuration newConfig) {
2130        Log.i(TAG, "onConfigurationChanged");
2131        super.onConfigurationChanged(newConfig);
2132
2133    }
2134
2135    public void onResume() {
2136        Log.i(TAG, "onResume");
2137        super.onResume();
2138        if(mFmServiceConnected == true)
2139        startup();
2140    }
2141
2142    public void onDestroy() {
2143        Log.i(TAG, "onDestroy");
2144        super.onDestroy();
2145        /*
2146         * Unregistering the receiver , so that we dont handle any FM events
2147         * when out of the FM application screen
2148         */
2149        unregisterReceiver(mReceiver);
2150    }
2151
2152    // Receives all of the FM intents and dispatches to the proper handler
2153
2154    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
2155
2156        public void onReceive(Context context, Intent intent) {
2157
2158            String fmAction = intent.getAction();
2159
2160            Log.i(TAG, "enter onReceive" + fmAction);
2161            if (fmAction.equals(FmRadioIntent.FM_ENABLED_ACTION)) {
2162                Log.i(TAG, "enter onReceive FM_ENABLED_ACTION " + fmAction);
2163
2164                mHandler.sendMessage(mHandler
2165                        .obtainMessage(EVENT_FM_ENABLED, 0));
2166            }
2167            if (fmAction.equals(FmRadioIntent.FM_DISABLED_ACTION)) {
2168                Log.i(TAG, "enter onReceive FM_DISABLED_ACTION " + fmAction);
2169                mHandler.sendMessage(mHandler.obtainMessage(EVENT_FM_DISABLED,
2170                        0));
2171            }
2172
2173            if (fmAction.equals(FmRadioIntent.SET_MODE_MONO_STEREO_ACTION)) {
2174                Log.i(TAG, "enter onReceive SET_MODE_MONO_STEREO_ACTION "
2175                        + fmAction);
2176                mHandler.sendMessage(mHandler.obtainMessage(
2177                        EVENT_MONO_STEREO_CHANGE, 0));
2178            }
2179            if (fmAction
2180                    .equals(FmRadioIntent.DISPLAY_MODE_MONO_STEREO_ACTION)) {
2181                Log.i(TAG, "enter onReceive DISPLAY_MODE_MONO_STEREO_ACTION "
2182                        + fmAction);
2183                Integer modeDisplay = intent.getIntExtra(
2184                        FmRadioIntent.MODE_MONO_STEREO, 0);
2185                mHandler.sendMessage(mHandler.obtainMessage(
2186                        EVENT_MONO_STEREO_DISPLAY, modeDisplay));
2187            }
2188
2189            if (fmAction.equals(FmRadioIntent.RDS_TEXT_CHANGED_ACTION)) {
2190                Log.i(TAG, "enter onReceive RDS_TEXT_CHANGED_ACTION "
2191                        + fmAction);
2192                if (FM_SEND_RDS_IN_BYTEARRAY == true) {
2193                    Bundle extras = intent.getExtras();
2194
2195                    byte[] rdsText = extras.getByteArray(FmRadioIntent.RDS);
2196                    int status = extras.getInt(FmRadioIntent.STATUS, 0);
2197
2198                    mHandler.sendMessage(mHandler.obtainMessage(EVENT_RDS_TEXT,
2199                            status, 0, rdsText));
2200                } else {
2201                    String rdstext = intent
2202                            .getStringExtra(FmRadioIntent.RADIOTEXT_CONVERTED);
2203                mHandler.sendMessage(mHandler.obtainMessage(EVENT_RDS_TEXT,
2204                        rdstext));
2205            }
2206            }
2207            if (fmAction.equals(FmRadioIntent.PI_CODE_CHANGED_ACTION)) {
2208                Log
2209                        .i(TAG, "enter onReceive PI_CODE_CHANGED_ACTION "
2210                                + fmAction);
2211
2212                Integer pi = intent.getIntExtra(FmRadioIntent.PI, 0);
2213
2214                mHandler.sendMessage(mHandler.obtainMessage(EVENT_PI_CODE, pi
2215                        .toString()));
2216            }
2217
2218            if (fmAction.equals(FmRadioIntent.TUNE_COMPLETE_ACTION)) {
2219                Log.i(TAG, "enter onReceive TUNE_COMPLETE_ACTION " + fmAction);
2220
2221                int tuneFreq = intent.getIntExtra(
2222                        FmRadioIntent.TUNED_FREQUENCY, 0);
2223
2224                mHandler.sendMessage(mHandler.obtainMessage(
2225                        EVENT_TUNE_COMPLETE, tuneFreq));
2226
2227            }
2228
2229            if (fmAction.equals(FmRadioIntent.COMPLETE_SCAN_PROGRESS_ACTION)) {
2230                Log.i(TAG, "enter onReceive COMPLETE_SCAN_PROGRESS_ACTION " + fmAction);
2231
2232                int progress = intent.getIntExtra(
2233                        FmRadioIntent.SCAN_PROGRESS, 0);
2234
2235                mHandler.sendMessage(mHandler.obtainMessage(
2236                        EVENT_COMPLETE_SCAN_PROGRESS, progress));
2237
2238            }
2239
2240
2241            if (fmAction.equals(FmRadioIntent.VOLUME_CHANGED_ACTION)) {
2242                Log.i(TAG, "enter onReceive VOLUME_CHANGED_ACTION " + fmAction);
2243                mHandler.sendMessage(mHandler.obtainMessage(
2244                        EVENT_VOLUME_CHANGE, 0));
2245            }
2246
2247            if (fmAction.equals(FmRadioIntent.MUTE_CHANGE_ACTION)) {
2248                Log.i(TAG, "enter onReceive MUTE_CHANGE_ACTION " + fmAction);
2249                mHandler.sendMessage(mHandler.obtainMessage(EVENT_MUTE_CHANGE,
2250                        0));
2251            }
2252
2253            if (fmAction.equals(FmRadioIntent.SEEK_STOP_ACTION)) {
2254                Log.i(TAG, "enter onReceive SEEK_STOP_ACTION " + fmAction);
2255
2256                int freq = intent.getIntExtra(FmRadioIntent.SEEK_FREQUENCY,
2257                        0);
2258
2259                mHandler.sendMessage(mHandler.obtainMessage(EVENT_SEEK_STOPPED,
2260                        freq));
2261            }
2262
2263            if (fmAction.equals(FmRadioIntent.SEEK_ACTION)) {
2264                Log.i(TAG, "enter onReceive SEEK_ACTION " + fmAction);
2265
2266                int freq = intent.getIntExtra(FmRadioIntent.SEEK_FREQUENCY,
2267                        0);
2268
2269                mHandler.sendMessage(mHandler.obtainMessage(EVENT_SEEK_STARTED,
2270                        freq));
2271            }
2272
2273            if (fmAction.equals(FmRadioIntent.BAND_CHANGE_ACTION)) {
2274                Log.i(TAG, "enter onReceive BAND_CHANGE_ACTION " + fmAction);
2275
2276                mHandler.sendMessage(mHandler.obtainMessage(EVENT_BAND_CHANGE,
2277                        0));
2278            }
2279
2280
2281            if (fmAction.equals(FmRadioIntent.GET_CHANNEL_SPACE_ACTION)) {
2282                Log.i(TAG, "enter onReceive GET_CHANNEL_SPACE_ACTION " + fmAction);
2283
2284                Long chSpace = intent.getLongExtra(
2285                        FmRadioIntent.GET_CHANNEL_SPACE, 0);
2286                mHandler.sendMessage(mHandler.obtainMessage(
2287                        EVENT_GET_CHANNEL_SPACE_CHANGE, chSpace));
2288            }
2289
2290
2291            if (fmAction.equals(FmRadioIntent.SET_CHANNEL_SPACE_ACTION)) {
2292                Log.i(TAG, "enter onReceive SET_CHANNEL_SPACE_ACTION " + fmAction);
2293
2294                mHandler.sendMessage(mHandler.obtainMessage(
2295                        EVENT_SET_CHANNELSPACE, 0));
2296            }
2297
2298
2299
2300            if (fmAction.equals(FmRadioIntent.GET_RDS_AF_SWITCH_MODE_ACTION)) {
2301                        Log.i(TAG, "enter onReceive GET_RDS_AF_SWITCH_MODE_ACTION " + fmAction);
2302
2303                        Long switchMode = intent.getLongExtra(
2304                                FmRadioIntent.GET_RDS_AF_SWITCHMODE, 0);
2305                        mHandler.sendMessage(mHandler.obtainMessage(
2306                                EVENT_GET_RDS_AF_SWITCHMODE, switchMode));
2307                    }
2308
2309
2310            if (fmAction.equals(FmRadioIntent.GET_VOLUME_ACTION)) {
2311            Log.i(TAG, "enter onReceive GET_VOLUME_ACTION " + fmAction);
2312
2313            Long gVolume = intent.getLongExtra(
2314                    FmRadioIntent.GET_VOLUME, 0);
2315            mHandler.sendMessage(mHandler.obtainMessage(
2316                    EVENT_GET_VOLUME, gVolume));
2317        }
2318
2319
2320                if (fmAction.equals(FmRadioIntent.GET_MONO_STEREO_MODE_ACTION)) {
2321            Log.i(TAG, "enter onReceive GET_MONO_STEREO_MODE_ACTION " + fmAction);
2322
2323            Long gMode = intent.getLongExtra(
2324                    FmRadioIntent.GET_MODE, 0);
2325            mHandler.sendMessage(mHandler.obtainMessage(
2326                    EVENT_GET_MODE, gMode));
2327        }
2328
2329
2330
2331                        if (fmAction.equals(FmRadioIntent.GET_MUTE_MODE_ACTION)) {
2332                    Log.i(TAG, "enter onReceive GET_MUTE_MODE_ACTION " + fmAction);
2333
2334                    Long gMuteMode = intent.getLongExtra(
2335                            FmRadioIntent.GET_MUTE_MODE, 0);
2336                    mHandler.sendMessage(mHandler.obtainMessage(
2337                            EVENT_GET_MUTE_MODE, gMuteMode));
2338                }
2339
2340
2341
2342
2343                if (fmAction.equals(FmRadioIntent.GET_BAND_ACTION)) {
2344                Log.i(TAG, "enter onReceive GET_BAND_ACTION " + fmAction);
2345
2346                Long gBand = intent.getLongExtra(
2347                        FmRadioIntent.GET_BAND, 0);
2348                mHandler.sendMessage(mHandler.obtainMessage(
2349                        EVENT_GET_BAND, gBand));
2350            }
2351
2352
2353            if (fmAction.equals(FmRadioIntent.GET_FREQUENCY_ACTION)) {
2354            Log.i(TAG, "enter onReceive GET_FREQUENCY_ACTION " + fmAction);
2355
2356            int gFreq = intent.getIntExtra(
2357                    FmRadioIntent.TUNED_FREQUENCY, 0);
2358            mHandler.sendMessage(mHandler.obtainMessage(
2359                    EVENT_GET_FREQUENCY, gFreq));
2360        }
2361
2362
2363                                if (fmAction.equals(FmRadioIntent.GET_RF_MUTE_MODE_ACTION)) {
2364                Log.i(TAG, "enter onReceive GET_RF_MUTE_MODE_ACTION " + fmAction);
2365
2366                Long gRfMuteMode = intent.getLongExtra(
2367                        FmRadioIntent.GET_RF_MUTE_MODE, 0);
2368                mHandler.sendMessage(mHandler.obtainMessage(
2369                        EVENT_GET_RF_MUTE_MODE, gRfMuteMode));
2370            }
2371
2372
2373
2374                if (fmAction.equals(FmRadioIntent.GET_RSSI_THRESHHOLD_ACTION)) {
2375                Log.i(TAG, "enter onReceive GET_RSSI_THRESHHOLD_ACTION " + fmAction);
2376
2377                Long gRssiThreshhold = intent.getLongExtra(
2378                        FmRadioIntent.GET_RSSI_THRESHHOLD, 0);
2379                mHandler.sendMessage(mHandler.obtainMessage(
2380                        EVENT_GET_RSSI_THRESHHOLD, gRssiThreshhold));
2381            }
2382
2383
2384                if (fmAction.equals(FmRadioIntent.GET_DEEMPHASIS_FILTER_ACTION)) {
2385                Log.i(TAG, "enter onReceive GET_DEEMPHASIS_FILTER_ACTION " + fmAction);
2386
2387                Long gFilter = intent.getLongExtra(
2388                        FmRadioIntent.GET_DEEMPHASIS_FILTER, 0);
2389                mHandler.sendMessage(mHandler.obtainMessage(
2390                        EVENT_GET_DEEMPHASIS_FILTER, gFilter));
2391            }
2392
2393
2394                if (fmAction.equals(FmRadioIntent.GET_RSSI_ACTION)) {
2395                    Log.i(TAG, "enter onReceive GET_RSSI_ACTION " + fmAction);
2396
2397                    int gRssi = intent.getIntExtra(
2398                            FmRadioIntent.GET_RSSI, 0);
2399                    mHandler.sendMessage(mHandler.obtainMessage(
2400                            EVENT_GET_RSSI, gRssi));
2401                }
2402
2403
2404                if (fmAction.equals(FmRadioIntent.GET_RDS_SYSTEM_ACTION)) {
2405                Log.i(TAG, "enter onReceive GET_RDS_SYSTEM_ACTION " + fmAction);
2406
2407                Long gRdsSystem = intent.getLongExtra(
2408                        FmRadioIntent.GET_RDS_SYSTEM, 0);
2409                mHandler.sendMessage(mHandler.obtainMessage(
2410                        EVENT_GET_RDS_SYSTEM, gRdsSystem));
2411            }
2412
2413
2414            if (fmAction.equals(FmRadioIntent.GET_RDS_GROUPMASK_ACTION)) {
2415                Log.i(TAG, "enter onReceive GET_RDS_GROUPMASK_ACTION " + fmAction);
2416
2417                Long gRdsMask = intent.getLongExtra(
2418                        FmRadioIntent.GET_RDS_GROUPMASK, 0);
2419                mHandler.sendMessage(mHandler.obtainMessage(
2420                        EVENT_GET_RDS_GROUPMASK, gRdsMask));
2421            }
2422
2423
2424
2425            if (fmAction.equals(FmRadioIntent.ENABLE_RDS_ACTION)) {
2426                Log.i(TAG, "enter onReceive ENABLE_RDS_ACTION " + fmAction);
2427
2428                mHandler.sendMessage(mHandler
2429                        .obtainMessage(EVENT_ENABLE_RDS, 0));
2430            }
2431
2432            if (fmAction.equals(FmRadioIntent.DISABLE_RDS_ACTION)) {
2433                Log.i(TAG, "enter onReceive DISABLE_RDS_ACTION " + fmAction);
2434
2435                mHandler.sendMessage(mHandler.obtainMessage(EVENT_DISABLE_RDS,
2436                        0));
2437            }
2438
2439            if (fmAction.equals(FmRadioIntent.SET_RDS_AF_ACTION)) {
2440                Log.i(TAG, "enter onReceive SET_RDS_AF_ACTION " + fmAction);
2441
2442                mHandler.sendMessage(mHandler
2443                        .obtainMessage(EVENT_SET_RDS_AF, 0));
2444            }
2445
2446            if (fmAction.equals(FmRadioIntent.SET_RDS_SYSTEM_ACTION)) {
2447                Log.i(TAG, "enter onReceive SET_RDS_SYSTEM_ACTION " + fmAction);
2448
2449                mHandler.sendMessage(mHandler.obtainMessage(
2450                        EVENT_SET_RDS_SYSTEM, 0));
2451            }
2452
2453            if (fmAction.equals(FmRadioIntent.SET_DEEMP_FILTER_ACTION)) {
2454                Log.i(TAG, "enter onReceive SET_DEEMP_FILTER_ACTION "
2455                        + fmAction);
2456
2457                mHandler.sendMessage(mHandler.obtainMessage(
2458                        EVENT_SET_DEEMP_FILTER, 0));
2459            }
2460
2461            if (fmAction.equals(FmRadioIntent.PS_CHANGED_ACTION)) {
2462                Log.i(TAG, "enter onReceive PS_CHANGED_ACTION " + fmAction);
2463
2464                if (FM_SEND_RDS_IN_BYTEARRAY == true) {
2465                    Bundle extras = intent.getExtras();
2466                    byte[] psName = extras.getByteArray(FmRadioIntent.PS);
2467                    int status = extras.getInt(FmRadioIntent.STATUS, 0);
2468
2469                mHandler.sendMessage(mHandler.obtainMessage(
2470                            EVENT_PS_CHANGED, status, 0, psName));
2471                } else {
2472
2473                    String name = intent
2474                            .getStringExtra(FmRadioIntent.PS_CONVERTED);
2475
2476                mHandler.sendMessage(mHandler.obtainMessage(
2477                            EVENT_PS_CHANGED, name));
2478                }
2479            }
2480
2481            if (fmAction.equals(FmRadioIntent.SET_RSSI_THRESHHOLD_ACTION)) {
2482                Log.i(TAG, "enter onReceive SET_RSSI_THRESHHOLD_ACTION "
2483                        + fmAction);
2484
2485                mHandler.sendMessage(mHandler.obtainMessage(
2486                        EVENT_SET_RSSI_THRESHHOLD, 0));
2487            }
2488
2489            if (fmAction.equals(FmRadioIntent.SET_RF_DEPENDENT_MUTE_ACTION)) {
2490                Log.i(TAG, "enter onReceive SET_RF_DEPENDENT_MUTE_ACTION "
2491                        + fmAction);
2492
2493                mHandler.sendMessage(mHandler.obtainMessage(
2494                        EVENT_SET_RF_DEPENDENT_MUTE, 0));
2495            }
2496
2497            if (fmAction.equals(FmRadioIntent.COMPLETE_SCAN_DONE_ACTION)) {
2498                Log.i(TAG, "enter onReceive COMPLETE_SCAN_DONE_ACTION "
2499                        + fmAction);
2500
2501                Bundle extras = intent.getExtras();
2502
2503                int[] channelList = extras
2504                        .getIntArray(FmRadioIntent.SCAN_LIST);
2505
2506                int noOfChannels = extras.getInt(
2507                        FmRadioIntent.SCAN_LIST_COUNT, 0);
2508
2509                int status = extras.getInt(FmRadioIntent.STATUS, 0);
2510
2511                Log.i(TAG, "noOfChannels" + noOfChannels);
2512
2513                for (int i = 0; i < noOfChannels; i++)
2514
2515                    Log.i(TAG, "channelList" + channelList[i]);
2516
2517                mHandler.sendMessage(mHandler.obtainMessage(
2518                        EVENT_COMPLETE_SCAN_DONE, status, noOfChannels,
2519                        channelList));
2520            }
2521
2522            if (fmAction.equals(FmRadioIntent.COMPLETE_SCAN_STOP_ACTION)) {
2523                Log.i(TAG, "enter onReceive COMPLETE_SCAN_STOP_ACTION "
2524                        + fmAction);
2525                Bundle extras = intent.getExtras();
2526                int status = extras.getInt(FmRadioIntent.STATUS, 0);
2527                int channelValue = extras.getInt(
2528                FmRadioIntent.LAST_SCAN_CHANNEL, 0);
2529                Log.i(TAG, "Last Scanned Channel Frequency before calling Stop Scan" + channelValue);
2530                mHandler.sendMessage(mHandler.obtainMessage(
2531                EVENT_COMPLETE_SCAN_STOP, status , channelValue));
2532            }
2533
2534            if (fmAction.equals(FmRadioIntent.MASTER_VOLUME_CHANGED_ACTION)) {
2535                Log.i(TAG, "enter onReceive MASTER_VOLUME_CHANGED_ACTION "
2536                        + fmAction);
2537                mVolume = intent.getIntExtra(FmRadioIntent.MASTER_VOLUME, 0);
2538                mHandler.sendMessage(mHandler.obtainMessage(
2539                        EVENT_MASTER_VOLUME_CHANGED, mVolume));
2540            }
2541
2542        }
2543    };
2544
2545    /* Get the volume */
2546    void getNewGain(int volume) {
2547        Log.d(TAG, "getNewGain" + volume);
2548        if (volume <= GAIN_STEP) {
2549            mVolume = MIN_VOLUME;
2550        }
2551
2552        else if (volume >= MAX_VOLUME) {
2553            mVolume = MAX_VOLUME;
2554        } else {
2555            mVolume = volume;
2556        }
2557    }
2558
2559}
2560