Search in sources :

Example 36 with Apk

use of org.fdroid.fdroid.data.Apk in project fdroidclient by f-droid.

the class RepoXMLHandlerTest method testObbIndex.

@Test
public void testObbIndex() throws IOException {
    writeResourceToObbDir("main.1101613.obb.main.twoversions.obb");
    writeResourceToObbDir("main.1101615.obb.main.twoversions.obb");
    writeResourceToObbDir("main.1434483388.obb.main.oldversion.obb");
    writeResourceToObbDir("main.1619.obb.mainpatch.current.obb");
    writeResourceToObbDir("patch.1619.obb.mainpatch.current.obb");
    RepoDetails actualDetails = getFromFile("obbIndex.xml");
    for (Apk indexApk : actualDetails.apks) {
        Apk localApk = new Apk();
        localApk.packageName = indexApk.packageName;
        localApk.versionCode = indexApk.versionCode;
        localApk.hashType = indexApk.hashType;
        App.initInstalledObbFiles(localApk);
        assertEquals(indexApk.obbMainFile, localApk.obbMainFile);
        assertEquals(indexApk.obbMainFileSha256, localApk.obbMainFileSha256);
        assertEquals(indexApk.obbPatchFile, localApk.obbPatchFile);
        assertEquals(indexApk.obbPatchFileSha256, localApk.obbPatchFileSha256);
    }
}
Also used : Apk(org.fdroid.fdroid.data.Apk) RepoDetails(org.fdroid.fdroid.mock.RepoDetails) Test(org.junit.Test)

Example 37 with Apk

use of org.fdroid.fdroid.data.Apk in project fdroidclient by f-droid.

the class DefaultInstallerActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    String action = intent.getAction();
    Apk apk = intent.getParcelableExtra(Installer.EXTRA_APK);
    installer = new DefaultInstaller(this, apk);
    if (ACTION_INSTALL_PACKAGE.equals(action)) {
        Uri localApkUri = intent.getData();
        canonicalUri = Uri.parse(intent.getStringExtra(org.fdroid.fdroid.net.Downloader.EXTRA_CANONICAL_URL));
        installPackage(localApkUri);
    } else if (ACTION_UNINSTALL_PACKAGE.equals(action)) {
        uninstallPackage(apk.packageName);
    } else {
        throw new IllegalStateException("Intent action not specified!");
    }
}
Also used : Intent(android.content.Intent) Apk(org.fdroid.fdroid.data.Apk) Uri(android.net.Uri)

Example 38 with Apk

use of org.fdroid.fdroid.data.Apk in project fdroidclient by f-droid.

the class AppListItemController method onActionButtonPressed.

protected void onActionButtonPressed(App app) {
    if (app == null) {
        return;
    }
    // When the button says "Open", then launch the app.
    if (currentStatus != null && currentStatus.status == AppUpdateStatusManager.Status.Installed) {
        Intent intent = activity.getPackageManager().getLaunchIntentForPackage(app.packageName);
        if (intent != null) {
            activity.startActivity(intent);
            // Once it is explicitly launched by the user, then we can pretty much forget about
            // any sort of notification that the app was successfully installed. It should be
            // apparent to the user because they just launched it.
            AppUpdateStatusManager.getInstance(activity).removeApk(currentStatus.getCanonicalUrl());
        }
        return;
    }
    if (currentStatus != null && currentStatus.status == AppUpdateStatusManager.Status.ReadyToInstall) {
        String canonicalUrl = currentStatus.apk.getCanonicalUrl();
        File apkFilePath = ApkCache.getApkDownloadPath(activity, canonicalUrl);
        Utils.debugLog(TAG, "skip download, we have already downloaded " + currentStatus.apk.getCanonicalUrl() + " to " + apkFilePath);
        final LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(activity);
        final BroadcastReceiver receiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                broadcastManager.unregisterReceiver(this);
                if (Installer.ACTION_INSTALL_USER_INTERACTION.equals(intent.getAction())) {
                    PendingIntent pendingIntent = intent.getParcelableExtra(Installer.EXTRA_USER_INTERACTION_PI);
                    try {
                        pendingIntent.send();
                    } catch (PendingIntent.CanceledException ignored) {
                    }
                }
            }
        };
        Uri canonicalUri = Uri.parse(canonicalUrl);
        broadcastManager.registerReceiver(receiver, Installer.getInstallIntentFilter(canonicalUri));
        Installer installer = InstallerFactory.create(activity, currentStatus.apk);
        installer.installPackage(Uri.parse(apkFilePath.toURI().toString()), canonicalUri);
    } else {
        final Apk suggestedApk = ApkProvider.Helper.findSuggestedApk(activity, app);
        InstallManagerService.queue(activity, app, suggestedApk);
    }
}
Also used : Context(android.content.Context) Installer(org.fdroid.fdroid.installer.Installer) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent) BroadcastReceiver(android.content.BroadcastReceiver) File(java.io.File) LocalBroadcastManager(androidx.localbroadcastmanager.content.LocalBroadcastManager) Uri(android.net.Uri) Apk(org.fdroid.fdroid.data.Apk)

