1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.fmradio; 18 19 import android.content.ContentValues; 20 import android.content.Context; 21 import android.content.SharedPreferences; 22 import android.database.Cursor; 23 import android.net.Uri; 24 import android.preference.PreferenceManager; 25 import android.provider.BaseColumns; 26 import android.text.TextUtils; 27 28 /** 29 * This class provider interface to operator databases, use by activity and 30 * service 31 */ 32 public class FmStation { 33 private static final String TAG = "FmStation"; 34 // authority use composite content provider uri 35 public static final String AUTHORITY = "com.android.fmradio"; 36 // use to composite content provider uri 37 public static final String STATION = "station"; 38 // store current station in share preference with this key 39 public static final String CURRENT_STATION = "curent_station"; 40 41 public static final String[] COLUMNS = new String[] { 42 Station._ID, 43 Station.FREQUENCY, 44 Station.IS_FAVORITE, 45 Station.STATION_NAME, 46 Station.PROGRAM_SERVICE, 47 Station.RADIO_TEXT, 48 }; 49 50 /** 51 * This class provider the columns of StationList table 52 */ 53 public static final class Station implements BaseColumns { 54 public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + STATION); 55 56 /** 57 * Station frequency(Hz) 58 * <P>Type: INTEGER </P> 59 */ 60 public static final String FREQUENCY = "frequency"; 61 62 /** 63 * If this station is favorite, it is 1, otherwise 0 64 * <P>Type: INTEGER (boolean)</P> 65 */ 66 public static final String IS_FAVORITE = "is_favorite"; 67 68 /** 69 * Station name, if user rename a station, this must be not null, otherwise is null. 70 * <P>Type: TEXT</P> 71 */ 72 public static final String STATION_NAME = "station_name"; 73 74 /** 75 * Program service(PS), station name provide by RDS station 76 * <P>Type: TEXT</P> 77 */ 78 public static final String PROGRAM_SERVICE = "program_service"; 79 80 /** 81 * Radio text(RT or rds), detail ration text provide by RDS station. 82 * <P>Type: TEXT</P> 83 */ 84 public static final String RADIO_TEXT = "radio_text"; 85 } 86 87 /** 88 * Insert station information to database 89 * 90 * @param context The context 91 * @param frequency The station frequency 92 * @param stationName The station name 93 */ insertStationToDb(Context context, int frequency, String stationName)94 public static void insertStationToDb(Context context, int frequency, String stationName) { 95 ContentValues values = new ContentValues(2); 96 values.put(Station.FREQUENCY, frequency); 97 values.put(Station.STATION_NAME, stationName); 98 context.getContentResolver().insert(Station.CONTENT_URI, values); 99 } 100 101 /** 102 * Insert station information to database with given frequency, station name, PS and RT 103 * 104 * @param context The context 105 * @param frequency The station frequency 106 * @param stationName The station name 107 * @param ps The program service 108 * @param rt The radio text 109 */ insertStationToDb( Context context, int frequency, String stationName, String ps, String rt)110 public static void insertStationToDb( 111 Context context, int frequency, String stationName, String ps, String rt) { 112 ContentValues values = new ContentValues(4); 113 values.put(Station.FREQUENCY, frequency); 114 values.put(Station.STATION_NAME, stationName); 115 values.put(Station.PROGRAM_SERVICE, ps); 116 values.put(Station.RADIO_TEXT, rt); 117 context.getContentResolver().insert(Station.CONTENT_URI, values); 118 } 119 120 /** 121 * Insert station information to database with given values 122 * 123 * @param context The context 124 * @param values Need inserted values 125 */ insertStationToDb(Context context, ContentValues values)126 public static void insertStationToDb(Context context, ContentValues values) { 127 context.getContentResolver().insert(Station.CONTENT_URI, values); 128 } 129 130 /** 131 * Update station name according to given frequency 132 * 133 * @param context The context 134 * @param frequency the station frequency need to update 135 * @param stationName The new station's name 136 */ updateStationToDb(Context context, int frequency, String stationName)137 public static void updateStationToDb(Context context, int frequency, String stationName) { 138 final int size = 1; 139 ContentValues values = new ContentValues(size); 140 values.put(Station.STATION_NAME, stationName); 141 context.getContentResolver().update( 142 Station.CONTENT_URI, 143 values, 144 Station.FREQUENCY + "=?", 145 new String[] { String.valueOf(frequency)}); 146 } 147 148 /** 149 * Update station information according to given frequency 150 * 151 * @param context The context 152 * @param frequency the station frequency need to update 153 * @param values The new station's values 154 */ updateStationToDb(Context context, int frequency, ContentValues values)155 public static void updateStationToDb(Context context, int frequency, ContentValues values) { 156 context.getContentResolver().update( 157 Station.CONTENT_URI, 158 values, 159 Station.FREQUENCY + "=?", 160 new String[] { String.valueOf(frequency)}); 161 } 162 163 /** 164 * Delete station according to given frequency 165 * 166 * @param context The context 167 * @param frequency The station frequency 168 */ deleteStationInDb(Context context, int frequency)169 public static void deleteStationInDb(Context context, int frequency) { 170 context.getContentResolver().delete( 171 Station.CONTENT_URI, 172 Station.FREQUENCY + "=?", 173 new String[] { String.valueOf(frequency)}); 174 } 175 176 /** 177 * Check whether the given frequency station is exist in database 178 * 179 * @param context The context 180 * @param frequency The station frequency 181 * 182 * @return true or false indicate whether station is exist 183 */ isStationExist(Context context, int frequency)184 public static boolean isStationExist(Context context, int frequency) { 185 boolean isExist = false; 186 Cursor cursor = null; 187 try { 188 cursor = context.getContentResolver().query( 189 Station.CONTENT_URI, 190 new String[] { Station.STATION_NAME }, 191 Station.FREQUENCY + "=?", 192 new String[] { String.valueOf(frequency) }, 193 null); 194 if (cursor != null && cursor.moveToFirst()) { 195 isExist = true; 196 } 197 } finally { 198 if (cursor != null) { 199 cursor.close(); 200 } 201 } 202 return isExist; 203 } 204 205 /** 206 * Get current station from share preference 207 * 208 * @param context The context 209 * 210 * @return the current station in store share preference 211 */ getCurrentStation(Context context)212 public static int getCurrentStation(Context context) { 213 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 214 int currentStation = prefs.getInt(CURRENT_STATION, FmUtils.DEFAULT_STATION); 215 return currentStation; 216 } 217 218 /** 219 * store current station to share preference 220 * 221 * @param context The context 222 * @param frequency The current station frequency 223 */ setCurrentStation(Context context, int frequency)224 public static void setCurrentStation(Context context, int frequency) { 225 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 226 SharedPreferences.Editor editor = prefs.edit(); 227 editor.putInt(CURRENT_STATION, frequency); 228 editor.commit(); 229 } 230 231 /** 232 * Get station name, if user rename station, we return user set station name, otherwise 233 * return program service. 234 * 235 * @param context The context 236 * @param frequency The station frequency 237 * 238 * @return The station name 239 */ getStationName(Context context, int frequency)240 public static String getStationName(Context context, int frequency) { 241 String stationName = null; 242 Cursor cursor = null; 243 try { 244 cursor = context.getContentResolver().query( 245 Station.CONTENT_URI, 246 new String[] { Station.STATION_NAME, Station.PROGRAM_SERVICE }, 247 Station.FREQUENCY + "=?", 248 new String[] { String.valueOf(frequency) }, 249 null); 250 if (cursor != null && cursor.moveToFirst()) { 251 // If the station name is not exist, show program service(PS) instead 252 stationName = cursor.getString(cursor.getColumnIndex(Station.STATION_NAME)); 253 if (TextUtils.isEmpty(stationName)) { 254 stationName = cursor.getString(cursor.getColumnIndex(Station.PROGRAM_SERVICE)); 255 } 256 } 257 } finally { 258 if (cursor != null) { 259 cursor.close(); 260 } 261 } 262 return stationName; 263 } 264 265 /** 266 * Judge whether station is a favorite station 267 * 268 * @param context The context 269 * @param frequency The station frequency 270 * 271 * @return true or false indicate whether the station is favorite 272 */ isFavoriteStation(Context context, int frequency)273 public static boolean isFavoriteStation(Context context, int frequency) { 274 boolean isFavorite = false; 275 Cursor cursor = null; 276 try { 277 cursor = context.getContentResolver().query( 278 Station.CONTENT_URI, 279 new String[] { Station.IS_FAVORITE }, 280 Station.FREQUENCY + "=?", 281 new String[] { String.valueOf(frequency) }, 282 null); 283 if (cursor != null && cursor.moveToFirst()) { 284 isFavorite = cursor.getInt(0) > 0; 285 } 286 } finally { 287 if (cursor != null) { 288 cursor.close(); 289 } 290 } 291 return isFavorite; 292 } 293 294 /** 295 * update db to mark it is a favorite frequency 296 * 297 * @param context The context 298 * @param frequency The target frequency 299 */ addToFavorite(Context context, int frequency)300 public static void addToFavorite(Context context, int frequency) { 301 ContentValues values = new ContentValues(1); 302 values.put(Station.IS_FAVORITE, true); 303 context.getContentResolver().update( 304 Station.CONTENT_URI, 305 values, 306 Station.FREQUENCY + "=?", 307 new String[] { String.valueOf(frequency) }); 308 } 309 310 /** 311 * update db to mark it is a normal frequency 312 * 313 * @param context The context 314 * @param frequency The target frequency 315 */ removeFromFavorite(Context context, int frequency)316 public static void removeFromFavorite(Context context, int frequency) { 317 ContentValues values = new ContentValues(1); 318 values.put(Station.IS_FAVORITE, false); 319 values.put(Station.STATION_NAME, ""); 320 context.getContentResolver().update( 321 Station.CONTENT_URI, 322 values, 323 Station.FREQUENCY + "=?", 324 new String[] { String.valueOf(frequency) }); 325 } 326 327 /** 328 * Get station count 329 * 330 * @param context The context 331 * 332 * @return The numbers of station 333 */ getStationCount(Context context)334 public static int getStationCount(Context context) { 335 int stationNus = 0; 336 Cursor cursor = null; 337 try { 338 cursor = context.getContentResolver().query( 339 Station.CONTENT_URI, 340 new String[] { Station._ID }, 341 null, 342 null, 343 null); 344 if (cursor != null) { 345 stationNus = cursor.getCount(); 346 } 347 } finally { 348 if (cursor != null) { 349 cursor.close(); 350 } 351 } 352 return stationNus; 353 } 354 355 /** 356 * Clean all stations which station type is searched 357 * 358 * @param context The context 359 */ cleanSearchedStations(Context context)360 public static void cleanSearchedStations(Context context) { 361 context.getContentResolver().delete(Station.CONTENT_URI, 362 Station.IS_FAVORITE + "=0", null); 363 } 364 365 /** 366 * Clear all station of FMRadio database 367 * 368 * @param context The context 369 */ cleanAllStations(Context context)370 public static void cleanAllStations(Context context) { 371 context.getContentResolver().delete(Station.CONTENT_URI, null, null); 372 } 373 } 374