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; 18 19 import android.app.Activity; 20 import android.content.ActivityNotFoundException; 21 import android.content.ComponentName; 22 import android.content.Intent; 23 import android.media.tv.TvInputInfo; 24 import android.os.Bundle; 25 import android.os.Handler; 26 import android.os.Looper; 27 import android.support.annotation.MainThread; 28 import android.util.Log; 29 30 import com.android.tv.common.CommonConstants; 31 import com.android.tv.common.SoftPreconditions; 32 import com.android.tv.common.actions.InputSetupActionUtils; 33 import com.android.tv.data.ChannelDataManager; 34 import com.android.tv.data.ChannelDataManager.Listener; 35 import com.android.tv.data.epg.EpgFetcher; 36 import com.android.tv.data.epg.EpgInputWhiteList; 37 import com.android.tv.features.TvFeatures; 38 import com.android.tv.util.SetupUtils; 39 import com.android.tv.util.TvInputManagerHelper; 40 import com.android.tv.util.Utils; 41 42 import com.google.android.tv.partner.support.EpgContract; 43 44 import dagger.android.AndroidInjection; 45 import dagger.android.ContributesAndroidInjector; 46 47 import java.util.concurrent.TimeUnit; 48 49 import javax.inject.Inject; 50 51 /** 52 * An activity to launch a TV input setup activity. 53 * 54 * <p>After setup activity is finished, all channels will be browsable. 55 */ 56 public class SetupPassthroughActivity extends Activity { 57 private static final String TAG = "SetupPassthroughAct"; 58 private static final boolean DEBUG = false; 59 60 private static final int REQUEST_START_SETUP_ACTIVITY = 200; 61 62 private static ScanTimeoutMonitor sScanTimeoutMonitor; 63 64 private TvInputInfo mTvInputInfo; 65 private Intent mActivityAfterCompletion; 66 private boolean mEpgFetcherDuringScan; 67 @Inject EpgInputWhiteList mEpgInputWhiteList; 68 @Inject TvInputManagerHelper mInputManager; 69 @Inject SetupUtils mSetupUtils; 70 @Inject ChannelDataManager mChannelDataManager; 71 @Inject EpgFetcher mEpgFetcher; 72 73 @Override onCreate(Bundle savedInstanceState)74 public void onCreate(Bundle savedInstanceState) { 75 if (DEBUG) Log.d(TAG, "onCreate"); 76 AndroidInjection.inject(this); 77 super.onCreate(savedInstanceState); 78 Intent intent = getIntent(); 79 String inputId = intent.getStringExtra(InputSetupActionUtils.EXTRA_INPUT_ID); 80 mTvInputInfo = mInputManager.getTvInputInfo(inputId); 81 mActivityAfterCompletion = InputSetupActionUtils.getExtraActivityAfter(intent); 82 boolean needToFetchEpg = 83 mTvInputInfo != null && Utils.isInternalTvInput(this, mTvInputInfo.getId()); 84 if (needToFetchEpg) { 85 // In case when the activity is restored, this flag should be restored as well. 86 mEpgFetcherDuringScan = true; 87 } 88 if (savedInstanceState == null) { 89 SoftPreconditions.checkArgument( 90 InputSetupActionUtils.hasInputSetupAction(intent), 91 TAG, 92 "Unsupported action %s", 93 intent.getAction()); 94 if (DEBUG) Log.d(TAG, "TvInputId " + inputId + " / TvInputInfo " + mTvInputInfo); 95 if (mTvInputInfo == null) { 96 Log.w(TAG, "There is no input with the ID " + inputId + "."); 97 finish(); 98 return; 99 } 100 if (intent.getExtras() == null) { 101 Log.w(TAG, "There is no extra info in the intent"); 102 finish(); 103 return; 104 } 105 Intent setupIntent = InputSetupActionUtils.getExtraSetupIntent(intent); 106 if (DEBUG) Log.d(TAG, "Setup activity launch intent: " + setupIntent); 107 if (setupIntent == null) { 108 Log.w(TAG, "The input (" + mTvInputInfo.getId() + ") doesn't have setup."); 109 finish(); 110 return; 111 } 112 SetupUtils.grantEpgPermission(this, mTvInputInfo.getServiceInfo().packageName); 113 if (DEBUG) Log.d(TAG, "Activity after completion " + mActivityAfterCompletion); 114 // If EXTRA_SETUP_INTENT is not removed, an infinite recursion happens during 115 // setupIntent.putExtras(intent.getExtras()). 116 Bundle extras = intent.getExtras(); 117 InputSetupActionUtils.removeSetupIntent(extras); 118 setupIntent.putExtras(extras); 119 try { 120 ComponentName callingActivity = getCallingActivity(); 121 if (callingActivity == null 122 || !callingActivity.getPackageName().equals(CommonConstants.BASE_PACKAGE)) { 123 String name = 124 callingActivity == null ? "null" : callingActivity.getPackageName(); 125 Log.w(TAG, 126 "Calling activity " + name + " is not trusted. Not forwarding intent."); 127 finish(); 128 return; 129 } 130 startActivityForResult(setupIntent, REQUEST_START_SETUP_ACTIVITY); 131 } catch (ActivityNotFoundException e) { 132 Log.e(TAG, "Can't find activity: " + setupIntent.getComponent()); 133 finish(); 134 return; 135 } 136 if (needToFetchEpg) { 137 if (sScanTimeoutMonitor == null) { 138 sScanTimeoutMonitor = new ScanTimeoutMonitor(mEpgFetcher, mChannelDataManager); 139 } 140 sScanTimeoutMonitor.startMonitoring(); 141 mEpgFetcher.onChannelScanStarted(); 142 } 143 } 144 } 145 146 @Override onActivityResult(int requestCode, final int resultCode, final Intent data)147 public void onActivityResult(int requestCode, final int resultCode, final Intent data) { 148 if (DEBUG) 149 Log.d(TAG, "onActivityResult(" + requestCode + ", " + resultCode + ", " + data + ")"); 150 if (sScanTimeoutMonitor != null) { 151 sScanTimeoutMonitor.stopMonitoring(); 152 } 153 // Note: It's not guaranteed that this method is always called after scanning. 154 boolean setupComplete = 155 requestCode == REQUEST_START_SETUP_ACTIVITY && resultCode == Activity.RESULT_OK; 156 // Tells EpgFetcher that channel source setup is finished. 157 158 if (mEpgFetcherDuringScan) { 159 mEpgFetcher.onChannelScanFinished(); 160 } 161 if (!setupComplete) { 162 setResult(resultCode, data); 163 finish(); 164 return; 165 } 166 if (TvFeatures.CLOUD_EPG_FOR_3RD_PARTY.isEnabled(this) 167 && data != null 168 && data.getBooleanExtra(EpgContract.EXTRA_USE_CLOUD_EPG, false)) { 169 if (DEBUG) Log.d(TAG, "extra " + data.getExtras()); 170 String inputId = data.getStringExtra(TvInputInfo.EXTRA_INPUT_ID); 171 if (mEpgInputWhiteList.isInputWhiteListed(inputId)) { 172 mEpgFetcher.fetchImmediately(); 173 } 174 } 175 176 if (mTvInputInfo == null) { 177 Log.w( 178 TAG, 179 "There is no input with ID " 180 + getIntent().getStringExtra(InputSetupActionUtils.EXTRA_INPUT_ID) 181 + "."); 182 setResult(resultCode, data); 183 finish(); 184 return; 185 } 186 mSetupUtils.onTvInputSetupFinished( 187 mTvInputInfo.getId(), 188 () -> { 189 if (mActivityAfterCompletion != null) { 190 try { 191 startActivity(mActivityAfterCompletion); 192 } catch (ActivityNotFoundException e) { 193 Log.w(TAG, "Activity launch failed", e); 194 } 195 } 196 setResult(resultCode, data); 197 finish(); 198 }); 199 } 200 201 /** 202 * Monitors the scan progress and notifies the timeout of the scanning. The purpose of this 203 * monitor is to call EpgFetcher.onChannelScanFinished() in case when 204 * SetupPassthroughActivity.onActivityResult() is not called properly. b/36008534 205 */ 206 @MainThread 207 private static class ScanTimeoutMonitor { 208 // Set timeout long enough. The message in Sony TV says the scanning takes about 30 minutes. 209 private static final long SCAN_TIMEOUT_MS = TimeUnit.MINUTES.toMillis(30); 210 211 private final EpgFetcher mEpgFetcher; 212 private final ChannelDataManager mChannelDataManager; 213 private final Handler mHandler = new Handler(Looper.getMainLooper()); 214 private final Runnable mScanTimeoutRunnable = 215 () -> { 216 Log.w( 217 TAG, 218 "No channels has been added for a while." 219 + " The scan might have finished unexpectedly."); 220 onScanTimedOut(); 221 }; 222 private final Listener mChannelDataManagerListener = 223 new Listener() { 224 @Override 225 public void onLoadFinished() { 226 setupTimer(); 227 } 228 229 @Override 230 public void onChannelListUpdated() { 231 setupTimer(); 232 } 233 234 @Override 235 public void onChannelBrowsableChanged() {} 236 }; 237 private boolean mStarted; 238 ScanTimeoutMonitor(EpgFetcher epgFetcher, ChannelDataManager mChannelDataManager)239 private ScanTimeoutMonitor(EpgFetcher epgFetcher, ChannelDataManager mChannelDataManager) { 240 mEpgFetcher = epgFetcher; 241 this.mChannelDataManager = mChannelDataManager; 242 } 243 startMonitoring()244 private void startMonitoring() { 245 if (!mStarted) { 246 mStarted = true; 247 mChannelDataManager.addListener(mChannelDataManagerListener); 248 } 249 if (mChannelDataManager.isDbLoadFinished()) { 250 setupTimer(); 251 } 252 } 253 stopMonitoring()254 private void stopMonitoring() { 255 if (mStarted) { 256 mStarted = false; 257 mHandler.removeCallbacks(mScanTimeoutRunnable); 258 mChannelDataManager.removeListener(mChannelDataManagerListener); 259 } 260 } 261 setupTimer()262 private void setupTimer() { 263 mHandler.removeCallbacks(mScanTimeoutRunnable); 264 mHandler.postDelayed(mScanTimeoutRunnable, SCAN_TIMEOUT_MS); 265 } 266 onScanTimedOut()267 private void onScanTimedOut() { 268 stopMonitoring(); 269 mEpgFetcher.onChannelScanFinished(); 270 } 271 } 272 273 /** Exports {@link MainActivity} for Dagger codegen to create the appropriate injector. */ 274 @dagger.Module 275 public abstract static class Module { 276 @ContributesAndroidInjector contributesSetupPassthroughActivity()277 abstract SetupPassthroughActivity contributesSetupPassthroughActivity(); 278 } 279 } 280