1/*
2 * Copyright (C) 2016 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.setup;
18
19import android.app.Activity;
20import android.app.Fragment;
21import android.content.ActivityNotFoundException;
22import android.content.ComponentName;
23import android.content.Intent;
24import android.media.tv.TvInputInfo;
25import android.os.Bundle;
26import android.widget.Toast;
27
28import com.android.tv.ApplicationSingletons;
29import com.android.tv.R;
30import com.android.tv.SetupPassthroughActivity;
31import com.android.tv.common.TvCommonUtils;
32import com.android.tv.common.ui.setup.SetupActivity;
33import com.android.tv.common.ui.setup.SetupFragment;
34import com.android.tv.common.ui.setup.SetupMultiPaneFragment;
35import com.android.tv.TvApplication;
36import com.android.tv.data.ChannelDataManager;
37import com.android.tv.onboarding.SetupSourcesFragment;
38import com.android.tv.util.OnboardingUtils;
39import com.android.tv.util.SetupUtils;
40import com.android.tv.util.TvInputManagerHelper;
41
42/**
43 * A activity to start input sources setup fragment for initial setup flow.
44 */
45public class SystemSetupActivity extends SetupActivity {
46    private static final String SYSTEM_SETUP =
47            "com.android.tv.action.LAUNCH_SYSTEM_SETUP";
48    private static final int SHOW_RIPPLE_DURATION_MS = 266;
49    private static final int REQUEST_CODE_START_SETUP_ACTIVITY = 1;
50
51    private TvInputManagerHelper mInputManager;
52
53    @Override
54    protected void onCreate(Bundle savedInstanceState) {
55        super.onCreate(savedInstanceState);
56        Intent intent = getIntent();
57        if (!SYSTEM_SETUP.equals(intent.getAction())) {
58            finish();
59            return;
60        }
61        ApplicationSingletons singletons = TvApplication.getSingletons(this);
62        mInputManager = singletons.getTvInputManagerHelper();
63    }
64
65    @Override
66    protected Fragment onCreateInitialFragment() {
67        return new SetupSourcesFragment();
68    }
69
70    private void showMerchantCollection() {
71        executeActionWithDelay(new Runnable() {
72            @Override
73            public void run() {
74                startActivity(OnboardingUtils.ONLINE_STORE_INTENT);
75            }
76        }, SHOW_RIPPLE_DURATION_MS);
77    }
78
79    @Override
80    public boolean executeAction(String category, int actionId, Bundle params) {
81        switch (category) {
82            case SetupSourcesFragment.ACTION_CATEGORY:
83                switch (actionId) {
84                    case SetupSourcesFragment.ACTION_ONLINE_STORE:
85                        showMerchantCollection();
86                        return true;
87                    case SetupSourcesFragment.ACTION_SETUP_INPUT: {
88                        String inputId = params.getString(
89                                SetupSourcesFragment.ACTION_PARAM_KEY_INPUT_ID);
90                        TvInputInfo input = mInputManager.getTvInputInfo(inputId);
91                        Intent intent = TvCommonUtils.createSetupIntent(input);
92                        if (intent == null) {
93                            Toast.makeText(this, R.string.msg_no_setup_activity, Toast.LENGTH_SHORT)
94                                    .show();
95                            return true;
96                        }
97                        // Even though other app can handle the intent, the setup launched by Live
98                        // channels should go through Live channels SetupPassthroughActivity.
99                        intent.setComponent(new ComponentName(this,
100                                SetupPassthroughActivity.class));
101                        try {
102                            // Now we know that the user intends to set up this input. Grant
103                            // permission for writing EPG data.
104                            SetupUtils.grantEpgPermission(this, input.getServiceInfo().packageName);
105                            startActivityForResult(intent, REQUEST_CODE_START_SETUP_ACTIVITY);
106                        } catch (ActivityNotFoundException e) {
107                            Toast.makeText(this,
108                                    getString(R.string.msg_unable_to_start_setup_activity,
109                                            input.loadLabel(this)), Toast.LENGTH_SHORT).show();
110                        }
111                        return true;
112                    }
113                    case SetupMultiPaneFragment.ACTION_DONE: {
114                        // To make sure user can finish setup flow, set result as RESULT_OK.
115                        setResult(Activity.RESULT_OK);
116                        finish();
117                        return true;
118                    }
119                }
120                break;
121        }
122        return false;
123    }
124}
125