Search in sources :

Example 31 with Apk

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

the class InstallManagerService method registerApkDownloaderReceivers.

private void registerApkDownloaderReceivers(String urlString) {
    BroadcastReceiver downloadReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (!running) {
                localBroadcastManager.unregisterReceiver(this);
                return;
            }
            Uri downloadUri = intent.getData();
            String urlString = downloadUri.toString();
            long repoId = intent.getLongExtra(Downloader.EXTRA_REPO_ID, 0);
            String mirrorUrlString = intent.getStringExtra(Downloader.EXTRA_MIRROR_URL);
            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(downloadUri);
                    PendingIntent action = PendingIntent.getService(context, 0, intentObject, 0);
                    appUpdateStatusManager.updateApk(urlString, AppUpdateStatusManager.Status.Downloading, action);
                    break;
                case Downloader.ACTION_PROGRESS:
                    int bytesRead = intent.getIntExtra(Downloader.EXTRA_BYTES_READ, 0);
                    int totalBytes = intent.getIntExtra(Downloader.EXTRA_TOTAL_BYTES, 0);
                    appUpdateStatusManager.updateApkProgress(urlString, 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 " + mirrorUrlString + " to " + localApkUri);
                    appUpdateStatusManager.updateApk(urlString, AppUpdateStatusManager.Status.ReadyToInstall, null);
                    localBroadcastManager.unregisterReceiver(this);
                    registerInstallerReceivers(downloadUri);
                    Apk apk = appUpdateStatusManager.getApk(urlString);
                    if (apk != null) {
                        InstallerService.install(context, localApkUri, downloadUri, apk);
                    }
                    break;
                case Downloader.ACTION_INTERRUPTED:
                    appUpdateStatusManager.markAsNoLongerPendingInstall(urlString);
                    appUpdateStatusManager.setDownloadError(urlString, intent.getStringExtra(Downloader.EXTRA_ERROR_MESSAGE));
                    localBroadcastManager.unregisterReceiver(this);
                    break;
                case Downloader.ACTION_CONNECTION_FAILED:
                    try {
                        DownloaderService.queue(context, FDroidApp.getMirror(mirrorUrlString, repoId), repoId, urlString);
                        DownloaderService.setTimeout(FDroidApp.getTimeout());
                    } catch (IOException e) {
                        appUpdateStatusManager.markAsNoLongerPendingInstall(urlString);
                        appUpdateStatusManager.setDownloadError(urlString, intent.getStringExtra(Downloader.EXTRA_ERROR_MESSAGE));
                        localBroadcastManager.unregisterReceiver(this);
                    }
                    break;
                default:
                    throw new RuntimeException("intent action not handled!");
            }
        }
    };
    localBroadcastManager.registerReceiver(downloadReceiver, DownloaderService.getIntentFilter(urlString));
}
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 32 with Apk

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

the class InstallManagerService method registerInstallerReceivers.

private void registerInstallerReceivers(Uri downloadUri) {
    BroadcastReceiver installReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (!running) {
                localBroadcastManager.unregisterReceiver(this);
                return;
            }
            String downloadUrl = intent.getDataString();
            Apk apk;
            switch(intent.getAction()) {
                case Installer.ACTION_INSTALL_STARTED:
                    appUpdateStatusManager.updateApk(downloadUrl, AppUpdateStatusManager.Status.Installing, null);
                    break;
                case Installer.ACTION_INSTALL_COMPLETE:
                    appUpdateStatusManager.markAsNoLongerPendingInstall(downloadUrl);
                    appUpdateStatusManager.updateApk(downloadUrl, AppUpdateStatusManager.Status.Installed, null);
                    Apk apkComplete = appUpdateStatusManager.getApk(downloadUrl);
                    if (apkComplete != null && apkComplete.isApk()) {
                        try {
                            PackageManagerCompat.setInstaller(context, getPackageManager(), apkComplete.packageName);
                        } catch (SecurityException e) {
                        // Will happen if we fell back to DefaultInstaller for some reason.
                        }
                    }
                    localBroadcastManager.unregisterReceiver(this);
                    break;
                case Installer.ACTION_INSTALL_INTERRUPTED:
                    apk = intent.getParcelableExtra(Installer.EXTRA_APK);
                    String errorMessage = intent.getStringExtra(Installer.EXTRA_ERROR_MESSAGE);
                    appUpdateStatusManager.markAsNoLongerPendingInstall(downloadUrl);
                    if (!TextUtils.isEmpty(errorMessage)) {
                        appUpdateStatusManager.setApkError(apk, errorMessage);
                    } else {
                        appUpdateStatusManager.removeApk(downloadUrl);
                    }
                    localBroadcastManager.unregisterReceiver(this);
                    break;
                case Installer.ACTION_INSTALL_USER_INTERACTION:
                    apk = intent.getParcelableExtra(Installer.EXTRA_APK);
                    PendingIntent installPendingIntent = intent.getParcelableExtra(Installer.EXTRA_USER_INTERACTION_PI);
                    appUpdateStatusManager.addApk(apk, AppUpdateStatusManager.Status.ReadyToInstall, installPendingIntent);
                    break;
                default:
                    throw new RuntimeException("intent action not handled!");
            }
        }
    };
    localBroadcastManager.registerReceiver(installReceiver, Installer.getInstallIntentFilter(downloadUri));
}
Also used : Context(android.content.Context) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent) BroadcastReceiver(android.content.BroadcastReceiver) Apk(org.fdroid.fdroid.data.Apk)

