aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/tuner/TunerInputController.java
blob: 65bbbdd0c3d563aea612ab6971c79b47caa3db99 (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
 * 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.tv.tuner;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.android.tv.Features;
import com.android.tv.R;
import com.android.tv.TvApplication;
import com.android.tv.tuner.setup.TunerSetupActivity;
import com.android.tv.tuner.tvinput.TunerTvInputService;
import com.android.tv.tuner.util.SystemPropertiesProxy;
import com.android.tv.tuner.util.TunerInputInfoUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * Controls the package visibility of {@link TunerTvInputService}.
 * <p>
 * Listens to broadcast intent for {@link Intent#ACTION_BOOT_COMPLETED},
 * {@code UsbManager.ACTION_USB_DEVICE_ATTACHED}, and {@code UsbManager.ACTION_USB_DEVICE_ATTACHED}
 * to update the connection status of the supported USB TV tuners.
 */
public class TunerInputController extends BroadcastReceiver {
    private static final boolean DEBUG = true;
    private static final String TAG = "TunerInputController";
    private static final String PREFERENCE_IS_NETWORK_TUNER_ATTACHED = "network_tuner";
    private static final String SECURITY_PATCH_LEVEL_KEY = "ro.build.version.security_patch";
    private static final String SECURITY_PATCH_LEVEL_FORMAT = "yyyy-MM-dd";

    /**
     * Action of {@link Intent} to check network connection repeatedly when it is necessary.
     */
    public static final String CHECKING_NETWORK_CONNECTION =
            "com.android.tv.action.CHECKING_NETWORK_CONNECTION";

    /**
     * Action of {@link Intent} when network tuner is attached.
     */
    public static final String NETWORK_TUNER_ATTACHED =
            "com.android.tv.action.NETWORK_TUNER_ATTACHED";

    /**
     * Action of {@link Intent} when network tuner is detached.
     */
    public static final String NETWORK_TUNER_DETACHED =
            "com.android.tv.action.NETWORK_TUNER_DETACHED";

    private static final String EXTRA_CHECKING_DURATION =
            "com.android.tv.action.extra.CHECKING_DURATION";

    private static final long INITIAL_CHECKING_DURATION_MS = TimeUnit.SECONDS.toMillis(10);
    private static final long MAXIMUM_CHECKING_DURATION_MS = TimeUnit.MINUTES.toMillis(10);

    private static final TunerDevice[] TUNER_DEVICES = {
        new TunerDevice(0x2040, 0xb123, null), // WinTV-HVR-955Q
        new TunerDevice(0x07ca, 0x0837, null), // AverTV Volar Hybrid Q
        // WinTV-dualHD (bulk) will be supported after 2017 April security patch.
        new TunerDevice(0x2040, 0x826d, "2017-04-01"), // WinTV-dualHD (bulk)
    };

    private static final int MSG_ENABLE_INPUT_SERVICE = 1000;
    private static final long DVB_DRIVER_CHECK_DELAY_MS = 300;

    private DvbDeviceAccessor mDvbDeviceAccessor;
    private final Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_ENABLE_INPUT_SERVICE:
                    Context context = (Context) msg.obj;
                    if (mDvbDeviceAccessor == null) {
                        mDvbDeviceAccessor = new DvbDeviceAccessor(context);
                    }
                    boolean enabled = mDvbDeviceAccessor.isDvbDeviceAvailable();
                    enableTunerTvInputService(
                            context, enabled, false, enabled ? TunerHal.TUNER_TYPE_USB : null);
                    break;
            }
        }
    };

    /**
     * Simple data holder for a USB device. Used to represent a tuner model, and compare
     * against {@link UsbDevice}.
     */
    private static class TunerDevice {
        private final int vendorId;
        private final int productId;

        // security patch level from which the specific tuner type is supported.
        private final String minSecurityLevel;

        private TunerDevice(int vendorId, int productId, String minSecurityLevel) {
            this.vendorId = vendorId;
            this.productId = productId;
            this.minSecurityLevel = minSecurityLevel;
        }

        private boolean equals(UsbDevice device) {
            return device.getVendorId() == vendorId && device.getProductId() == productId;
        }

        private boolean isSupported(String currentSecurityLevel) {
            if (minSecurityLevel == null) {
                return true;
            }

            long supportSecurityLevelTimeStamp = 0;
            long currentSecurityLevelTimestamp = 0;
            try {
                SimpleDateFormat format = new SimpleDateFormat(SECURITY_PATCH_LEVEL_FORMAT);
                supportSecurityLevelTimeStamp = format.parse(minSecurityLevel).getTime();
                currentSecurityLevelTimestamp = format.parse(currentSecurityLevel).getTime();
            } catch (ParseException e) {
            }
            return supportSecurityLevelTimeStamp != 0
                    && supportSecurityLevelTimeStamp <= currentSecurityLevelTimestamp;
        }
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        if (DEBUG) Log.d(TAG, "Broadcast intent received:" + intent);
        TvApplication.setCurrentRunningProcess(context, true);
        if (!Features.TUNER.isEnabled(context)) {
            enableTunerTvInputService(context, false, false, null);
            return;
        }

        SharedPreferences sharedPreferences =
                PreferenceManager.getDefaultSharedPreferences(context);
        switch (intent.getAction()) {
            case Intent.ACTION_BOOT_COMPLETED:
                executeNetworkTunerDiscoveryAsyncTask(context, INITIAL_CHECKING_DURATION_MS);
            case TvApplication.ACTION_APPLICATION_FIRST_LAUNCHED:
            case UsbManager.ACTION_USB_DEVICE_ATTACHED:
            case UsbManager.ACTION_USB_DEVICE_DETACHED:
                if (TunerHal.useBuiltInTuner(context)) {
                    enableTunerTvInputService(context, true, false, TunerHal.TUNER_TYPE_BUILT_IN);
                    break;
                }
                // Falls back to the below to check USB tuner devices.
                boolean enabled = isUsbTunerConnected(context);
                mHandler.removeMessages(MSG_ENABLE_INPUT_SERVICE);
                if (enabled) {
                    // Need to check if DVB driver is accessible. Since the driver creation
                    // could be happen after the USB event, delay the checking by
                    // DVB_DRIVER_CHECK_DELAY_MS.
                    mHandler.sendMessageDelayed(
                            mHandler.obtainMessage(MSG_ENABLE_INPUT_SERVICE, context),
                            DVB_DRIVER_CHECK_DELAY_MS);
                } else {
                    if (sharedPreferences.getBoolean(PREFERENCE_IS_NETWORK_TUNER_ATTACHED, false)) {
                        // Since network tuner is attached, do not disable TunerTvInput,
                        // just updates the TvInputInfo.
                        TunerInputInfoUtils.updateTunerInputInfo(context);
                        break;
                    }
                    enableTunerTvInputService(context, false, false, TextUtils
                            .equals(intent.getAction(), UsbManager.ACTION_USB_DEVICE_DETACHED) ?
                            TunerHal.TUNER_TYPE_USB : null);
                }
                break;
            case CHECKING_NETWORK_CONNECTION:
                long repeatedDurationMs = intent.getLongExtra(EXTRA_CHECKING_DURATION,
                        INITIAL_CHECKING_DURATION_MS);
                executeNetworkTunerDiscoveryAsyncTask(context,
                        Math.min(repeatedDurationMs * 2, MAXIMUM_CHECKING_DURATION_MS));
                break;
            case NETWORK_TUNER_ATTACHED:
                // Network tuner detection is initiated by UI. So the app should not
                // be killed.
                sharedPreferences.edit()
                        .putBoolean(PREFERENCE_IS_NETWORK_TUNER_ATTACHED, true).apply();
                enableTunerTvInputService(context, true, true, TunerHal.TUNER_TYPE_NETWORK);
                break;
            case NETWORK_TUNER_DETACHED:
                sharedPreferences.edit()
                        .putBoolean(PREFERENCE_IS_NETWORK_TUNER_ATTACHED, false).apply();
                if(!isUsbTunerConnected(context) && !TunerHal.useBuiltInTuner(context)) {
                    // Network tuner detection is initiated by UI. So the app should not
                    // be killed.
                    enableTunerTvInputService(context, false, true, TunerHal.TUNER_TYPE_NETWORK);
                } else {
                    // Since USB tuner is attached, do not disable TunerTvInput,
                    // just updates the TvInputInfo.
                    TunerInputInfoUtils.updateTunerInputInfo(context);
                }
                break;
        }
    }

    /**
     * See if any USB tuner hardware is attached in the system.
     *
     * @param context {@link Context} instance
     * @return {@code true} if any tuner device we support is plugged in
     */
    private boolean isUsbTunerConnected(Context context) {
        UsbManager manager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
        Map<String, UsbDevice> deviceList = manager.getDeviceList();
        String currentSecurityLevel =
                SystemPropertiesProxy.getString(SECURITY_PATCH_LEVEL_KEY, null);

        for (UsbDevice device : deviceList.values()) {
            if (DEBUG) {
                Log.d(TAG, "Device: " + device);
            }
            for (TunerDevice tuner : TUNER_DEVICES) {
                if (tuner.equals(device) && tuner.isSupported(currentSecurityLevel)) {
                    Log.i(TAG, "Tuner found");
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * Enable/disable the component {@link TunerTvInputService}.
     *
     * @param context {@link Context} instance
     * @param enabled {@code true} to enable the service; otherwise {@code false}
     */
    private void enableTunerTvInputService(Context context, boolean enabled,
            boolean forceDontKillApp, Integer tunerType) {
        if (DEBUG) Log.d(TAG, "enableTunerTvInputService: " + enabled);
        PackageManager pm  = context.getPackageManager();
        ComponentName componentName = new ComponentName(context, TunerTvInputService.class);

        // Don't kill app by enabling/disabling TvActivity. If LC is killed by enabling/disabling
        // TvActivity, the following pm.setComponentEnabledSetting doesn't work.
        ((TvApplication) context.getApplicationContext()).handleInputCountChanged(
                true, enabled, true);
        // Since PackageManager.DONT_KILL_APP delays the operation by 10 seconds
        // (PackageManagerService.BROADCAST_DELAY), we'd better avoid using it. It is used only
        // when the LiveChannels app is active since we don't want to kill the running app.
        int flags = forceDontKillApp
                || TvApplication.getSingletons(context).getMainActivityWrapper().isCreated()
                ? PackageManager.DONT_KILL_APP : 0;
        int newState = enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
                : PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
        if (newState != pm.getComponentEnabledSetting(componentName)) {
            // Send/cancel the USB tuner TV input setup recommendation card.
            TunerSetupActivity.onTvInputEnabled(context, enabled);
            // Enable/disable the USB tuner TV input.
            pm.setComponentEnabledSetting(componentName, newState, flags);
            if (!enabled && tunerType != null) {
                if (tunerType == TunerHal.TUNER_TYPE_USB) {
                    Toast.makeText(context, R.string.msg_usb_tuner_disconnected,
                            Toast.LENGTH_SHORT).show();
                } else if (tunerType == TunerHal.TUNER_TYPE_NETWORK) {
                    Toast.makeText(context, R.string.msg_network_tuner_disconnected,
                            Toast.LENGTH_SHORT).show();
                }
            }
            if (DEBUG) Log.d(TAG, "Status updated:" + enabled);
        } else if (enabled) {
            // When # of tuners is changed or the tuner input service is switching from/to using
            // network tuners or the device just boots.
            TunerInputInfoUtils.updateTunerInputInfo(context);
        }
    }

    /**
     * Discovers a network tuner. If the network connection is down, it won't repeatedly checking.
     */
    public static void executeNetworkTunerDiscoveryAsyncTask(final Context context) {
        executeNetworkTunerDiscoveryAsyncTask(context, 0);
    }

    /**
     * Discovers a network tuner.
     * @param context {@link Context}
     * @param repeatedDurationMs the time length to wait to repeatedly check network status to start
     *                           finding network tuner when the network connection is not available.
     *                           {@code 0} to disable repeatedly checking.
     */
    private static void executeNetworkTunerDiscoveryAsyncTask(final Context context,
            final long repeatedDurationMs) {
        if (!Features.NETWORK_TUNER.isEnabled(context)) {
            return;
        }
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                if (isNetworkConnected(context)) {
                    // Implement and execute network tuner discovery AsyncTask here.
                } else if (repeatedDurationMs > 0) {
                    AlarmManager alarmManager =
                            (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
                    Intent networkCheckingIntent = new Intent(context, TunerInputController.class);
                    networkCheckingIntent.setAction(CHECKING_NETWORK_CONNECTION);
                    networkCheckingIntent.putExtra(EXTRA_CHECKING_DURATION, repeatedDurationMs);
                    PendingIntent alarmIntent = PendingIntent.getBroadcast(
                            context, 0, networkCheckingIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                    alarmManager.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime()
                            + repeatedDurationMs, alarmIntent);
                }
                return null;
            }
        }.execute();
    }

    private static boolean isNetworkConnected(Context context) {
        ConnectivityManager cm = (ConnectivityManager)
                context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        return networkInfo != null && networkInfo.isConnected();
    }
}