aboutsummaryrefslogtreecommitdiff
path: root/usbtuner/src/com/android/usbtuner/UsbTunerPreferences.java
blob: 0394648dc12e1bc2314dbc32182795d686ba3718 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.usbtuner;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;

import com.android.tv.common.SoftPreconditions;
import com.android.usbtuner.UsbTunerPreferenceProvider.Preferences;
import com.android.usbtuner.util.TisConfiguration;

/**
 * A helper class for the USB tuner preferences.
 */
// TODO: Change this class to run on the worker thread.
public class UsbTunerPreferences {
    private static final String TAG = "UsbTunerPreferences";

    private static final String PREFS_KEY_CHANNEL_DATA_VERSION = "channel_data_version";
    private static final String PREFS_KEY_SCANNED_CHANNEL_COUNT = "scanned_channel_count";
    private static final String PREFS_KEY_SCAN_DONE = "scan_done";
    private static final String PREFS_KEY_LAUNCH_SETUP = "launch_setup";

    private static final String SHARED_PREFS_NAME = "com.android.usbtuner.preferences";

    private static final Bundle PREFERENCE_VALUES = new Bundle();

    private static boolean useContentProvider(Context context) {
        // If TIS is a part of LC, it should use ContentProvider to resolve multiple process access.
        return TisConfiguration.isPackagedWithLiveChannels(context);
    }

    public static int getChannelDataVersion(Context context) {
        if (useContentProvider(context)) {
            return getPreferenceInt(context, PREFS_KEY_CHANNEL_DATA_VERSION);
        } else {
            return getSharedPreferences(context)
                    .getInt(UsbTunerPreferences.PREFS_KEY_CHANNEL_DATA_VERSION, 0);
        }
    }

    public static void setChannelDataVersion(Context context, int version) {
        if (useContentProvider(context)) {
            setPreference(context, PREFS_KEY_CHANNEL_DATA_VERSION, version);
        } else {
            getSharedPreferences(context).edit()
                    .putInt(UsbTunerPreferences.PREFS_KEY_CHANNEL_DATA_VERSION, version)
                    .apply();
        }
    }

    public static int getScannedChannelCount(Context context) {
        if (useContentProvider(context)) {
            return getPreferenceInt(context, PREFS_KEY_SCANNED_CHANNEL_COUNT);
        } else {
            return getSharedPreferences(context)
                    .getInt(UsbTunerPreferences.PREFS_KEY_SCANNED_CHANNEL_COUNT, 0);
        }
    }

    public static void setScannedChannelCount(Context context, int channelCount) {
        if (useContentProvider(context)) {
            setPreference(context, PREFS_KEY_SCANNED_CHANNEL_COUNT, channelCount);
        } else {
            getSharedPreferences(context).edit()
                    .putInt(UsbTunerPreferences.PREFS_KEY_SCANNED_CHANNEL_COUNT, channelCount)
                    .apply();
        }
    }

    public static boolean isScanDone(Context context) {
        if (useContentProvider(context)) {
            return getPreferenceBoolean(context, PREFS_KEY_SCAN_DONE);
        } else {
            return getSharedPreferences(context)
                    .getBoolean(UsbTunerPreferences.PREFS_KEY_SCAN_DONE, false);
        }
    }

    public static void setScanDone(Context context) {
        if (useContentProvider(context)) {
            setPreference(context, PREFS_KEY_SCAN_DONE, true);
        } else {
            getSharedPreferences(context).edit()
                    .putBoolean(UsbTunerPreferences.PREFS_KEY_SCAN_DONE, true)
                    .apply();
        }
    }

    public static boolean shouldShowSetupActivity(Context context) {
        if (useContentProvider(context)) {
            return getPreferenceBoolean(context, PREFS_KEY_LAUNCH_SETUP);
        } else {
            return getSharedPreferences(context)
                    .getBoolean(UsbTunerPreferences.PREFS_KEY_LAUNCH_SETUP, false);
        }
    }

    public static void setShouldShowSetupActivity(Context context, boolean need) {
        if (useContentProvider(context)) {
            setPreference(context, PREFS_KEY_LAUNCH_SETUP, need);
        } else {
            getSharedPreferences(context).edit()
                    .putBoolean(UsbTunerPreferences.PREFS_KEY_LAUNCH_SETUP, need)
                    .apply();
        }
    }

    private static SharedPreferences getSharedPreferences(Context context) {
        return context.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE);
    }

    // Content provider helpers
    private static String getPreference(Context context, String key) {
        ContentResolver resolver = context.getContentResolver();
        String[] projection = new String[] { Preferences.COLUMN_VALUE };
        String selection = Preferences.COLUMN_KEY + " like ?";
        String[] selectionArgs = new String[] { key };
        try (Cursor cursor = resolver.query(UsbTunerPreferenceProvider.buildPreferenceUri(key),
                projection, selection, selectionArgs, null)) {
            if (cursor != null && cursor.moveToFirst()) {
                return cursor.getString(0);
            }
        } catch (Exception e) {
            SoftPreconditions.warn(TAG, "getPreference", "Error querying preference values", e);
        }
        return null;
    }

    private static int getPreferenceInt(Context context, String key) {
        if (PREFERENCE_VALUES.containsKey(key)) {
            return PREFERENCE_VALUES.getInt(key);
        }
        try {
            return Integer.parseInt(getPreference(context, key));
        } catch (NumberFormatException e) {
            return 0;
        }
    }

    private static boolean getPreferenceBoolean(Context context, String key) {
        if (PREFERENCE_VALUES.containsKey(key)) {
            return PREFERENCE_VALUES.getBoolean(key);
        }
        return Boolean.valueOf(getPreference(context, key));
    }

    private static void setPreference(final Context context, final String key, final String value) {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                ContentResolver resolver = context.getContentResolver();
                ContentValues values = new ContentValues();
                values.put(Preferences.COLUMN_KEY, key);
                values.put(Preferences.COLUMN_VALUE, value);
                try {
                    resolver.insert(Preferences.CONTENT_URI, values);
                } catch (Exception e) {
                    SoftPreconditions.warn(TAG, "setPreference", "Error writing preference values",
                            e);
                }
                return null;
            }
        }.execute();
    }

    private static void setPreference(Context context, String key, int value) {
        PREFERENCE_VALUES.putInt(key, value);
        setPreference(context, key, Integer.toString(value));
    }

    private static void setPreference(Context context, String key, boolean value) {
        PREFERENCE_VALUES.putBoolean(key, value);
        setPreference(context, key, Boolean.toString(value));
    }
}