summaryrefslogtreecommitdiffstats
path: root/fmradio/java/com/stericsson/hardware/fm/FmReceiverService.java
blob: 45703d52583bfd32b4286ae477154cc9eacf3853 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
/*
 * Copyright (C) ST-Ericsson SA 2010
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author: Markus Grape (markus.grape@stericsson.com) for ST-Ericsson
 */

package com.stericsson.hardware.fm;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.provider.Settings;
import android.util.Log;
import android.util.Slog;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

/**
 * The implementation of the FM receiver service.
 *
 * @hide
 */
public class FmReceiverService extends IFmReceiver.Stub {

    private static final String TAG = "FmReceiverService";

    private Context mContext;

    private final HashMap<Object, OnStateChangedReceiver> mOnStateChangedReceivers =
        new HashMap<Object, OnStateChangedReceiver>();

    private final HashMap<Object, OnStartedReceiver> mOnStartedReceivers =
        new HashMap<Object, OnStartedReceiver>();

    private final HashMap<Object, OnErrorReceiver> mOnErrorReceivers =
        new HashMap<Object, OnErrorReceiver>();

    private final HashMap<Object, OnScanReceiver> mOnScanReceivers =
        new HashMap<Object, OnScanReceiver>();

    private final HashMap<Object, OnForcedPauseReceiver> mOnForcedPauseReceivers =
        new HashMap<Object, OnForcedPauseReceiver>();

    private final HashMap<Object, OnForcedResetReceiver> mOnForcedResetReceivers =
        new HashMap<Object, OnForcedResetReceiver>();

    private final HashMap<Object, OnRDSDataReceiver> mOnRDSDataReceivers =
        new HashMap<Object, OnRDSDataReceiver>();

    private final HashMap<Object, OnSignalStrengthReceiver> mOnSignalStrengthReceivers =
        new HashMap<Object, OnSignalStrengthReceiver>();

    private final HashMap<Object, OnStereoReceiver> mOnStereoReceivers =
        new HashMap<Object, OnStereoReceiver>();

    private final HashMap<Object, OnExtraCommandReceiver> mOnExtraCommandReceivers =
        new HashMap<Object, OnExtraCommandReceiver>();

    private final HashMap<Object, OnAutomaticSwitchReceiver> mOnAutomaticSwitchReceivers =
        new HashMap<Object, OnAutomaticSwitchReceiver>();

    private final class OnStateChangedReceiver implements IBinder.DeathRecipient {
        final IOnStateChangedListener mListener;

        final Object mKey;

        OnStateChangedReceiver(IOnStateChangedListener listener) {
            mListener = listener;
            mKey = listener.asBinder();
        }

        public IOnStateChangedListener getListener() {
            return mListener;
        }

        public boolean callOnStateChanged(int oldState, int newState) {
            try {
                synchronized (this) {
                    mListener.onStateChanged(oldState, newState);
                }
            } catch (RemoteException ex) {
                Log.e(TAG, "callOnStateChanged: RemoteException", ex);
                return false;
            }
            return true;
        }

        public void binderDied() {
            Log.d(TAG, "FM receiver listener died");

            synchronized (mOnStateChangedReceivers) {
                mOnStateChangedReceivers.remove(this.mKey);
            }
            if (mListener != null) {
                mListener.asBinder().unlinkToDeath(this, 0);
            }
        }
    }

    private final class OnStartedReceiver implements IBinder.DeathRecipient {
        final IOnStartedListener mListener;

        final Object mKey;

        OnStartedReceiver(IOnStartedListener listener) {
            mListener = listener;
            mKey = listener.asBinder();
        }

        public IOnStartedListener getListener() {
            return mListener;
        }

        public boolean callOnStarted() {
            try {
                synchronized (this) {
                    mListener.onStarted();
                }
            } catch (RemoteException ex) {
                Log.e(TAG, "callOnStarted: RemoteException", ex);
                return false;
            }
            return true;
        }

        public void binderDied() {
            Log.d(TAG, "FM receiver listener died");

            synchronized (mOnStartedReceivers) {
                mOnStartedReceivers.remove(this.mKey);
            }
            if (mListener != null) {
                mListener.asBinder().unlinkToDeath(this, 0);
            }
        }
    }

    private final class OnErrorReceiver implements IBinder.DeathRecipient {
        final IOnErrorListener mListener;

        final Object mKey;

        OnErrorReceiver(IOnErrorListener listener) {
            mListener = listener;
            mKey = listener.asBinder();
        }

        public IOnErrorListener getListener() {
            return mListener;
        }

