Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit eeca9a9f authored by Heemin Seog's avatar Heemin Seog
Browse files

Create a single car service to share in sysui

Using this as an opportunity to daggerize related classes.

Bug: 144189328
Test: manual, atest CarNavigationBarControllerTest
Change-Id: I58d142bf20b50dca509d84fce6b6258e7d47e98e
parent ec3c03ea
Loading
Loading
Loading
Loading
+71 −0
Original line number Diff line number Diff line
/*
 * 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.car;

import android.car.Car;
import android.content.Context;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Singleton;

/** Provides a common connection to the car service that can be shared. */
@Singleton
public class CarServiceProvider {

    private final Context mContext;
    private final List<CarServiceOnConnectedListener> mListeners = new ArrayList<>();
    private Car mCar;

    @Inject
    public CarServiceProvider(Context context) {
        mContext = context;
        mCar = Car.createCar(mContext, /* handler= */ null, Car.CAR_WAIT_TIMEOUT_DO_NOT_WAIT,
                (car, ready) -> {
                    mCar = car;

                    synchronized (mListeners) {
                        for (CarServiceOnConnectedListener listener : mListeners) {
                            if (ready) {
                                listener.onConnected(mCar);
                            }
                        }
                    }
                });
    }

    /**
     * Let's other components hook into the connection to the car service. If we're already
     * connected to the car service, the callback is immediately triggered.
     */
    public void addListener(CarServiceOnConnectedListener listener) {
        if (mCar.isConnected()) {
            listener.onConnected(mCar);
        }
        mListeners.add(listener);
    }

    /**
     * Listener which is triggered when Car Service is connected.
     */
    public interface CarServiceOnConnectedListener {
        /** This will be called when the car service has successfully been connected. */
        void onConnected(Car car);
    }
}
+28 −18
Original line number Diff line number Diff line
@@ -66,6 +66,7 @@ import com.android.systemui.UiOffloadThread;
import com.android.systemui.assist.AssistManager;
import com.android.systemui.broadcast.BroadcastDispatcher;
import com.android.systemui.bubbles.BubbleController;
import com.android.systemui.car.CarServiceProvider;
import com.android.systemui.classifier.FalsingLog;
import com.android.systemui.colorextraction.SysuiColorExtractor;
import com.android.systemui.fragments.FragmentHostManager;
@@ -164,6 +165,7 @@ public class CarStatusBar extends StatusBar implements CarBatteryController.Batt
    private float mBackgroundAlphaDiff;
    private float mInitialBackgroundAlpha;

    private final Lazy<FullscreenUserSwitcher> mFullscreenUserSwitcherLazy;
    private FullscreenUserSwitcher mFullscreenUserSwitcher;

    private CarBatteryController mCarBatteryController;
@@ -175,6 +177,9 @@ public class CarStatusBar extends StatusBar implements CarBatteryController.Batt

    private final Object mQueueLock = new Object();
    private final CarNavigationBarController mCarNavigationBarController;
    private final Lazy<DrivingStateHelper> mDrivingStateHelperLazy;
    private final Lazy<PowerManagerHelper> mPowerManagerHelperLazy;
    private final CarServiceProvider mCarServiceProvider;
    private CarFacetButtonController mCarFacetButtonController;
    private DeviceProvisionedController mDeviceProvisionedController;
    private boolean mDeviceIsSetUpForUser = true;
@@ -311,6 +316,10 @@ public class CarStatusBar extends StatusBar implements CarBatteryController.Batt
            ViewMediatorCallback viewMediatorCallback,
            DismissCallbackRegistry dismissCallbackRegistry,
            /* Car Settings injected components. */
            CarServiceProvider carServiceProvider,
            Lazy<DrivingStateHelper> drivingStateHelperLazy,
            Lazy<PowerManagerHelper> powerManagerHelperLazy,
            Lazy<FullscreenUserSwitcher> fullscreenUserSwitcherLazy,
            CarNavigationBarController carNavigationBarController) {
        super(
                context,
@@ -384,6 +393,10 @@ public class CarStatusBar extends StatusBar implements CarBatteryController.Batt
                viewMediatorCallback,
                dismissCallbackRegistry);
        mScrimController = scrimController;
        mCarServiceProvider = carServiceProvider;
        mDrivingStateHelperLazy = drivingStateHelperLazy;
        mPowerManagerHelperLazy = powerManagerHelperLazy;
        mFullscreenUserSwitcherLazy = fullscreenUserSwitcherLazy;
        mCarNavigationBarController = carNavigationBarController;
    }

