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 String action = intent.getAction(); 60 if (!Intent.ACTION_BOOT_COMPLETED.equals(action)) { 61 Log.w(TAG, "invalid action " + action); 62 return; 63 } 64 if (DEBUG) Log.d(TAG, "boot completed " + intent); 65 Starter.start(context); 66 67 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 68 ChannelPreviewUpdater.getInstance(context).updatePreviewDataForChannelsImmediately(); 69 } else { 70 Intent notificationIntent = new Intent(context, NotificationService.class); 71 notificationIntent.setAction(NotificationService.ACTION_SHOW_RECOMMENDATION); 72 context.startService(notificationIntent); 73 } 74 75 // Grant permission to already set up packages after the system has finished booting. 76 SetupUtils.grantEpgPermissionToSetUpPackages(context); 77 78 if (TvFeatures.UNHIDE.isEnabled(context.getApplicationContext())) { 79 if (OnboardingUtils.isFirstBoot(context)) { 80 // Enable the application if this is the first "unhide" feature is enabled just in 81 // case when the app has been disabled before. 82 PackageManager pm = context.getPackageManager(); 83 ComponentName name = new ComponentName(context, TvActivity.class); 84 if (pm.getComponentEnabledSetting(name) 85 != PackageManager.COMPONENT_ENABLED_STATE_ENABLED) { 86 pm.setComponentEnabledSetting( 87 name, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 0); 88 } 89 OnboardingUtils.setFirstBootCompleted(context); 90 } 91 } 92 93 RecordingScheduler scheduler = TvSingletons.getSingletons(context).getRecordingScheduler(); 94 if (scheduler != null) { 95 scheduler.updateAndStartServiceIfNeeded(); 96 } 97 } 98 } 99