PackageIntentsReceiver.java revision 7d67089aa1e9aa2123c3cd2f386d7019a1544db1
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.media.tv.TvInputInfo; 25import android.media.tv.TvInputManager; 26import android.os.Handler; 27 28import com.android.tv.Features; 29import com.android.tv.TvActivity; 30import com.android.tv.util.SetupUtils; 31 32import java.util.List; 33 34/** 35 * A class for handling the broadcast intents from PackageManager. 36 */ 37public class PackageIntentsReceiver extends BroadcastReceiver { 38 // Delay before checking TvInputManager's input list. 39 // Sometimes TvInputManager's input list isn't updated yet when this receiver is called. 40 // So we should check the list after some delay. 41 private static final long TV_INPUT_UPDATE_DELAY_MS = 500; 42 43 private TvInputManager mTvInputManager; 44 private final Handler mHandler = new Handler(); 45 private Runnable mOnPackageUpdatedRunnable; 46 private PackageManager mPackageManager; 47 private ComponentName mTvActivityComponentName; 48 49 private void init(Context context) { 50 mTvInputManager = (TvInputManager) context.getSystemService(Context.TV_INPUT_SERVICE); 51 52 final Context applicationContext = context.getApplicationContext(); 53 mOnPackageUpdatedRunnable = new Runnable() { 54 @Override 55 public void run() { 56 if (!Features.ONBOARDING_EXPERIENCE.isEnabled(applicationContext)) { 57 List<TvInputInfo> inputs = mTvInputManager.getTvInputList(); 58 // Enable the MainActivity only if there is at least one tuner type input. 59 boolean enable = false; 60 for (TvInputInfo input : inputs) { 61 if (input.getType() == TvInputInfo.TYPE_TUNER) { 62 enable = true; 63 break; 64 } 65 } 66 enableTvActivityWithinPackageManager(applicationContext, enable); 67 } 68 69 SetupUtils.getInstance(applicationContext).onInputListUpdated(mTvInputManager); 70 } 71 }; 72 73 mPackageManager = applicationContext.getPackageManager(); 74 mTvActivityComponentName = new ComponentName(applicationContext, TvActivity.class); 75 } 76 77 @Override 78 public void onReceive(Context context, Intent intent) { 79 if (mTvInputManager == null) { 80 init(context); 81 } 82 83 mHandler.removeCallbacks(mOnPackageUpdatedRunnable); 84 mHandler.postDelayed(mOnPackageUpdatedRunnable, TV_INPUT_UPDATE_DELAY_MS); 85 86 } 87 88 89 /** 90 * Enables/Disables {@link TvActivity}. 91 */ 92 private void enableTvActivityWithinPackageManager(Context context, boolean enable) { 93 PackageManager pm = context.getPackageManager(); 94 ComponentName name = new ComponentName(context, TvActivity.class); 95 96 int newState = enable ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED : 97 PackageManager.COMPONENT_ENABLED_STATE_DISABLED; 98 if (pm.getComponentEnabledSetting(name) != newState) { 99 pm.setComponentEnabledSetting(name, newState, 0); 100 } 101 } 102} 103