@@ -449,10 +462,12 @@ public class CarStatusBar extends StatusBar implements CarBatteryController.Batt
        mSwitchToGuestTimer = new SwitchToGuestTimer(mContext);

        // Register a listener for driving state changes.
        mDrivingStateHelper = new DrivingStateHelper(mContext, this::onDrivingStateChanged);
        mDrivingStateHelper = mDrivingStateHelperLazy.get();
        mDrivingStateHelper.setCarDrivingStateEventListener(this::onDrivingStateChanged);
        mDrivingStateHelper.connectToCarService();

        mPowerManagerHelper = new PowerManagerHelper(mContext, mCarPowerStateListener);
        mPowerManagerHelper = mPowerManagerHelperLazy.get();
        mPowerManagerHelper.setCarPowerStateListener(mCarPowerStateListener);
        mPowerManagerHelper.connectToCarService();
    }

@@ -592,14 +607,7 @@ public class CarStatusBar extends StatusBar implements CarBatteryController.Batt
        });
        CarNotificationListener carNotificationListener = new CarNotificationListener();
        mCarUxRestrictionManagerWrapper = new CarUxRestrictionManagerWrapper();
        // This can take time if car service is not ready up to this time.
        // TODO(b/142808072) Refactor CarUxRestrictionManagerWrapper to allow setting
        // CarUxRestrictionsManager later and switch to Car.CAR_WAIT_TIMEOUT_DO_NOT_WAIT.
        Car.createCar(mContext, /* handler= */ null, Car.CAR_WAIT_TIMEOUT_WAIT_FOREVER,
                (car, ready) -> {
                    if (!ready) {
                        return;
                    }
        mCarServiceProvider.addListener(car -> {
            CarUxRestrictionsManager carUxRestrictionsManager =
                    (CarUxRestrictionsManager)
                            car.getCarManager(Car.CAR_UX_RESTRICTION_SERVICE);
@@ -974,8 +982,10 @@ public class CarStatusBar extends StatusBar implements CarBatteryController.Batt
        UserSwitcherController userSwitcherController =
                Dependency.get(UserSwitcherController.class);
        if (userSwitcherController.useFullscreenUserSwitcher()) {
            mFullscreenUserSwitcher = new FullscreenUserSwitcher(this,
                    mStatusBarWindow.findViewById(R.id.fullscreen_user_switcher_stub), mContext);
            mFullscreenUserSwitcher = mFullscreenUserSwitcherLazy.get();
            mFullscreenUserSwitcher.setStatusBar(this);
            mFullscreenUserSwitcher.setContainer(
                    mStatusBarWindow.findViewById(R.id.fullscreen_user_switcher_stub));
        } else {
            super.createUserSwitcher();
        }
+9 −0
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import com.android.systemui.UiOffloadThread;
import com.android.systemui.assist.AssistManager;
import com.android.systemui.broadcast.BroadcastDispatcher;
import com.android.systemui.bubbles.BubbleController;
import com.android.systemui.car.CarServiceProvider;
import com.android.systemui.colorextraction.SysuiColorExtractor;
import com.android.systemui.keyguard.DismissCallbackRegistry;
import com.android.systemui.keyguard.KeyguardViewMediator;
@@ -179,6 +180,10 @@ public class CarStatusBarModule {
            StatusBarKeyguardViewManager statusBarKeyguardViewManager,
            ViewMediatorCallback viewMediatorCallback,
            DismissCallbackRegistry dismissCallbackRegistry,
            CarServiceProvider carServiceProvider,
            Lazy<DrivingStateHelper> drivingStateHelperLazy,
            Lazy<PowerManagerHelper> powerManagerHelperLazy,
            Lazy<FullscreenUserSwitcher> fullscreenUserSwitcherLazy,
            CarNavigationBarController carNavigationBarController) {
        return new CarStatusBar(
                context,
@@ -250,6 +255,10 @@ public class CarStatusBarModule {
                statusBarKeyguardViewManager,
                viewMediatorCallback,
                dismissCallbackRegistry,
                carServiceProvider,
                drivingStateHelperLazy,
                powerManagerHelperLazy,
                fullscreenUserSwitcherLazy,
                carNavigationBarController);
    }
}
+14 −4
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.os.UserHandle;
@@ -36,15 +37,21 @@ import android.widget.TextView;

import com.android.internal.widget.LockPatternUtils;
import com.android.systemui.R;
import com.android.systemui.dagger.qualifiers.MainResources;

import javax.inject.Inject;
import javax.inject.Singleton;

/**
 * A helper class displays an unlock dialog and receives broadcast about detecting trusted device
 * & unlocking state to show the appropriate message on the dialog.
 */
@Singleton
class CarTrustAgentUnlockDialogHelper extends BroadcastReceiver{
    private static final String TAG = CarTrustAgentUnlockDialogHelper.class.getSimpleName();

    private final Context mContext;
    private final Resources mResources;
    private final WindowManager mWindowManager;
    private final UserManager mUserManager;
    private final WindowManager.LayoutParams mParams;
@@ -60,10 +67,13 @@ class CarTrustAgentUnlockDialogHelper extends BroadcastReceiver{
    private boolean mIsDialogShowing;
    private OnHideListener mOnHideListener;

    CarTrustAgentUnlockDialogHelper(Context context) {
    @Inject
    CarTrustAgentUnlockDialogHelper(Context context, @MainResources Resources resources,
            UserManager userManager, WindowManager windowManager) {
        mContext = context;
        mUserManager = mContext.getSystemService(UserManager.class);
        mWindowManager = mContext.getSystemService(WindowManager.class);
        mResources = resources;
        mUserManager = userManager;
        mWindowManager = windowManager;
        mParams = createLayoutParams();
        mFilter = getIntentFilter();

@@ -125,7 +135,7 @@ class CarTrustAgentUnlockDialogHelper extends BroadcastReceiver{
     * @param listener listener that listens to dialog hide
     */
    void showUnlockDialogAfterDelay(int uid, OnHideListener listener) {
        long delayMillis = mContext.getResources().getInteger(R.integer.unlock_dialog_delay_ms);
        long delayMillis = mResources.getInteger(R.integer.unlock_dialog_delay_ms);
        showUnlockDialogAfterDelay(uid, delayMillis, listener);
    }

+34 −25
Original line number Diff line number Diff line
@@ -17,31 +17,43 @@
package com.android.systemui.statusbar.car;

import android.car.Car;
import android.car.Car.CarServiceLifecycleListener;
import android.car.drivingstate.CarDrivingStateEvent;
import android.car.drivingstate.CarDrivingStateManager;
import android.car.drivingstate.CarDrivingStateManager.CarDrivingStateEventListener;
import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;

import com.android.systemui.car.CarServiceProvider;

import javax.inject.Inject;
import javax.inject.Singleton;

/**
 * Helper class for connecting to the {@link CarDrivingStateManager} and listening for driving state
 * changes.
 */
@Singleton
public class DrivingStateHelper {
    public static final String TAG = "DrivingStateHelper";

    private final Context mContext;
    private final CarServiceProvider mCarServiceProvider;

    private CarDrivingStateManager mDrivingStateManager;
    private Car mCar;
    private CarDrivingStateEventListener mDrivingStateHandler;

    public DrivingStateHelper(Context context,
            @NonNull CarDrivingStateEventListener drivingStateHandler) {
        mContext = context;
        mDrivingStateHandler = drivingStateHandler;
    @Inject
    public DrivingStateHelper(CarServiceProvider carServiceProvider) {
        mCarServiceProvider = carServiceProvider;
    }

    /**
     * Sets the {@link CarDrivingStateEventListener}. Should be set before calling {@link
     * #connectToCarService()}.
     */
    public void setCarDrivingStateEventListener(
            @NonNull CarDrivingStateEventListener carDrivingStateEventListener) {
        mDrivingStateHandler = carDrivingStateEventListener;
    }

    /**
@@ -64,14 +76,11 @@ public class DrivingStateHelper {
     * Establishes connection with the Car service.
     */
    public void connectToCarService() {
        mCar = Car.createCar(mContext, /* handler= */ null, Car.CAR_WAIT_TIMEOUT_DO_NOT_WAIT,
                mCarServiceLifecycleListener);
        mCarServiceProvider.addListener(mCarServiceLifecycleListener);
    }

    private final CarServiceLifecycleListener mCarServiceLifecycleListener = (car, ready) -> {
        if (!ready) {
            return;
        }
    private final CarServiceProvider.CarServiceOnConnectedListener mCarServiceLifecycleListener =
            car -> {
                logD("Car Service connected");
                mDrivingStateManager = (CarDrivingStateManager) car.getCarManager(
                        Car.CAR_DRIVING_STATE_SERVICE);
Loading