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

Commit a6adcb70 authored by Android Build Coastguard Worker's avatar Android Build Coastguard Worker
Browse files

Snap for 9077201 from 1d073c96 to tm-qpr1-release

Change-Id: Ia1b9829ced15373a6759c3df87fa671325c47993
parents 8205e8fe 1d073c96
Loading
Loading
Loading
Loading
+94 −0
Original line number Diff line number Diff line
@@ -16,12 +16,20 @@

package com.android.internal.os;

import android.annotation.NonNull;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.os.BatteryStats;
import android.os.BatteryStats.BitDescription;
import android.os.BatteryStats.HistoryItem;
import android.os.BatteryStats.HistoryTag;
import android.os.Build;
import android.os.Parcel;
import android.os.Process;
import android.os.StatFs;
import android.os.SystemClock;
import android.os.SystemProperties;
import android.os.Trace;
import android.util.ArraySet;
import android.util.AtomicFile;
import android.util.Slog;
@@ -104,6 +112,49 @@ public class BatteryStatsHistory {
     */
    private int mParcelIndex = 0;

    /**
     * A delegate for android.os.Trace to allow testing static calls. Due to
     * limitations in Android Tracing (b/153319140), the delegate also records
     * counter values in system properties which allows reading the value at the
     * start of a tracing session. This overhead is limited to userdebug builds.
     * On user builds, tracing still occurs but the counter value will be missing
     * until the first change occurs.
     */
    @VisibleForTesting
    public static class TraceDelegate {
        // Note: certain tests currently run as platform_app which is not allowed
        // to set debug system properties. To ensure that system properties are set
        // only when allowed, we check the current UID.
        private final boolean mShouldSetProperty =
                Build.IS_USERDEBUG && (Process.myUid() == Process.SYSTEM_UID);

        /**
         * Returns true if trace counters should be recorded.
         */
        public boolean tracingEnabled() {
            return Trace.isTagEnabled(Trace.TRACE_TAG_POWER) || mShouldSetProperty;
        }

        /**
         * Records the counter value with the given name.
         */
        public void traceCounter(@NonNull String name, int value) {
            Trace.traceCounter(Trace.TRACE_TAG_POWER, name, value);
            if (mShouldSetProperty) {
                SystemProperties.set("debug.tracing." + name, Integer.toString(value));
            }
        }

        /**
         * Records an instant event (one with no duration).
         */
        public void traceInstantEvent(@NonNull String track, @NonNull String name) {
            Trace.instantForTrack(Trace.TRACE_TAG_POWER, track, name);
        }
    }

    private TraceDelegate mTracer = new TraceDelegate();

    /**
     * Constructor
     * @param stats BatteryStatsImpl object.
@@ -497,4 +548,47 @@ public class BatteryStatsHistory {
        }
        return ret;
    }

    /**
     * Writes event details into Atrace.
     */
    public void recordTraceEvents(int code, HistoryTag tag) {
        if (code == HistoryItem.EVENT_NONE) return;
        if (!mTracer.tracingEnabled()) return;

        final int idx = code & HistoryItem.EVENT_TYPE_MASK;
        final String prefix = (code & HistoryItem.EVENT_FLAG_START) != 0 ? "+" :
                  (code & HistoryItem.EVENT_FLAG_FINISH) != 0 ? "-" : "";

        final String[] names = BatteryStats.HISTORY_EVENT_NAMES;
        if (idx < 0 || idx >= BatteryStats.HISTORY_EVENT_NAMES.length) return;

        final String track = "battery_stats." + names[idx];
        final String name = prefix + names[idx] + "=" + tag.uid + ":\"" + tag.string + "\"";
        mTracer.traceInstantEvent(track, name);
    }

    /**
     * Writes changes to a HistoryItem state bitmap to Atrace.
     */
    public void recordTraceCounters(int oldval, int newval, BitDescription[] descriptions) {
        if (!mTracer.tracingEnabled()) return;

        int diff = oldval ^ newval;
        if (diff == 0) return;

        for (int i = 0; i < descriptions.length; i++) {
            BitDescription bd = descriptions[i];
            if ((diff & bd.mask) == 0) continue;

            int value;
            if (bd.shift < 0) {
                value = (newval & bd.mask) != 0 ? 1 : 0;
            } else {
                value = (newval & bd.mask) >> bd.shift;
            }

            mTracer.traceCounter("battery_stats." + bd.name, value);
        }
    }
}
+6 −0
Original line number Diff line number Diff line
@@ -4432,6 +4432,12 @@ public class BatteryStatsImpl extends BatteryStats {
                    + Integer.toHexString(diffStates2) + " lastDiff2="
                    + Integer.toHexString(lastDiffStates2));
        }
        mBatteryStatsHistory.recordTraceEvents(cur.eventCode, cur.eventTag);
        mBatteryStatsHistory.recordTraceCounters(mHistoryLastWritten.states,
                cur.states & mActiveHistoryStates, BatteryStats.HISTORY_STATE_DESCRIPTIONS);
        mBatteryStatsHistory.recordTraceCounters(mHistoryLastWritten.states2,
                cur.states2 & mActiveHistoryStates2, BatteryStats.HISTORY_STATE2_DESCRIPTIONS);
        if (mHistoryBufferLastPos >= 0 && mHistoryLastWritten.cmd == HistoryItem.CMD_UPDATE
                && timeDiffMs < 1000 && (diffStates & lastDiffStates) == 0
                && (diffStates2&lastDiffStates2) == 0
+22 −1
Original line number Diff line number Diff line
@@ -55,6 +55,8 @@ import com.android.wm.shell.common.annotations.ShellBackgroundThread;
import com.android.wm.shell.common.annotations.ShellMainThread;
import com.android.wm.shell.common.annotations.ShellSplashscreenThread;
import com.android.wm.shell.compatui.CompatUIController;
import com.android.wm.shell.desktopmode.DesktopMode;
import com.android.wm.shell.desktopmode.DesktopModeTaskRepository;
import com.android.wm.shell.displayareahelper.DisplayAreaHelper;
import com.android.wm.shell.displayareahelper.DisplayAreaHelperController;
import com.android.wm.shell.draganddrop.DragAndDropController;
@@ -477,11 +479,12 @@ public abstract class WMShellBaseModule {
            ShellInit shellInit,
            ShellCommandHandler shellCommandHandler,
            TaskStackListenerImpl taskStackListener,
            Optional<DesktopModeTaskRepository> desktopModeTaskRepository,
            @ShellMainThread ShellExecutor mainExecutor
    ) {
        return Optional.ofNullable(
                RecentTasksController.create(context, shellInit, shellCommandHandler,
                        taskStackListener, mainExecutor));
                        taskStackListener, desktopModeTaskRepository, mainExecutor));
    }

    //
