Search in sources :

Example 6 with SanitizedFile

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

the class LocalRepoManager method writeFdroidApkToWebroot.

private String writeFdroidApkToWebroot() {
    ApplicationInfo appInfo;
    String fdroidClientURL = "https://f-droid.org/FDroid.apk";
    try {
        appInfo = pm.getApplicationInfo(fdroidPackageName, PackageManager.GET_META_DATA);
        SanitizedFile apkFile = SanitizedFile.knownSanitized(appInfo.publicSourceDir);
        SanitizedFile fdroidApkLink = new SanitizedFile(fdroidDir, "F-Droid.apk");
        attemptToDelete(fdroidApkLink);
        if (Utils.symlinkOrCopyFileQuietly(apkFile, fdroidApkLink)) {
            fdroidClientURL = "/" + fdroidDir.getName() + "/" + fdroidApkLink.getName();
        }
    } catch (PackageManager.NameNotFoundException e) {
        Log.e(TAG, "Could not set up F-Droid apk in the webroot", e);
    }
    return fdroidClientURL;
}
Also used : PackageManager(android.content.pm.PackageManager) SanitizedFile(org.fdroid.fdroid.data.SanitizedFile) ApplicationInfo(android.content.pm.ApplicationInfo)

Example 7 with SanitizedFile

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

the class ApkCache method copyInstalledApkToFiles.

/**
 * Same as {@link #copyApkFromCacheToFiles(Context, File, Apk)}, except it does not need to
 * verify the hash after copying. This is because we are copying from an installed apk, which
 * other apps do not have permission to modify.
 */
public static SanitizedFile copyInstalledApkToFiles(Context context, PackageInfo packageInfo) throws IOException {
    ApplicationInfo appInfo = packageInfo.applicationInfo;
    CharSequence name = context.getPackageManager().getApplicationLabel(appInfo);
    String apkFileName = name + "-" + packageInfo.versionName + ".apk";
    return copyApkToFiles(context, new File(appInfo.publicSourceDir), apkFileName, false, null, null);
}
Also used : ApplicationInfo(android.content.pm.ApplicationInfo) SanitizedFile(org.fdroid.fdroid.data.SanitizedFile) File(java.io.File)

Example 8 with SanitizedFile

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

the class ApkCache method copyApkToFiles.

/**
 * Copy an APK from {@param apkFile} to our internal files directory for 20 minutes.
 *
 * @param verifyHash If the file was just downloaded, then you should mark this as true and
 *                   request the file to be verified once it has finished copying. Otherwise,
 *                   if the app was installed from part of the system where it can't be tampered
 *                   with (e.g. installed apks on disk) then
 */
private static SanitizedFile copyApkToFiles(Context context, File apkFile, String destinationName, boolean verifyHash, String hash, String hashType) throws IOException {
    SanitizedFile sanitizedApkFile = new SanitizedFile(context.getFilesDir(), destinationName);
    // in FileUtils#copyFileToDirectory() - which delegates to copyFile()).
    if (sanitizedApkFile.exists()) {
        sanitizedApkFile.delete();
    }
    FileUtils.copyFile(apkFile, sanitizedApkFile);
    // verify copied file's hash with expected hash from Apk class
    if (verifyHash && !Utils.isFileMatchingHash(sanitizedApkFile, hash, hashType)) {
        FileUtils.deleteQuietly(apkFile);
        throw new IOException(apkFile + " failed to verify!");
    }
    // 20 minutes the start of the install process, delete the file
    final File apkToDelete = sanitizedApkFile;
    new Thread() {

        @Override
        public void run() {
            android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_LOWEST);
            try {
                Thread.sleep(1200000);
            } catch (InterruptedException ignored) {
            } finally {
                FileUtils.deleteQuietly(apkToDelete);
            }
        }
    }.start();
    return sanitizedApkFile;
}
Also used : SanitizedFile(org.fdroid.fdroid.data.SanitizedFile) IOException(java.io.IOException) SanitizedFile(org.fdroid.fdroid.data.SanitizedFile) File(java.io.File)

Example 9 with SanitizedFile

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

the class FileCompatTest method setUp.

@Before
public void setUp() {
    Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
    File dir = getWriteableDir(instrumentation);
    sourceFile = SanitizedFile.knownSanitized(AssetUtils.copyAssetToDir(instrumentation.getContext(), "simpleIndex.jar", dir));
    destFile = new SanitizedFile(dir, "dest-" + UUID.randomUUID() + ".testproduct");
    assertFalse(destFile.exists());
    assertTrue(sourceFile.getAbsolutePath() + " should exist.", sourceFile.exists());
}
Also used : SanitizedFile(org.fdroid.fdroid.data.SanitizedFile) Instrumentation(android.app.Instrumentation) File(java.io.File) SanitizedFile(org.fdroid.fdroid.data.SanitizedFile) Before(org.junit.Before)

Aggregations

SanitizedFile (org.fdroid.fdroid.data.SanitizedFile)9 File (java.io.File)6 ApplicationInfo (android.content.pm.ApplicationInfo)3 PackageManager (android.content.pm.PackageManager)2 IOException (java.io.IOException)2 Instrumentation (android.app.Instrumentation)1 Intent (android.content.Intent)1 Uri (android.net.Uri)1 ConnectException (java.net.ConnectException)1 HttpRetryException (java.net.HttpRetryException)1 NoRouteToHostException (java.net.NoRouteToHostException)1 ProtocolException (java.net.ProtocolException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 UnknownHostException (java.net.UnknownHostException)1 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)1 SSLKeyException (javax.net.ssl.SSLKeyException)1 SSLPeerUnverifiedException (javax.net.ssl.SSLPeerUnverifiedException)1 SSLProtocolException (javax.net.ssl.SSLProtocolException)1 ProgressListener (org.fdroid.fdroid.ProgressListener)1 Before (org.junit.Before)1