SetupPassthroughActivity.java revision 1abddd9f6225298066094e20a6c29061b6af4590
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.TvCommonConstants;
27import com.android.tv.util.SetupUtils;
28import com.android.tv.util.SoftPreconditions;
29import com.android.tv.util.TvInputManagerHelper;
30
31/**
32 * An activity to launch a TV input setup activity.
33 *
34 * <p> After setup activity is finished, all channels will be browsable.
35 */
36public class SetupPassthroughActivity extends Activity {
37    private static final String TAG = "SetupPassthroughAct";
38    private static final boolean DEBUG = false;
39
40    private static final int REQUEST_START_SETUP_ACTIVITY = 200;
41
42    private TvInputInfo mTvInputInfo;
43    private Intent mActivityAfterCompletion;
44
45    @Override
46    public void onCreate(Bundle savedInstanceState) {
47        super.onCreate(savedInstanceState);
48        Intent intent = getIntent();
49        SoftPreconditions.checkState(
50                intent.getAction().equals(TvCommonConstants.INTENT_ACTION_INPUT_SETUP));
51        ApplicationSingletons appSingletons = TvApplication.getSingletons(this);
52        TvInputManagerHelper inputManager = appSingletons.getTvInputManagerHelper();
53        String inputId = intent.getStringExtra(TvCommonConstants.EXTRA_INPUT_ID);
54        mTvInputInfo = inputManager.getTvInputInfo(inputId);
55        if (DEBUG) Log.d(TAG, "TvInputId " + inputId + " / TvInputInfo " + mTvInputInfo);
56        if (mTvInputInfo == null) {
57            Log.w(TAG, "There is no input with the ID " + inputId + ".");
58            finish();
59            return;
60        }
61        Intent setupIntent = intent.getExtras().getParcelable(TvCommonConstants.EXTRA_SETUP_INTENT);
62        if (DEBUG) Log.d(TAG, "Setup activity launch intent: " + setupIntent);
63        if (setupIntent == null) {
64            Log.w(TAG, "The input (" + mTvInputInfo.getId() + ") doesn't have setup.");
65            finish();
66            return;
67        }
68        SetupUtils.grantEpgPermission(this, mTvInputInfo.getServiceInfo().packageName);
69        mActivityAfterCompletion = intent.getParcelableExtra(
70                TvCommonConstants.EXTRA_ACTIVITY_AFTER_COMPLETION);
71        if (DEBUG) Log.d(TAG, "Activity after completion " + mActivityAfterCompletion);
72        // If EXTRA_SETUP_INTENT is not removed, an infinite recursion happens during
73        // setupIntent.putExtras(intent.getExtras()).
74        Bundle extras = intent.getExtras();
75        extras.remove(TvCommonConstants.EXTRA_SETUP_INTENT);
76        setupIntent.putExtras(extras);
77        startActivityForResult(setupIntent, REQUEST_START_SETUP_ACTIVITY);
78    }
79
80    @Override
81    public void onActivityResult(int requestCode, final int resultCode, final Intent data) {
82        if (requestCode != REQUEST_START_SETUP_ACTIVITY || resultCode != Activity.RESULT_OK) {
83            setResult(resultCode, data);
84            finish();
85            return;
86        }
87        SetupUtils.getInstance(this).onTvInputSetupFinished(mTvInputInfo.getId(), new Runnable() {
88            @Override
89            public void run() {
90                if (mActivityAfterCompletion != null) {
91                    try {
92                        startActivity(mActivityAfterCompletion);
93                    } catch (ActivityNotFoundException e) {
94                        Log.w(TAG, "Activity launch failed", e);
95                    }
96                }
97                setResult(resultCode, data);
98                finish();
99            }
100        });
101    }
102}
103