Home | History | Annotate | Download | only in onboarding
      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.onboarding;
     18 
     19 import android.app.Fragment;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.content.pm.PackageManager;
     23 import android.os.Build;
     24 import android.os.Bundle;
     25 import android.support.annotation.NonNull;
     26 import android.widget.Toast;
     27 
     28 import com.android.tv.R;
     29 import com.android.tv.TvApplication;
     30 import com.android.tv.common.ui.setup.SetupActivity;
     31 import com.android.tv.common.ui.setup.SetupMultiPaneFragment;
     32 import com.android.tv.data.ChannelDataManager;
     33 import com.android.tv.util.OnboardingUtils;
     34 import com.android.tv.util.PermissionUtils;
     35 import com.android.tv.util.SetupUtils;
     36 
     37 public class OnboardingActivity extends SetupActivity {
     38     private static final String KEY_INTENT_AFTER_COMPLETION = "key_intent_after_completion";
     39 
     40     private static final int PERMISSIONS_REQUEST_READ_TV_LISTINGS = 1;
     41     private static final String PERMISSION_READ_TV_LISTINGS = "android.permission.READ_TV_LISTINGS";
     42 
     43     private static final int SHOW_RIPPLE_DURATION_MS = 266;
     44 
     45     private ChannelDataManager mChannelDataManager;
     46     private final ChannelDataManager.Listener mChannelListener = new ChannelDataManager.Listener() {
     47         @Override
     48         public void onLoadFinished() {
     49             mChannelDataManager.removeListener(this);
     50             SetupUtils.getInstance(OnboardingActivity.this).markNewChannelsBrowsable();
     51         }
     52 
     53         @Override
     54         public void onChannelListUpdated() { }
     55 
     56         @Override
     57         public void onChannelBrowsableChanged() { }
     58     };
     59 
     60     /**
     61      * Returns an intent to start {@link OnboardingActivity}.
     62      *
     63      * @param context context to create an intent. Should not be {@code null}.
     64      * @param intentAfterCompletion intent which will be used to start a new activity when this
     65      * activity finishes. Should not be {@code null}.
     66      */
     67     public static Intent buildIntent(@NonNull Context context,
     68             @NonNull Intent intentAfterCompletion) {
     69         return new Intent(context, OnboardingActivity.class)
     70                 .putExtra(OnboardingActivity.KEY_INTENT_AFTER_COMPLETION, intentAfterCompletion);
     71     }
     72 
     73     @Override
     74     protected void onCreate(Bundle savedInstanceState) {
     75         super.onCreate(savedInstanceState);
     76         if (!PermissionUtils.hasAccessAllEpg(this)) {
     77             if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
     78                 Toast.makeText(this, R.string.msg_not_supported_device, Toast.LENGTH_LONG).show();
     79                 finish();
     80                 return;
     81             } else if (checkSelfPermission(PERMISSION_READ_TV_LISTINGS)
     82                     != PackageManager.PERMISSION_GRANTED) {
     83                 requestPermissions(new String[]{PERMISSION_READ_TV_LISTINGS},
     84                         PERMISSIONS_REQUEST_READ_TV_LISTINGS);
     85             }
     86         }
     87     }
     88 
     89     @Override
     90     protected void onDestroy() {
     91         if (mChannelDataManager != null) {
     92             mChannelDataManager.removeListener(mChannelListener);
     93         }
     94         super.onDestroy();
     95     }
     96 
     97     @Override
     98     protected Fragment onCreateInitialFragment() {
     99         if (PermissionUtils.hasAccessAllEpg(this) || PermissionUtils.hasReadTvListings(this)) {
    100             // Make the channels of the new inputs which have been setup outside Live TV
    101             // browsable.
    102             mChannelDataManager = TvApplication.getSingletons(this).getChannelDataManager();
    103             if (mChannelDataManager.isDbLoadFinished()) {
    104                 SetupUtils.getInstance(this).markNewChannelsBrowsable();
    105             } else {
    106                 mChannelDataManager.addListener(mChannelListener);
    107             }
    108             return OnboardingUtils.isFirstRunWithCurrentVersion(this) ? new WelcomeFragment()
    109                     : new SetupSourcesFragment();
    110         }
    111         return null;
    112     }
    113 
    114     @Override
    115     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
    116             @NonNull int[] grantResults) {
    117         if (requestCode == PERMISSIONS_REQUEST_READ_TV_LISTINGS) {
    118             if (grantResults != null && grantResults.length > 0
    119                     && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
    120                 finish();
    121                 Intent intentForNextActivity = getIntent().getParcelableExtra(
    122                         KEY_INTENT_AFTER_COMPLETION);
    123                 startActivity(buildIntent(this, intentForNextActivity));
    124             } else {
    125                 Toast.makeText(this, R.string.msg_read_tv_listing_permission_denied,
    126                         Toast.LENGTH_LONG).show();
    127                 finish();
    128             }
    129         }
    130     }
    131 
    132     void finishActivity() {
    133         Intent intentForNextActivity = getIntent().getParcelableExtra(
    134                 KEY_INTENT_AFTER_COMPLETION);
    135         if (intentForNextActivity != null) {
    136             startActivity(intentForNextActivity);
    137         }
    138         finish();
    139     }
    140 
    141     void showMerchantCollection() {
    142         executeActionWithDelay(new Runnable() {
    143             @Override
    144             public void run() {
    145                 startActivity(OnboardingUtils.PLAY_STORE_INTENT);
    146             }
    147         }, SHOW_RIPPLE_DURATION_MS);
    148     }
    149 
    150     @Override
    151     protected void executeAction(String category, int actionId) {
    152         switch (category) {
    153             case WelcomeFragment.ACTION_CATEGORY:
    154                 switch (actionId) {
    155                     case WelcomeFragment.ACTION_NEXT:
    156                         OnboardingUtils.setFirstRunWithCurrentVersionCompleted(
    157                                 OnboardingActivity.this);
    158                         showFragment(new SetupSourcesFragment(), false);
    159                         break;
    160                 }
    161                 break;
    162             case SetupSourcesFragment.ACTION_CATEGORY:
    163                 switch (actionId) {
    164                     case SetupSourcesFragment.ACTION_PLAY_STORE:
    165                         showMerchantCollection();
    166                         break;
    167                     case SetupMultiPaneFragment.ACTION_DONE: {
    168                         ChannelDataManager manager = TvApplication.getSingletons(
    169                                 OnboardingActivity.this).getChannelDataManager();
    170                         if (manager.getChannelCount() == 0) {
    171                             finish();
    172                         } else {
    173                             finishActivity();
    174                         }
    175                         break;
    176                     }
    177                 }
    178                 break;
    179         }
    180     }
    181 }
    182