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.prefs;
18 
19 import android.content.Context;
20 import android.content.SharedPreferences;
21 import com.android.tv.common.CommonConstants;
22 import com.android.tv.common.CommonPreferences;
23 import com.android.tv.common.SoftPreconditions;
24 
25 /** A helper class for the tuner preferences. */
26 public class TunerPreferences extends CommonPreferences {
27     private static final String TAG = "TunerPreferences";
28 
29     private static final String PREFS_KEY_CHANNEL_DATA_VERSION = "channel_data_version";
30     private static final String PREFS_KEY_SCANNED_CHANNEL_COUNT = "scanned_channel_count";
31     private static final String PREFS_KEY_SCAN_DONE = "scan_done";
32     private static final String PREFS_KEY_TRICKPLAY_EXPIRED_MS = "trickplay_expired_ms";
33 
34     private static final String SHARED_PREFS_NAME =
35             CommonConstants.BASE_PACKAGE + ".tuner.preferences";
36 
37     public static final int CHANNEL_DATA_VERSION_NOT_SET = -1;
38 
getSharedPreferences(Context context)39     protected static SharedPreferences getSharedPreferences(Context context) {
40         return context.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE);
41     }
42 
getChannelDataVersion(Context context)43     public static synchronized int getChannelDataVersion(Context context) {
44         SoftPreconditions.checkState(sInitialized);
45         return getSharedPreferences(context)
46                 .getInt(
47                         TunerPreferences.PREFS_KEY_CHANNEL_DATA_VERSION,
48                         CHANNEL_DATA_VERSION_NOT_SET);
49     }
50 
setChannelDataVersion(Context context, int version)51     public static synchronized void setChannelDataVersion(Context context, int version) {
52         SoftPreconditions.checkState(sInitialized);
53         getSharedPreferences(context)
54                 .edit()
55                 .putInt(TunerPreferences.PREFS_KEY_CHANNEL_DATA_VERSION, version)
56                 .apply();
57     }
58 
getScannedChannelCount(Context context)59     public static synchronized int getScannedChannelCount(Context context) {
60         SoftPreconditions.checkState(sInitialized);
61         return getSharedPreferences(context)
62                 .getInt(TunerPreferences.PREFS_KEY_SCANNED_CHANNEL_COUNT, 0);
63     }
64 
setScannedChannelCount(Context context, int channelCount)65     public static synchronized void setScannedChannelCount(Context context, int channelCount) {
66         SoftPreconditions.checkState(sInitialized);
67         getSharedPreferences(context)
68                 .edit()
69                 .putInt(TunerPreferences.PREFS_KEY_SCANNED_CHANNEL_COUNT, channelCount)
70                 .apply();
71     }
72 
isScanDone(Context context)73     public static synchronized boolean isScanDone(Context context) {
74         SoftPreconditions.checkState(sInitialized);
75         return getSharedPreferences(context)
76                 .getBoolean(TunerPreferences.PREFS_KEY_SCAN_DONE, false);
77     }
78 
setScanDone(Context context)79     public static synchronized void setScanDone(Context context) {
80         SoftPreconditions.checkState(sInitialized);
81         getSharedPreferences(context)
82                 .edit()
83                 .putBoolean(TunerPreferences.PREFS_KEY_SCAN_DONE, true)
84                 .apply();
85     }
86 
getTrickplayExpiredMs(Context context)87     public static synchronized long getTrickplayExpiredMs(Context context) {
88         SoftPreconditions.checkState(sInitialized);
89         return getSharedPreferences(context)
90                 .getLong(TunerPreferences.PREFS_KEY_TRICKPLAY_EXPIRED_MS, 0);
91     }
92 
setTrickplayExpiredMs(Context context, long timeMs)93     public static synchronized void setTrickplayExpiredMs(Context context, long timeMs) {
94         SoftPreconditions.checkState(sInitialized);
95         getSharedPreferences(context)
96                 .edit()
97                 .putLong(TunerPreferences.PREFS_KEY_TRICKPLAY_EXPIRED_MS, timeMs)
98                 .apply();
99     }
100 }
101