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.tuner.setup; 18 19 import android.app.FragmentManager; 20 import android.os.AsyncTask; 21 import android.view.KeyEvent; 22 import com.android.tv.tuner.TunerHal; 23 24 /** An activity that serves tuner setup process. */ 25 public class LiveTvTunerSetupActivity extends BaseTunerSetupActivity { 26 private static final String TAG = "LiveTvTunerSetupActivity"; 27 28 @Override 29 protected void executeGetTunerTypeAndCountAsyncTask() { 30 new AsyncTask<Void, Void, Integer>() { 31 @Override 32 protected Integer doInBackground(Void... arg0) { 33 return TunerHal.getTunerTypeAndCount(LiveTvTunerSetupActivity.this).first; 34 } 35 36 @Override 37 protected void onPostExecute(Integer result) { 38 if (!LiveTvTunerSetupActivity.this.isDestroyed()) { 39 mTunerType = result; 40 if (result == null) { 41 finish(); 42 } else if (!mActivityStopped) { 43 showInitialFragment(); 44 } else { 45 mPendingShowInitialFragment = true; 46 } 47 } 48 } 49 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); 50 } 51 52 @Override 53 public boolean onKeyUp(int keyCode, KeyEvent event) { 54 if (keyCode == KeyEvent.KEYCODE_BACK) { 55 FragmentManager manager = getFragmentManager(); 56 int count = manager.getBackStackEntryCount(); 57 if (count > 0) { 58 String lastTag = manager.getBackStackEntryAt(count - 1).getName(); 59 if (ScanResultFragment.class.getCanonicalName().equals(lastTag) && count >= 2) { 60 String secondLastTag = manager.getBackStackEntryAt(count - 2).getName(); 61 if (ScanFragment.class.getCanonicalName().equals(secondLastTag)) { 62 // Pops fragment including ScanFragment. 63 manager.popBackStack( 64 secondLastTag, FragmentManager.POP_BACK_STACK_INCLUSIVE); 65 return true; 66 } 67 } else if (ScanFragment.class.getCanonicalName().equals(lastTag)) { 68 mLastScanFragment.finishScan(true); 69 return true; 70 } 71 } 72 } 73 return super.onKeyUp(keyCode, event); 74 } 75 } 76