        public boolean callOnError() {
            try {
                synchronized (this) {
                    mListener.onError();
                }
            } catch (RemoteException ex) {
                Log.e(TAG, "callOnError: RemoteException", ex);
                return false;
            }
            return true;
        }

        public void binderDied() {
            Log.d(TAG, "FM receiver listener died");

            synchronized (mOnErrorReceivers) {
                mOnErrorReceivers.remove(this.mKey);
            }
            if (mListener != null) {
                mListener.asBinder().unlinkToDeath(this, 0);
            }
        }
    }

    private final class OnScanReceiver implements IBinder.DeathRecipient {
        final IOnScanListener mListener;

        final Object mKey;

        OnScanReceiver(IOnScanListener listener) {
            mListener = listener;
            mKey = listener.asBinder();
        }

        public IOnScanListener getListener() {
            return mListener;
        }

        public boolean callOnScan(int tunedFrequency, int signalLevel, int scanDirection, boolean aborted) {
            try {
                synchronized (this) {
                    mListener.onScan(tunedFrequency, signalLevel, scanDirection, aborted);
                }
            } catch (RemoteException ex) {
                Log.e(TAG, "callOnScan: RemoteException", ex);
                return false;
            }
            return true;
        }

        public boolean callOnFullScan(int[] frequency, int[] signalLevel, boolean aborted) {
            try {
                synchronized (this) {
                    mListener.onFullScan(frequency, signalLevel, aborted);
                }
            } catch (RemoteException ex) {
                Log.e(TAG, "callOnFullScan: RemoteException", ex);
                return false;
            }
            return true;
        }

        public void binderDied() {
            Log.d(TAG, "FM receiver listener died");

            synchronized (mOnScanReceivers) {
                mOnScanReceivers.remove(this.mKey);
            }
            if (mListener != null) {
                mListener.asBinder().unlinkToDeath(this, 0);
            }
        }
    }

    private final class OnForcedPauseReceiver implements IBinder.DeathRecipient {
        final IOnForcedPauseListener mListener;

        final Object mKey;

        OnForcedPauseReceiver(IOnForcedPauseListener listener) {
            mListener = listener;
            mKey = listener.asBinder();
        }

        public IOnForcedPauseListener getListener() {
            return mListener;
        }

        public boolean callOnForcedPause() {
            try {
                synchronized (this) {
                    mListener.onForcedPause();
                }
            } catch (RemoteException ex) {
                Log.e(TAG, "callOnForcedPause: RemoteException", ex);
                return false;
            }
            return true;
        }

        public void binderDied() {
            Log.d(TAG, "FM receiver listener died");

            synchronized (mOnForcedPauseReceivers) {
                mOnForcedPauseReceivers.remove(this.mKey);
            }
            if (mListener != null) {
                mListener.asBinder().unlinkToDeath(this, 0);
            }
        }
    }

    private final class OnForcedResetReceiver implements IBinder.DeathRecipient {
        final IOnForcedResetListener mListener;

        final Object mKey;

        OnForcedResetReceiver(IOnForcedResetListener listener) {
            mListener = listener;
            mKey = listener.asBinder();
        }

        public IOnForcedResetListener getListener() {
            return mListener;
        }

        public boolean callOnForcedReset(int reason) {
            try {
                synchronized (this) {
                    mListener.onForcedReset(reason);
                }
            } catch (RemoteException ex) {
                Log.e(TAG, "callOnForcedReset: RemoteException", ex);
                return false;
            }
            return true;
        }

        public void binderDied() {
            Log.d(TAG, "FM receiver listener died");

            synchronized (mOnForcedResetReceivers) {
                mOnForcedResetReceivers.remove(this.mKey);
            }
            if (mListener != null) {
                mListener.asBinder().unlinkToDeath(this, 0);
            }
        }
    }

    private final class OnRDSDataReceiver implements IBinder.DeathRecipient {
        final IOnRDSDataFoundListener mListener;

        final Object mKey;

        OnRDSDataReceiver(IOnRDSDataFoundListener listener) {
            mListener = listener;
            mKey = listener.asBinder();
        }

        public IOnRDSDataFoundListener getListener() {
            return mListener;
        }

        public boolean callOnRDSDataFound(Bundle bundle, int frequency) {
            try {
                synchronized (this) {
                    mListener.onRDSDataFound(bundle, frequency);
                }
            } catch (RemoteException ex) {
                Log.e(TAG, "callOnRDSDataFound: RemoteException", ex);
                return false;
            }
            return true;
        }

