17d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko/*
27d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko * Copyright (C) 2015 The Android Open Source Project
37d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko *
47d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko * Licensed under the Apache License, Version 2.0 (the "License");
57d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko * you may not use this file except in compliance with the License.
67d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko * You may obtain a copy of the License at
77d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko *
87d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko *      http://www.apache.org/licenses/LICENSE-2.0
97d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko *
107d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko * Unless required by applicable law or agreed to in writing, software
117d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko * distributed under the License is distributed on an "AS IS" BASIS,
127d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko * See the License for the specific language governing permissions and
147d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko * limitations under the License.
157d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko */
167d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko
177d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalkopackage com.android.tv;
187d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko
197d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalkoimport android.app.Activity;
207d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalkoimport android.content.ActivityNotFoundException;
217d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalkoimport android.content.Intent;
227d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalkoimport android.media.tv.TvInputInfo;
237d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalkoimport android.os.Bundle;
247d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalkoimport android.util.Log;
257d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko
2648dadb49248271b01997862e1335912a4f2e189fYoungsang Choimport com.android.tv.common.SoftPreconditions;
277d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalkoimport com.android.tv.common.TvCommonConstants;
287d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalkoimport com.android.tv.util.SetupUtils;
297d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalkoimport com.android.tv.util.TvInputManagerHelper;
307d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko
317d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko/**
327d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko * An activity to launch a TV input setup activity.
337d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko *
347d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko * <p> After setup activity is finished, all channels will be browsable.
357d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko */
367d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalkopublic class SetupPassthroughActivity extends Activity {
371abddd9f6225298066094e20a6c29061b6af4590Nick Chalko    private static final String TAG = "SetupPassthroughAct";
387d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko    private static final boolean DEBUG = false;
397d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko
407d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko    private static final int REQUEST_START_SETUP_ACTIVITY = 200;
417d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko
427d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko    private TvInputInfo mTvInputInfo;
437d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko    private Intent mActivityAfterCompletion;
447d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko
457d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko    @Override
467d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko    public void onCreate(Bundle savedInstanceState) {
477d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko        super.onCreate(savedInstanceState);
481abddd9f6225298066094e20a6c29061b6af4590Nick Chalko        Intent intent = getIntent();
491abddd9f6225298066094e20a6c29061b6af4590Nick Chalko        SoftPreconditions.checkState(
501abddd9f6225298066094e20a6c29061b6af4590Nick Chalko                intent.getAction().equals(TvCommonConstants.INTENT_ACTION_INPUT_SETUP));
511abddd9f6225298066094e20a6c29061b6af4590Nick Chalko        ApplicationSingletons appSingletons = TvApplication.getSingletons(this);
521abddd9f6225298066094e20a6c29061b6af4590Nick Chalko        TvInputManagerHelper inputManager = appSingletons.getTvInputManagerHelper();
531abddd9f6225298066094e20a6c29061b6af4590Nick Chalko        String inputId = intent.getStringExtra(TvCommonConstants.EXTRA_INPUT_ID);
547d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko        mTvInputInfo = inputManager.getTvInputInfo(inputId);
557d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko        if (DEBUG) Log.d(TAG, "TvInputId " + inputId + " / TvInputInfo " + mTvInputInfo);
567d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko        if (mTvInputInfo == null) {
577d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko            Log.w(TAG, "There is no input with the ID " + inputId + ".");
587d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko            finish();
597d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko            return;
607d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko        }
611abddd9f6225298066094e20a6c29061b6af4590Nick Chalko        Intent setupIntent = intent.getExtras().getParcelable(TvCommonConstants.EXTRA_SETUP_INTENT);
621abddd9f6225298066094e20a6c29061b6af4590Nick Chalko        if (DEBUG) Log.d(TAG, "Setup activity launch intent: " + setupIntent);
637d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko        if (setupIntent == null) {
647d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko            Log.w(TAG, "The input (" + mTvInputInfo.getId() + ") doesn't have setup.");
657d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko            finish();
667d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko            return;
677d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko        }
687d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko        SetupUtils.grantEpgPermission(this, mTvInputInfo.getServiceInfo().packageName);
691abddd9f6225298066094e20a6c29061b6af4590Nick Chalko        mActivityAfterCompletion = intent.getParcelableExtra(
707d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko                TvCommonConstants.EXTRA_ACTIVITY_AFTER_COMPLETION);
717d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko        if (DEBUG) Log.d(TAG, "Activity after completion " + mActivityAfterCompletion);
721abddd9f6225298066094e20a6c29061b6af4590Nick Chalko        // If EXTRA_SETUP_INTENT is not removed, an infinite recursion happens during
731abddd9f6225298066094e20a6c29061b6af4590Nick Chalko        // setupIntent.putExtras(intent.getExtras()).
741abddd9f6225298066094e20a6c29061b6af4590Nick Chalko        Bundle extras = intent.getExtras();
751abddd9f6225298066094e20a6c29061b6af4590Nick Chalko        extras.remove(TvCommonConstants.EXTRA_SETUP_INTENT);
761abddd9f6225298066094e20a6c29061b6af4590Nick Chalko        setupIntent.putExtras(extras);
777d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko        startActivityForResult(setupIntent, REQUEST_START_SETUP_ACTIVITY);
787d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko    }
797d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko
807d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko    @Override
811abddd9f6225298066094e20a6c29061b6af4590Nick Chalko    public void onActivityResult(int requestCode, final int resultCode, final Intent data) {
82ba5845f23b8fbc985890f892961abc8b39886611Nick Chalko        boolean setupComplete = requestCode == REQUEST_START_SETUP_ACTIVITY
83ba5845f23b8fbc985890f892961abc8b39886611Nick Chalko                && resultCode == Activity.RESULT_OK;
84ba5845f23b8fbc985890f892961abc8b39886611Nick Chalko        if (!setupComplete) {
851abddd9f6225298066094e20a6c29061b6af4590Nick Chalko            setResult(resultCode, data);
867d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko            finish();
877d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko            return;
887d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko        }
897d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko        SetupUtils.getInstance(this).onTvInputSetupFinished(mTvInputInfo.getId(), new Runnable() {
907d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko            @Override
917d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko            public void run() {
927d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko                if (mActivityAfterCompletion != null) {
937d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko                    try {
947d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko                        startActivity(mActivityAfterCompletion);
957d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko                    } catch (ActivityNotFoundException e) {
967d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko                        Log.w(TAG, "Activity launch failed", e);
977d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko                    }
987d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko                }
991abddd9f6225298066094e20a6c29061b6af4590Nick Chalko                setResult(resultCode, data);
1007d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko                finish();
1017d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko            }
1027d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko        });
1037d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko    }
1047d67089aa1e9aa2123c3cd2f386d7019a1544db1Nick Chalko}
105