Example usage for android.os ParcelFileDescriptor getFd

List of usage examples for android.os ParcelFileDescriptor getFd

Introduction

In this page you can find the example usage for android.os ParcelFileDescriptor getFd.

Prototype

public int getFd() 

Source Link

Document

Return the native fd int for this ParcelFileDescriptor.

Usage

From source file:Main.java

static int getFd(ParcelFileDescriptor parcelFileDescriptor) {
    return parcelFileDescriptor.getFd();
}

From source file:com.github.chenxiaolong.dualbootpatcher.switcher.SwitcherUtils.java

public static String getTargetInstallLocation(@NonNull Context context, @NonNull Uri uri) {
    ThreadUtils.enforceExecutionOnNonMainThread();

    ParcelFileDescriptor pfd = null;
    MiniZipInputFile mzif = null;// ww w  . ja va  2 s .c o  m

    try {
        pfd = context.getContentResolver().openFileDescriptor(uri, "r");
        if (pfd == null) {
            return null;
        }

        mzif = new MiniZipInputFile("/proc/self/fd/" + pfd.getFd());

        Properties prop = null;

        MiniZipEntry entry;
        while ((entry = mzif.nextEntry()) != null) {
            if (entry.getName().equals(ZIP_INFO_PROP)) {
                prop = new Properties();
                prop.load(mzif.getInputStream());
                break;
            }
        }

        if (prop == null) {
            return null;
        }

        return prop.getProperty(PROP_INSTALL_LOCATION, null);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } finally {
        IOUtils.closeQuietly(mzif);
        IOUtils.closeQuietly(pfd);
    }
}

From source file:com.github.chenxiaolong.dualbootpatcher.switcher.SwitcherUtils.java

public static VerificationResult verifyZipMbtoolVersion(@NonNull Context context, @NonNull Uri uri) {
    ThreadUtils.enforceExecutionOnNonMainThread();

    ParcelFileDescriptor pfd = null;
    MiniZipInputFile mzif = null;//  www  .j av  a 2s .  c  o  m

    try {
        pfd = context.getContentResolver().openFileDescriptor(uri, "r");
        if (pfd == null) {
            return VerificationResult.ERROR_ZIP_READ_FAIL;
        }

        mzif = new MiniZipInputFile("/proc/self/fd/" + pfd.getFd());

        boolean isMultiboot = false;
        Properties prop = null;

        MiniZipEntry entry;
        while ((entry = mzif.nextEntry()) != null) {
            if (entry.getName().startsWith(ZIP_MULTIBOOT_DIR)) {
                isMultiboot = true;
            }
            if (entry.getName().equals(ZIP_INFO_PROP)) {
                prop = new Properties();
                prop.load(mzif.getInputStream());
                break;
            }
        }

        if (!isMultiboot) {
            return VerificationResult.ERROR_NOT_MULTIBOOT;
        }

        if (prop == null) {
            return VerificationResult.ERROR_VERSION_TOO_OLD;
        }

        Version version = new Version(prop.getProperty(PROP_INSTALLER_VERSION, "0.0.0"));
        Version minVersion = MbtoolUtils.getMinimumRequiredVersion(Feature.IN_APP_INSTALLATION);

        if (version.compareTo(minVersion) < 0) {
            return VerificationResult.ERROR_VERSION_TOO_OLD;
        }

        return VerificationResult.NO_ERROR;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return VerificationResult.ERROR_ZIP_NOT_FOUND;
    } catch (IOException e) {
        e.printStackTrace();
        return VerificationResult.ERROR_ZIP_READ_FAIL;
    } catch (VersionParseException e) {
        e.printStackTrace();
        return VerificationResult.ERROR_VERSION_TOO_OLD;
    } finally {
        IOUtils.closeQuietly(mzif);
        IOUtils.closeQuietly(pfd);
    }
}

From source file:com.github.chenxiaolong.dualbootpatcher.FileUtils.java

public static void zipExtractFile(@NonNull Context context, @NonNull Uri uri, @NonNull String filename,
        @NonNull String destFile) throws IOException {
    ParcelFileDescriptor pfd = null;
    MiniZipInputFile mzif = null;/*from   w w w  .j a  va  2  s  .  c o m*/
    FileOutputStream fos = null;

    try {
        pfd = context.getContentResolver().openFileDescriptor(uri, "r");
        if (pfd == null) {
            throw new IOException("Failed to open URI: " + uri);
        }

        mzif = new MiniZipInputFile("/proc/self/fd/" + pfd.getFd());

        MiniZipEntry entry;
        while ((entry = mzif.nextEntry()) != null) {
            if (entry.getName().equals(filename)) {
                fos = new FileOutputStream(destFile);
                IOUtils.copy(mzif.getInputStream(), fos);
                return;
            }
        }
    } finally {
        IOUtils.closeQuietly(mzif);
        IOUtils.closeQuietly(fos);
        IOUtils.closeQuietly(pfd);
    }
}

From source file:com.github.chenxiaolong.dualbootpatcher.switcher.service.MbtoolTask.java

private String getPathFromUri(@NonNull Uri uri, @NonNull MbtoolInterface iface)
        throws IOException, MbtoolException {
    // Get URI from DocumentFile, which will handle the translation of tree URIs to document
    // URIs, which continuing to work with file:// URIs
    DocumentFile df = FileUtils.getDocumentFile(getContext(), uri);
    uri = df.getUri();/*  w  w w . j a  va 2s. c  om*/

    ParcelFileDescriptor pfd = null;
    try {
        pfd = getContext().getContentResolver().openFileDescriptor(uri, "r");
        if (pfd == null) {
            printBoldText(Color.RED, "Failed to open: " + uri);
            return null;
        }

        String fdSource = "/proc/" + LibC.CWrapper.getpid() + "/fd/" + pfd.getFd();
        try {
            return iface.pathReadlink(fdSource);
        } catch (MbtoolCommandException e) {
            printBoldText(Color.RED, "Failed to resolve symlink: " + fdSource + ": " + e.getMessage() + "\n");
            return null;
        }
    } finally {
        IOUtils.closeQuietly(pfd);
    }
}