Example 39 with Apk

use of org.fdroid.fdroid.data.Apk in project fdroidclient by f-droid.

the class InstallManagerService method registerPackageDownloaderReceivers.

/**
 * Register a {@link BroadcastReceiver} for tracking download progress for a
 * give {@code canonicalUrl}.  There can be multiple of these registered at a time.
 */
private void registerPackageDownloaderReceivers(String canonicalUrl) {
    BroadcastReceiver downloadReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (!running) {
                localBroadcastManager.unregisterReceiver(this);
                return;
            }
            Uri canonicalUri = intent.getData();
            String canonicalUrl = intent.getDataString();
            long repoId = intent.getLongExtra(Downloader.EXTRA_REPO_ID, 0);
            switch(intent.getAction()) {
                case Downloader.ACTION_STARTED:
                    // App should currently be in the "PendingDownload" state, so this changes it to "Downloading".
                    Intent intentObject = new Intent(context, InstallManagerService.class);
                    intentObject.setAction(ACTION_CANCEL);
                    intentObject.setData(canonicalUri);
                    PendingIntent action = PendingIntent.getService(context, 0, intentObject, 0);
                    appUpdateStatusManager.updateApk(canonicalUrl, AppUpdateStatusManager.Status.Downloading, action);
                    break;
                case Downloader.ACTION_PROGRESS:
                    long bytesRead = intent.getLongExtra(Downloader.EXTRA_BYTES_READ, 0);
                    long totalBytes = intent.getLongExtra(Downloader.EXTRA_TOTAL_BYTES, 0);
                    appUpdateStatusManager.updateApkProgress(canonicalUrl, totalBytes, bytesRead);
                    break;
                case Downloader.ACTION_COMPLETE:
                    File localFile = new File(intent.getStringExtra(Downloader.EXTRA_DOWNLOAD_PATH));
                    Uri localApkUri = Uri.fromFile(localFile);
                    Utils.debugLog(TAG, "download completed of " + intent.getStringExtra(Downloader.EXTRA_MIRROR_URL) + " to " + localApkUri);
                    appUpdateStatusManager.updateApk(canonicalUrl, AppUpdateStatusManager.Status.ReadyToInstall, null);
                    localBroadcastManager.unregisterReceiver(this);
                    registerInstallReceiver(canonicalUrl);
                    Apk apk = appUpdateStatusManager.getApk(canonicalUrl);
                    if (apk != null) {
                        InstallerService.install(context, localApkUri, canonicalUri, apk);
                    }
                    break;
                case Downloader.ACTION_INTERRUPTED:
                    appUpdateStatusManager.setDownloadError(canonicalUrl, intent.getStringExtra(Downloader.EXTRA_ERROR_MESSAGE));
                    localBroadcastManager.unregisterReceiver(this);
                    break;
                case Downloader.ACTION_CONNECTION_FAILED:
                    // TODO move this logic into DownloaderService to hide the mirror URL stuff from this class
                    try {
                        String currentUrlString = FDroidApp.getNewMirrorOnError(intent.getStringExtra(Downloader.EXTRA_MIRROR_URL), RepoProvider.Helper.findById(InstallManagerService.this, repoId));
                        DownloaderService.queue(context, currentUrlString, repoId, canonicalUrl);
                        DownloaderService.setTimeout(FDroidApp.getTimeout());
                    } catch (IOException e) {
                        appUpdateStatusManager.setDownloadError(canonicalUrl, intent.getStringExtra(Downloader.EXTRA_ERROR_MESSAGE));
                        localBroadcastManager.unregisterReceiver(this);
                    }
                    break;
                default:
                    throw new RuntimeException("intent action not handled!");
            }
        }
    };
    localBroadcastManager.registerReceiver(downloadReceiver, DownloaderService.getIntentFilter(canonicalUrl));
}
Also used : Context(android.content.Context) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent) IOException(java.io.IOException) BroadcastReceiver(android.content.BroadcastReceiver) Uri(android.net.Uri) File(java.io.File) Apk(org.fdroid.fdroid.data.Apk)

