/** * Obtains a predicate that checks if a file is in an input set. * * @param inputSet the input set * @return the predicate */ @NonNull private Function<File, RelativeFile> inInputSet(@NonNull InputSet inputSet) { Map<File, RelativeFile> inverseFiltered = mFiles.entrySet().stream() .filter(e -> e.getValue() == inputSet) .map(Map.Entry::getKey) .collect( HashMap::new, (m, rf) -> m.put(rf.getFile(), rf), Map::putAll); return inverseFiltered::get; }
/** * Writes incremental state. * * @throws IOException failed to write state */ private void writeState() throws IOException { File stateFile = new File(mIncrementalDir, STATE_FILE); Properties props = new Properties(); int currIdx = 0; for (BiMap.Entry<RelativeFile, String> entry : mNameMap.entrySet()) { props.put(BASE_KEY_PREFIX + currIdx, entry.getKey().getBase().getPath()); props.put(FILE_KEY_PREFIX + currIdx, entry.getKey().getFile().getPath()); props.put(RENAMED_KEY_PREFIX + currIdx, entry.getValue()); currIdx++; } Closer closer = Closer.create(); try { props.store(closer.register(new FileWriter(stateFile)), null); } catch (Throwable t) { throw closer.rethrow(t); } finally { closer.close(); } }
/** * Obtains all relative files stored in the save data that have the provided input set and * whose files are included in the provided set of files. This method allows retrieving * the original relative files from the files, while filtering for the desired input set. * * @param files the files to filter * @param inputSet the input set to filter * @return all saved relative files that have the given input set and whose files exist * in the provided set */ @NonNull private ImmutableSet<RelativeFile> find(@NonNull Set<File> files, @NonNull InputSet inputSet) { Set<RelativeFile> found = Sets.newHashSet(); for (RelativeFile rf : Maps.filterValues(mFiles, Predicates.equalTo(inputSet)).keySet()) { if (files.contains(rf.getFile())) { found.add(rf); } } return ImmutableSet.copyOf(found); }
/** * Updates the atom metadata file. * * @param files the atom metadata folder. * @throws IOException failed */ public void updateAtomMetadata(@NonNull ImmutableMap<RelativeFile, FileStatus> files) throws IOException { Predicate<File> isAtomMetadataFile = input -> input.getName().equals(SdkConstants.FN_ATOM_METADATA) || input.getName().equals(SdkConstants.FN_INSTANTAPP_METADATA); updateFiles(PackagedFileUpdates.fromIncrementalRelativeFileSet( Maps.filterKeys(files, rf -> isAtomMetadataFile.test(rf.getFile()))) .stream() .map(pfu -> new PackagedFileUpdate( pfu.getSource(), "META-INF/" + pfu.getName(), pfu.getStatus())) .collect(Collectors.toSet()) ); }
Verify.verify(!basePath.isEmpty()); String filePath = Verify.verifyNotNull(rf.getFile().getPath()); Verify.verify(!filePath.isEmpty());
/** * Incrementally updates a resource in the packaging. The resource can be added or removed, * depending on the change made to the file. * * @param file the file to update * @param modificationType the type of file modification * @throws PackagerException failed to update the package */ public void updateResource(@NonNull RelativeFile file, @NonNull FileStatus modificationType) throws PackagerException { if (modificationType == FileStatus.NEW || modificationType == FileStatus.CHANGED) { doAddFile(file.getFile(), file.getOsIndependentRelativePath()); } else { throw new UnsupportedOperationException("Cannot remove a file from archive."); } }
mApkCreator.writeFile(rf.getSource().getFile(), rf.getName());