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

Commit c730a448 authored by Oleg Petsjonkin's avatar Oleg Petsjonkin Committed by Oleg Petšjonkin
Browse files

Making WorkSource uids not nullable to comply with WorkSource.h

Bug: b/273752423
Test: atest WorkSourceTest, atest WorkSourceParcelTest
Change-Id: I8c6764cc9fb555ff7b9baa7ed487eba033320e45
parent a7ed3f38
Loading
Loading
Loading
Loading
+27 −36
Original line number Diff line number Diff line
@@ -12,11 +12,11 @@ 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;
import java.util.Objects;

/**
 * Describes the source of some work that may be done by someone else.
@@ -29,9 +29,13 @@ public class WorkSource implements Parcelable {

    @UnsupportedAppUsage
    int mNum;

    @UnsupportedAppUsage
    int[] mUids;
    @NonNull
    int[] mUids = new int[0];

    @UnsupportedAppUsage
    @Nullable
    String[] mNames;

    private ArrayList<WorkChain> mChains;
@@ -73,13 +77,8 @@ public class WorkSource implements Parcelable {
            return;
        }
        mNum = orig.mNum;
        if (orig.mUids != null) {
        mUids = orig.mUids.clone();
        mNames = orig.mNames != null ? orig.mNames.clone() : null;
        } else {
            mUids = null;
            mNames = null;
        }

        if (orig.mChains != null) {
            // Make a copy of all WorkChains that exist on |orig| since they are mutable.
@@ -114,7 +113,7 @@ public class WorkSource implements Parcelable {
     */
    @SystemApi
    public WorkSource(int uid, @NonNull String packageName) {
        Preconditions.checkNotNull(packageName, "packageName can't be null");
        Objects.requireNonNull(packageName, "packageName can't be null");
        mNum = 1;
        mUids = new int[] { uid, 0 };
        mNames = new String[] { packageName, null };
@@ -124,7 +123,7 @@ public class WorkSource implements Parcelable {
    @UnsupportedAppUsage
    WorkSource(Parcel in) {
        mNum = in.readInt();
        mUids = in.createIntArray();
        mUids = Objects.requireNonNullElse(in.createIntArray(), new int[0]);
        mNames = in.createStringArray();

        int numChains = in.readInt();
@@ -318,15 +317,11 @@ public class WorkSource implements Parcelable {
     */
    public void set(WorkSource other) {
        if (other == null) {
            mNum = 0;
            if (mChains != null) {
                mChains.clear();
            }
            clear();
            return;
        }
        mNum = other.mNum;
        if (other.mUids != null) {
            if (mUids != null && mUids.length >= mNum) {
        if (mUids.length >= mNum) { // this has more data than other
            System.arraycopy(other.mUids, 0, mUids, 0, mNum);
        } else {
            mUids = other.mUids.clone();
@@ -340,10 +335,6 @@ public class WorkSource implements Parcelable {
        } else {
            mNames = null;
        }
        } else {
            mUids = null;
            mNames = null;
        }

        if (other.mChains != null) {
            if (mChains != null) {
@@ -361,7 +352,7 @@ public class WorkSource implements Parcelable {
    /** @hide */
    public void set(int uid) {
        mNum = 1;
        if (mUids == null) mUids = new int[2];
        if (mUids.length == 0) mUids = new int[2];
        mUids[0] = uid;
        mNames = null;
        if (mChains != null) {
@@ -375,7 +366,7 @@ public class WorkSource implements Parcelable {
            throw new NullPointerException("Name can't be null");
        }
        mNum = 1;
        if (mUids == null) {
        if (mUids.length == 0) {
            mUids = new int[2];
            mNames = new String[2];
        }
@@ -727,7 +718,7 @@ public class WorkSource implements Parcelable {
                if (DEBUG) Log.d(TAG, "i1=" + i1 + " i2=" + i2 + " N1=" + N1
                        + ": insert " + uids2[i2]);
                changed = true;
                if (uids1 == null) {
                if (uids1.length == 0) {
                    uids1 = new int[4];
                    uids1[0] = uids2[i2];
                } else if (N1 >= uids1.length) {
@@ -866,7 +857,7 @@ public class WorkSource implements Parcelable {

    private void insert(int index, int uid)  {
        if (DEBUG) Log.d(TAG, "Insert in " + this + " @ " + index + " uid " + uid);
        if (mUids == null) {
        if (mUids.length == 0) {
            mUids = new int[4];
            mUids[0] = uid;
            mNum = 1;
@@ -891,7 +882,7 @@ public class WorkSource implements Parcelable {
    }

    private void insert(int index, int uid, String name)  {
        if (mUids == null) {
        if (mNum == 0) {
            mUids = new int[4];
            mUids[0] = uid;
            mNames = new String[4];
@@ -1244,8 +1235,8 @@ public class WorkSource implements Parcelable {
        proto.end(workSourceToken);
    }

    public static final @android.annotation.NonNull Parcelable.Creator<WorkSource> CREATOR
            = new Parcelable.Creator<WorkSource>() {
    @NonNull
    public static final Parcelable.Creator<WorkSource> CREATOR = new Parcelable.Creator<>() {
        public WorkSource createFromParcel(Parcel in) {
            return new WorkSource(in);
        }
+2 −3
Original line number Diff line number Diff line
@@ -55,7 +55,6 @@ public class WorkSourceParcelTest {
        unparcelWorkSourceFromNativeAndVerify(/* uids= */ null , /* names= */ null);
        unparcelWorkSourceFromNativeAndVerify(uids1, /* names= */ null);
        unparcelWorkSourceFromNativeAndVerify(uids2, /* names= */ null);
        //TODO(b/273752423)
        unparcelWorkSourceFromNativeAndVerify(/* uids= */ null , names1);
        unparcelWorkSourceFromNativeAndVerify(uids1, names1);
        unparcelWorkSourceFromNativeAndVerify(uids2, names2);
@@ -70,8 +69,8 @@ public class WorkSourceParcelTest {
        final int[] uids2 = {1000, 2000};
        final String[] names1 = {"testGetWorkSource1"};
        final String[] names2 = {"testGetWorkSource1", "testGetWorkSource2"};
        //TODO(b/273752423)
        //parcelWorkSourceToNativeAndVerify(/* uids= */ null , /* names= */ null );

        parcelWorkSourceToNativeAndVerify(/* uids= */ null , /* names= */ null);
        parcelWorkSourceToNativeAndVerify(uids1, /* names= */ null);
        parcelWorkSourceToNativeAndVerify(uids2, /* names= */ null);
        parcelWorkSourceToNativeAndVerify(uids1, names1);