blob: f59f886d487b6c90ad46ce5fa710c7f8f7054fab [file] [log] [blame]
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.systemui.navigationbar.car;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.android.systemui.R;
import com.android.systemui.statusbar.car.hvac.HvacController;
import com.android.systemui.statusbar.car.hvac.TemperatureView;
import javax.inject.Inject;
import javax.inject.Singleton;
import dagger.Lazy;
/** A single class which controls the navigation bar views. */
@Singleton
public class CarNavigationBarController {
private final Context mContext;
private final NavigationBarViewFactory mNavigationBarViewFactory;
private final Lazy<HvacController> mHvacControllerLazy;
private boolean mShowBottom;
private boolean mShowLeft;
private boolean mShowRight;
private View.OnTouchListener mTopBarTouchListener;
private View.OnTouchListener mBottomBarTouchListener;
private View.OnTouchListener mLeftBarTouchListener;
private View.OnTouchListener mRightBarTouchListener;
private NotificationsShadeController mNotificationsShadeController;
private CarNavigationBarView mTopView;
private CarNavigationBarView mBottomView;
private CarNavigationBarView mLeftView;
private CarNavigationBarView mRightView;
@Inject
public CarNavigationBarController(Context context,
NavigationBarViewFactory navigationBarViewFactory,
Lazy<HvacController> hvacControllerLazy) {
mContext = context;
mNavigationBarViewFactory = navigationBarViewFactory;
mHvacControllerLazy = hvacControllerLazy;
// Read configuration.
mShowBottom = mContext.getResources().getBoolean(R.bool.config_enableBottomNavigationBar);
mShowLeft = mContext.getResources().getBoolean(R.bool.config_enableLeftNavigationBar);
mShowRight = mContext.getResources().getBoolean(R.bool.config_enableRightNavigationBar);
}
/** Connect to hvac service. */
public void connectToHvac() {
mHvacControllerLazy.get().connectToCarService();
}
/** Clean up hvac. */
public void removeAllFromHvac() {
mHvacControllerLazy.get().removeAllComponents();
}
/** Gets the bottom window if configured to do so. */
@Nullable
public ViewGroup getBottomWindow() {
return mShowBottom ? mNavigationBarViewFactory.getBottomWindow() : null;
}
/** Gets the left window if configured to do so. */
@Nullable
public ViewGroup getLeftWindow() {
return mShowLeft ? mNavigationBarViewFactory.getLeftWindow() : null;
}
/** Gets the right window if configured to do so. */
@Nullable
public ViewGroup getRightWindow() {
return mShowRight ? mNavigationBarViewFactory.getRightWindow() : null;
}
/** Toggles the bottom nav bar visibility. */
public boolean setBottomWindowVisibility(boolean isVisible) {
return setWindowVisibility(getBottomWindow(), isVisible);
}
/** Toggles the left nav bar visibility. */
public boolean setLeftWindowVisibility(boolean isVisible) {
return setWindowVisibility(getLeftWindow(), isVisible);
}
/** Toggles the right nav bar visibility. */
public boolean setRightWindowVisibility(boolean isVisible) {
return setWindowVisibility(getRightWindow(), isVisible);
}
private boolean setWindowVisibility(ViewGroup window, boolean isVisible) {
if (window == null) {
return false;
}
int newVisibility = isVisible ? View.VISIBLE : View.GONE;
if (window.getVisibility() == newVisibility) {
return false;
}
window.setVisibility(newVisibility);
return true;
}
/** Gets the top navigation bar with the appropriate listeners set. */
@NonNull
public CarNavigationBarView getTopBar(boolean isSetUp) {
mTopView = mNavigationBarViewFactory.getTopBar(isSetUp);
mTopView.setStatusBarWindowTouchListener(mTopBarTouchListener);
mTopView.setNotificationsPanelController(mNotificationsShadeController);
addTemperatureViewToController(mTopView);
return mTopView;
}
/** Gets the bottom navigation bar with the appropriate listeners set. */
@Nullable
public CarNavigationBarView getBottomBar(boolean isSetUp) {
if (!mShowBottom) {
return null;
}
mBottomView = mNavigationBarViewFactory.getBottomBar(isSetUp);
mBottomView.setStatusBarWindowTouchListener(mBottomBarTouchListener);
mBottomView.setNotificationsPanelController(mNotificationsShadeController);
addTemperatureViewToController(mBottomView);
return mBottomView;
}
/** Gets the left navigation bar with the appropriate listeners set. */
@Nullable
public CarNavigationBarView getLeftBar(boolean isSetUp) {
if (!mShowLeft) {
return null;
}
mLeftView = mNavigationBarViewFactory.getLeftBar(isSetUp);
mLeftView.setStatusBarWindowTouchListener(mLeftBarTouchListener);
mLeftView.setNotificationsPanelController(mNotificationsShadeController);
addTemperatureViewToController(mLeftView);
return mLeftView;
}
/** Gets the right navigation bar with the appropriate listeners set. */
@Nullable
public CarNavigationBarView getRightBar(boolean isSetUp) {
if (!mShowRight) {
return null;
}
mRightView = mNavigationBarViewFactory.getRightBar(isSetUp);
mRightView.setStatusBarWindowTouchListener(mRightBarTouchListener);
mRightView.setNotificationsPanelController(mNotificationsShadeController);
addTemperatureViewToController(mRightView);
return mRightView;
}
/** Sets a touch listener for the top navigation bar. */
public void registerTopBarTouchListener(View.OnTouchListener listener) {
mTopBarTouchListener = listener;
if (mTopView != null) {
mTopView.setStatusBarWindowTouchListener(mTopBarTouchListener);
}
}
/** Sets a touch listener for the bottom navigation bar. */
public void registerBottomBarTouchListener(View.OnTouchListener listener) {
mBottomBarTouchListener = listener;
if (mBottomView != null) {
mBottomView.setStatusBarWindowTouchListener(mBottomBarTouchListener);
}
}
/** Sets a touch listener for the left navigation bar. */
public void registerLeftBarTouchListener(View.OnTouchListener listener) {
mLeftBarTouchListener = listener;
if (mLeftView != null) {
mLeftView.setStatusBarWindowTouchListener(mLeftBarTouchListener);
}
}
/** Sets a touch listener for the right navigation bar. */
public void registerRightBarTouchListener(View.OnTouchListener listener) {
mRightBarTouchListener = listener;
if (mRightView != null) {
mRightView.setStatusBarWindowTouchListener(mRightBarTouchListener);
}
}
/** Sets a notification controller which toggles the notification panel. */
public void registerNotificationController(
NotificationsShadeController notificationsShadeController) {
mNotificationsShadeController = notificationsShadeController;
if (mTopView != null) {
mTopView.setNotificationsPanelController(mNotificationsShadeController);
}
if (mBottomView != null) {
mBottomView.setNotificationsPanelController(mNotificationsShadeController);
}
if (mLeftView != null) {
mLeftView.setNotificationsPanelController(mNotificationsShadeController);
}
if (mRightView != null) {
mRightView.setNotificationsPanelController(mNotificationsShadeController);
}
}
/** Interface for controlling the notifications shade. */
public interface NotificationsShadeController {
/** Toggles the visibility of the notifications shade. */
void togglePanel();
}
private void addTemperatureViewToController(View v) {
if (v instanceof TemperatureView) {
mHvacControllerLazy.get().addHvacTextView((TemperatureView) v);
} else if (v instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) v;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
addTemperatureViewToController(viewGroup.getChildAt(i));
}
}
}
}