| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package android.content.pm.split; |
| 17 |
|
| 18 |
import android.annotation.IntRange; |
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.content.pm.parsing.PackageLite; |
| 21 |
import android.util.IntArray; |
| 22 |
import android.util.SparseArray; |
| 23 |
|
| 24 |
import libcore.util.EmptyArray; |
| 25 |
|
| 26 |
import java.util.Arrays; |
| 27 |
import java.util.BitSet; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public abstract class SplitDependencyLoader<E extends Exception> { |
| 42 |
private final @NonNull SparseArray<int[]> mDependencies; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
protected SplitDependencyLoader(@NonNull SparseArray<int[]> dependencies) { |
| 50 |
mDependencies = dependencies; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
protected void loadDependenciesForSplit(@IntRange(from = 0) int splitIdx) throws E { |
| 62 |
|
| 63 |
if (isSplitCached(splitIdx)) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
if (splitIdx == 0) { |
| 69 |
final int[] configSplitIndices = collectConfigSplitIndices(0); |
| 70 |
constructSplit(0, configSplitIndices, -1); |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
final IntArray linearDependencies = new IntArray(); |
| 76 |
linearDependencies.add(splitIdx); |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
while (true) { |
| 81 |
|
| 82 |
|
| 83 |
final int[] deps = mDependencies.get(splitIdx); |
| 84 |
if (deps != null && deps.length > 0) { |
| 85 |
splitIdx = deps[0]; |
| 86 |
} else { |
| 87 |
splitIdx = -1; |
| 88 |
} |
| 89 |
|
| 90 |
if (splitIdx < 0 || isSplitCached(splitIdx)) { |
| 91 |
break; |
| 92 |
} |
| 93 |
|
| 94 |
linearDependencies.add(splitIdx); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
int parentIdx = splitIdx; |
| 99 |
for (int i = linearDependencies.size() - 1; i >= 0; i--) { |
| 100 |
final int idx = linearDependencies.get(i); |
| 101 |
final int[] configSplitIndices = collectConfigSplitIndices(idx); |
| 102 |
constructSplit(idx, configSplitIndices, parentIdx); |
| 103 |
parentIdx = idx; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
private @NonNull int[] collectConfigSplitIndices(int splitIdx) { |
| 108 |
|
| 109 |
final int[] deps = mDependencies.get(splitIdx); |
| 110 |
if (deps == null || deps.length <= 1) { |
| 111 |
return EmptyArray.INT; |
| 112 |
} |
| 113 |
return Arrays.copyOfRange(deps, 1, deps.length); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
protected abstract boolean isSplitCached(@IntRange(from = 0) int splitIdx); |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
protected abstract void constructSplit(@IntRange(from = 0) int splitIdx, |
| 134 |
@NonNull @IntRange(from = 1) int[] configSplitIndices, |
| 135 |
@IntRange(from = -1) int parentSplitIdx) throws E; |
| 136 |
|
| 137 |
public static class IllegalDependencyException extends Exception { |
| 138 |
private IllegalDependencyException(String message) { |
| 139 |
super(message); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
private static int[] append(int[] src, int elem) { |
| 144 |
if (src == null) { |
| 145 |
return new int[] { elem }; |
| 146 |
} |
| 147 |
int[] dst = Arrays.copyOf(src, src.length + 1); |
| 148 |
dst[src.length] = elem; |
| 149 |
return dst; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public static @NonNull SparseArray<int[]> createDependenciesFromPackage(PackageLite pkg) |
| 162 |
throws IllegalDependencyException { |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
final SparseArray<int[]> splitDependencies = new SparseArray<>(); |
| 167 |
|
| 168 |
|
| 169 |
splitDependencies.put(0, new int[] {-1}); |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
for (int splitIdx = 0; splitIdx < pkg.getSplitNames().length; splitIdx++) { |
| 174 |
if (!pkg.getIsFeatureSplits()[splitIdx]) { |
| 175 |
|
| 176 |
continue; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
final int targetIdx; |
| 181 |
final String splitDependency = pkg.getUsesSplitNames()[splitIdx]; |
| 182 |
if (splitDependency != null) { |
| 183 |
final int depIdx = Arrays.binarySearch(pkg.getSplitNames(), splitDependency); |
| 184 |
if (depIdx < 0) { |
| 185 |
throw new IllegalDependencyException("Split '" + pkg.getSplitNames()[splitIdx] |
| 186 |
+ "' requires split '" + splitDependency + "', which is missing."); |
| 187 |
} |
| 188 |
targetIdx = depIdx + 1; |
| 189 |
} else { |
| 190 |
|
| 191 |
targetIdx = 0; |
| 192 |
} |
| 193 |
splitDependencies.put(splitIdx + 1, new int[] {targetIdx}); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
for (int splitIdx = 0, size = pkg.getSplitNames().length; splitIdx < size; splitIdx++) { |
| 201 |
if (pkg.getIsFeatureSplits()[splitIdx]) { |
| 202 |
|
| 203 |
continue; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
final int targetSplitIdx; |
| 208 |
final String configForSplit = pkg.getConfigForSplit()[splitIdx]; |
| 209 |
if (configForSplit != null) { |
| 210 |
final int depIdx = Arrays.binarySearch(pkg.getSplitNames(), configForSplit); |
| 211 |
if (depIdx < 0) { |
| 212 |
throw new IllegalDependencyException("Split '" + pkg.getSplitNames()[splitIdx] |
| 213 |
+ "' targets split '" + configForSplit + "', which is missing."); |
| 214 |
} |
| 215 |
|
| 216 |
if (!pkg.getIsFeatureSplits()[depIdx]) { |
| 217 |
throw new IllegalDependencyException("Split '" + pkg.getSplitNames()[splitIdx] |
| 218 |
+ "' declares itself as configuration split for a non-feature split '" |
| 219 |
+ pkg.getSplitNames()[depIdx] + "'"); |
| 220 |
} |
| 221 |
targetSplitIdx = depIdx + 1; |
| 222 |
} else { |
| 223 |
targetSplitIdx = 0; |
| 224 |
} |
| 225 |
splitDependencies.put(targetSplitIdx, |
| 226 |
append(splitDependencies.get(targetSplitIdx), splitIdx + 1)); |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
final BitSet bitset = new BitSet(); |
| 231 |
for (int i = 0, size = splitDependencies.size(); i < size; i++) { |
| 232 |
int splitIdx = splitDependencies.keyAt(i); |
| 233 |
|
| 234 |
bitset.clear(); |
| 235 |
while (splitIdx != -1) { |
| 236 |
|
| 237 |
if (bitset.get(splitIdx)) { |
| 238 |
throw new IllegalDependencyException("Cycle detected in split dependencies."); |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
bitset.set(splitIdx); |
| 243 |
|
| 244 |
|
| 245 |
final int[] deps = splitDependencies.get(splitIdx); |
| 246 |
splitIdx = deps != null ? deps[0] : -1; |
| 247 |
} |
| 248 |
} |
| 249 |
return splitDependencies; |
| 250 |
} |
| 251 |
} |