@@ -665,6 +668,24 @@ public abstract class WMShellBaseModule {
        return new ShellController(shellInit, shellCommandHandler, mainExecutor);
    }

    //
    // Desktop mode (optional feature)
    //

    @BindsOptionalOf
    @DynamicOverride
    abstract DesktopModeTaskRepository optionalDesktopModeTaskRepository();

    @WMSingleton
    @Provides
    static Optional<DesktopModeTaskRepository> providesDesktopModeTaskRepository(
            @DynamicOverride Optional<DesktopModeTaskRepository> desktopModeTaskRepository) {
        if (DesktopMode.IS_SUPPORTED) {
            return desktopModeTaskRepository;
        }
        return Optional.empty();
    }

    //
    // Misc
    //
+10 −2
Original line number Diff line number Diff line
@@ -51,6 +51,7 @@ import com.android.wm.shell.common.annotations.ShellBackgroundThread;
import com.android.wm.shell.common.annotations.ShellMainThread;
import com.android.wm.shell.desktopmode.DesktopMode;
import com.android.wm.shell.desktopmode.DesktopModeController;
import com.android.wm.shell.desktopmode.DesktopModeTaskRepository;
import com.android.wm.shell.draganddrop.DragAndDropController;
import com.android.wm.shell.freeform.FreeformComponents;
import com.android.wm.shell.freeform.FreeformTaskListener;
@@ -220,14 +221,14 @@ public abstract class WMShellModule {
            Context context,
            ShellInit shellInit,
            ShellTaskOrganizer shellTaskOrganizer,
            Optional<RecentTasksController> recentTasksController,
            Optional<DesktopModeTaskRepository> desktopModeTaskRepository,
            WindowDecorViewModel<?> windowDecorViewModel) {
        // TODO(b/238217847): Temporarily add this check here until we can remove the dynamic
        //                    override for this controller from the base module
        ShellInit init = FreeformComponents.isFreeformEnabled(context)
                ? shellInit
                : null;
        return new FreeformTaskListener<>(init, shellTaskOrganizer, recentTasksController,
        return new FreeformTaskListener<>(init, shellTaskOrganizer, desktopModeTaskRepository,
                windowDecorViewModel);
    }

@@ -610,6 +611,13 @@ public abstract class WMShellModule {
        }
    }

    @WMSingleton
    @Provides
    @DynamicOverride
    static DesktopModeTaskRepository provideDesktopModeTaskRepository() {
        return new DesktopModeTaskRepository();
    }

    //
    // Misc
    //
+89 −0
Original line number Diff line number Diff line
/*
 * 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.wm.shell.desktopmode

import android.util.ArraySet

/**
 * Keeps track of task data related to desktop mode.
 */
class DesktopModeTaskRepository {

    /**
     * Set of task ids that are marked as active in desktop mode.
     * Active tasks in desktop mode are freeform tasks that are visible or have been visible after
     * desktop mode was activated.
     * Task gets removed from this list when it vanishes. Or when desktop mode is turned off.
     */
    private val activeTasks = ArraySet<Int>()
    private val listeners = ArraySet<Listener>()

    /**
     * Add a [Listener] to be notified of updates to the repository.
     */
    fun addListener(listener: Listener) {
        listeners.add(listener)
    }

    /**
     * Remove a previously registered [Listener]
     */
    fun removeListener(listener: Listener) {
        listeners.remove(listener)
    }

    /**
     * Mark a task with given [taskId] as active.
     */
    fun addActiveTask(taskId: Int) {
        val added = activeTasks.add(taskId)
        if (added) {
            listeners.onEach { it.onActiveTasksChanged() }
        }
    }

    /**
     * Remove task with given [taskId] from active tasks.
     */
    fun removeActiveTask(taskId: Int) {
        val removed = activeTasks.remove(taskId)
        if (removed) {
            listeners.onEach { it.onActiveTasksChanged() }
        }
    }

    /**
     * Check if a task with the given [taskId] was marked as an active task
     */
    fun isActiveTask(taskId: Int): Boolean {
        return activeTasks.contains(taskId)
    }

    /**
     * Get a set of the active tasks
     */
    fun getActiveTasks(): ArraySet<Int> {
        return ArraySet(activeTasks)
    }

    /**
     * Defines interface for classes that can listen to changes in repository state.
     */
    interface Listener {
        fun onActiveTasksChanged()
    }
}
Loading