1 /*
2  * Copyright (C) 2015 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.tv.tuner;
18 
19 import android.content.ContentProvider;
20 import android.content.ContentValues;
21 import android.content.Context;
22 import android.content.UriMatcher;
23 import android.database.Cursor;
24 import android.database.sqlite.SQLiteDatabase;
25 import android.database.sqlite.SQLiteException;
26 import android.database.sqlite.SQLiteOpenHelper;
27 import android.net.Uri;
28 
29 /**
30  * A content provider for storing the preferences. It's used across TV app and USB tuner TV input.
31  */
32 public class TunerPreferenceProvider extends ContentProvider {
33     /** The authority of the provider */
34     public static final String AUTHORITY = "com.android.tv.tuner.preferences";
35 
36     private static final String PATH_PREFERENCES = "preferences";
37 
38     private static final int DATABASE_VERSION = 1;
39     private static final String DATABASE_NAME = "usbtuner_preferences.db";
40     private static final String PREFERENCES_TABLE = "preferences";
41 
42     private static final int MATCH_PREFERENCE = 1;
43     private static final int MATCH_PREFERENCE_KEY = 2;
44 
45     private static final UriMatcher sUriMatcher;
46 
47     private DatabaseOpenHelper mDatabaseOpenHelper;
48 
49     static {
50         sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
sUriMatcher.addURI(AUTHORITY, "preferences", MATCH_PREFERENCE)51         sUriMatcher.addURI(AUTHORITY, "preferences", MATCH_PREFERENCE);
sUriMatcher.addURI(AUTHORITY, "preferences/*", MATCH_PREFERENCE_KEY)52         sUriMatcher.addURI(AUTHORITY, "preferences/*", MATCH_PREFERENCE_KEY);
53     }
54 
55     /**
56      * Builds a Uri that points to a specific preference.
57 
58      * @param key a key of the preference to point to
59      */
buildPreferenceUri(String key)60     public static Uri buildPreferenceUri(String key) {
61         return Preferences.CONTENT_URI.buildUpon().appendPath(key).build();
62     }
63 
64     /**
65      * Columns definitions for the preferences table.
66      */
67     public interface Preferences {
68 
69         /**
70          * The content:// style for the preferences table.
71          */
72         Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + PATH_PREFERENCES);
73 
74         /**
75          * The MIME type of a directory of preferences.
76          */
77         String CONTENT_TYPE = "vnd.android.cursor.dir/preferences";
78 
79         /**
80          * The MIME type of a single preference.
81          */
82         String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/preferences";
83 
84         /**
85          * The ID of this preference.
86          *
87          * <p>This is auto-incremented.
88          *
89          * <p>Type: INTEGER
90          */
91         String _ID = "_id";
92 
93         /**
94          * The key of this preference.
95          *
96          * <p>Should be unique.
97          *
98          * <p>Type: TEXT
99          */
100         String COLUMN_KEY = "key";
101 
102         /**
103          * The value of this preference.
104          *
105          * <p>Type: TEXT
106          */
107         String COLUMN_VALUE = "value";
108     }
109 
110     private static class DatabaseOpenHelper extends SQLiteOpenHelper {
DatabaseOpenHelper(Context context)111         public DatabaseOpenHelper(Context context) {
112             super(context, DATABASE_NAME, null, DATABASE_VERSION);
113         }
114 
115         @Override
onCreate(SQLiteDatabase db)116         public void onCreate(SQLiteDatabase db) {
117             db.execSQL("CREATE TABLE " + PREFERENCES_TABLE + " ("
118                     + Preferences._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
119                     + Preferences.COLUMN_KEY + " TEXT NOT NULL,"
120                     + Preferences.COLUMN_VALUE + " TEXT);");
121         }
122 
123         @Override
onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)124         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
125             // No-op
126         }
127     }
128 
129     @Override
onCreate()130     public boolean onCreate() {
131         mDatabaseOpenHelper = new DatabaseOpenHelper(getContext());
132         return true;
133     }
134 
135     @Override
query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)136     public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
137             String sortOrder) {
138         int match = sUriMatcher.match(uri);
139         if (match != MATCH_PREFERENCE && match != MATCH_PREFERENCE_KEY) {
140             throw new UnsupportedOperationException();
141         }
142         SQLiteDatabase db = mDatabaseOpenHelper.getReadableDatabase();
143         Cursor cursor = db.query(PREFERENCES_TABLE, projection, selection, selectionArgs,
144                 null, null, sortOrder);
145         cursor.setNotificationUri(getContext().getContentResolver(), uri);
146         return cursor;
147     }
148 
149     @Override
getType(Uri uri)150     public String getType(Uri uri) {
151         switch (sUriMatcher.match(uri)) {
152             case MATCH_PREFERENCE:
153                 return Preferences.CONTENT_TYPE;
154             case MATCH_PREFERENCE_KEY:
155                 return Preferences.CONTENT_ITEM_TYPE;
156         }
157         throw new IllegalArgumentException("Unknown URI " + uri);
158     }
159 
160     /**
161      * Inserts a preference row into the preference table.
162      *
163      * If a key is already exists in the table, it removes the old row and inserts a new row.
164      *
165      * @param uri the URL of the table to insert into
166      * @param values the initial values for the newly inserted row
167      * @return the URL of the newly created row
168      */
169     @Override
insert(Uri uri, ContentValues values)170     public Uri insert(Uri uri, ContentValues values) {
171         if (sUriMatcher.match(uri) != MATCH_PREFERENCE) {
172             throw new UnsupportedOperationException();
173         }
174         return insertRow(uri, values);
175     }
176 
insertRow(Uri uri, ContentValues values)177     private Uri insertRow(Uri uri, ContentValues values) {
178         SQLiteDatabase db = mDatabaseOpenHelper.getWritableDatabase();
179 
180         // Remove the old row.
181         db.delete(PREFERENCES_TABLE, Preferences.COLUMN_KEY + " like ?",
182                 new String[]{values.getAsString(Preferences.COLUMN_KEY)});
183 
184         long rowId = db.insert(PREFERENCES_TABLE, null, values);
185         if (rowId > 0) {
186             Uri rowUri = buildPreferenceUri(values.getAsString(Preferences.COLUMN_KEY));
187             getContext().getContentResolver().notifyChange(rowUri, null);
188             return rowUri;
189         }
190 
191         throw new SQLiteException("Failed to insert row into " + uri);
192     }
193 
194     @Override
delete(Uri uri, String selection, String[] selectionArgs)195     public int delete(Uri uri, String selection, String[] selectionArgs) {
196         throw new UnsupportedOperationException();
197     }
198 
199     @Override
update(Uri uri, ContentValues values, String selection, String[] selectionArgs)200     public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
201         throw new UnsupportedOperationException();
202     }
203 }
204