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