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.EpgInputAllowList;
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 EpgInputAllowList mEpgInputAllowList;
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                     Log.w(
124                             TAG,
125                             "Calling activity "
126                                     + callingActivity.getPackageName()
127                                     + " is not trusted. Not forwarding intent.");
128                     finish();
129                     return;
130                 }
131                 startActivityForResult(setupIntent, REQUEST_START_SETUP_ACTIVITY);
132             } catch (ActivityNotFoundException e) {
133                 Log.e(TAG, "Can't find activity: " + setupIntent.getComponent());
134                 finish();
135                 return;
136             }
137             if (needToFetchEpg) {
138                 if (sScanTimeoutMonitor == null) {
139                     sScanTimeoutMonitor = new ScanTimeoutMonitor(mEpgFetcher, mChannelDataManager);
140                 }
141                 sScanTimeoutMonitor.startMonitoring();
142                 mEpgFetcher.onChannelScanStarted();
143             }
144         }
145     }
146 
147     @Override
onActivityResult(int requestCode, final int resultCode, final Intent data)148     public void onActivityResult(int requestCode, final int resultCode, final Intent data) {
149         if (DEBUG)
150             Log.d(TAG, "onActivityResult(" + requestCode + ",  " + resultCode + ",  " + data + ")");
151         if (sScanTimeoutMonitor != null) {
152             sScanTimeoutMonitor.stopMonitoring();
153         }
154         // Note: It's not guaranteed that this method is always called after scanning.
155         boolean setupComplete =
156                 requestCode == REQUEST_START_SETUP_ACTIVITY && resultCode == Activity.RESULT_OK;
157         // Tells EpgFetcher that channel source setup is finished.
158 
159         if (mEpgFetcherDuringScan) {
160             mEpgFetcher.onChannelScanFinished();
161         }
162         if (!setupComplete) {
163             setResult(resultCode, data);
164             finish();
165             return;
166         }
167         if (TvFeatures.CLOUD_EPG_FOR_3RD_PARTY.isEnabled(this)
168                 && data != null
169                 && data.getBooleanExtra(EpgContract.EXTRA_USE_CLOUD_EPG, false)) {
170             if (DEBUG) Log.d(TAG, "extra " + data.getExtras());
171             String inputId = data.getStringExtra(TvInputInfo.EXTRA_INPUT_ID);
172             if (mEpgInputAllowList.isInputAllowed(inputId)) {
173                 mEpgFetcher.fetchImmediately();
174             }
175         }
176 
177         if (mTvInputInfo == null) {
178             Log.w(
179                     TAG,
180                     "There is no input with ID "
181                             + getIntent().getStringExtra(InputSetupActionUtils.EXTRA_INPUT_ID)
182                             + ".");
183             setResult(resultCode, data);
184             finish();
185             return;
186         }
187         mSetupUtils.onTvInputSetupFinished(
188                 mTvInputInfo.getId(),
189                 () -> {
190                     if (mActivityAfterCompletion != null) {
191                         try {
192                             startActivity(mActivityAfterCompletion);
193                         } catch (ActivityNotFoundException e) {
194                             Log.w(TAG, "Activity launch failed", e);
195                         }
196                     }
197                     setResult(resultCode, data);
198                     finish();
199                 });
200     }
201 
202     /**
203      * Monitors the scan progress and notifies the timeout of the scanning. The purpose of this
204      * monitor is to call EpgFetcher.onChannelScanFinished() in case when
205      * SetupPassthroughActivity.onActivityResult() is not called properly. b/36008534
206      */
207     @MainThread
208     private static class ScanTimeoutMonitor {
209         // Set timeout long enough. The message in Sony TV says the scanning takes about 30 minutes.
210         private static final long SCAN_TIMEOUT_MS = TimeUnit.MINUTES.toMillis(30);
211 
212         private final EpgFetcher mEpgFetcher;
213         private final ChannelDataManager mChannelDataManager;
214         private final Handler mHandler = new Handler(Looper.getMainLooper());
215         private final Runnable mScanTimeoutRunnable =
216                 () -> {
217                     Log.w(
218                             TAG,
219                             "No channels has been added for a while."
220                                     + " The scan might have finished unexpectedly.");
221                     onScanTimedOut();
222                 };
223         private final Listener mChannelDataManagerListener =
224                 new Listener() {
225                     @Override
226                     public void onLoadFinished() {
227                         setupTimer();
228                     }
229 
230                     @Override
231                     public void onChannelListUpdated() {
232                         setupTimer();
233                     }
234 
235                     @Override
236                     public void onChannelBrowsableChanged() {}
237                 };
238         private boolean mStarted;
239 
ScanTimeoutMonitor(EpgFetcher epgFetcher, ChannelDataManager mChannelDataManager)240         private ScanTimeoutMonitor(EpgFetcher epgFetcher, ChannelDataManager mChannelDataManager) {
241             mEpgFetcher = epgFetcher;
242             this.mChannelDataManager = mChannelDataManager;
243         }
244 
startMonitoring()245         private void startMonitoring() {
246             if (!mStarted) {
247                 mStarted = true;
248                 mChannelDataManager.addListener(mChannelDataManagerListener);
249             }
250             if (mChannelDataManager.isDbLoadFinished()) {
251                 setupTimer();
252             }
253         }
254 
stopMonitoring()255         private void stopMonitoring() {
256             if (mStarted) {
257                 mStarted = false;
258                 mHandler.removeCallbacks(mScanTimeoutRunnable);
259                 mChannelDataManager.removeListener(mChannelDataManagerListener);
260             }
261         }
262 
setupTimer()263         private void setupTimer() {
264             mHandler.removeCallbacks(mScanTimeoutRunnable);
265             mHandler.postDelayed(mScanTimeoutRunnable, SCAN_TIMEOUT_MS);
266         }
267 
onScanTimedOut()268         private void onScanTimedOut() {
269             stopMonitoring();
270             mEpgFetcher.onChannelScanFinished();
271         }
272     }
273 
274     /** Exports {@link MainActivity} for Dagger codegen to create the appropriate injector. */
275     @dagger.Module
276     public abstract static class Module {
277         @ContributesAndroidInjector
contributesSetupPassthroughActivity()278         abstract SetupPassthroughActivity contributesSetupPassthroughActivity();
279     }
280 }
281