        public void binderDied() {
            Log.d(TAG, "FM receiver listener died");

            synchronized (mOnRDSDataReceivers) {
                mOnRDSDataReceivers.remove(this.mKey);
            }
            if (mListener != null) {
                mListener.asBinder().unlinkToDeath(this, 0);
            }
        }
    }

    private final class OnSignalStrengthReceiver implements IBinder.DeathRecipient {
        final IOnSignalStrengthListener mListener;

        final Object mKey;

        OnSignalStrengthReceiver(IOnSignalStrengthListener listener) {
            mListener = listener;
            mKey = listener.asBinder();
        }

        public IOnSignalStrengthListener getListener() {
            return mListener;
        }

        public boolean callOnSignalStrengthChanged(int signalStrength) {
            try {
                synchronized (this) {
                    mListener.onSignalStrengthChanged(signalStrength);
                }
            } catch (RemoteException ex) {
                Log.e(TAG, "callOnSignalStrengthChanged: RemoteException", ex);
                return false;
            }
            return true;
        }

        public void binderDied() {
            Log.d(TAG, "FM receiver listener died");

            synchronized (mOnSignalStrengthReceivers) {
                mOnSignalStrengthReceivers.remove(this.mKey);
            }
            if (mListener != null) {
                mListener.asBinder().unlinkToDeath(this, 0);
            }
        }
    }

    private final class OnStereoReceiver implements IBinder.DeathRecipient {
        final IOnStereoListener mListener;

        final Object mKey;

        OnStereoReceiver(IOnStereoListener listener) {
            mListener = listener;
            mKey = listener.asBinder();
        }

        public IOnStereoListener getListener() {
            return mListener;
        }

        public boolean callOnPlayingInStereo(boolean inStereo) {
            try {
                synchronized (this) {
                    mListener.onPlayingInStereo(inStereo);
                }
            } catch (RemoteException ex) {
                Log.e(TAG, "callOnPlayingInStereo: RemoteException", ex);
                return false;
            }
            return true;
        }

        public void binderDied() {
            Log.d(TAG, "FM receiver listener died");

            synchronized (mOnStereoReceivers) {
                mOnStereoReceivers.remove(this.mKey);
            }
            if (mListener != null) {
                mListener.asBinder().unlinkToDeath(this, 0);
            }
        }
    }

    private final class OnExtraCommandReceiver implements IBinder.DeathRecipient {
        final IOnExtraCommandListener mListener;

        final Object mKey;

        OnExtraCommandReceiver(IOnExtraCommandListener listener) {
            mListener = listener;
            mKey = listener.asBinder();
        }

        public IOnExtraCommandListener getListener() {
            return mListener;
        }

        public boolean callOnExtraCommand(String response, Bundle extras) {
            try {
                synchronized (this) {
                    mListener.onExtraCommand(response, extras);
                }
            } catch (RemoteException ex) {
                Log.e(TAG, "callOnExtraCommand: RemoteException", ex);
                return false;
            }
            return true;
        }

        public void binderDied() {
            Log.d(TAG, "FM receiver listener died");

            synchronized (mOnExtraCommandReceivers) {
                mOnExtraCommandReceivers.remove(this.mKey);
            }
            if (mListener != null) {
                mListener.asBinder().unlinkToDeath(this, 0);
            }
        }
    }

    private final class OnAutomaticSwitchReceiver implements IBinder.DeathRecipient {
        final IOnAutomaticSwitchListener mListener;

        final Object mKey;

        OnAutomaticSwitchReceiver(IOnAutomaticSwitchListener listener) {
            mListener = listener;
            mKey = listener.asBinder();
        }

        public IOnAutomaticSwitchListener getListener() {
            return mListener;
        }

        public boolean callOnAutomaticSwitch(int newFrequency, int reason) {
            try {
                synchronized (this) {
                    mListener.onAutomaticSwitch(newFrequency, reason);
                }
            } catch (RemoteException ex) {
                Log.e(TAG, "callOnAutomaticSwitch: RemoteException", ex);
                return false;
            }
            return true;
        }

        public void binderDied() {
            Log.d(TAG, "FM receiver listener died");

            synchronized (mOnAutomaticSwitchReceivers) {
                mOnAutomaticSwitchReceivers.remove(this.mKey);
            }
            if (mListener != null) {
                mListener.asBinder().unlinkToDeath(this, 0);
            }
        }
    }

