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

Commit f7cffa7a authored by Suprabh Shukla's avatar Suprabh Shukla
Browse files

Exposing WorkSource hidden APIs as system APIs

Users of these APIs are planning to move to apex.

Test: atest FrameworksCoreTests:android.os.WorkSourceTest
atest CtsOsTestCases:android.os.cts.WorkSourceTest

Bug: 143551137
Change-Id: I2e97a8b469254ea92e3ee21571ee8ae3cbb9abbe
parent 8c198a83
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -5780,7 +5780,15 @@ package android.os {
  }
  public class WorkSource implements android.os.Parcelable {
    ctor public WorkSource(int);
    ctor public WorkSource(int, @NonNull String);
    method public android.os.WorkSource.WorkChain createWorkChain();
    method @Nullable public String getPackageName(int);
    method public int getUid(int);
    method @Nullable public java.util.List<android.os.WorkSource.WorkChain> getWorkChains();
    method public boolean isEmpty();
    method public int size();
    method @NonNull public android.os.WorkSource withoutNames();
  }
  public static final class WorkSource.WorkChain implements android.os.Parcelable {
+4 −2
Original line number Diff line number Diff line
@@ -2182,10 +2182,12 @@ package android.os {
    method public boolean add(int);
    method public boolean add(int, String);
    method @Deprecated public android.os.WorkSource addReturningNewbs(android.os.WorkSource);
    method public int get(int);
    method public String getName(int);
    method @Nullable public String getPackageName(int);
    method public int getUid(int);
    method public boolean isEmpty();
    method @Deprecated public android.os.WorkSource[] setReturningDiffs(android.os.WorkSource);
    method public int size();
    method @NonNull public android.os.WorkSource withoutNames();
  }

}
+1 −1
Original line number Diff line number Diff line
@@ -139,7 +139,7 @@ public final class StatsLogEventWrapper implements Parcelable {
        out.writeLong(mElapsedTimeNs);
        out.writeLong(mWallClockTimeNs);
        if (mWorkSource != null) {
            ArrayList<android.os.WorkSource.WorkChain> workChains = mWorkSource.getWorkChains();
            List<WorkSource.WorkChain> workChains = mWorkSource.getWorkChains();
            // number of chains
            out.writeInt(workChains.size());
            for (int i = 0; i < workChains.size(); i++) {
+81 −19
Original line number Diff line number Diff line
@@ -13,13 +13,15 @@ import android.util.Log;
import android.util.proto.ProtoOutputStream;

import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.Preconditions;

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

/**
 * Describes the source of some work that may be done by someone else.
 * Currently the public representation of what a work source is is not
 * Currently the public representation of what a work source is not
 * defined; this is an opaque container.
 */
public class WorkSource implements Parcelable {
@@ -48,7 +50,7 @@ public class WorkSource implements Parcelable {
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
    static WorkSource sNewbWork;
    /**
     * For returning gone work form a modification operation.
     * For returning gone work from a modification operation.
     */
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
    static WorkSource sGoneWork;
@@ -91,9 +93,14 @@ public class WorkSource implements Parcelable {
        }
    }

    /** @hide */
    @UnsupportedAppUsage

    /**
     * Creates a work source with the given uid.
     * @param uid the uid performing the work
     * @hide
     */
    @TestApi
    @SystemApi
    public WorkSource(int uid) {
        mNum = 1;
        mUids = new int[] { uid, 0 };
@@ -101,14 +108,18 @@ public class WorkSource implements Parcelable {
        mChains = null;
    }

    /** @hide */
    public WorkSource(int uid, String name) {
        if (name == null) {
            throw new NullPointerException("Name can't be null");
        }
    /**
     * Creates a work source with the given uid and package name.
     * @param uid the uid performing the work
     * @param packageName the package performing the work
     * @hide
     */
    @SystemApi
    public WorkSource(int uid, @NonNull String packageName) {
        Preconditions.checkNotNull(packageName, "packageName can't be null");
        mNum = 1;
        mUids = new int[] { uid, 0 };
        mNames = new String[] { name, null };
        mNames = new String[] { packageName, null };
        mChains = null;
    }

@@ -138,17 +149,34 @@ public class WorkSource implements Parcelable {
                Global.CHAINED_BATTERY_ATTRIBUTION_ENABLED, 0) == 1;
    }

    /** @hide */
    @UnsupportedAppUsage
    /**
     * Returns the size of this work source.
     * @hide
     */
    @TestApi
    @SystemApi
    public int size() {
        return mNum;
    }

    /** @hide */
    /**
     * @deprecated use {{@link #getUid(int)}} instead.
     * @hide
     */
    @UnsupportedAppUsage
    @TestApi
    @Deprecated
    public int get(int index) {
        return getUid(index);
    }

    /**
     * Get the uid at the given index.
     * If {@code index} < 0 or {@code index} >= {@link #size() N}, then the behavior is undefined.
     * @hide
     */
    @TestApi
    @SystemApi
    public int getUid(int index) {
        return mUids[index];
    }

@@ -167,10 +195,25 @@ public class WorkSource implements Parcelable {
        return mNum > 0 ? mUids[0] : mChains.get(0).getAttributionUid();
    }

    /** @hide */
    /**
     * @deprecated use {{@link #getPackageName(int)}} instead.
     * @hide
     */
    @UnsupportedAppUsage
    @TestApi
    @Deprecated
    public String getName(int index) {
        return getPackageName(index);
    }

    /**
     * Get the package name at the given index.
     * If {@code index} < 0 or {@code index} >= {@link #size() N}, then the behavior is undefined.
     * @hide
     */
    @TestApi
    @SystemApi
    @Nullable
    public String getPackageName(int index) {
        return mNames != null ? mNames[index] : null;
    }

@@ -179,9 +222,8 @@ public class WorkSource implements Parcelable {
     * intact.
     *
     * <p>Useful when combining with another WorkSource that doesn't have names.
     * @hide
     */
    public void clearNames() {
    private void clearNames() {
        if (mNames != null) {
            mNames = null;
            // Clear out any duplicate uids now that we don't have names to disambiguate them.
@@ -406,6 +448,22 @@ public class WorkSource implements Parcelable {
        }
    }

    /**
     * Returns a copy of this work source without any package names.
     * If any {@link WorkChain WorkChains} are present, they are left intact.
     *
     * @return a {@link WorkSource} without any package names.
     * @hide
     */
    @SystemApi
    @TestApi
    @NonNull
    public WorkSource withoutNames() {
        final WorkSource copy = new WorkSource(this);
        copy.clearNames();
        return copy;
    }

    /**
     * Legacy API: DO NOT USE. Only in use from unit tests.
     *
@@ -524,6 +582,8 @@ public class WorkSource implements Parcelable {
     *
     * @hide for internal use only.
     */
    @SystemApi
    @TestApi
    public boolean isEmpty() {
        return mNum == 0 && (mChains == null || mChains.isEmpty());
    }
@@ -532,7 +592,9 @@ public class WorkSource implements Parcelable {
     * @return the list of {@code WorkChains} associated with this {@code WorkSource}.
     * @hide
     */
    public ArrayList<WorkChain> getWorkChains() {
    @SystemApi
    @Nullable
    public List<WorkChain> getWorkChains() {
        return mChains;
    }

+27 −27
Original line number Diff line number Diff line
@@ -4086,7 +4086,7 @@ public class BatteryStatsImpl extends BatteryStats {
        if (workSource != null) {
            for (int i = 0; i < workSource.size(); ++i) {
                uid = mapUid(workSource.get(i));
                uid = mapUid(workSource.getUid(i));
                if (mActiveEvents.updateState(historyItem, name, uid, 0)) {
                    addHistoryEventLocked(elapsedRealtime, uptime, historyItem, name, uid);
                }
@@ -4114,8 +4114,8 @@ public class BatteryStatsImpl extends BatteryStats {
            String tag) {
        if (workSource != null) {
            for (int i = 0; i < workSource.size(); ++i) {
                uid = workSource.get(i);
                final String workSourceName = workSource.getName(i);
                uid = workSource.getUid(i);
                final String workSourceName = workSource.getPackageName(i);
                if (isOnBattery()) {
                    BatteryStatsImpl.Uid.Pkg pkg = getPackageStatsLocked(uid,
@@ -4124,7 +4124,7 @@ public class BatteryStatsImpl extends BatteryStats {
                }
            }
            ArrayList<WorkChain> workChains = workSource.getWorkChains();
            List<WorkChain> workChains = workSource.getWorkChains();
            if (workChains != null) {
                for (int i = 0; i < workChains.size(); ++i) {
                    final WorkChain wc = workChains.get(i);
@@ -4350,7 +4350,7 @@ public class BatteryStatsImpl extends BatteryStats {
        final long uptime = mClocks.uptimeMillis();
        final int N = ws.size();
        for (int i=0; i<N; i++) {
            noteStartWakeLocked(ws.get(i), pid, null, name, historyName, type,
            noteStartWakeLocked(ws.getUid(i), pid, null, name, historyName, type,
                    unimportantForLogging, elapsedRealtime, uptime);
        }
@@ -4379,7 +4379,7 @@ public class BatteryStatsImpl extends BatteryStats {
        // First the starts :
        final int NN = newWs.size();
        for (int i=0; i<NN; i++) {
            noteStartWakeLocked(newWs.get(i), newPid, null, newName, newHistoryName, newType,
            noteStartWakeLocked(newWs.getUid(i), newPid, null, newName, newHistoryName, newType,
                    newUnimportantForLogging, elapsedRealtime, uptime);
        }
        if (wcs != null) {
@@ -4397,7 +4397,7 @@ public class BatteryStatsImpl extends BatteryStats {
        // Then the stops :
        final int NO = ws.size();
        for (int i=0; i<NO; i++) {
            noteStopWakeLocked(ws.get(i), pid, null, name, historyName, type, elapsedRealtime,
            noteStopWakeLocked(ws.getUid(i), pid, null, name, historyName, type, elapsedRealtime,
                    uptime);
        }
        if (wcs != null) {
@@ -4418,7 +4418,7 @@ public class BatteryStatsImpl extends BatteryStats {
        final long uptime = mClocks.uptimeMillis();
        final int N = ws.size();
        for (int i=0; i<N; i++) {
            noteStopWakeLocked(ws.get(i), pid, null, name, historyName, type, elapsedRealtime,
            noteStopWakeLocked(ws.getUid(i), pid, null, name, historyName, type, elapsedRealtime,
                    uptime);
        }
@@ -4441,11 +4441,11 @@ public class BatteryStatsImpl extends BatteryStats {
            WorkSource workSource) {
        final int N = workSource.size();
        for (int i = 0; i < N; ++i) {
            final int uid = mapUid(workSource.get(i));
            final int uid = mapUid(workSource.getUid(i));
            noteLongPartialWakeLockStartInternal(name, historyName, uid);
        }
        final ArrayList<WorkChain> workChains = workSource.getWorkChains();
        final List<WorkChain> workChains = workSource.getWorkChains();
        if (workChains != null) {
            for (int i = 0; i < workChains.size(); ++i) {
                final WorkChain workChain = workChains.get(i);
@@ -4478,11 +4478,11 @@ public class BatteryStatsImpl extends BatteryStats {
            WorkSource workSource) {
        final int N = workSource.size();
        for (int i = 0; i < N; ++i) {
            final int uid = mapUid(workSource.get(i));
            final int uid = mapUid(workSource.getUid(i));
            noteLongPartialWakeLockFinishInternal(name, historyName, uid);
        }
        final ArrayList<WorkChain> workChains = workSource.getWorkChains();
        final List<WorkChain> workChains = workSource.getWorkChains();
        if (workChains != null) {
            for (int i = 0; i < workChains.size(); ++i) {
                final WorkChain workChain = workChains.get(i);
@@ -4615,11 +4615,11 @@ public class BatteryStatsImpl extends BatteryStats {
    public void noteGpsChangedLocked(WorkSource oldWs, WorkSource newWs) {
        for (int i = 0; i < newWs.size(); ++i) {
            noteStartGpsLocked(newWs.get(i), null);
            noteStartGpsLocked(newWs.getUid(i), null);
        }
        for (int i = 0; i < oldWs.size(); ++i) {
            noteStopGpsLocked((oldWs.get(i)), null);
            noteStopGpsLocked((oldWs.getUid(i)), null);
        }
        List<WorkChain>[] wcs = WorkSource.diffChains(oldWs, newWs);
@@ -5564,7 +5564,7 @@ public class BatteryStatsImpl extends BatteryStats {
    public void noteBluetoothScanStartedFromSourceLocked(WorkSource ws, boolean isUnoptimized) {
        final int N = ws.size();
        for (int i = 0; i < N; i++) {
            noteBluetoothScanStartedLocked(null, ws.get(i), isUnoptimized);
            noteBluetoothScanStartedLocked(null, ws.getUid(i), isUnoptimized);
        }
        final List<WorkChain> workChains = ws.getWorkChains();
@@ -5602,7 +5602,7 @@ public class BatteryStatsImpl extends BatteryStats {
    public void noteBluetoothScanStoppedFromSourceLocked(WorkSource ws, boolean isUnoptimized) {
        final int N = ws.size();
        for (int i = 0; i < N; i++) {
            noteBluetoothScanStoppedLocked(null, ws.get(i), isUnoptimized);
            noteBluetoothScanStoppedLocked(null, ws.getUid(i), isUnoptimized);
        }
        final List<WorkChain> workChains = ws.getWorkChains();
@@ -5633,7 +5633,7 @@ public class BatteryStatsImpl extends BatteryStats {
    public void noteBluetoothScanResultsFromSourceLocked(WorkSource ws, int numNewResults) {
        final int N = ws.size();
        for (int i = 0; i < N; i++) {
            int uid = mapUid(ws.get(i));
            int uid = mapUid(ws.getUid(i));
            getUidStatsLocked(uid).noteBluetoothScanResultsLocked(numNewResults);
        }
@@ -5692,7 +5692,7 @@ public class BatteryStatsImpl extends BatteryStats {
            mGlobalWifiRunningTimer.startRunningLocked(elapsedRealtime);
            int N = ws.size();
            for (int i=0; i<N; i++) {
                int uid = mapUid(ws.get(i));
                int uid = mapUid(ws.getUid(i));
                getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
            }
@@ -5715,7 +5715,7 @@ public class BatteryStatsImpl extends BatteryStats {
            final long elapsedRealtime = mClocks.elapsedRealtime();
            int N = oldWs.size();
            for (int i=0; i<N; i++) {
                int uid = mapUid(oldWs.get(i));
                int uid = mapUid(oldWs.getUid(i));
                getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
            }
@@ -5729,7 +5729,7 @@ public class BatteryStatsImpl extends BatteryStats {
            N = newWs.size();
            for (int i=0; i<N; i++) {
                int uid = mapUid(newWs.get(i));
                int uid = mapUid(newWs.getUid(i));
                getUidStatsLocked(uid).noteWifiRunningLocked(elapsedRealtime);
            }
@@ -5757,7 +5757,7 @@ public class BatteryStatsImpl extends BatteryStats {
            mGlobalWifiRunningTimer.stopRunningLocked(elapsedRealtime);
            int N = ws.size();
            for (int i=0; i<N; i++) {
                int uid = mapUid(ws.get(i));
                int uid = mapUid(ws.getUid(i));
                getUidStatsLocked(uid).noteWifiStoppedLocked(elapsedRealtime);
            }
@@ -5963,7 +5963,7 @@ public class BatteryStatsImpl extends BatteryStats {
    public void noteFullWifiLockAcquiredFromSourceLocked(WorkSource ws) {
        int N = ws.size();
        for (int i=0; i<N; i++) {
            final int uid = mapUid(ws.get(i));
            final int uid = mapUid(ws.getUid(i));
            noteFullWifiLockAcquiredLocked(uid);
        }
@@ -5980,7 +5980,7 @@ public class BatteryStatsImpl extends BatteryStats {
    public void noteFullWifiLockReleasedFromSourceLocked(WorkSource ws) {
        int N = ws.size();
        for (int i=0; i<N; i++) {
            final int uid = mapUid(ws.get(i));
            final int uid = mapUid(ws.getUid(i));
            noteFullWifiLockReleasedLocked(uid);
        }
@@ -5997,7 +5997,7 @@ public class BatteryStatsImpl extends BatteryStats {
    public void noteWifiScanStartedFromSourceLocked(WorkSource ws) {
        int N = ws.size();
        for (int i=0; i<N; i++) {
            final int uid = mapUid(ws.get(i));
            final int uid = mapUid(ws.getUid(i));
            noteWifiScanStartedLocked(uid);
        }
@@ -6014,7 +6014,7 @@ public class BatteryStatsImpl extends BatteryStats {
    public void noteWifiScanStoppedFromSourceLocked(WorkSource ws) {
        int N = ws.size();
        for (int i=0; i<N; i++) {
            final int uid = mapUid(ws.get(i));
            final int uid = mapUid(ws.getUid(i));
            noteWifiScanStoppedLocked(uid);
        }
@@ -6031,7 +6031,7 @@ public class BatteryStatsImpl extends BatteryStats {
    public void noteWifiBatchedScanStartedFromSourceLocked(WorkSource ws, int csph) {
        int N = ws.size();
        for (int i=0; i<N; i++) {
            noteWifiBatchedScanStartedLocked(ws.get(i), csph);
            noteWifiBatchedScanStartedLocked(ws.getUid(i), csph);
        }
        final List<WorkChain> workChains = ws.getWorkChains();
@@ -6045,7 +6045,7 @@ public class BatteryStatsImpl extends BatteryStats {
    public void noteWifiBatchedScanStoppedFromSourceLocked(WorkSource ws) {
        int N = ws.size();
        for (int i=0; i<N; i++) {
            noteWifiBatchedScanStoppedLocked(ws.get(i));
            noteWifiBatchedScanStoppedLocked(ws.getUid(i));
        }
        final List<WorkChain> workChains = ws.getWorkChains();
Loading