/*
 * Copyright (C) 2022 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;

import static android.car.CarOccupantZoneManager.DISPLAY_TYPE_MAIN;

import android.car.Car;
import android.car.CarOccupantZoneManager;
import android.view.Display;

import com.android.systemui.car.users.CarSystemUIUserUtil;

/**
 * Application class for CarSystemUI.
 */
public class CarSystemUIApplication extends SystemUIApplication {

    private boolean mIsVisibleBackgroundUserSysUI;

    @Override
    public void onCreate() {
        mIsVisibleBackgroundUserSysUI = CarSystemUIUserUtil.isSecondaryMUMDSystemUI();
        super.onCreate();
        if (mIsVisibleBackgroundUserSysUI) {
            Car car = Car.createCar(this);
            if (car == null) {
                return;
            }
            CarOccupantZoneManager manager = (CarOccupantZoneManager) car.getCarManager(
                    Car.CAR_OCCUPANT_ZONE_SERVICE);
            if (manager != null) {
                CarOccupantZoneManager.OccupantZoneInfo info = manager.getMyOccupantZone();
                if (info != null) {
                    Display display = manager.getDisplayForOccupant(info, DISPLAY_TYPE_MAIN);
                    if (display != null) {
                        updateDisplay(display.getDisplayId());
                        startSystemUserServicesIfNeeded();
                    }
                }
            }
            car.disconnect();
        }
    }

    @Override
    protected boolean shouldStartSystemUserServices() {
        if (mIsVisibleBackgroundUserSysUI) {
            // visible background user SystemUI instances should start the same services as the
            // normal system user SystemUI instance.
            return true;
        }
        return super.shouldStartSystemUserServices();
    }

    @Override
    protected boolean shouldStartSecondaryUserServices() {
        if (mIsVisibleBackgroundUserSysUI) {
            // visible background user SystemUI instances should start the same services as the
            // normal system user SystemUI instance - this already includes the secondary user
            // services.
            return false;
        }
        return super.shouldStartSecondaryUserServices();
    }
}
