diff options
| author | 2017-07-12 18:25:59 +0100 | |
|---|---|---|
| committer | 2017-07-14 14:10:51 +0000 | |
| commit | c89535567bc80583fd6ca7cfecda44d124456ee3 (patch) | |
| tree | 352777f456f33bd654a6bb9c2f8477029dd6ddf4 | |
| parent | 6c1cb7f0902df73b66419d5037de485f2b9727fa (diff) | |
Move duplicated union of HashSets logic into helper class
Bug: 36850431
Test: adb shell am instrument -w -e package com.android.server.backup com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner
Change-Id: I83a95919d7b85131cc7ad73575d7a89c7f2d78f9
3 files changed, 111 insertions, 18 deletions
diff --git a/services/backup/java/com/android/server/backup/RefactoredBackupManagerService.java b/services/backup/java/com/android/server/backup/RefactoredBackupManagerService.java index 674e9725cc8a..3c28d87ee0cf 100644 --- a/services/backup/java/com/android/server/backup/RefactoredBackupManagerService.java +++ b/services/backup/java/com/android/server/backup/RefactoredBackupManagerService.java @@ -117,10 +117,14 @@ import com.android.server.backup.restore.PerformUnifiedRestoreTask; 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; @@ -2317,21 +2321,13 @@ public class RefactoredBackupManagerService implements BackupManagerServiceInter } // 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) { @@ -2423,14 +2419,7 @@ public class RefactoredBackupManagerService implements BackupManagerServiceInter // 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 000000000000..954d714f6d60 --- /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 000000000000..db55120dc023 --- /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()); + } +} |