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