Search in sources :

Example 21 with Apk

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

the class SwapRepoEmulatorTest method testSwap.

/**
 * @see org.fdroid.fdroid.nearby.WifiStateChangeService.WifiInfoThread#run()
 */
@Ignore
@Test
public void testSwap() throws IOException, LocalRepoKeyStore.InitException, IndexUpdater.UpdateException, InterruptedException {
    Looper.prepare();
    LocalHTTPD localHttpd = null;
    try {
        Log.i(TAG, "REPO: " + FDroidApp.repo);
        final Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
        Preferences.setupForTests(context);
        FDroidApp.initWifiSettings();
        assertNull(FDroidApp.repo.address);
        final CountDownLatch latch = new CountDownLatch(1);
        new Thread() {

            @Override
            public void run() {
                while (FDroidApp.repo.address == null) {
                    try {
                        Log.i(TAG, "Waiting for IP address... " + FDroidApp.repo.address);
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                    // ignored
                    }
                }
                latch.countDown();
            }
        }.start();
        latch.await(10, TimeUnit.MINUTES);
        assertNotNull(FDroidApp.repo.address);
        LocalRepoService.runProcess(context, new String[] { context.getPackageName() });
        Log.i(TAG, "REPO: " + FDroidApp.repo);
        File indexJarFile = LocalRepoManager.get(context).getIndexJar();
        assertTrue(indexJarFile.isFile());
        localHttpd = new LocalHTTPD(context, null, FDroidApp.port, LocalRepoManager.get(context).getWebRoot(), false);
        localHttpd.start();
        // give the server some tine to start.
        Thread.sleep(100);
        assertTrue(localHttpd.isAlive());
        LocalRepoKeyStore localRepoKeyStore = LocalRepoKeyStore.get(context);
        Certificate localCert = localRepoKeyStore.getCertificate();
        String signingCert = Hasher.hex(localCert);
        assertFalse(TextUtils.isEmpty(signingCert));
        assertFalse(TextUtils.isEmpty(Utils.calcFingerprint(localCert)));
        Repo repoToDelete = RepoProvider.Helper.findByAddress(context, FDroidApp.repo.address);
        while (repoToDelete != null) {
            Log.d(TAG, "Removing old test swap repo matching this one: " + repoToDelete.address);
            RepoProvider.Helper.remove(context, repoToDelete.getId());
            repoToDelete = RepoProvider.Helper.findByAddress(context, FDroidApp.repo.address);
        }
        ContentValues values = new ContentValues(4);
        values.put(Schema.RepoTable.Cols.SIGNING_CERT, signingCert);
        values.put(Schema.RepoTable.Cols.ADDRESS, FDroidApp.repo.address);
        values.put(Schema.RepoTable.Cols.NAME, FDroidApp.repo.name);
        values.put(Schema.RepoTable.Cols.IS_SWAP, true);
        final String lastEtag = UUID.randomUUID().toString();
        values.put(Schema.RepoTable.Cols.LAST_ETAG, lastEtag);
        RepoProvider.Helper.insert(context, values);
        Repo repo = RepoProvider.Helper.findByAddress(context, FDroidApp.repo.address);
        assertTrue(repo.isSwap);
        assertNotEquals(-1, repo.getId());
        assertTrue(repo.name.startsWith(FDroidApp.repo.name));
        assertEquals(lastEtag, repo.lastetag);
        assertNull(repo.lastUpdated);
        assertTrue(isPortInUse(FDroidApp.ipAddressString, FDroidApp.port));
        Thread.sleep(100);
        IndexUpdater updater = new IndexUpdater(context, repo);
        updater.update();
        assertTrue(updater.hasChanged());
        repo = RepoProvider.Helper.findByAddress(context, FDroidApp.repo.address);
        final Date lastUpdated = repo.lastUpdated;
        assertTrue("repo lastUpdated should be updated", new Date(2019, 5, 13).compareTo(repo.lastUpdated) > 0);
        App app = AppProvider.Helper.findSpecificApp(context.getContentResolver(), context.getPackageName(), repo.getId());
        assertEquals(context.getPackageName(), app.packageName);
        List<Apk> apks = ApkProvider.Helper.findByRepo(context, repo, Schema.ApkTable.Cols.ALL);
        assertEquals(1, apks.size());
        for (Apk apk : apks) {
            Log.i(TAG, "Apk: " + apk);
            assertEquals(context.getPackageName(), apk.packageName);
            assertEquals(BuildConfig.VERSION_NAME, apk.versionName);
            assertEquals(BuildConfig.VERSION_CODE, apk.versionCode);
            assertEquals(app.repoId, apk.repoId);
        }
        Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
        mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        List<ResolveInfo> resolveInfoList = context.getPackageManager().queryIntentActivities(mainIntent, 0);
        HashSet<String> packageNames = new HashSet<>();
        for (ResolveInfo resolveInfo : resolveInfoList) {
            if (!isSystemPackage(resolveInfo)) {
                Log.i(TAG, "resolveInfo: " + resolveInfo);
                packageNames.add(resolveInfo.activityInfo.packageName);
            }
        }
        LocalRepoService.runProcess(context, packageNames.toArray(new String[0]));
        updater = new IndexUpdater(context, repo);
        updater.update();
        assertTrue(updater.hasChanged());
        assertTrue("repo lastUpdated should be updated", lastUpdated.compareTo(repo.lastUpdated) < 0);
        for (String packageName : packageNames) {
            assertNotNull(ApkProvider.Helper.findByPackageName(context, packageName));
        }
    } finally {
        if (localHttpd != null) {
            localHttpd.stop();
        }
    }
    if (localHttpd != null) {
        assertFalse(localHttpd.isAlive());
    }
}
Also used : Context(android.content.Context) ContentValues(android.content.ContentValues) FDroidApp(org.fdroid.fdroid.FDroidApp) App(org.fdroid.fdroid.data.App) LocalHTTPD(org.fdroid.fdroid.nearby.LocalHTTPD) LocalRepoKeyStore(org.fdroid.fdroid.nearby.LocalRepoKeyStore) Intent(android.content.Intent) CountDownLatch(java.util.concurrent.CountDownLatch) Date(java.util.Date) ResolveInfo(android.content.pm.ResolveInfo) Repo(org.fdroid.fdroid.data.Repo) File(java.io.File) Apk(org.fdroid.fdroid.data.Apk) IndexUpdater(org.fdroid.fdroid.IndexUpdater) Certificate(java.security.cert.Certificate) HashSet(java.util.HashSet) Ignore(org.junit.Ignore) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 22 with Apk

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

