summaryrefslogtreecommitdiff
path: root/core/java/android
diff options
context:
space:
mode:
authorNarayan Kamath <narayan@google.com>2017-12-27 08:43:37 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2017-12-27 08:43:37 +0000
commitbbab9c4e28dc8dd7101d9cbf7ec860beb32a4048 (patch)
treed030b4a4e3042b8fe8aafc4f86b8b52eadbfad7a /core/java/android
parent6c313d3224c878d832db3ed833f4a3dd3786fb1f (diff)
parent8182202ac884331a8f0a8d5094d5aad7d9323c08 (diff)
Merge "Support WorkChains for WakeLock start / stop / change events."
Diffstat (limited to 'core/java/android')
-rw-r--r--core/java/android/os/PowerManager.java4
-rw-r--r--core/java/android/os/WorkSource.java67
2 files changed, 69 insertions, 2 deletions
diff --git a/core/java/android/os/PowerManager.java b/core/java/android/os/PowerManager.java
index 56c639135657..cd6d41b3f43c 100644
--- a/core/java/android/os/PowerManager.java
+++ b/core/java/android/os/PowerManager.java
@@ -1540,7 +1540,7 @@ public final class PowerManager {
*/
public void setWorkSource(WorkSource ws) {
synchronized (mToken) {
- if (ws != null && ws.size() == 0) {
+ if (ws != null && ws.isEmpty()) {
ws = null;
}
@@ -1552,7 +1552,7 @@ public final class PowerManager {
changed = true;
mWorkSource = new WorkSource(ws);
} else {
- changed = mWorkSource.diff(ws);
+ changed = !mWorkSource.equals(ws);
if (changed) {
mWorkSource.set(ws);
}
diff --git a/core/java/android/os/WorkSource.java b/core/java/android/os/WorkSource.java
index bf145a08e06c..e9d4fe82cec2 100644
--- a/core/java/android/os/WorkSource.java
+++ b/core/java/android/os/WorkSource.java
@@ -456,6 +456,16 @@ public class WorkSource implements Parcelable {
}
/**
+ * Returns {@code true} iff. this work source contains zero UIDs and zero WorkChains to
+ * attribute usage to.
+ *
+ * @hide for internal use only.
+ */
+ public boolean isEmpty() {
+ return mNum == 0 && (mChains == null || mChains.isEmpty());
+ }
+
+ /**
* @return the list of {@code WorkChains} associated with this {@code WorkSource}.
* @hide
*/
@@ -842,6 +852,14 @@ public class WorkSource implements Parcelable {
return this;
}
+ /**
+ * Return the UID to which this WorkChain should be attributed to, i.e, the UID performing
+ * the actual work.
+ */
+ public int getAttributionUid() {
+ return mUids[mSize - 1];
+ }
+
// TODO: The following three trivial getters are purely for testing and will be removed
// once we have higher level logic in place, e.g for serializing this WorkChain to a proto,
// diffing it etc.
@@ -932,6 +950,55 @@ public class WorkSource implements Parcelable {
};
}
+ /**
+ * Computes the differences in WorkChains contained between {@code oldWs} and {@code newWs}.
+ *
+ * Returns {@code null} if no differences exist, otherwise returns a two element array. The
+ * first element is a list of "new" chains, i.e WorkChains present in {@code newWs} but not in
+ * {@code oldWs}. The second element is a list of "gone" chains, i.e WorkChains present in
+ * {@code oldWs} but not in {@code newWs}.
+ *
+ * @hide
+ */
+ public static ArrayList<WorkChain>[] diffChains(WorkSource oldWs, WorkSource newWs) {
+ ArrayList<WorkChain> newChains = null;
+ ArrayList<WorkChain> goneChains = null;
+
+ // TODO(narayan): This is a dumb O(M*N) algorithm that determines what has changed across
+ // WorkSource objects. We can replace this with something smarter, for e.g by defining
+ // a Comparator between WorkChains. It's unclear whether that will be more efficient if
+ // the number of chains associated with a WorkSource is expected to be small
+ if (oldWs.mChains != null) {
+ for (int i = 0; i < oldWs.mChains.size(); ++i) {
+ final WorkChain wc = oldWs.mChains.get(i);
+ if (newWs.mChains == null || !newWs.mChains.contains(wc)) {
+ if (goneChains == null) {
+ goneChains = new ArrayList<>(oldWs.mChains.size());
+ }
+ goneChains.add(wc);
+ }
+ }
+ }
+
+ if (newWs.mChains != null) {
+ for (int i = 0; i < newWs.mChains.size(); ++i) {
+ final WorkChain wc = newWs.mChains.get(i);
+ if (oldWs.mChains == null || !oldWs.mChains.contains(wc)) {
+ if (newChains == null) {
+ newChains = new ArrayList<>(newWs.mChains.size());
+ }
+ newChains.add(wc);
+ }
+ }
+ }
+
+ if (newChains != null || goneChains != null) {
+ return new ArrayList[] { newChains, goneChains };
+ }
+
+ return null;
+ }
+
@Override
public int describeContents() {
return 0;