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
|
/*
* Copyright (C) 2014 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.
*/
package com.android.fmradio;
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.preference.PreferenceManager;
import android.provider.BaseColumns;
import android.text.TextUtils;
/**
* This class provider interface to operator databases, use by activity and
* service
*/
public class FmStation {
private static final String TAG = "FmStation";
// authority use composite content provider uri
public static final String AUTHORITY = "com.android.fmradio";
// use to composite content provider uri
public static final String STATION = "station";
// store current station in share preference with this key
public static final String CURRENT_STATION = "curent_station";
public static final String[] COLUMNS = new String[] {
Station._ID,
Station.FREQUENCY,
Station.IS_FAVORITE,
Station.STATION_NAME,
Station.PROGRAM_SERVICE,
Station.RADIO_TEXT,
};
/**
* This class provider the columns of StationList table
*/
public static final class Station implements BaseColumns {
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + STATION);
/**
* Station frequency(Hz)
* <P>Type: INTEGER </P>
*/
public static final String FREQUENCY = "frequency";
/**
* If this station is favorite, it is 1, otherwise 0
* <P>Type: INTEGER (boolean)</P>
*/
public static final String IS_FAVORITE = "is_favorite";
/**
* Station name, if user rename a station, this must be not null, otherwise is null.
* <P>Type: TEXT</P>
*/
public static final String STATION_NAME = "station_name";
/**
* Program service(PS), station name provide by RDS station
* <P>Type: TEXT</P>
*/
public static final String PROGRAM_SERVICE = "program_service";
/**
* Radio text(RT or rds), detail ration text provide by RDS station.
* <P>Type: TEXT</P>
*/
public static final String RADIO_TEXT = "radio_text";
}
/**
* Insert station information to database
*
* @param context The context
* @param frequency The station frequency
* @param stationName The station name
*/
public static void insertStationToDb(Context context, int frequency, String stationName) {
ContentValues values = new ContentValues(2);
values.put(Station.FREQUENCY, frequency);
values.put(Station.STATION_NAME, stationName);
context.getContentResolver().insert(Station.CONTENT_URI, values);
}
/**
* Insert station information to database with given frequency, station name, PS and RT
*
* @param context The context
* @param frequency The station frequency
* @param stationName The station name
* @param ps The program service
* @param rt The radio text
*/
public static void insertStationToDb(
Context context, int frequency, String stationName, String ps, String rt) {
ContentValues values = new ContentValues(4);
values.put(Station.FREQUENCY, frequency);
values.put(Station.STATION_NAME, stationName);
values.put(Station.PROGRAM_SERVICE, ps);
values.put(Station.RADIO_TEXT, rt);
context.getContentResolver().insert(Station.CONTENT_URI, values);
}
/**
* Insert station information to database with given values
*
* @param context The context
* @param values Need inserted values
*/
public static void insertStationToDb(Context context, ContentValues values) {
context.getContentResolver().insert(Station.CONTENT_URI, values);
}
/**
* Update station name according to given frequency
*
* @param context The context
* @param frequency the station frequency need to update
* @param stationName The new station's name
*/
public static void updateStationToDb(Context context, int frequency, String stationName) {
final int size = 1;
ContentValues values = new ContentValues(size);
values.put(Station.STATION_NAME, stationName);
context.getContentResolver().update(
Station.CONTENT_URI,
values,
Station.FREQUENCY + "=?",
new String[] { String.valueOf(frequency)});
}
/**
* Update station information according to given frequency
*
* @param context The context
* @param frequency the station frequency need to update
* @param values The new station's values
*/
public static void updateStationToDb(Context context, int frequency, ContentValues values) {
context.getContentResolver().update(
Station.CONTENT_URI,
values,
Station.FREQUENCY + "=?",
new String[] { String.valueOf(frequency)});
}
/**
* Delete station according to given frequency
*
* @param context The context
* @param frequency The station frequency
*/
public static void deleteStationInDb(Context context, int frequency) {
context.getContentResolver().delete(
Station.CONTENT_URI,
Station.FREQUENCY + "=?",
new String[] { String.valueOf(frequency)});
}
/**
* Check whether the given frequency station is exist in database
*
* @param context The context
* @param frequency The station frequency
*
* @return true or false indicate whether station is exist
*/
public static boolean isStationExist(Context context, int frequency) {
boolean isExist = false;
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(
Station.CONTENT_URI,
new String[] { Station.STATION_NAME },
Station.FREQUENCY + "=?",
new String[] { String.valueOf(frequency) },
null);
if (cursor != null && cursor.moveToFirst()) {
isExist = true;
}
} finally {
if (cursor != null) {
cursor.close();
}
}
return isExist;
}
/**
* Get current station from share preference
*
* @param context The context
*
* @return the current station in store share preference
*/
public static int getCurrentStation(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
int currentStation = prefs.getInt(CURRENT_STATION, FmUtils.DEFAULT_STATION);
return currentStation;
}
/**
* store current station to share preference
*
* @param context The context
* @param frequency The current station frequency
*/
public static void setCurrentStation(Context context, int frequency) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt(CURRENT_STATION, frequency);
editor.commit();
}
/**
* Get station name, if user rename station, we return user set station name, otherwise
* return program service.
*
* @param context The context
* @param frequency The station frequency
*
* @return The station name
*/
public static String getStationName(Context context, int frequency) {
String stationName = null;
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(
Station.CONTENT_URI,
new String[] { Station.STATION_NAME, Station.PROGRAM_SERVICE },
Station.FREQUENCY + "=?",
new String[] { String.valueOf(frequency) },
null);
if (cursor != null && cursor.moveToFirst()) {
// If the station name is not exist, show program service(PS) instead
stationName = cursor.getString(cursor.getColumnIndex(Station.STATION_NAME));
if (TextUtils.isEmpty(stationName)) {
stationName = cursor.getString(cursor.getColumnIndex(Station.PROGRAM_SERVICE));
}
}
} finally {
if (cursor != null) {
cursor.close();
}
}
return stationName;
}
/**
* Judge whether station is a favorite station
*
* @param context The context
* @param frequency The station frequency
*
* @return true or false indicate whether the station is favorite
*/
public static boolean isFavoriteStation(Context context, int frequency) {
boolean isFavorite = false;
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(
Station.CONTENT_URI,
new String[] { Station.IS_FAVORITE },
Station.FREQUENCY + "=?",
new String[] { String.valueOf(frequency) },
null);
if (cursor != null && cursor.moveToFirst()) {
isFavorite = cursor.getInt(0) > 0;
}
} finally {
if (cursor != null) {
cursor.close();
}
}
return isFavorite;
}
/**
* update db to mark it is a favorite frequency
*
* @param context The context
* @param frequency The target frequency
*/
public static void addToFavorite(Context context, int frequency) {
ContentValues values = new ContentValues(1);
values.put(Station.IS_FAVORITE, true);
context.getContentResolver().update(
Station.CONTENT_URI,
values,
Station.FREQUENCY + "=?",
new String[] { String.valueOf(frequency) });
}
/**
* update db to mark it is a normal frequency
*
* @param context The context
* @param frequency The target frequency
*/
public static void removeFromFavorite(Context context, int frequency) {
ContentValues values = new ContentValues(1);
values.put(Station.IS_FAVORITE, false);
values.put(Station.STATION_NAME, "");
context.getContentResolver().update(
Station.CONTENT_URI,
values,
Station.FREQUENCY + "=?",
new String[] { String.valueOf(frequency) });
}
/**
* Get station count
*
* @param context The context
*
* @return The numbers of station
*/
public static int getStationCount(Context context) {
int stationNus = 0;
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(
Station.CONTENT_URI,
new String[] { Station._ID },
null,
null,
null);
if (cursor != null) {
stationNus = cursor.getCount();
}
} finally {
if (cursor != null) {
cursor.close();
}
}
return stationNus;
}
/**
* Clean all stations which station type is searched
*
* @param context The context
*/
public static void cleanSearchedStations(Context context) {
context.getContentResolver().delete(Station.CONTENT_URI,
Station.IS_FAVORITE + "=0", null);
}
/**
* Clear all station of FMRadio database
*
* @param context The context
*/
public static void cleanAllStations(Context context) {
context.getContentResolver().delete(Station.CONTENT_URI, null, null);
}
}
|