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.receiver;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.pm.PackageManager;
24 import android.os.Build;
25 import android.util.Log;
26 import com.android.tv.Starter;
27 import com.android.tv.TvActivity;
28 import com.android.tv.TvSingletons;
29 import com.android.tv.dvr.recorder.DvrRecordingService;
30 import com.android.tv.dvr.recorder.RecordingScheduler;
31 import com.android.tv.features.TvFeatures;
32 import com.android.tv.recommendation.ChannelPreviewUpdater;
33 import com.android.tv.recommendation.NotificationService;
34 import com.android.tv.util.OnboardingUtils;
35 import com.android.tv.util.SetupUtils;
36 
37 /**
38  * Boot completed receiver for TV app.
39  *
40  * <p>It's used to
41  *
42  * <ul>
43  *   <li>start the {@link NotificationService} for recommendation
44  *   <li>grant permission to the TIS's
45  *   <li>enable {@link TvActivity} if necessary
46  *   <li>start the {@link DvrRecordingService}
47  * </ul>
48  */
49 public class BootCompletedReceiver extends BroadcastReceiver {
50     private static final String TAG = "BootCompletedReceiver";
51     private static final boolean DEBUG = false;
52 
53     @Override
onReceive(Context context, Intent intent)54     public void onReceive(Context context, Intent intent) {
55         if (!TvSingletons.getSingletons(context).getTvInputManagerHelper().hasTvInputManager()) {
56             Log.wtf(TAG, "Stopping because device does not have a TvInputManager");
57             return;
58         }
59         if (DEBUG) Log.d(TAG, "boot completed " + intent);
60         Starter.start(context);
61 
62         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
63             ChannelPreviewUpdater.getInstance(context).updatePreviewDataForChannelsImmediately();
64         } else {
65             Intent notificationIntent = new Intent(context, NotificationService.class);
66             notificationIntent.setAction(NotificationService.ACTION_SHOW_RECOMMENDATION);
67             context.startService(notificationIntent);
68         }
69 
70         // Grant permission to already set up packages after the system has finished booting.
71         SetupUtils.grantEpgPermissionToSetUpPackages(context);
72 
73         if (TvFeatures.UNHIDE.isEnabled(context.getApplicationContext())) {
74             if (OnboardingUtils.isFirstBoot(context)) {
75                 // Enable the application if this is the first "unhide" feature is enabled just in
76                 // case when the app has been disabled before.
77                 PackageManager pm = context.getPackageManager();
78                 ComponentName name = new ComponentName(context, TvActivity.class);
79                 if (pm.getComponentEnabledSetting(name)
80                         != PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
81                     pm.setComponentEnabledSetting(
82                             name, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 0);
83                 }
84                 OnboardingUtils.setFirstBootCompleted(context);
85             }
86         }
87 
88         RecordingScheduler scheduler = TvSingletons.getSingletons(context).getRecordingScheduler();
89         if (scheduler != null) {
90             scheduler.updateAndStartServiceIfNeeded();
91         }
92     }
93 }
94