the class UpdateService method showAppUpdatesNotification.

private void showAppUpdatesNotification(List<App> canUpdate) {
    if (canUpdate.size() > 0) {
        List<Apk> apksToUpdate = new ArrayList<>(canUpdate.size());
        for (App app : canUpdate) {
            apksToUpdate.add(ApkProvider.Helper.findSuggestedApk(this, app));
        }
        appUpdateStatusManager.addApks(apksToUpdate, AppUpdateStatusManager.Status.UpdateAvailable);
    }
}
Also used : App(org.fdroid.fdroid.data.App) ArrayList(java.util.ArrayList) Apk(org.fdroid.fdroid.data.Apk)

Example 23 with Apk

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

the class UpdateService method autoDownloadUpdates.

/**
 * Queues all apps needing update.  If this app itself (e.g. F-Droid) needs
 * to be updated, it is queued last.
 */
public static void autoDownloadUpdates(Context context) {
    List<App> canUpdate = AppProvider.Helper.findCanUpdate(context, Schema.AppMetadataTable.Cols.ALL);
    String packageName = context.getPackageName();
    App updateLastApp = null;
    Apk updateLastApk = null;
    for (App app : canUpdate) {
        if (TextUtils.equals(packageName, app.packageName)) {
            updateLastApp = app;
            updateLastApk = ApkProvider.Helper.findSuggestedApk(context, app);
            continue;
        }
        Apk apk = ApkProvider.Helper.findSuggestedApk(context, app);
        InstallManagerService.queue(context, app, apk);
    }
    if (updateLastApp != null && updateLastApk != null) {
        InstallManagerService.queue(context, updateLastApp, updateLastApk);
    }
}
Also used : App(org.fdroid.fdroid.data.App) Apk(org.fdroid.fdroid.data.Apk)

Example 24 with Apk

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

the class MultiRepoUpdaterTest method assertApksExist.

/**
 * Checks that each version of appId as specified in versionCodes is present in apksToCheck.
 */
protected void assertApksExist(List<Apk> apksToCheck, String appId, int[] versionCodes) {
    for (int versionCode : versionCodes) {
        boolean found = false;
        for (Apk apk : apksToCheck) {
            if (apk.versionCode == versionCode && apk.packageName.equals(appId)) {
                found = true;
                break;
            }
        }
        assertTrue("Couldn't find app " + appId + ", v" + versionCode, found);
    }
}
Also used : Apk(org.fdroid.fdroid.data.Apk)

Example 25 with Apk

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

the class AppUpdateStatusService method onHandleIntent.

@Override
protected void onHandleIntent(@Nullable Intent intent) {
    Utils.debugLog(TAG, "Scanning apk cache to see if we need to prompt the user to install any apks.");
    File cacheDir = ApkCache.getApkCacheDir(this);
    if (cacheDir == null) {
        return;
    }
    String[] cacheDirList = cacheDir.list();
    if (cacheDirList == null) {
        return;
    }
    PackageManager packageManager = getPackageManager();
    List<Apk> apksReadyToInstall = new ArrayList<>();
    for (String repoDirName : cacheDirList) {
        File repoDir = new File(cacheDir, repoDirName);
        String[] apks = repoDir.list();
        if (apks == null) {
            continue;
        }
        for (String apkFileName : apks) {
            Apk apk = processDownloadedApk(new File(repoDir, apkFileName));
            if (apk != null) {
                PackageInfo packageInfo = null;
                try {
                    packageInfo = packageManager.getPackageInfo(apk.packageName, 0);
                } catch (PackageManager.NameNotFoundException e) {
                // ignored
                }
                if (packageInfo == null || packageInfo.versionCode != apk.versionCode) {
                    Utils.debugLog(TAG, "Marking downloaded apk " + apk.apkName + " as ReadyToInstall");
                    apksReadyToInstall.add(apk);
                }
            }
        }
    }
    if (apksReadyToInstall.size() > 0) {
        AppUpdateStatusManager.getInstance(this).addApks(apksReadyToInstall, AppUpdateStatusManager.Status.ReadyToInstall);
        InstallManagerService.managePreviouslyDownloadedApks(this);
    }
}
Also used : PackageManager(android.content.pm.PackageManager) PackageInfo(android.content.pm.PackageInfo) ArrayList(java.util.ArrayList) File(java.io.File) 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