Example 40 with Apk

use of org.fdroid.fdroid.data.Apk in project fdroidclient by f-droid.

the class ObfInstallerService method onHandleIntent.

@Override
protected void onHandleIntent(Intent intent) {
    if (intent == null || !ACTION_INSTALL_OBF.equals(intent.getAction())) {
        Log.e(TAG, "received invalid intent: " + intent);
        return;
    }
    Uri canonicalUri = Uri.parse(intent.getStringExtra(org.fdroid.fdroid.net.Downloader.EXTRA_CANONICAL_URL));
    final Apk apk = intent.getParcelableExtra(Installer.EXTRA_APK);
    final String path = intent.getStringExtra(EXTRA_OBF_PATH);
    final String extension = MimeTypeMap.getFileExtensionFromUrl(path);
    if ("obf".equals(extension)) {
        sendPostInstallAndCompleteIntents(canonicalUri, apk, new File(path));
        return;
    }
    if (!"zip".equals(extension)) {
        sendBroadcastInstall(Installer.ACTION_INSTALL_INTERRUPTED, canonicalUri, apk, "Only .obf and .zip files are supported: " + path);
        return;
    }
    try {
        File zip = new File(path);
        ZipFile zipFile = new ZipFile(zip);
        if (zipFile.size() < 1) {
            sendBroadcastInstall(Installer.ACTION_INSTALL_INTERRUPTED, canonicalUri, apk, "Corrupt or empty ZIP file!");
        }
        ZipEntry zipEntry = zipFile.entries().nextElement();
        File extracted = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), zipEntry.getName());
        FileUtils.copyInputStreamToFile(zipFile.getInputStream(zipEntry), extracted);
        zip.delete();
        sendPostInstallAndCompleteIntents(canonicalUri, apk, extracted);
    } catch (IOException e) {
        e.printStackTrace();
        sendBroadcastInstall(Installer.ACTION_INSTALL_INTERRUPTED, canonicalUri, apk, e.getMessage());
    }
}
Also used : ZipFile(java.util.zip.ZipFile) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) Uri(android.net.Uri) Apk(org.fdroid.fdroid.data.Apk) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Aggregations

Apk (org.fdroid.fdroid.data.Apk)63 Test (org.junit.Test)17 Uri (android.net.Uri)16 File (java.io.File)12 Intent (android.content.Intent)11 App (org.fdroid.fdroid.data.App)10 Repo (org.fdroid.fdroid.data.Repo)10 ArrayList (java.util.ArrayList)8 PendingIntent (android.app.PendingIntent)7 Context (android.content.Context)7 PackageInfo (android.content.pm.PackageInfo)7 FileCompatTest (org.fdroid.fdroid.compat.FileCompatTest)7 BroadcastReceiver (android.content.BroadcastReceiver)6 PackageManager (android.content.pm.PackageManager)5 IOException (java.io.IOException)4 HashSet (java.util.HashSet)4 FDroidProviderTest (org.fdroid.fdroid.data.FDroidProviderTest)4 LocalBroadcastManager (androidx.localbroadcastmanager.content.LocalBroadcastManager)3 RepoDetails (org.fdroid.fdroid.mock.RepoDetails)3 SuppressLint (android.annotation.SuppressLint)2