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.usbtuner;
18 
19 import android.content.ContentResolver;
20 import android.content.ContentValues;
21 import android.content.Context;
22 import android.content.SharedPreferences;
23 import android.database.Cursor;
24 import android.os.AsyncTask;
25 import android.os.Bundle;
26 
27 import com.android.tv.common.SoftPreconditions;
28 import com.android.usbtuner.UsbTunerPreferenceProvider.Preferences;
29 import com.android.usbtuner.util.TisConfiguration;
30 
31 /**
32  * A helper class for the USB tuner preferences.
33  */
34 // TODO: Change this class to run on the worker thread.
35 public class UsbTunerPreferences {
36     private static final String TAG = "UsbTunerPreferences";
37 
38     private static final String PREFS_KEY_CHANNEL_DATA_VERSION = "channel_data_version";
39     private static final String PREFS_KEY_SCANNED_CHANNEL_COUNT = "scanned_channel_count";
40     private static final String PREFS_KEY_SCAN_DONE = "scan_done";
41     private static final String PREFS_KEY_LAUNCH_SETUP = "launch_setup";
42 
43     private static final String SHARED_PREFS_NAME = "com.android.usbtuner.preferences";
44 
45     private static final Bundle PREFERENCE_VALUES = new Bundle();
46 
useContentProvider(Context context)47     private static boolean useContentProvider(Context context) {
48         // If TIS is a part of LC, it should use ContentProvider to resolve multiple process access.
49         return TisConfiguration.isPackagedWithLiveChannels(context);
50     }
51 
getChannelDataVersion(Context context)52     public static int getChannelDataVersion(Context context) {
53         if (useContentProvider(context)) {
54             return getPreferenceInt(context, PREFS_KEY_CHANNEL_DATA_VERSION);
55         } else {
56             return getSharedPreferences(context)
57                     .getInt(UsbTunerPreferences.PREFS_KEY_CHANNEL_DATA_VERSION, 0);
58         }
59     }
60 
setChannelDataVersion(Context context, int version)61     public static void setChannelDataVersion(Context context, int version) {
62         if (useContentProvider(context)) {
63             setPreference(context, PREFS_KEY_CHANNEL_DATA_VERSION, version);
64         } else {
65             getSharedPreferences(context).edit()
66                     .putInt(UsbTunerPreferences.PREFS_KEY_CHANNEL_DATA_VERSION, version)
67                     .apply();
68         }
69     }
70 
getScannedChannelCount(Context context)71     public static int getScannedChannelCount(Context context) {
72         if (useContentProvider(context)) {
73             return getPreferenceInt(context, PREFS_KEY_SCANNED_CHANNEL_COUNT);
74         } else {
75             return getSharedPreferences(context)
76                     .getInt(UsbTunerPreferences.PREFS_KEY_SCANNED_CHANNEL_COUNT, 0);
77         }
78     }
79 
setScannedChannelCount(Context context, int channelCount)80     public static void setScannedChannelCount(Context context, int channelCount) {
81         if (useContentProvider(context)) {
82             setPreference(context, PREFS_KEY_SCANNED_CHANNEL_COUNT, channelCount);
83         } else {
84             getSharedPreferences(context).edit()
85                     .putInt(UsbTunerPreferences.PREFS_KEY_SCANNED_CHANNEL_COUNT, channelCount)
86                     .apply();
87         }
88     }
89 
isScanDone(Context context)90     public static boolean isScanDone(Context context) {
91         if (useContentProvider(context)) {
92             return getPreferenceBoolean(context, PREFS_KEY_SCAN_DONE);
93         } else {
94             return getSharedPreferences(context)
95                     .getBoolean(UsbTunerPreferences.PREFS_KEY_SCAN_DONE, false);
96         }
97     }
98 
setScanDone(Context context)99     public static void setScanDone(Context context) {
100         if (useContentProvider(context)) {
101             setPreference(context, PREFS_KEY_SCAN_DONE, true);
102         } else {
103             getSharedPreferences(context).edit()
104                     .putBoolean(UsbTunerPreferences.PREFS_KEY_SCAN_DONE, true)
105                     .apply();
106         }
107     }
108 
shouldShowSetupActivity(Context context)109     public static boolean shouldShowSetupActivity(Context context) {
110         if (useContentProvider(context)) {
111             return getPreferenceBoolean(context, PREFS_KEY_LAUNCH_SETUP);
112         } else {
113             return getSharedPreferences(context)
114                     .getBoolean(UsbTunerPreferences.PREFS_KEY_LAUNCH_SETUP, false);
115         }
116     }
117 
setShouldShowSetupActivity(Context context, boolean need)118     public static void setShouldShowSetupActivity(Context context, boolean need) {
119         if (useContentProvider(context)) {
120             setPreference(context, PREFS_KEY_LAUNCH_SETUP, need);
121         } else {
122             getSharedPreferences(context).edit()
123                     .putBoolean(UsbTunerPreferences.PREFS_KEY_LAUNCH_SETUP, need)
124                     .apply();
125         }
126     }
127 
getSharedPreferences(Context context)128     private static SharedPreferences getSharedPreferences(Context context) {
129         return context.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE);
130     }
131 
132     // Content provider helpers
getPreference(Context context, String key)133     private static String getPreference(Context context, String key) {
134         ContentResolver resolver = context.getContentResolver();
135         String[] projection = new String[] { Preferences.COLUMN_VALUE };
136         String selection = Preferences.COLUMN_KEY + " like ?";
137         String[] selectionArgs = new String[] { key };
138         try (Cursor cursor = resolver.query(UsbTunerPreferenceProvider.buildPreferenceUri(key),
139                 projection, selection, selectionArgs, null)) {
140             if (cursor != null && cursor.moveToFirst()) {
141                 return cursor.getString(0);
142             }
143         } catch (Exception e) {
144             SoftPreconditions.warn(TAG, "getPreference", "Error querying preference values", e);
145         }
146         return null;
147     }
148 
getPreferenceInt(Context context, String key)149     private static int getPreferenceInt(Context context, String key) {
150         if (PREFERENCE_VALUES.containsKey(key)) {
151             return PREFERENCE_VALUES.getInt(key);
152         }
153         try {
154             return Integer.parseInt(getPreference(context, key));
155         } catch (NumberFormatException e) {
156             return 0;
157         }
158     }
159 
getPreferenceBoolean(Context context, String key)160     private static boolean getPreferenceBoolean(Context context, String key) {
161         if (PREFERENCE_VALUES.containsKey(key)) {
162             return PREFERENCE_VALUES.getBoolean(key);
163         }
164         return Boolean.valueOf(getPreference(context, key));
165     }
166 
setPreference(final Context context, final String key, final String value)167     private static void setPreference(final Context context, final String key, final String value) {
168         new AsyncTask<Void, Void, Void>() {
169             @Override
170             protected Void doInBackground(Void... params) {
171                 ContentResolver resolver = context.getContentResolver();
172                 ContentValues values = new ContentValues();
173                 values.put(Preferences.COLUMN_KEY, key);
174                 values.put(Preferences.COLUMN_VALUE, value);
175                 try {
176                     resolver.insert(Preferences.CONTENT_URI, values);
177                 } catch (Exception e) {
178                     SoftPreconditions.warn(TAG, "setPreference", "Error writing preference values",
179                             e);
180                 }
181                 return null;
182             }
183         }.execute();
184     }
185 
setPreference(Context context, String key, int value)186     private static void setPreference(Context context, String key, int value) {
187         PREFERENCE_VALUES.putInt(key, value);
188         setPreference(context, key, Integer.toString(value));
189     }
190 
setPreference(Context context, String key, boolean value)191     private static void setPreference(Context context, String key, boolean value) {
192         PREFERENCE_VALUES.putBoolean(key, value);
193         setPreference(context, key, Boolean.toString(value));
194     }
195 }
196