Making WorkSource uids not nullable to comply with WorkSource.h

Bug: b/273752423
Test: atest WorkSourceTest, atest WorkSourceParcelTest
Change-Id: I8c6764cc9fb555ff7b9baa7ed487eba033320e45
This commit is contained in:
Oleg Petsjonkin
2023-03-28 19:01:11 +00:00
committed by Oleg Petšjonkin
parent a7ed3f3819
commit c730a448a2
2 changed files with 29 additions and 39 deletions

View File

@@ -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;
}
mUids = orig.mUids.clone();
mNames = orig.mNames != null ? orig.mNames.clone() : 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,30 +317,22 @@ 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) {
System.arraycopy(other.mUids, 0, mUids, 0, mNum);
if (mUids.length >= mNum) { // this has more data than other
System.arraycopy(other.mUids, 0, mUids, 0, mNum);
} else {
mUids = other.mUids.clone();
}
if (other.mNames != null) {
if (mNames != null && mNames.length >= mNum) {
System.arraycopy(other.mNames, 0, mNames, 0, mNum);
} else {
mUids = other.mUids.clone();
}
if (other.mNames != null) {
if (mNames != null && mNames.length >= mNum) {
System.arraycopy(other.mNames, 0, mNames, 0, mNum);
} else {
mNames = other.mNames.clone();
}
} else {
mNames = null;
mNames = other.mNames.clone();
}
} else {
mUids = null;
mNames = 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);
}

View File

@@ -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);