1 /* 2 * Copyright (C) 2020 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.google.android.car.kitchensink.systembars; 18 19 import android.graphics.Color; 20 import android.os.Bundle; 21 import android.view.LayoutInflater; 22 import android.view.View; 23 import android.view.ViewGroup; 24 import android.view.Window; 25 import android.view.WindowInsets; 26 import android.view.WindowInsetsController; 27 28 import androidx.annotation.NonNull; 29 import androidx.annotation.Nullable; 30 import androidx.fragment.app.Fragment; 31 32 import com.google.android.car.kitchensink.R; 33 34 /** Test fragment for controlling window insets. */ 35 public final class SystemBarsFragment extends Fragment { 36 37 private static final int COLOR_UNSET = 0; 38 39 private WindowInsetsController mWindowInsetsController; 40 private boolean mStatusBarColorApplied; 41 private boolean mNavigationBarColorApplied; 42 private int mStatusBarDefaultColor = COLOR_UNSET; 43 private int mNavigatioNBarDefaultColor = COLOR_UNSET; 44 45 @Nullable 46 @Override onCreateView(@onNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)47 public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, 48 @Nullable Bundle savedInstanceState) { 49 return inflater.inflate(R.layout.system_bars_fragment, container, 50 /* attachToRoot= */ false); 51 } 52 53 @Override onViewCreated(@onNull View view, @Nullable Bundle savedInstanceState)54 public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { 55 super.onViewCreated(view, savedInstanceState); 56 mWindowInsetsController = view.getWindowInsetsController(); 57 initStatusBarInsetsButtons(view); 58 initNavigationBarInsetsButtons(view); 59 initSystemBarInsetsButtons(view); 60 initSystemBarBehaviorButtons(view); 61 initSystemBarColorButtons(view); 62 } 63 initStatusBarInsetsButtons(View view)64 private void initStatusBarInsetsButtons(View view) { 65 view.findViewById(R.id.show_status_bar_insets).setOnClickListener( 66 v -> mWindowInsetsController.show(WindowInsets.Type.statusBars())); 67 view.findViewById(R.id.hide_status_bar_insets).setOnClickListener( 68 v -> mWindowInsetsController.hide(WindowInsets.Type.statusBars())); 69 } 70 initNavigationBarInsetsButtons(View view)71 private void initNavigationBarInsetsButtons(View view) { 72 view.findViewById(R.id.show_nav_bar_insets).setOnClickListener( 73 v -> mWindowInsetsController.show(WindowInsets.Type.navigationBars())); 74 view.findViewById(R.id.hide_nav_bar_insets).setOnClickListener( 75 v -> mWindowInsetsController.hide(WindowInsets.Type.navigationBars())); 76 } 77 initSystemBarInsetsButtons(View view)78 private void initSystemBarInsetsButtons(View view) { 79 view.findViewById(R.id.show_system_bar_insets).setOnClickListener( 80 v -> mWindowInsetsController.show(WindowInsets.Type.systemBars())); 81 view.findViewById(R.id.hide_system_bar_insets).setOnClickListener( 82 v -> mWindowInsetsController.hide(WindowInsets.Type.systemBars())); 83 } 84 initSystemBarBehaviorButtons(View view)85 private void initSystemBarBehaviorButtons(View view) { 86 view.findViewById(R.id.show_bars_by_touch).setOnClickListener( 87 v -> mWindowInsetsController.setSystemBarsBehavior( 88 WindowInsetsController.BEHAVIOR_SHOW_BARS_BY_TOUCH)); 89 view.findViewById(R.id.show_bars_by_swipe).setOnClickListener( 90 v -> mWindowInsetsController.setSystemBarsBehavior( 91 WindowInsetsController.BEHAVIOR_SHOW_BARS_BY_SWIPE)); 92 view.findViewById(R.id.show_transient_bars_by_swipe).setOnClickListener( 93 v -> mWindowInsetsController.setSystemBarsBehavior( 94 WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE)); 95 } 96 initSystemBarColorButtons(View view)97 private void initSystemBarColorButtons(View view) { 98 view.findViewById(R.id.color_status_bars).setOnClickListener( 99 v -> { 100 Window window = getActivity().getWindow(); 101 if (mStatusBarColorApplied) { 102 window.setStatusBarColor(mStatusBarDefaultColor); 103 mStatusBarColorApplied = false; 104 } else { 105 // If status bar default color is unset, first get its current color as the 106 // default so we can toggle between the default and red (rgb(255, 0, 0)). 107 if (mStatusBarDefaultColor == COLOR_UNSET) { 108 mStatusBarDefaultColor = window.getStatusBarColor(); 109 } 110 window.setStatusBarColor(Color.RED); 111 mStatusBarColorApplied = true; 112 } 113 }); 114 view.findViewById(R.id.color_navigation_bars).setOnClickListener( 115 v -> { 116 Window window = getActivity().getWindow(); 117 if (mNavigationBarColorApplied) { 118 window.setNavigationBarColor(mNavigatioNBarDefaultColor); 119 mNavigationBarColorApplied = false; 120 } else { 121 // If nav bar default color is unset, first get its current color as the 122 // default so we can toggle between the default and red (rgb(255, 0, 0)). 123 if (mNavigatioNBarDefaultColor == COLOR_UNSET) { 124 mNavigatioNBarDefaultColor = window.getNavigationBarColor(); 125 } 126 window.setNavigationBarColor(Color.RED); 127 mNavigationBarColorApplied = true; 128 } 129 }); 130 } 131 } 132