Example 33 with Apk

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

the class InstallerService method onHandleIntent.

@Override
protected void onHandleIntent(Intent intent) {
    final Apk apk = intent.getParcelableExtra(Installer.EXTRA_APK);
    if (apk == null) {
        Utils.debugLog(TAG, "ignoring intent with null EXTRA_APK: " + intent);
        return;
    }
    Installer installer = InstallerFactory.create(this, apk);
    if (ACTION_INSTALL.equals(intent.getAction())) {
        Uri uri = intent.getData();
        Uri downloadUri = intent.getParcelableExtra(Installer.EXTRA_DOWNLOAD_URI);
        installer.installPackage(uri, downloadUri);
    } else if (ACTION_UNINSTALL.equals(intent.getAction())) {
        installer.uninstallPackage();
        new Thread() {

            @Override
            public void run() {
                setPriority(MIN_PRIORITY);
                File mainObbFile = apk.getMainObbFile();
                if (mainObbFile == null) {
                    return;
                }
                File obbDir = mainObbFile.getParentFile();
                if (obbDir == null) {
                    return;
                }
                FileFilter filter = new WildcardFileFilter("*.obb");
                File[] obbFiles = obbDir.listFiles(filter);
                if (obbFiles == null) {
                    return;
                }
                for (File f : obbFiles) {
                    Utils.debugLog(TAG, "Uninstalling OBB " + f);
                    FileUtils.deleteQuietly(f);
                }
            }
        }.start();
    }
}
Also used : FileFilter(java.io.FileFilter) WildcardFileFilter(org.apache.commons.io.filefilter.WildcardFileFilter) Apk(org.fdroid.fdroid.data.Apk) Uri(android.net.Uri) File(java.io.File) WildcardFileFilter(org.apache.commons.io.filefilter.WildcardFileFilter)

Example 34 with Apk

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

the class ProperMultiRepoUpdaterTest method assertMainRepo.

/**
 * + 2048 (com.uberspot.a2048)
 * - Version 1.96 (19)
 * - Version 1.95 (18)
 * + AdAway (org.adaway)
 * - Version 3.0.2 (54)
 * - Version 3.0.1 (53)
 * - Version 3.0 (52)
 * + adbWireless (siir.es.adbWireless)
 * - Version 1.5.4 (12)
 */
private void assertMainRepo(List<Repo> allRepos) {
    Repo repo = findRepo(REPO_MAIN, allRepos);
    List<Apk> apks = ApkProvider.Helper.findByRepo(context, repo, Schema.ApkTable.Cols.ALL);
    assertEquals("Apks for main repo", apks.size(), 6);
    assertApksExist(apks, "com.uberspot.a2048", new int[] { 18, 19 });
    assertApksExist(apks, "org.adaway", new int[] { 52, 53, 54 });
    assertApksExist(apks, "siir.es.adbWireless", new int[] { 12 });
    assert2048Metadata(repo, "Normal");
    assertAdAwayMetadata(repo, "Normal");
    assertAdbMetadata(repo, "Normal");
}
Also used : Repo(org.fdroid.fdroid.data.Repo) Apk(org.fdroid.fdroid.data.Apk)

Example 35 with Apk

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

the class ProperMultiRepoUpdaterTest method assertConflictingRepo.

/**
 * + AdAway (org.adaway)
 * - Version 3.0.1 (53) *
 * - Version 3.0 (52) *
 * - Version 2.9.2 (51) *
 * - Version 2.2.1 (50) *
 * + Add to calendar (org.dgtale.icsimport)
 * - Version 1.2 (3)
 * - Version 1.1 (2)
 */
private void assertConflictingRepo(List<Repo> allRepos) {
    Repo repo = findRepo(REPO_CONFLICTING, allRepos);
    List<Apk> apks = ApkProvider.Helper.findByRepo(context, repo, Schema.ApkTable.Cols.ALL);
    assertEquals("Apks for conflicting repo", 6, apks.size());
    assertApksExist(apks, "org.adaway", new int[] { 50, 51, 52, 53 });
    assertApksExist(apks, "org.dgtale.icsimport", new int[] { 2, 3 });
    assertAdAwayMetadata(repo, "Conflicting");
    assertCalendarMetadata(repo, "Conflicting");
}
Also used : Repo(org.fdroid.fdroid.data.Repo) Apk(org.fdroid.fdroid.data.Apk)

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