• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (C) 2018 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.car.radio.storage;
18 
19 import android.content.Context;
20 import android.hardware.radio.ProgramSelector;
21 
22 import androidx.annotation.NonNull;
23 import androidx.annotation.WorkerThread;
24 import androidx.lifecycle.LiveData;
25 import androidx.lifecycle.Transformations;
26 import androidx.room.Dao;
27 import androidx.room.Database;
28 import androidx.room.Insert;
29 import androidx.room.OnConflictStrategy;
30 import androidx.room.Query;
31 import androidx.room.Room;
32 import androidx.room.RoomDatabase;
33 import androidx.room.TypeConverters;
34 
35 import com.android.car.broadcastradio.support.Program;
36 
37 import java.util.List;
38 import java.util.stream.Collectors;
39 
40 /**
41  * Radio app database schema.
42  *
43  * This class should not be accessed directly.
44  * Instead, {@link RadioStorage} interfaces directly with it.
45  */
46 @Database(entities = {Favorite.class}, exportSchema = false, version = 1)
47 @TypeConverters({ProgramSelectorConverter.class})
48 abstract class RadioDatabase extends RoomDatabase {
49     @Dao
50     protected interface FavoriteDao {
51         @Query("SELECT * FROM Favorite ORDER BY primaryId_type, primaryId_value")
loadAll()52         LiveData<List<Favorite>> loadAll();
53 
54         @Insert(onConflict = OnConflictStrategy.REPLACE)
insertAll(Favorite... favorites)55         void insertAll(Favorite... favorites);
56 
57         @Query("DELETE FROM Favorite WHERE "
58                 + "primaryId_type = :primaryIdType AND primaryId_value = :primaryIdValue")
delete(int primaryIdType, long primaryIdValue)59         void delete(int primaryIdType, long primaryIdValue);
60 
delete(@onNull ProgramSelector.Identifier id)61         default void delete(@NonNull ProgramSelector.Identifier id) {
62             delete(id.getType(), id.getValue());
63         }
64     }
65 
favoriteDao()66     protected abstract FavoriteDao favoriteDao();
67 
buildInstance(Context context)68     public static RadioDatabase buildInstance(Context context) {
69         return Room.databaseBuilder(context.getApplicationContext(), RadioDatabase.class,
70                 RadioDatabase.class.getSimpleName())
71                 .enableMultiInstanceInvalidation()
72                 .build();
73     }
74 
75     /**
76      * Returns a list of all user stored radio favorites sorted by primary identifier.
77      */
78     @WorkerThread
79     @NonNull
getAllFavorites()80     public LiveData<List<Program>> getAllFavorites() {
81         return Transformations.map(favoriteDao().loadAll(), favorites ->
82                 favorites.stream().map(Favorite::toProgram).collect(Collectors.toList()));
83     }
84 
85     /**
86      * Saves a given {@link Program} as a favorite.
87      *
88      * The favorite will replace any existing entry for a given primary
89      * identifier if there is a conflict.
90      */
91     @WorkerThread
insertFavorite(@onNull Program favorite)92     public void insertFavorite(@NonNull Program favorite) {
93         favoriteDao().insertAll(new Favorite(favorite));
94     }
95 
96     /**
97      * Removes a favorite by primary id of its {@link ProgramSelector}.
98      */
99     @WorkerThread
removeFavorite(@onNull ProgramSelector sel)100     public void removeFavorite(@NonNull ProgramSelector sel) {
101         favoriteDao().delete(sel.getPrimaryId());
102     }
103 }
104