    private final BroadcastReceiver mReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent == null) {
                return;
            }

            String action = intent.getAction();
            if (action.equals(Intent.ACTION_AIRPLANE_MODE_CHANGED)) {
                Log.d(TAG, "onReceive:ACTION_AIRPLANE_MODE_CHANGED");

                // check that airplane mode is off
                if (!isAirplaneModeOn()) {
                    return;
                }

                // power down hardware
                if (_fm_receiver_reset() > FmReceiver.STATE_IDLE) {
                    notifyOnForcedReset(FmReceiver.RESET_RADIO_FORBIDDEN);
                }
            }
        }

    };

    /* Returns true if airplane mode is currently on */
    private boolean isAirplaneModeOn() {
        return Settings.System.getInt(mContext.getContentResolver(),
                Settings.System.AIRPLANE_MODE_ON, 0) == 1;
    }

    public FmReceiverService(Context context) {
        Log.i(TAG, "FmReceiverService created");
        mContext = context;

        // Register for airplane mode
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        filter.addAction(Intent.ACTION_DOCK_EVENT);

        mContext.registerReceiver(mReceiver, filter);
    }

    public void start(FmBand band) {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        _fm_receiver_start(band.getMinFrequency(), band.getMaxFrequency(), band
                .getDefaultFrequency(), band.getChannelOffset());

        if (mOnRDSDataReceivers.size() > 0) {
            Log.d(TAG, "Started with RDS receiver(s), switching on RDS");
            _fm_receiver_setRDS(true);
        }
    }

    public void startAsync(FmBand band) {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        _fm_receiver_startAsync(band.getMinFrequency(), band.getMaxFrequency(), band
                .getDefaultFrequency(), band.getChannelOffset());
    }

    public void reset() {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        _fm_receiver_reset();
    }

    public void pause() {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        _fm_receiver_pause();
    }

    public void resume() {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        _fm_receiver_resume();
    }

    public int getState() {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        return _fm_receiver_getState();
    }

    public void setFrequency(int frequency) {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        _fm_receiver_setFrequency(frequency);
    }

    public int getFrequency() {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        return _fm_receiver_getFrequency();
    }

    public void scanUp() {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        _fm_receiver_scanUp();
    }

    public void scanDown() {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        _fm_receiver_scanDown();
    }

    public void startFullScan() {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        _fm_receiver_startFullScan();
    }

    public void stopScan() {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        _fm_receiver_stopScan();
    }

    public boolean isRDSDataSupported() {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        return _fm_receiver_isRDSDataSupported();
    }

    public boolean isTunedToValidChannel() {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        return _fm_receiver_isTunedToValidChannel();
    }

    public void setThreshold(int threshold) {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        _fm_receiver_setThreshold(threshold);
    }

    public int getThreshold() {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        return _fm_receiver_getThreshold();
    }

    public int getSignalStrength() {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        return _fm_receiver_getSignalStrength();
    }

    public boolean isPlayingInStereo() {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        return _fm_receiver_isPlayingInStereo();
    }

    public void setForceMono(boolean forceMono) {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        _fm_receiver_setForceMono(forceMono);
    }

    public void setAutomaticAFSwitching(boolean automatic) {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        _fm_receiver_setAutomaticAFSwitching(automatic);
    }

    public void setAutomaticTASwitching(boolean automatic) {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        _fm_receiver_setAutomaticTASwitching(automatic);
    }

    public boolean sendExtraCommand(String command, String[] extras) {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        return _fm_receiver_sendExtraCommand(command, extras);
    }

    public void addOnStateChangedListener(IOnStateChangedListener listener) {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        IBinder binder = listener.asBinder();
        OnStateChangedReceiver receiver = mOnStateChangedReceivers.get(binder);
        if (receiver == null) {
            receiver = new OnStateChangedReceiver(listener);
            mOnStateChangedReceivers.put(binder, receiver);
            Log.d(TAG, "addOnStateChangedListener(), new receiver added");
            try {
                receiver.getListener().asBinder().linkToDeath(receiver, 0);
            } catch (RemoteException ex) {
                Slog.e(TAG, "linkToDeath failed:", ex);
            }
        }
    }

    public void removeOnStateChangedListener(IOnStateChangedListener listener) {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        IBinder binder = listener.asBinder();
        OnStateChangedReceiver receiver = mOnStateChangedReceivers.get(binder);
        if (receiver != null) {
            mOnStateChangedReceivers.remove(receiver.mKey);
            Log.d(TAG, "removeOnStateChangedListener(), receiver removed");
            receiver.getListener().asBinder().unlinkToDeath(receiver, 0);
        }
    }

    public void addOnStartedListener(IOnStartedListener listener) {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        IBinder binder = listener.asBinder();
        OnStartedReceiver receiver = mOnStartedReceivers.get(binder);
        if (receiver == null) {
            receiver = new OnStartedReceiver(listener);
            mOnStartedReceivers.put(binder, receiver);
            Log.d(TAG, "addOnStartedListener(), new receiver added");
            try {
                receiver.getListener().asBinder().linkToDeath(receiver, 0);
            } catch (RemoteException ex) {
                Slog.e(TAG, "linkToDeath failed:", ex);
            }
        }
    }

    public void removeOnStartedListener(IOnStartedListener listener) {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        IBinder binder = listener.asBinder();
        OnStartedReceiver receiver = mOnStartedReceivers.get(binder);
        if (receiver != null) {
            mOnStartedReceivers.remove(receiver.mKey);
            Log.d(TAG, "removeOnStartedListener(), receiver removed");
            receiver.getListener().asBinder().unlinkToDeath(receiver, 0);
        }
    }

    public void addOnErrorListener(IOnErrorListener listener) {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        IBinder binder = listener.asBinder();
        OnErrorReceiver receiver = mOnErrorReceivers.get(binder);
        if (receiver == null) {
            receiver = new OnErrorReceiver(listener);
            mOnErrorReceivers.put(binder, receiver);
            Log.d(TAG, "addOnErrorListener(), new receiver added");
            try {
                receiver.getListener().asBinder().linkToDeath(receiver, 0);
            } catch (RemoteException ex) {
                Slog.e(TAG, "linkToDeath failed:", ex);
            }
        }
    }

    public void removeOnErrorListener(IOnErrorListener listener) {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        IBinder binder = listener.asBinder();
        OnErrorReceiver receiver = mOnErrorReceivers.get(binder);
        if (receiver != null) {
            mOnErrorReceivers.remove(receiver.mKey);
            Log.d(TAG, "removeOnErrorListener(), receiver removed");
            receiver.getListener().asBinder().unlinkToDeath(receiver, 0);
        }
    }

    public void addOnScanListener(IOnScanListener listener) {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        IBinder binder = listener.asBinder();
        OnScanReceiver receiver = mOnScanReceivers.get(binder);
        if (receiver == null) {
            receiver = new OnScanReceiver(listener);
            mOnScanReceivers.put(binder, receiver);
            Log.d(TAG, "addOnScanListener(), new receiver added");
            try {
                receiver.getListener().asBinder().linkToDeath(receiver, 0);
            } catch (RemoteException ex) {
                Slog.e(TAG, "linkToDeath failed:", ex);
            }
        }
    }

    public void removeOnScanListener(IOnScanListener listener) {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        IBinder binder = listener.asBinder();
        OnScanReceiver receiver = mOnScanReceivers.get(binder);
        if (receiver != null) {
            mOnScanReceivers.remove(receiver.mKey);
            Log.d(TAG, "removeOnScanListener(), receiver removed");
            receiver.getListener().asBinder().unlinkToDeath(receiver, 0);
        }
    }

    public void addOnForcedPauseListener(IOnForcedPauseListener listener) {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        IBinder binder = listener.asBinder();
        OnForcedPauseReceiver receiver = mOnForcedPauseReceivers.get(binder);
        if (receiver == null) {
            receiver = new OnForcedPauseReceiver(listener);
            mOnForcedPauseReceivers.put(binder, receiver);
            Log.d(TAG, "addOnForcedPauseListener(), new receiver added");
            try {
                receiver.getListener().asBinder().linkToDeath(receiver, 0);
            } catch (RemoteException ex) {
                Slog.e(TAG, "linkToDeath failed:", ex);
            }
        }
    }

    public void removeOnForcedPauseListener(IOnForcedPauseListener listener) {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        IBinder binder = listener.asBinder();
        OnForcedPauseReceiver receiver = mOnForcedPauseReceivers.get(binder);
        if (receiver != null) {
            mOnForcedPauseReceivers.remove(receiver.mKey);
            Log.d(TAG, "removeOnForcedPauseListener(), receiver removed");
            receiver.getListener().asBinder().unlinkToDeath(receiver, 0);
        }
    }

    public void addOnForcedResetListener(IOnForcedResetListener listener) {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        IBinder binder = listener.asBinder();
        OnForcedResetReceiver receiver = mOnForcedResetReceivers.get(binder);
        if (receiver == null) {
            receiver = new OnForcedResetReceiver(listener);
            mOnForcedResetReceivers.put(binder, receiver);
            Log.d(TAG, "addOnForcedResetListener(), new receiver added");
            try {
                receiver.getListener().asBinder().linkToDeath(receiver, 0);
            } catch (RemoteException ex) {
                Slog.e(TAG, "linkToDeath failed:", ex);
            }
        }
    }

    public void removeOnForcedResetListener(IOnForcedResetListener listener) {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        IBinder binder = listener.asBinder();
        OnForcedResetReceiver receiver = mOnForcedResetReceivers.get(binder);
        if (receiver != null) {
            mOnForcedResetReceivers.remove(receiver.mKey);
            Log.d(TAG, "removeOnForcedResetListener(), receiver removed");
            receiver.getListener().asBinder().unlinkToDeath(receiver, 0);
        }
    }

    public void addOnRDSDataFoundListener(IOnRDSDataFoundListener listener) {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        IBinder binder = listener.asBinder();
        OnRDSDataReceiver receiver = mOnRDSDataReceivers.get(binder);
        if (receiver == null) {
            receiver = new OnRDSDataReceiver(listener);
            mOnRDSDataReceivers.put(binder, receiver);
            Log.d(TAG, "addOnRDSDataFoundListener(), new receiver added");
            try {
                receiver.getListener().asBinder().linkToDeath(receiver, 0);
            } catch (RemoteException ex) {
                Slog.e(TAG, "linkToDeath failed:", ex);
            }
            if ((getState() >= FmReceiver.STATE_STARTED) &&
                (mOnRDSDataReceivers.size() == 1)) {
                Log.d(TAG, "First RDS receiver added, switching on RDS");
                _fm_receiver_setRDS(true);
            }
        }
    }

    public void removeOnRDSDataFoundListener(IOnRDSDataFoundListener listener) {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        IBinder binder = listener.asBinder();
        OnRDSDataReceiver receiver = mOnRDSDataReceivers.get(binder);
        if (receiver != null) {
            mOnRDSDataReceivers.remove(receiver.mKey);
            Log.d(TAG, "removeOnRDSDataFoundListener(), receiver removed");
            receiver.getListener().asBinder().unlinkToDeath(receiver, 0);
            if ((getState() >= FmReceiver.STATE_STARTED) &&
                mOnRDSDataReceivers.isEmpty()) {
                Log.d(TAG, "Last RDS receiver removed, switching off RDS");
                _fm_receiver_setRDS(false);
            }
        }
    }

    public void addOnSignalStrengthChangedListener(IOnSignalStrengthListener listener) {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        IBinder binder = listener.asBinder();
        OnSignalStrengthReceiver receiver = mOnSignalStrengthReceivers.get(binder);
        if (receiver == null) {
            receiver = new OnSignalStrengthReceiver(listener);
            mOnSignalStrengthReceivers.put(binder, receiver);
            Log.d(TAG, "addOnSignalStrengthChangedListener(), new receiver added");
            try {
                receiver.getListener().asBinder().linkToDeath(receiver, 0);
            } catch (RemoteException ex) {
                Slog.e(TAG, "linkToDeath failed:", ex);
            }
        }
    }

    public void removeOnSignalStrengthChangedListener(IOnSignalStrengthListener listener) {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        IBinder binder = listener.asBinder();
        OnSignalStrengthReceiver receiver = mOnSignalStrengthReceivers.get(binder);
        if (receiver != null) {
            mOnSignalStrengthReceivers.remove(receiver.mKey);
            Log.d(TAG, "removeOnSignalStrengthChangedListener(), receiver removed");
            receiver.getListener().asBinder().unlinkToDeath(receiver, 0);
        }
    }

    public void addOnPlayingInStereoListener(IOnStereoListener listener) {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        IBinder binder = listener.asBinder();
        OnStereoReceiver receiver = mOnStereoReceivers.get(binder);
        if (receiver == null) {
            receiver = new OnStereoReceiver(listener);
            mOnStereoReceivers.put(binder, receiver);
            Log.d(TAG, "addOnPlayingInStereoListener(), new receiver added");
            try {
                receiver.getListener().asBinder().linkToDeath(receiver, 0);
            } catch (RemoteException ex) {
                Slog.e(TAG, "linkToDeath failed:", ex);
            }
        }
    }

    public void removeOnPlayingInStereoListener(IOnStereoListener listener) {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        IBinder binder = listener.asBinder();
        OnStereoReceiver receiver = mOnStereoReceivers.get(binder);
        if (receiver != null) {
            mOnStereoReceivers.remove(receiver.mKey);
            Log.d(TAG, "removeOnPlayingInStereoListener(), receiver removed");
            receiver.getListener().asBinder().unlinkToDeath(receiver, 0);
        }
    }

    public void addOnExtraCommandListener(IOnExtraCommandListener listener) {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        IBinder binder = listener.asBinder();
        OnExtraCommandReceiver receiver = mOnExtraCommandReceivers.get(binder);
        if (receiver == null) {
            receiver = new OnExtraCommandReceiver(listener);
            mOnExtraCommandReceivers.put(binder, receiver);
            Log.d(TAG, "addOnExtraCommandListener(), new receiver added");
            try {
                receiver.getListener().asBinder().linkToDeath(receiver, 0);
            } catch (RemoteException ex) {
                Slog.e(TAG, "linkToDeath failed:", ex);
            }
        }
    }

    public void removeOnExtraCommandListener(IOnExtraCommandListener listener) {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        IBinder binder = listener.asBinder();
        OnExtraCommandReceiver receiver = mOnExtraCommandReceivers.get(binder);
        if (receiver != null) {
            mOnExtraCommandReceivers.remove(receiver.mKey);
            Log.d(TAG, "removeOnExtraCommandListener(), receiver removed");
            receiver.getListener().asBinder().unlinkToDeath(receiver, 0);
        }
    }

    public void addOnAutomaticSwitchListener(IOnAutomaticSwitchListener listener) {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        IBinder binder = listener.asBinder();
        OnAutomaticSwitchReceiver receiver = mOnAutomaticSwitchReceivers.get(binder);
        if (receiver == null) {
            receiver = new OnAutomaticSwitchReceiver(listener);
            mOnAutomaticSwitchReceivers.put(binder, receiver);
            Log.d(TAG, "addOnAutomaticSwitchListener(), new receiver added");
            try {
                receiver.getListener().asBinder().linkToDeath(receiver, 0);
            } catch (RemoteException ex) {
                Slog.e(TAG, "linkToDeath failed:", ex);
            }
        }
    }

    public void removeOnAutomaticSwitchListener(IOnAutomaticSwitchListener listener) {
        if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.FM_RADIO_RECEIVER)
                != PackageManager.PERMISSION_GRANTED) {
            throw new SecurityException("Requires FM_RADIO_RECEIVER permission");
        }

        IBinder binder = listener.asBinder();
        OnAutomaticSwitchReceiver receiver = mOnAutomaticSwitchReceivers.get(binder);
        if (receiver != null) {
            mOnAutomaticSwitchReceivers.remove(receiver.mKey);
            Log.d(TAG, "removeOnAutomaticSwitchListener(), receiver removed");
            receiver.getListener().asBinder().unlinkToDeath(receiver, 0);
        }
    }

    private void notifyOnStateChanged(int oldState, int newState) {
        synchronized (mOnStateChangedReceivers) {
            Collection c = mOnStateChangedReceivers.values();
            Iterator iterator = c.iterator();
            while (iterator.hasNext()) {
                OnStateChangedReceiver m = (OnStateChangedReceiver) iterator.next();
                m.callOnStateChanged(oldState, newState);
            }
        }
    }

    private void notifyOnStarted() {
        synchronized (mOnStartedReceivers) {
            Collection c = mOnStartedReceivers.values();
            Iterator iterator = c.iterator();
            while (iterator.hasNext()) {
                OnStartedReceiver m = (OnStartedReceiver) iterator.next();
                m.callOnStarted();
            }

            if (mOnRDSDataReceivers.size() > 0) {
                Log.d(TAG, "Started event with RDS receiver(s), switching on RDS");
                _fm_receiver_setRDS(true);
            }
        }
    }

    private void notifyOnScan(int frequency, int signalLevel, int scanDirection, boolean aborted) {
        synchronized (mOnScanReceivers) {
            Collection c = mOnScanReceivers.values();
            Iterator iterator = c.iterator();
            while (iterator.hasNext()) {
                OnScanReceiver m = (OnScanReceiver) iterator.next();
                m.callOnScan(frequency, signalLevel, scanDirection, aborted);
            }
        }
    }

    private void notifyOnFullScan(int[] frequency, int[] signalLevel, boolean aborted) {
        synchronized (mOnScanReceivers) {
            Collection c = mOnScanReceivers.values();
            Iterator iterator = c.iterator();
            while (iterator.hasNext()) {
                OnScanReceiver m = (OnScanReceiver) iterator.next();
                m.callOnFullScan(frequency, signalLevel, aborted);
            }
        }
    }

    private void notifyOnForcedPause() {
        synchronized (mOnForcedPauseReceivers) {
            Collection c = mOnForcedPauseReceivers.values();
            Iterator iterator = c.iterator();
            while (iterator.hasNext()) {
                OnForcedPauseReceiver m = (OnForcedPauseReceiver) iterator.next();
                m.callOnForcedPause();
            }
        }
    }

    private void notifyOnForcedReset(int reason) {
        synchronized (mOnForcedResetReceivers) {
            Collection c = mOnForcedResetReceivers.values();
            Iterator iterator = c.iterator();
            while (iterator.hasNext()) {
                OnForcedResetReceiver m = (OnForcedResetReceiver) iterator.next();
                m.callOnForcedReset(reason);
            }
        }
    }

    private void notifyOnError() {
        synchronized (mOnErrorReceivers) {
            Collection c = mOnErrorReceivers.values();
            Iterator iterator = c.iterator();
            while (iterator.hasNext()) {
                OnErrorReceiver m = (OnErrorReceiver) iterator.next();
                m.callOnError();
            }
        }
    }

    private void notifyOnRDSDataFound(Bundle bundle, int frequency) {
        synchronized (mOnRDSDataReceivers) {
            Collection c = mOnRDSDataReceivers.values();
            Iterator iterator = c.iterator();
            while (iterator.hasNext()) {
                OnRDSDataReceiver m = (OnRDSDataReceiver) iterator.next();
                m.callOnRDSDataFound(bundle, frequency);
            }
        }
    }

    private void notifyOnSignalStrengthChanged(int signalStrength) {
        synchronized (mOnSignalStrengthReceivers) {
            Collection c = mOnSignalStrengthReceivers.values();
            Iterator iterator = c.iterator();
            while (iterator.hasNext()) {
                OnSignalStrengthReceiver m = (OnSignalStrengthReceiver) iterator.next();
                m.callOnSignalStrengthChanged(signalStrength);
            }
        }
    }

    private void notifyOnPlayingInStereo(boolean inStereo) {
        synchronized (mOnStereoReceivers) {
            Collection c = mOnStereoReceivers.values();
            Iterator iterator = c.iterator();
            while (iterator.hasNext()) {
                OnStereoReceiver m = (OnStereoReceiver) iterator.next();
                m.callOnPlayingInStereo(inStereo);
            }
        }
    }

    private void notifyOnExtraCommand(String response, Bundle extras) {
        synchronized (mOnExtraCommandReceivers) {
            Collection c = mOnExtraCommandReceivers.values();
            Iterator iterator = c.iterator();
            while (iterator.hasNext()) {
                OnExtraCommandReceiver m = (OnExtraCommandReceiver) iterator.next();
                m.callOnExtraCommand(response, extras);
            }
        }
    }

    private void notifyOnAutomaticSwitching(int newFrequency, int reason) {
        synchronized (mOnAutomaticSwitchReceivers) {
            Collection c = mOnAutomaticSwitchReceivers.values();
            Iterator iterator = c.iterator();
            while (iterator.hasNext()) {
                OnAutomaticSwitchReceiver m = (OnAutomaticSwitchReceiver) iterator.next();
                m.callOnAutomaticSwitch(newFrequency, reason);
            }
        }
    }

    static
    {
        System.loadLibrary("analogradiobroadcasting");
    }

    private native void _fm_receiver_start(int minFreq, int maxFreq, int defaultFreq, int offset);

    private native void _fm_receiver_startAsync(int minFreq, int maxFreq, int defaultFreq,
            int offset);

    private native int _fm_receiver_reset();

    private native void _fm_receiver_pause();

    private native void _fm_receiver_resume();

    private native int _fm_receiver_getState();

    private native void _fm_receiver_setFrequency(int frequency);

    private native int _fm_receiver_getFrequency();

    private native void _fm_receiver_scanUp();

    private native void _fm_receiver_scanDown();

    private native void _fm_receiver_startFullScan();

    private native void _fm_receiver_stopScan();

    private native boolean _fm_receiver_isRDSDataSupported();

    private native boolean _fm_receiver_isTunedToValidChannel();

    private native void _fm_receiver_setThreshold(int threshold);

    private native int _fm_receiver_getThreshold();

    private native int _fm_receiver_getSignalStrength();

    private native boolean _fm_receiver_isPlayingInStereo();

    private native void _fm_receiver_setForceMono(boolean forceMono);

    private native void _fm_receiver_setAutomaticAFSwitching(boolean automatic);

    private native void _fm_receiver_setAutomaticTASwitching(boolean automatic);

    private native void _fm_receiver_setRDS(boolean receiveRDS);

    private native boolean _fm_receiver_sendExtraCommand(String command, String[] extras);
}