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 18 import android.annotation.Nullable; 19 import android.car.drivingstate.CarUxRestrictions; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.os.Bundle; 23 import android.support.v4.app.FragmentManager.OnBackStackChangedListener; 24 import android.support.v7.app.AppCompatActivity; 25 import android.support.v7.widget.Toolbar; 26 import android.view.View; 27 import android.view.inputmethod.InputMethodManager; 28 import android.widget.Toast; 29 30 import com.android.car.settings.R; 31 import com.android.car.settings.common.BaseFragment.UXRestrictionsProvider; 32 import com.android.car.settings.quicksettings.QuickSettingFragment; 33 34 /** 35 * Base activity class for car settings, provides a action bar with a back button that goes to 36 * previous activity. 37 */ 38 public class CarSettingActivity extends AppCompatActivity implements 39 BaseFragment.FragmentController, UXRestrictionsProvider, OnBackStackChangedListener{ 40 private CarUxRestrictionsHelper mUxRestrictionsHelper; 41 private View mRestrictedMessage; 42 // Default to minimum restriction. 43 private CarUxRestrictions mCarUxRestrictions = new CarUxRestrictions.Builder( 44 /* reqOpt= */ true, 45 CarUxRestrictions.UX_RESTRICTIONS_BASELINE, 46 /* timestamp= */ 0 47 ).build(); 48 49 @Override onCreate(Bundle savedInstanceState)50 protected void onCreate(Bundle savedInstanceState) { 51 super.onCreate(savedInstanceState); 52 setContentView(R.layout.app_compat_activity); 53 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 54 setSupportActionBar(toolbar); 55 if (mUxRestrictionsHelper == null) { 56 mUxRestrictionsHelper = 57 new CarUxRestrictionsHelper(this, carUxRestrictions -> { 58 mCarUxRestrictions = carUxRestrictions; 59 BaseFragment currentFragment = getCurrentFragment(); 60 if (currentFragment != null) { 61 currentFragment.onUxRestrictionChanged(carUxRestrictions); 62 updateBlockingView(currentFragment); 63 } 64 }); 65 } 66 mUxRestrictionsHelper.start(); 67 getSupportFragmentManager().addOnBackStackChangedListener(this); 68 mRestrictedMessage = findViewById(R.id.restricted_message); 69 } 70 71 @Override onBackStackChanged()72 public void onBackStackChanged() { 73 updateBlockingView(getCurrentFragment()); 74 } 75 updateBlockingView(@ullable BaseFragment currentFragment)76 private void updateBlockingView(@Nullable BaseFragment currentFragment) { 77 if (currentFragment == null) { 78 return; 79 } 80 boolean canBeShown = currentFragment.canBeShown(mCarUxRestrictions); 81 mRestrictedMessage.setVisibility(canBeShown ? View.GONE : View.VISIBLE); 82 } 83 84 @Override onStart()85 public void onStart() { 86 super.onStart(); 87 if (getCurrentFragment() == null) { 88 launchFragment(QuickSettingFragment.newInstance()); 89 } 90 } 91 92 @Override getCarUxRestrictions()93 public CarUxRestrictions getCarUxRestrictions() { 94 return mCarUxRestrictions; 95 } 96 97 @Override onDestroy()98 public void onDestroy() { 99 super.onDestroy(); 100 mUxRestrictionsHelper.stop(); 101 mUxRestrictionsHelper = null; 102 } 103 104 @Override onNewIntent(Intent intent)105 public void onNewIntent(Intent intent) { 106 setIntent(intent); 107 } 108 109 @Override launchFragment(BaseFragment fragment)110 public void launchFragment(BaseFragment fragment) { 111 getSupportFragmentManager() 112 .beginTransaction() 113 .setCustomAnimations( 114 R.animator.trans_right_in , 115 R.animator.trans_left_out, 116 R.animator.trans_left_in, 117 R.animator.trans_right_out) 118 .replace(R.id.fragment_container, fragment) 119 .addToBackStack(null) 120 .commit(); 121 } 122 123 @Override goBack()124 public void goBack() { 125 onBackPressed(); 126 } 127 128 @Override showDOBlockingMessage()129 public void showDOBlockingMessage() { 130 Toast.makeText( 131 this, R.string.restricted_while_driving, Toast.LENGTH_SHORT).show(); 132 } 133 134 @Override onBackPressed()135 public void onBackPressed() { 136 super.onBackPressed(); 137 hideKeyboard(); 138 // if the backstack is empty, finish the activity. 139 if (getSupportFragmentManager().getBackStackEntryCount() == 0) { 140 finish(); 141 } 142 } 143 getCurrentFragment()144 private BaseFragment getCurrentFragment() { 145 return (BaseFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_container); 146 } 147 hideKeyboard()148 private void hideKeyboard() { 149 InputMethodManager imm = (InputMethodManager)this.getSystemService( 150 Context.INPUT_METHOD_SERVICE); 151 imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0); 152 } 153 } 154