blob: 1565d9ce77d492a50c0f02f9395b3667db4f9d66 [file] [log] [blame]
Paul Duffinbeee5dc2018-01-23 13:39:00 +00001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package android.content.pm;
17
18import android.annotation.NonNull;
19import android.annotation.Nullable;
20
21import com.android.internal.annotations.VisibleForTesting;
22import com.android.internal.util.ArrayUtils;
23
24import java.util.ArrayList;
25
26/**
27 * Base for classes that update a {@link PackageParser.Package}'s shared libraries.
28 *
29 * @hide
30 */
31@VisibleForTesting
32public abstract class PackageSharedLibraryUpdater {
33
34 /**
35 * Update the package's shared libraries.
36 *
37 * @param pkg the package to update.
38 */
39 public abstract void updatePackage(PackageParser.Package pkg);
40
Paul Duffin0692a562018-01-09 12:35:32 +000041 static void removeLibrary(PackageParser.Package pkg, String libraryName) {
42 pkg.usesLibraries = ArrayUtils.remove(pkg.usesLibraries, libraryName);
43 pkg.usesOptionalLibraries =
44 ArrayUtils.remove(pkg.usesOptionalLibraries, libraryName);
45 }
46
Paul Duffinbeee5dc2018-01-23 13:39:00 +000047 static @NonNull
48 <T> ArrayList<T> prefix(@Nullable ArrayList<T> cur, T val) {
49 if (cur == null) {
50 cur = new ArrayList<>();
51 }
52 cur.add(0, val);
53 return cur;
54 }
55
Paul Duffin0692a562018-01-09 12:35:32 +000056 private static boolean isLibraryPresent(ArrayList<String> usesLibraries,
Paul Duffinbeee5dc2018-01-23 13:39:00 +000057 ArrayList<String> usesOptionalLibraries, String apacheHttpLegacy) {
58 return ArrayUtils.contains(usesLibraries, apacheHttpLegacy)
59 || ArrayUtils.contains(usesOptionalLibraries, apacheHttpLegacy);
60 }
Paul Duffin0692a562018-01-09 12:35:32 +000061
Paul Duffin0692a562018-01-09 12:35:32 +000062 /**
63 * Add an implicit dependency.
64 *
65 * <p>If the package has an existing dependency on {@code existingLibrary} then prefix it with
66 * the {@code implicitDependency} if it is not already in the list of libraries.
67 *
68 * @param pkg the {@link PackageParser.Package} to update.
69 * @param existingLibrary the existing library.
70 * @param implicitDependency the implicit dependency to add
71 */
72 void prefixImplicitDependency(PackageParser.Package pkg, String existingLibrary,
73 String implicitDependency) {
74 ArrayList<String> usesLibraries = pkg.usesLibraries;
75 ArrayList<String> usesOptionalLibraries = pkg.usesOptionalLibraries;
76
77 if (!isLibraryPresent(usesLibraries, usesOptionalLibraries, implicitDependency)) {
78 if (ArrayUtils.contains(usesLibraries, existingLibrary)) {
79 prefix(usesLibraries, implicitDependency);
80 } else if (ArrayUtils.contains(usesOptionalLibraries, existingLibrary)) {
81 prefix(usesOptionalLibraries, implicitDependency);
82 }
83
84 pkg.usesLibraries = usesLibraries;
85 pkg.usesOptionalLibraries = usesOptionalLibraries;
86 }
87 }
88
89 void prefixRequiredLibrary(PackageParser.Package pkg, String libraryName) {
90 ArrayList<String> usesLibraries = pkg.usesLibraries;
91 ArrayList<String> usesOptionalLibraries = pkg.usesOptionalLibraries;
92
93 boolean alreadyPresent = isLibraryPresent(
94 usesLibraries, usesOptionalLibraries, libraryName);
95 if (!alreadyPresent) {
96 usesLibraries = prefix(usesLibraries, libraryName);
97
98 pkg.usesLibraries = usesLibraries;
99 }
100 }
Paul Duffinbeee5dc2018-01-23 13:39:00 +0000101}