From source file:com.almalence.plugins.capture.video.VideoCapturePlugin.java

/**
 * Appends mp4 audio/video from {@code anotherFileName} to
 * {@code mainFileName}.//  w  w w  . j ava  2s  .  c om
 */
public static File append(File[] inputFiles) {
    try {
        File targetFile = inputFiles[0];
        int[] inputFilesFds = new int[inputFiles.length];
        ArrayList<ParcelFileDescriptor> pfdsList = new ArrayList<ParcelFileDescriptor>();

        int i = 0;
        for (File f : inputFiles) {
            ParcelFileDescriptor pfd = ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_WRITE);
            pfdsList.add(pfd);
            inputFilesFds[i] = pfd.getFd();
            i++;
        }

        if (targetFile.exists() && targetFile.length() > 0) {
            File tmpTargetFile = new File(targetFile.getAbsolutePath() + ".tmp");

            ParcelFileDescriptor targetFilePfd = ParcelFileDescriptor.open(tmpTargetFile,
                    ParcelFileDescriptor.MODE_CREATE | ParcelFileDescriptor.MODE_READ_WRITE);

            Mp4Editor.appendFds(inputFilesFds, targetFilePfd.getFd());

            targetFilePfd.close();
            for (ParcelFileDescriptor pfd : pfdsList) {
                pfd.close();
            }

            return tmpTargetFile;
        } else if (targetFile.createNewFile()) {
            copyFile(inputFiles[1].getAbsolutePath(), inputFiles[0].getAbsolutePath());
            return targetFile;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.almalence.plugins.capture.video.VideoCapturePlugin.java

/**
 * Appends mp4 audio/video from {@code anotherFileDescriptor} to
 * {@code mainFileDescriptor}.//from   w  w w .  j  a  v  a 2 s .  co  m
 */
public static DocumentFile appendNew(DocumentFile[] inputFiles) {
    try {
        DocumentFile targetFile = inputFiles[0];
        int[] inputFilesFds = new int[inputFiles.length];
        ArrayList<ParcelFileDescriptor> pfdsList = new ArrayList<ParcelFileDescriptor>();

        int i = 0;
        for (DocumentFile f : inputFiles) {
            ParcelFileDescriptor pfd = ApplicationScreen.instance.getContentResolver()
                    .openFileDescriptor(f.getUri(), "rw");
            pfdsList.add(pfd);
            inputFilesFds[i] = pfd.getFd();
            i++;
        }

        if (targetFile.exists() && targetFile.length() > 0) {
            String tmpFileName = targetFile.getName() + ".tmp";
            DocumentFile tmpTargetFile = targetFile.getParentFile().createFile("video/mp4", tmpFileName);
            ParcelFileDescriptor targetFilePfd = ApplicationScreen.instance.getContentResolver()
                    .openFileDescriptor(tmpTargetFile.getUri(), "rw");

            Mp4Editor.appendFds(inputFilesFds, targetFilePfd.getFd());

            targetFilePfd.close();
            for (ParcelFileDescriptor pfd : pfdsList) {
                pfd.close();
            }

            return tmpTargetFile;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:eu.faircode.adblocker.ServiceSinkhole.java

private void stopNative(ParcelFileDescriptor vpn, boolean datagram, boolean stream) {
    Log.i(TAG, "Stop native clear=" + datagram + "/" + stream);
    jni_stop(vpn.getFd(), datagram, stream);
}

From source file:eu.faircode.netguard.ServiceSinkhole.java

private void stopNative(ParcelFileDescriptor vpn, boolean clear) {
    Log.i(TAG, "Stop native clear=" + clear);
    try {/* w  ww .  ja  v  a 2s . c o  m*/
        jni_stop(vpn.getFd(), clear);
    } catch (Throwable ex) {
        // File descriptor might be closed
        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
        jni_stop(-1, clear);
    }
}

From source file:eu.faircode.adblocker.ServiceSinkhole.java

private void startNative(ParcelFileDescriptor vpn, List<Rule> listAllowed, List<Rule> listRule) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ServiceSinkhole.this);
    boolean log = prefs.getBoolean("log", false);
    boolean log_app = prefs.getBoolean("log_app", false);
    boolean filter = prefs.getBoolean("filter", false);

    Log.i(TAG, "Start native log=" + log + "/" + log_app + " filter=" + filter);

    // Prepare rules
    if (filter) {
        prepareUidAllowed(listAllowed, listRule);
        prepareHostsBlocked();/*ww w.j a  v  a  2  s .  com*/
        prepareUidIPFilters(null);
        prepareForwarding();
    } else {
        mapUidAllowed.clear();
        mapUidKnown.clear();
        mapHostsBlocked.clear();
        mapUidIPFilters.clear();
        mapForward.clear();
    }

    if (log_app)
        prepareNotify(listRule);
    else
        mapNoNotify.clear();

    if (log || log_app || filter) {
        int prio = Integer.parseInt(prefs.getString("loglevel", Integer.toString(Log.WARN)));
        jni_start(vpn.getFd(), mapForward.containsKey(53), prio);
    }
}