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

Commit c8953556 authored by Robert Berry's avatar Robert Berry
Browse files

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
parent 6c1cb7f0
Loading
Loading
Loading
Loading
+7 −18
Original line number Diff line number Diff line
@@ -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?
+49 −0
Original line number Diff line number Diff line
/*
 * 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;
    }
}
+55 −0
Original line number Diff line number Diff line
/*
 * 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());
    }
}