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;
18
19import android.app.Activity;
20import android.content.ActivityNotFoundException;
21import android.content.Intent;
22import android.media.tv.TvInputInfo;
23import android.os.Bundle;
24import android.util.Log;
25
26import com.android.tv.common.SoftPreconditions;
27import com.android.tv.common.TvCommonConstants;
28import com.android.tv.data.epg.EpgFetcher;
29import com.android.tv.experiments.Experiments;
30import com.android.tv.util.SetupUtils;
31import com.android.tv.util.TvInputManagerHelper;
32import com.android.tv.util.Utils;
33
34/**
35 * An activity to launch a TV input setup activity.
36 *
37 * <p> After setup activity is finished, all channels will be browsable.
38 */
39public class SetupPassthroughActivity extends Activity {
40    private static final String TAG = "SetupPassthroughAct";
41    private static final boolean DEBUG = false;
42
43    private static final int REQUEST_START_SETUP_ACTIVITY = 200;
44
45    private TvInputInfo mTvInputInfo;
46    private Intent mActivityAfterCompletion;
47
48    @Override
49    public void onCreate(Bundle savedInstanceState) {
50        super.onCreate(savedInstanceState);
51        Intent intent = getIntent();
52        SoftPreconditions.checkState(
53                intent.getAction().equals(TvCommonConstants.INTENT_ACTION_INPUT_SETUP));
54        ApplicationSingletons appSingletons = TvApplication.getSingletons(this);
55        TvInputManagerHelper inputManager = appSingletons.getTvInputManagerHelper();
56        String inputId = intent.getStringExtra(TvCommonConstants.EXTRA_INPUT_ID);
57        mTvInputInfo = inputManager.getTvInputInfo(inputId);
58        if (DEBUG) Log.d(TAG, "TvInputId " + inputId + " / TvInputInfo " + mTvInputInfo);
59        if (mTvInputInfo == null) {
60            Log.w(TAG, "There is no input with the ID " + inputId + ".");
61            finish();
62            return;
63        }
64        Intent setupIntent = intent.getExtras().getParcelable(TvCommonConstants.EXTRA_SETUP_INTENT);
65        if (DEBUG) Log.d(TAG, "Setup activity launch intent: " + setupIntent);
66        if (setupIntent == null) {
67            Log.w(TAG, "The input (" + mTvInputInfo.getId() + ") doesn't have setup.");
68            finish();
69            return;
70        }
71        SetupUtils.grantEpgPermission(this, mTvInputInfo.getServiceInfo().packageName);
72        mActivityAfterCompletion = intent.getParcelableExtra(
73                TvCommonConstants.EXTRA_ACTIVITY_AFTER_COMPLETION);
74        if (DEBUG) Log.d(TAG, "Activity after completion " + mActivityAfterCompletion);
75        // If EXTRA_SETUP_INTENT is not removed, an infinite recursion happens during
76        // setupIntent.putExtras(intent.getExtras()).
77        Bundle extras = intent.getExtras();
78        extras.remove(TvCommonConstants.EXTRA_SETUP_INTENT);
79        setupIntent.putExtras(extras);
80        try {
81            startActivityForResult(setupIntent, REQUEST_START_SETUP_ACTIVITY);
82        } catch (ActivityNotFoundException e) {
83            Log.e(TAG, "Can't find activity: " + setupIntent.getComponent());
84            finish();
85            return;
86        }
87        if (Utils.isInternalTvInput(this, mTvInputInfo.getId()) && Experiments.CLOUD_EPG.get()) {
88            EpgFetcher.getInstance(this).stop();
89        }
90    }
91
92    @Override
93    protected void onDestroy() {
94        if (mTvInputInfo != null && Utils.isInternalTvInput(this, mTvInputInfo.getId())
95                && Experiments.CLOUD_EPG.get()) {
96            EpgFetcher.getInstance(this).start();
97        }
98        super.onDestroy();
99    }
100
101    @Override
102    public void onActivityResult(int requestCode, final int resultCode, final Intent data) {
103        boolean setupComplete = requestCode == REQUEST_START_SETUP_ACTIVITY
104                && resultCode == Activity.RESULT_OK;
105        if (!setupComplete) {
106            setResult(resultCode, data);
107            finish();
108            return;
109        }
110        SetupUtils.getInstance(this).onTvInputSetupFinished(mTvInputInfo.getId(), new Runnable() {
111            @Override
112            public void run() {
113                if (mActivityAfterCompletion != null) {
114                    try {
115                        startActivity(mActivityAfterCompletion);
116                    } catch (ActivityNotFoundException e) {
117                        Log.w(TAG, "Activity launch failed", e);
118                    }
119                }
120                setResult(resultCode, data);
121                finish();
122            }
123        });
124    }
125}
126