Search in sources :

Example 1 with RepoParserCallback

use of de.robv.android.xposed.installer.repo.RepoParser.RepoParserCallback in project XposedInstaller by rovo89.

the class RepoLoader method downloadAndParseFiles.

private boolean downloadAndParseFiles(List<String> messages) {
    // These variables don't need to be atomic, just mutable
    final AtomicBoolean hasChanged = new AtomicBoolean(false);
    final AtomicInteger insertCounter = new AtomicInteger();
    final AtomicInteger deleteCounter = new AtomicInteger();
    for (Entry<Long, Repository> repoEntry : mRepositories.entrySet()) {
        final long repoId = repoEntry.getKey();
        final Repository repo = repoEntry.getValue();
        String url = (repo.partialUrl != null && repo.version != null) ? String.format(repo.partialUrl, repo.version) : repo.url;
        File cacheFile = getRepoCacheFile(url);
        SyncDownloadInfo info = DownloadsUtil.downloadSynchronously(url, cacheFile);
        Log.i(XposedApp.TAG, String.format("Downloaded %s with status %d (error: %s), size %d bytes", url, info.status, info.errorMessage, cacheFile.length()));
        if (info.status != SyncDownloadInfo.STATUS_SUCCESS) {
            if (info.errorMessage != null)
                messages.add(info.errorMessage);
            continue;
        }
        InputStream in = null;
        RepoDb.beginTransation();
        try {
            in = new FileInputStream(cacheFile);
            if (url.endsWith(".gz"))
                in = new GZIPInputStream(in);
            RepoParser.parse(in, new RepoParserCallback() {

                @Override
                public void onRepositoryMetadata(Repository repository) {
                    if (!repository.isPartial) {
                        RepoDb.deleteAllModules(repoId);
                        hasChanged.set(true);
                    }
                }

                @Override
                public void onNewModule(Module module) {
                    RepoDb.insertModule(repoId, module);
                    hasChanged.set(true);
                    insertCounter.incrementAndGet();
                }

                @Override
                public void onRemoveModule(String packageName) {
                    RepoDb.deleteModule(repoId, packageName);
                    hasChanged.set(true);
                    deleteCounter.decrementAndGet();
                }

                @Override
                public void onCompleted(Repository repository) {
                    if (!repository.isPartial) {
                        RepoDb.updateRepository(repoId, repository);
                        repo.name = repository.name;
                        repo.partialUrl = repository.partialUrl;
                        repo.version = repository.version;
                    } else {
                        RepoDb.updateRepositoryVersion(repoId, repository.version);
                        repo.version = repository.version;
                    }
                    Log.i(XposedApp.TAG, String.format("Updated repository %s to version %s (%d new / %d removed modules)", repo.url, repo.version, insertCounter.get(), deleteCounter.get()));
                }
            });
            RepoDb.setTransactionSuccessful();
        } catch (Throwable t) {
            Log.e(XposedApp.TAG, "Cannot load repository from " + url, t);
            messages.add(sApp.getString(R.string.repo_load_failed, url, t.getMessage()));
            DownloadsUtil.clearCache(url);
        } finally {
            if (in != null)
                try {
                    in.close();
                } catch (IOException ignored) {
                }
            cacheFile.delete();
            RepoDb.endTransation();
        }
    }
    // repositories
    return hasChanged.get();
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SyncDownloadInfo(de.robv.android.xposed.installer.util.DownloadsUtil.SyncDownloadInfo) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) GZIPInputStream(java.util.zip.GZIPInputStream) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Repository(de.robv.android.xposed.installer.repo.Repository) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Module(de.robv.android.xposed.installer.repo.Module) File(java.io.File) RepoParserCallback(de.robv.android.xposed.installer.repo.RepoParser.RepoParserCallback)

Aggregations

Module (de.robv.android.xposed.installer.repo.Module)1 RepoParserCallback (de.robv.android.xposed.installer.repo.RepoParser.RepoParserCallback)1 Repository (de.robv.android.xposed.installer.repo.Repository)1 SyncDownloadInfo (de.robv.android.xposed.installer.util.DownloadsUtil.SyncDownloadInfo)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 GZIPInputStream (java.util.zip.GZIPInputStream)1