blob: 222cd171c2abef98751d821790827edd716c9729 [file] [log] [blame]
roger xueb65a0762017-03-24 13:20:14 -07001/*
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 */
16package com.android.car.settings.common;
17
Lujiang Xueeaff6c32018-04-10 08:20:29 -070018import android.car.drivingstate.CarUxRestrictions;
Lujiang Xueefc2c5f2017-12-19 13:16:13 -080019import android.content.Context;
Lujiang Xue0daa9192017-12-06 11:48:39 -080020import android.content.Intent;
roger xueb65a0762017-03-24 13:20:14 -070021import android.os.Bundle;
roger xue24b50802017-04-04 11:59:04 -070022import android.support.v7.app.AppCompatActivity;
23import android.support.v7.widget.Toolbar;
Lujiang Xueefc2c5f2017-12-19 13:16:13 -080024import android.view.inputmethod.InputMethodManager;
roger xueb65a0762017-03-24 13:20:14 -070025
roger xueddb75442017-03-29 09:27:04 -070026import com.android.car.settings.R;
Lujiang Xueeaff6c32018-04-10 08:20:29 -070027import com.android.car.settings.quicksettings.QuickSettingFragment;
roger xueb65a0762017-03-24 13:20:14 -070028
29/**
30 * Base activity class for car settings, provides a action bar with a back button that goes to
31 * previous activity.
32 */
roger xue24b50802017-04-04 11:59:04 -070033public class CarSettingActivity extends AppCompatActivity implements
34 BaseFragment.FragmentController {
Lujiang Xue0daa9192017-12-06 11:48:39 -080035 private static final String TAG = "CarSetting";
36
Lujiang Xueeaff6c32018-04-10 08:20:29 -070037 private CarUxRestrictionsHelper mUxRestrictionsHelper;
38 private CarUxRestrictions mCarUxRestrictions;
Lujiang Xue73f45732018-04-05 09:42:19 -070039
roger xueb65a0762017-03-24 13:20:14 -070040 @Override
41 protected void onCreate(Bundle savedInstanceState) {
42 super.onCreate(savedInstanceState);
roger xue24b50802017-04-04 11:59:04 -070043 setContentView(R.layout.app_compat_activity);
44 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
45 setSupportActionBar(toolbar);
Lujiang Xue0daa9192017-12-06 11:48:39 -080046 }
47
48 @Override
49 public void onStart() {
50 super.onStart();
Lujiang Xueeaff6c32018-04-10 08:20:29 -070051 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 Xue0daa9192017-12-06 11:48:39 -080068 }
69
70 @Override
71 public void onNewIntent(Intent intent) {
72 setIntent(intent);
roger xueb65a0762017-03-24 13:20:14 -070073 }
74
roger xue24b50802017-04-04 11:59:04 -070075 @Override
76 public void launchFragment(BaseFragment fragment) {
Lujiang Xueeaff6c32018-04-10 08:20:29 -070077 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 xue24b50802017-04-04 11:59:04 -070085 fragment.setFragmentController(this);
Anthony Chen89070482017-11-30 14:33:46 -080086 getSupportFragmentManager()
roger xue24b50802017-04-04 11:59:04 -070087 .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 Xue73f45732018-04-05 09:42:19 -070099 public void goBack() {
100 onBackPressed();
roger xue24b50802017-04-04 11:59:04 -0700101 }
102
103 @Override
104 public void onBackPressed() {
Lujiang Xue73f45732018-04-05 09:42:19 -0700105 super.onBackPressed();
106 hideKeyboard();
107 // if the backstack is empty, finish the activity.
108 if (getSupportFragmentManager().getBackStackEntryCount() == 0) {
109 finish();
roger xue24b50802017-04-04 11:59:04 -0700110 }
roger xueb65a0762017-03-24 13:20:14 -0700111 }
Lujiang Xueefc2c5f2017-12-19 13:16:13 -0800112
Lujiang Xueeaff6c32018-04-10 08:20:29 -0700113 private BaseFragment getCurrentFragment() {
114 return (BaseFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_container);
115 }
116
Lujiang Xueefc2c5f2017-12-19 13:16:13 -0800117 private void hideKeyboard() {
118 InputMethodManager imm = (InputMethodManager)this.getSystemService(
119 Context.INPUT_METHOD_SERVICE);
120 imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
121 }
roger xueb65a0762017-03-24 13:20:14 -0700122}