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.Context; 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.SoftPreconditions; 31 import com.android.tv.common.TvCommonConstants; 32 import com.android.tv.data.ChannelDataManager; 33 import com.android.tv.data.ChannelDataManager.Listener; 34 import com.android.tv.data.epg.EpgFetcher; 35 import com.android.tv.experiments.Experiments; 36 import com.android.tv.util.SetupUtils; 37 import com.android.tv.util.TvInputManagerHelper; 38 import com.android.tv.util.Utils; 39 40 import java.util.concurrent.TimeUnit; 41 42 /** 43 * An activity to launch a TV input setup activity. 44 * 45 * <p> After setup activity is finished, all channels will be browsable. 46 */ 47 public class SetupPassthroughActivity extends Activity { 48 private static final String TAG = "SetupPassthroughAct"; 49 private static final boolean DEBUG = false; 50 51 private static final int REQUEST_START_SETUP_ACTIVITY = 200; 52 53 private static ScanTimeoutMonitor sScanTimeoutMonitor; 54 55 private TvInputInfo mTvInputInfo; 56 private Intent mActivityAfterCompletion; 57 private boolean mEpgFetcherDuringScan; 58 59 @Override 60 public void onCreate(Bundle savedInstanceState) { 61 if (DEBUG) Log.d(TAG, "onCreate"); 62 super.onCreate(savedInstanceState); 63 ApplicationSingletons appSingletons = TvApplication.getSingletons(this); 64 TvInputManagerHelper inputManager = appSingletons.getTvInputManagerHelper(); 65 Intent intent = getIntent(); 66 String inputId = intent.getStringExtra(TvCommonConstants.EXTRA_INPUT_ID); 67 mTvInputInfo = inputManager.getTvInputInfo(inputId); 68 mActivityAfterCompletion = intent.getParcelableExtra( 69 TvCommonConstants.EXTRA_ACTIVITY_AFTER_COMPLETION); 70 boolean needToFetchEpg = Utils.isInternalTvInput(this, mTvInputInfo.getId()) 71 && Experiments.CLOUD_EPG.get(); 72 if (needToFetchEpg) { 73 // In case when the activity is restored, this flag should be restored as well. 74 mEpgFetcherDuringScan = true; 75 } 76 if (savedInstanceState == null) { 77 SoftPreconditions.checkState( 78 intent.getAction().equals(TvCommonConstants.INTENT_ACTION_INPUT_SETUP)); 79 if (DEBUG) Log.d(TAG, "TvInputId " + inputId + " / TvInputInfo " + mTvInputInfo); 80 if (mTvInputInfo == null) { 81 Log.w(TAG, "There is no input with the ID " + inputId + "."); 82 finish(); 83 return; 84 } 85 Intent setupIntent = 86 intent.getExtras().getParcelable(TvCommonConstants.EXTRA_SETUP_INTENT); 87 if (DEBUG) Log.d(TAG, "Setup activity launch intent: " + setupIntent); 88 if (setupIntent == null) { 89 Log.w(TAG, "The input (" + mTvInputInfo.getId() + ") doesn't have setup."); 90 finish(); 91 return; 92 } 93 SetupUtils.grantEpgPermission(this, mTvInputInfo.getServiceInfo().packageName); 94 if (DEBUG) Log.d(TAG, "Activity after completion " + mActivityAfterCompletion); 95 // If EXTRA_SETUP_INTENT is not removed, an infinite recursion happens during 96 // setupIntent.putExtras(intent.getExtras()). 97 Bundle extras = intent.getExtras(); 98 extras.remove(TvCommonConstants.EXTRA_SETUP_INTENT); 99 setupIntent.putExtras(extras); 100 try { 101 startActivityForResult(setupIntent, REQUEST_START_SETUP_ACTIVITY); 102 } catch (ActivityNotFoundException e) { 103 Log.e(TAG, "Can't find activity: " + setupIntent.getComponent()); 104 finish(); 105 return; 106 } 107 if (needToFetchEpg) { 108 if (sScanTimeoutMonitor == null) { 109 sScanTimeoutMonitor = new ScanTimeoutMonitor(this); 110 } 111 sScanTimeoutMonitor.startMonitoring(); 112 EpgFetcher.getInstance(this).onChannelScanStarted(); 113 } 114 } 115 } 116 117 @Override 118 public void onActivityResult(int requestCode, final int resultCode, final Intent data) { 119 if (DEBUG) Log.d(TAG, "onActivityResult"); 120 if (sScanTimeoutMonitor != null) { 121 sScanTimeoutMonitor.stopMonitoring(); 122 } 123 // Note: It's not guaranteed that this method is always called after scanning. 124 boolean setupComplete = requestCode == REQUEST_START_SETUP_ACTIVITY 125 && resultCode == Activity.RESULT_OK; 126 // Tells EpgFetcher that channel source setup is finished. 127 if (mEpgFetcherDuringScan) { 128 EpgFetcher.getInstance(this).onChannelScanFinished(); 129 } 130 if (!setupComplete) { 131 setResult(resultCode, data); 132 finish(); 133 return; 134 } 135 SetupUtils.getInstance(this).onTvInputSetupFinished(mTvInputInfo.getId(), new Runnable() { 136 @Override 137 public void run() { 138 if (mActivityAfterCompletion != null) { 139 try { 140 startActivity(mActivityAfterCompletion); 141 } catch (ActivityNotFoundException e) { 142 Log.w(TAG, "Activity launch failed", e); 143 } 144 } 145 setResult(resultCode, data); 146 finish(); 147 } 148 }); 149 } 150 151 /** 152 * Monitors the scan progress and notifies the timeout of the scanning. 153 * The purpose of this monitor is to call EpgFetcher.onChannelScanFinished() in case when 154 * SetupPassthroughActivity.onActivityResult() is not called properly. b/36008534 155 */ 156 @MainThread 157 private static class ScanTimeoutMonitor { 158 // Set timeout long enough. The message in Sony TV says the scanning takes about 30 minutes. 159 private static final long SCAN_TIMEOUT_MS = TimeUnit.MINUTES.toMillis(30); 160 161 private final Context mContext; 162 private final ChannelDataManager mChannelDataManager; 163 private final Handler mHandler = new Handler(Looper.getMainLooper()); 164 private final Runnable mScanTimeoutRunnable = new Runnable() { 165 @Override 166 public void run() { 167 Log.w(TAG, "No channels has been added for a while." + 168 " The scan might have finished unexpectedly."); 169 onScanTimedOut(); 170 } 171 }; 172 private final Listener mChannelDataManagerListener = new Listener() { 173 @Override 174 public void onLoadFinished() { 175 setupTimer(); 176 } 177 178 @Override 179 public void onChannelListUpdated() { 180 setupTimer(); 181 } 182 183 @Override 184 public void onChannelBrowsableChanged() { } 185 }; 186 private boolean mStarted; 187 188 private ScanTimeoutMonitor(Context context) { 189 mContext = context.getApplicationContext(); 190 mChannelDataManager = TvApplication.getSingletons(context).getChannelDataManager(); 191 } 192 193 private void startMonitoring() { 194 if (!mStarted) { 195 mStarted = true; 196 mChannelDataManager.addListener(mChannelDataManagerListener); 197 } 198 if (mChannelDataManager.isDbLoadFinished()) { 199 setupTimer(); 200 } 201 } 202 203 private void stopMonitoring() { 204 if (mStarted) { 205 mStarted = false; 206 mHandler.removeCallbacks(mScanTimeoutRunnable); 207 mChannelDataManager.removeListener(mChannelDataManagerListener); 208 } 209 } 210 211 private void setupTimer() { 212 mHandler.removeCallbacks(mScanTimeoutRunnable); 213 mHandler.postDelayed(mScanTimeoutRunnable, SCAN_TIMEOUT_MS); 214 } 215 216 private void onScanTimedOut() { 217 stopMonitoring(); 218 EpgFetcher.getInstance(mContext).onChannelScanFinished(); 219 } 220 } 221 } 222