Merge "Move duplicated union of HashSets logic into helper class"
diff --git a/services/backup/java/com/android/server/backup/RefactoredBackupManagerService.java b/services/backup/java/com/android/server/backup/RefactoredBackupManagerService.java
index bc5bebb..c760806 100644
--- a/services/backup/java/com/android/server/backup/RefactoredBackupManagerService.java
+++ b/services/backup/java/com/android/server/backup/RefactoredBackupManagerService.java
@@ -118,10 +118,14 @@
import com.android.server.backup.utils.AppBackupUtils;
import com.android.server.backup.utils.BackupManagerMonitorUtils;
import com.android.server.backup.utils.BackupObserverUtils;
+import com.android.server.backup.utils.PasswordUtils;
+import com.android.server.backup.utils.SparseArrayUtils;
import com.android.server.power.BatterySaverPolicy.ServiceType;
import libcore.io.IoUtils;
+import com.google.android.collect.Sets;
+
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
@@ -2300,21 +2304,13 @@
}
// a caller with full permission can ask to back up any participating app
- HashSet<String> targets = new HashSet<>();
if (PACKAGE_MANAGER_SENTINEL.equals(packageName)) {
- targets.add(PACKAGE_MANAGER_SENTINEL);
+ return Sets.newHashSet(PACKAGE_MANAGER_SENTINEL);
} else {
synchronized (mBackupParticipants) {
- int N = mBackupParticipants.size();
- for (int i = 0; i < N; i++) {
- HashSet<String> s = mBackupParticipants.valueAt(i);
- if (s != null) {
- targets.addAll(s);
- }
- }
+ return SparseArrayUtils.union(mBackupParticipants);
}
}
- return targets;
}
private void writeToJournalLocked(String str) {
@@ -2398,14 +2394,7 @@
// a caller with full permission can ask to back up any participating app
// !!! TODO: allow data-clear of ANY app?
if (MORE_DEBUG) Slog.v(TAG, "Privileged caller, allowing clear of other apps");
- apps = new HashSet<>();
- int N = mBackupParticipants.size();
- for (int i = 0; i < N; i++) {
- HashSet<String> s = mBackupParticipants.valueAt(i);
- if (s != null) {
- apps.addAll(s);
- }
- }
+ apps = SparseArrayUtils.union(mBackupParticipants);
}
// Is the given app an available participant?
diff --git a/services/backup/java/com/android/server/backup/utils/SparseArrayUtils.java b/services/backup/java/com/android/server/backup/utils/SparseArrayUtils.java
new file mode 100644
index 0000000..954d714
--- /dev/null
+++ b/services/backup/java/com/android/server/backup/utils/SparseArrayUtils.java
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2017 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.server.backup.utils;
+
+import android.util.SparseArray;
+
+import java.util.HashSet;
+
+/**
+ * Helper functions for manipulating instances of {@link SparseArray}.
+ */
+public final class SparseArrayUtils {
+ // Statics only
+ private SparseArrayUtils() {}
+
+ /**
+ * Given a {@link SparseArray<HashSet>}, returns a new {@link HashSet} containing every element
+ * from every set in the array.
+ *
+ * @param sets The array of sets from which to take the union.
+ * @param <V> The type of element contained in the set.
+ * @return The complete set.
+ */
+ public static<V> HashSet<V> union(SparseArray<HashSet<V>> sets) {
+ HashSet<V> unionSet = new HashSet<>();
+ int n = sets.size();
+ for (int i = 0; i < n; i++) {
+ HashSet<V> ithSet = sets.valueAt(i);
+ if (ithSet != null) {
+ unionSet.addAll(ithSet);
+ }
+ }
+ return unionSet;
+ }
+}
diff --git a/services/tests/servicestests/src/com/android/server/backup/utils/SparseArrayUtilsTest.java b/services/tests/servicestests/src/com/android/server/backup/utils/SparseArrayUtilsTest.java
new file mode 100644
index 0000000..db55120
--- /dev/null
+++ b/services/tests/servicestests/src/com/android/server/backup/utils/SparseArrayUtilsTest.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2017 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.server.backup.utils;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.platform.test.annotations.Presubmit;
+import android.support.test.filters.SmallTest;
+import android.support.test.runner.AndroidJUnit4;
+import android.util.SparseArray;
+
+import com.google.android.collect.Sets;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.util.HashSet;
+
+@SmallTest
+@Presubmit
+@RunWith(AndroidJUnit4.class)
+public class SparseArrayUtilsTest {
+ @Test
+ public void union_mergesSets() {
+ SparseArray<HashSet<String>> sparseArray = new SparseArray<>();
+ sparseArray.append(12, Sets.newHashSet("a", "b", "c"));
+ sparseArray.append(45, Sets.newHashSet("d", "e"));
+ sparseArray.append(46, Sets.newHashSet());
+ sparseArray.append(66, Sets.newHashSet("a", "e", "f"));
+
+ assertThat(SparseArrayUtils.union(sparseArray)).isEqualTo(
+ Sets.newHashSet("a", "b", "c", "d", "e", "f"));
+ }
+
+ @Test
+ public void union_returnsEmptySetForEmptyList() {
+ SparseArray<HashSet<String>> sparseArray = new SparseArray<>();
+
+ assertThat(SparseArrayUtils.union(sparseArray)).isEqualTo(Sets.newHashSet());
+ }
+}