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
17package com.android.tv.receiver;
18
19import android.content.BroadcastReceiver;
20import android.content.ComponentName;
21import android.content.Context;
22import android.content.Intent;
23import android.content.pm.PackageManager;
24import android.support.v4.os.BuildCompat;
25import android.util.Log;
26
27import com.android.tv.Features;
28import com.android.tv.TvActivity;
29import com.android.tv.common.feature.CommonFeatures;
30import com.android.tv.dvr.DvrRecordingService;
31import com.android.tv.recommendation.NotificationService;
32import com.android.tv.util.OnboardingUtils;
33import com.android.tv.util.SetupUtils;
34
35/**
36 * Boot completed receiver for TV app.
37 *
38 * <p>It's used to
39 * <ul>
40 *     <li>start the {@link NotificationService} for recommendation</li>
41 *     <li>grant permission to the TIS's </li>
42 *     <li>enable {@link TvActivity} if necessary</li>
43 *     <li>start the {@link DvrRecordingService} </li>
44 * </ul>
45 */
46public class BootCompletedReceiver extends BroadcastReceiver {
47    private static final String TAG = "BootCompletedReceiver";
48    private static final boolean DEBUG = false;
49
50    @Override
51    public void onReceive(Context context, Intent intent) {
52        if (DEBUG) Log.d(TAG, "boot completed " + intent);
53        // Start {@link NotificationService}.
54        Intent notificationIntent = new Intent(context, NotificationService.class);
55        notificationIntent.setAction(NotificationService.ACTION_SHOW_RECOMMENDATION);
56        context.startService(notificationIntent);
57
58        // Grant permission to already set up packages after the system has finished booting.
59        SetupUtils.grantEpgPermissionToSetUpPackages(context);
60
61        if (Features.UNHIDE.isEnabled(context)) {
62            if (OnboardingUtils.isFirstBoot(context)) {
63                // Enable the application if this is the first "unhide" feature is enabled just in
64                // case when the app has been disabled before.
65                PackageManager pm = context.getPackageManager();
66                ComponentName name = new ComponentName(context, TvActivity.class);
67                if (pm.getComponentEnabledSetting(name)
68                        != PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
69                    pm.setComponentEnabledSetting(name,
70                            PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 0);
71                }
72                OnboardingUtils.setFirstBootCompleted(context);
73            }
74        }
75
76        if (CommonFeatures.DVR.isEnabled(context) && BuildCompat.isAtLeastN()) {
77            DvrRecordingService.startService(context);
78        }
79    }
80}
81