roger xue | b65a076 | 2017-03-24 13:20:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | package com.android.car.settings.common; |
| 17 | |
Lujiang Xue | eaff6c3 | 2018-04-10 08:20:29 -0700 | [diff] [blame^] | 18 | import android.car.drivingstate.CarUxRestrictions; |
Lujiang Xue | efc2c5f | 2017-12-19 13:16:13 -0800 | [diff] [blame] | 19 | import android.content.Context; |
Lujiang Xue | 0daa919 | 2017-12-06 11:48:39 -0800 | [diff] [blame] | 20 | import android.content.Intent; |
roger xue | b65a076 | 2017-03-24 13:20:14 -0700 | [diff] [blame] | 21 | import android.os.Bundle; |
roger xue | 24b5080 | 2017-04-04 11:59:04 -0700 | [diff] [blame] | 22 | import android.support.v7.app.AppCompatActivity; |
| 23 | import android.support.v7.widget.Toolbar; |
Lujiang Xue | efc2c5f | 2017-12-19 13:16:13 -0800 | [diff] [blame] | 24 | import android.view.inputmethod.InputMethodManager; |
roger xue | b65a076 | 2017-03-24 13:20:14 -0700 | [diff] [blame] | 25 | |
roger xue | ddb7544 | 2017-03-29 09:27:04 -0700 | [diff] [blame] | 26 | import com.android.car.settings.R; |
Lujiang Xue | eaff6c3 | 2018-04-10 08:20:29 -0700 | [diff] [blame^] | 27 | import com.android.car.settings.quicksettings.QuickSettingFragment; |
roger xue | b65a076 | 2017-03-24 13:20:14 -0700 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Base activity class for car settings, provides a action bar with a back button that goes to |
| 31 | * previous activity. |
| 32 | */ |
roger xue | 24b5080 | 2017-04-04 11:59:04 -0700 | [diff] [blame] | 33 | public class CarSettingActivity extends AppCompatActivity implements |
| 34 | BaseFragment.FragmentController { |
Lujiang Xue | 0daa919 | 2017-12-06 11:48:39 -0800 | [diff] [blame] | 35 | private static final String TAG = "CarSetting"; |
| 36 | |
Lujiang Xue | eaff6c3 | 2018-04-10 08:20:29 -0700 | [diff] [blame^] | 37 | private CarUxRestrictionsHelper mUxRestrictionsHelper; |
| 38 | private CarUxRestrictions mCarUxRestrictions; |
Lujiang Xue | 73f4573 | 2018-04-05 09:42:19 -0700 | [diff] [blame] | 39 | |
roger xue | b65a076 | 2017-03-24 13:20:14 -0700 | [diff] [blame] | 40 | @Override |
| 41 | protected void onCreate(Bundle savedInstanceState) { |
| 42 | super.onCreate(savedInstanceState); |
roger xue | 24b5080 | 2017-04-04 11:59:04 -0700 | [diff] [blame] | 43 | setContentView(R.layout.app_compat_activity); |
| 44 | Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); |
| 45 | setSupportActionBar(toolbar); |
Lujiang Xue | 0daa919 | 2017-12-06 11:48:39 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public void onStart() { |
| 50 | super.onStart(); |
Lujiang Xue | eaff6c3 | 2018-04-10 08:20:29 -0700 | [diff] [blame^] | 51 | mUxRestrictionsHelper = |
| 52 | new CarUxRestrictionsHelper(this, carUxRestrictions -> { |
| 53 | mCarUxRestrictions = carUxRestrictions; |
| 54 | BaseFragment currentFragment = getCurrentFragment(); |
| 55 | if (currentFragment != null) { |
| 56 | currentFragment.setCarUxRestrictions(carUxRestrictions); |
| 57 | } |
| 58 | }); |
| 59 | mUxRestrictionsHelper.start(); |
| 60 | QuickSettingFragment qs = QuickSettingFragment.newInstance(); |
| 61 | launchFragment(qs); |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public void onStop() { |
| 66 | super.onStop(); |
| 67 | mUxRestrictionsHelper.stop(); |
Lujiang Xue | 0daa919 | 2017-12-06 11:48:39 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public void onNewIntent(Intent intent) { |
| 72 | setIntent(intent); |
roger xue | b65a076 | 2017-03-24 13:20:14 -0700 | [diff] [blame] | 73 | } |
| 74 | |
roger xue | 24b5080 | 2017-04-04 11:59:04 -0700 | [diff] [blame] | 75 | @Override |
| 76 | public void launchFragment(BaseFragment fragment) { |
Lujiang Xue | eaff6c3 | 2018-04-10 08:20:29 -0700 | [diff] [blame^] | 77 | if (mCarUxRestrictions != null && !fragment.canBeShown(mCarUxRestrictions)) { |
| 78 | DoBlockingDialogFragment alertDialog = new DoBlockingDialogFragment(); |
| 79 | alertDialog.show(getSupportFragmentManager(), DoBlockingDialogFragment.DIALOG_TAG); |
| 80 | return; |
| 81 | } |
| 82 | if (mCarUxRestrictions != null) { |
| 83 | fragment.setCarUxRestrictions(mCarUxRestrictions); |
| 84 | } |
roger xue | 24b5080 | 2017-04-04 11:59:04 -0700 | [diff] [blame] | 85 | fragment.setFragmentController(this); |
Anthony Chen | 8907048 | 2017-11-30 14:33:46 -0800 | [diff] [blame] | 86 | getSupportFragmentManager() |
roger xue | 24b5080 | 2017-04-04 11:59:04 -0700 | [diff] [blame] | 87 | .beginTransaction() |
| 88 | .setCustomAnimations( |
| 89 | R.animator.trans_right_in , |
| 90 | R.animator.trans_left_out, |
| 91 | R.animator.trans_left_in, |
| 92 | R.animator.trans_right_out) |
| 93 | .replace(R.id.fragment_container, fragment) |
| 94 | .addToBackStack(null) |
| 95 | .commit(); |
| 96 | } |
| 97 | |
| 98 | @Override |
Lujiang Xue | 73f4573 | 2018-04-05 09:42:19 -0700 | [diff] [blame] | 99 | public void goBack() { |
| 100 | onBackPressed(); |
roger xue | 24b5080 | 2017-04-04 11:59:04 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void onBackPressed() { |
Lujiang Xue | 73f4573 | 2018-04-05 09:42:19 -0700 | [diff] [blame] | 105 | super.onBackPressed(); |
| 106 | hideKeyboard(); |
| 107 | // if the backstack is empty, finish the activity. |
| 108 | if (getSupportFragmentManager().getBackStackEntryCount() == 0) { |
| 109 | finish(); |
roger xue | 24b5080 | 2017-04-04 11:59:04 -0700 | [diff] [blame] | 110 | } |
roger xue | b65a076 | 2017-03-24 13:20:14 -0700 | [diff] [blame] | 111 | } |
Lujiang Xue | efc2c5f | 2017-12-19 13:16:13 -0800 | [diff] [blame] | 112 | |
Lujiang Xue | eaff6c3 | 2018-04-10 08:20:29 -0700 | [diff] [blame^] | 113 | private BaseFragment getCurrentFragment() { |
| 114 | return (BaseFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_container); |
| 115 | } |
| 116 | |
Lujiang Xue | efc2c5f | 2017-12-19 13:16:13 -0800 | [diff] [blame] | 117 | private void hideKeyboard() { |
| 118 | InputMethodManager imm = (InputMethodManager)this.getSystemService( |
| 119 | Context.INPUT_METHOD_SERVICE); |
| 120 | imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0); |
| 121 | } |
roger xue | b65a076 | 2017-03-24 13:20:14 -0700 | [diff] [blame] | 122 | } |