Java Code Examples for android.os.ParcelFileDescriptor#getFd()

The following examples show how to use android.os.ParcelFileDescriptor#getFd() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TextClassifierImpl.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private TextClassifierImplNative getNative(LocaleList localeList)
        throws FileNotFoundException {
    synchronized (mLock) {
        localeList = localeList == null ? LocaleList.getEmptyLocaleList() : localeList;
        final ModelFile bestModel = findBestModelLocked(localeList);
        if (bestModel == null) {
            throw new FileNotFoundException("No model for " + localeList.toLanguageTags());
        }
        if (mNative == null || mNative.isClosed() || !Objects.equals(mModel, bestModel)) {
            Log.d(DEFAULT_LOG_TAG, "Loading " + bestModel);
            final ParcelFileDescriptor fd = ParcelFileDescriptor.open(
                    new File(bestModel.getPath()), ParcelFileDescriptor.MODE_READ_ONLY);
            mNative = new TextClassifierImplNative(fd.getFd());
            mNativeCleaner = Cleaner.create(this, new NativeCloser(mNative));
            closeAndLogError(fd);
            mModel = bestModel;
        }
        return mNative;
    }
}
 
Example 2
Source File: SAFUtils.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * 指定したidに対応するUriが存在する時にその下に生成したファイルのrawファイルディスクリプタを返す
 * @param context
 * @param treeUri
 * @param mime
 * @param fileName
 * @return
 * @throws UnsupportedOperationException
 * @throws FileNotFoundException
 * @throws IOException
 */
@Deprecated
public static int createStorageFileFD(
	@NonNull final Context context,
	final Uri treeUri, final String mime, final String fileName)
		throws IOException, UnsupportedOperationException {

	if (DEBUG) Log.v(TAG, "createStorageFileFD:" + fileName);
	if (BuildCheck.isLollipop()) {
		if ((treeUri != null) && !TextUtils.isEmpty(fileName)) {
			final DocumentFile saveTree = DocumentFile.fromTreeUri(context, treeUri);
			final DocumentFile target = saveTree.createFile(mime, fileName);
			try {
				final ParcelFileDescriptor fd
					= context.getContentResolver().openFileDescriptor(target.getUri(), "rw");
				return fd != null ? fd.getFd() : 0;
			} catch (final FileNotFoundException e) {
				Log.w(TAG, e);
			}
		}
		throw new FileNotFoundException();
	} else {
		throw new UnsupportedOperationException("should be API>=21");
	}
}
 
Example 3
Source File: OpenVpnManagementThread.java    From Cake-VPN with GNU General Public License v2.0 5 votes vote down vote up
private boolean sendTunFD(String needed, String extra) {
    if (!extra.equals("tun")) {
        // We only support tun
        VpnStatus.logError(String.format("Device type %s requested, but only tun is possible with the Android API, sorry!", extra));
        return false;
    }
    ParcelFileDescriptor pfd = mOpenVPNService.openTun();
    if (pfd == null) return false;
    Method setInt;
    int fdint = pfd.getFd();
    try {
        setInt = FileDescriptor.class.getDeclaredMethod("setInt$", int.class);
        FileDescriptor fdtosend = new FileDescriptor();
        setInt.invoke(fdtosend, fdint);
        FileDescriptor[] fds = {fdtosend};
        mSocket.setFileDescriptorsForSend(fds);
        // Trigger a send so we can close the fd on our side of the channel
        // The API documentation fails to mention that it will not reset the file descriptor to
        // be send and will happily send the file descriptor on every write ...
        String cmd = String.format("needok '%s' %s\n", needed, "ok");
        managmentCommand(cmd);
        // Set the FileDescriptor to null to stop this mad behavior
        mSocket.setFileDescriptorsForSend(null);
        pfd.close();
        return true;
    } catch (NoSuchMethodException | IllegalArgumentException | InvocationTargetException |
            IOException | IllegalAccessException exp) {
        VpnStatus.logException("Could not send fd over socket", exp);
    }
    return false;
}
 
Example 4
Source File: OpenVpnManagementThread.java    From SimpleOpenVpn-Android with Apache License 2.0 5 votes vote down vote up
private boolean sendTunFD(String needed, String extra) {
    if (!extra.equals("tun")) {
        // We only support tun
        VpnStatus.logError(String.format("Device type %s requested, but only tun is possible with the Android API, sorry!", extra));

        return false;
    }
    ParcelFileDescriptor pfd = mOpenVPNService.openTun();
    if (pfd == null)
        return false;

    Method setInt;
    int fdint = pfd.getFd();
    try {
        setInt = FileDescriptor.class.getDeclaredMethod("setInt$", int.class);
        FileDescriptor fdtosend = new FileDescriptor();
        setInt.invoke(fdtosend, fdint);
        FileDescriptor[] fds = {fdtosend};
        mSocket.setFileDescriptorsForSend(fds);

        // Trigger a send so we can close the fd on our side of the channel
        // The API documentation fails to mention that it will not reset the file descriptor to
        // be send and will happily send the file descriptor on every write ...
        String cmd = String.format("needok '%s' %s\n", needed, "ok");
        managmentCommand(cmd);
        // Set the FileDescriptor to null to stop this mad behavior
        mSocket.setFileDescriptorsForSend(null);
        pfd.close();
        return true;
    } catch (NoSuchMethodException | IllegalArgumentException | InvocationTargetException |
            IOException | IllegalAccessException exp) {
        VpnStatus.logException("Could not send fd over socket", exp);
    }

    return false;
}
 
Example 5
Source File: OpenVpnManagementThread.java    From Cybernet-VPN with GNU General Public License v3.0 5 votes vote down vote up
private boolean sendTunFD(String needed, String extra) {
    if (!extra.equals("tun")) {
        // We only support tun
        VpnStatus.logError(String.format("Device type %s requested, but only tun is possible with the Android API, sorry!", extra));
        return false;
    }
    ParcelFileDescriptor pfd = mOpenVPNService.openTun();
    if (pfd == null) return false;
    Method setInt;
    int fdint = pfd.getFd();
    try {
        setInt = FileDescriptor.class.getDeclaredMethod("setInt$", int.class);
        FileDescriptor fdtosend = new FileDescriptor();
        setInt.invoke(fdtosend, fdint);
        FileDescriptor[] fds = {fdtosend};
        mSocket.setFileDescriptorsForSend(fds);
        // Trigger a send so we can close the fd on our side of the channel
        // The API documentation fails to mention that it will not reset the file descriptor to
        // be send and will happily send the file descriptor on every write ...
        String cmd = String.format("needok '%s' %s\n", needed, "ok");
        managmentCommand(cmd);
        // Set the FileDescriptor to null to stop this mad behavior
        mSocket.setFileDescriptorsForSend(null);
        pfd.close();
        return true;
    } catch (NoSuchMethodException | IllegalArgumentException | InvocationTargetException |
            IOException | IllegalAccessException exp) {
        VpnStatus.logException("Could not send fd over socket", exp);
    }
    return false;
}
 
Example 6
Source File: SafUtils.java    From SAI with GNU General Public License v3.0 4 votes vote down vote up
public static File parcelFdToFile(ParcelFileDescriptor fd) {
    return new File("/proc/self/fd/" + fd.getFd());
}
 
Example 7
Source File: PFDRandomAccessIO.java    From edslite with GNU General Public License v2.0 4 votes vote down vote up
public PFDRandomAccessIO(ParcelFileDescriptor pfd)
{
    super(pfd.getFd());
    _pfd = pfd;
}
 
Example 8
Source File: OpenVpnManagementThread.java    From EasyVPN-Free with GNU General Public License v3.0 4 votes vote down vote up
private boolean sendTunFD(String needed, String extra) {
    if (!extra.equals("tun")) {
        // We only support tun
        VpnStatus.logError(String.format("Device type %s requested, but only tun is possible with the Android API, sorry!", extra));

        return false;
    }
    ParcelFileDescriptor pfd = mOpenVPNService.openTun();
    if (pfd == null)
        return false;

    Method setInt;
    int fdint = pfd.getFd();
    try {
        setInt = FileDescriptor.class.getDeclaredMethod("setInt$", int.class);
        FileDescriptor fdtosend = new FileDescriptor();

        setInt.invoke(fdtosend, fdint);

        FileDescriptor[] fds = {fdtosend};
        mSocket.setFileDescriptorsForSend(fds);

        // Trigger a send so we can close the fd on our side of the channel
        // The API documentation fails to mention that it will not reset the file descriptor to
        // be send and will happily send the file descriptor on every write ...
        String cmd = String.format("needok '%s' %s\n", needed, "ok");
        managmentCommand(cmd);

        // Set the FileDescriptor to null to stop this mad behavior
        mSocket.setFileDescriptorsForSend(null);

        pfd.close();

        return true;
    } catch (NoSuchMethodException | IllegalArgumentException | InvocationTargetException |
            IOException | IllegalAccessException exp) {
        VpnStatus.logException("Could not send fd over socket", exp);
    }

    return false;
}
 
Example 9
Source File: PrintingControllerImpl.java    From 365browser with Apache License 2.0 4 votes vote down vote up
@Override
public void onWrite(
        final PageRange[] ranges,
        final ParcelFileDescriptor destination,
        final CancellationSignal cancellationSignal,
        final PrintDocumentAdapterWrapper.WriteResultCallbackWrapper callback) {
    if (mPrintingContext == null) {
        callback.onWriteFailed(mErrorMessage);
        resetCallbacks();
        return;
    }

    // TODO(cimamoglu): Make use of CancellationSignal.
    mOnWriteCallback = callback;

    mFileDescriptor = destination.getFd();
    // Update file descriptor to PrintingContext mapping in the owner class.
    mPrintingContext.updatePrintingContextMap(mFileDescriptor, false);

    // We need to convert ranges list into an array of individual numbers for
    // easier JNI passing and compatibility with the native side.
    if (ranges.length == 1 && ranges[0].equals(PageRange.ALL_PAGES)) {
        // null corresponds to all pages in Chromium printing logic.
        mPages = null;
    } else {
        mPages = normalizeRanges(ranges);
    }

    if (mPrintingState == PRINTING_STATE_READY) {
        // If this onWrite is without a preceding onLayout, start Chromium PDF generation here.
        if (mPrintable.print()) {
            mPrintingState = PRINTING_STATE_STARTED_FROM_ONWRITE;
        } else {
            callback.onWriteFailed(mErrorMessage);
            resetCallbacks();
        }
    } else if (mPrintingState == PRINTING_STATE_STARTED_FROM_ONLAYOUT) {
        // Otherwise, continue previously started operation.
        mPrintingContext.askUserForSettingsReply(true);
    }
    // We are guaranteed by the framework that we will not have two onWrite calls at once.
    // We may get a CancellationSignal, after replying it (via WriteResultCallback) we might
    // get another onWrite call.
}
 
Example 10
Source File: Exec.java    From Ansole with GNU General Public License v2.0 4 votes vote down vote up
public static int getFd(ParcelFileDescriptor descriptor) {
	return descriptor.getFd();
}
 
Example 11
Source File: OpenVpnManagementThread.java    From bitmask_android with GNU General Public License v3.0 4 votes vote down vote up
private boolean sendTunFD(String needed, String extra) {
    if (!extra.equals("tun")) {
        // We only support tun
        VpnStatus.logError(String.format("Device type %s requested, but only tun is possible with the Android API, sorry!", extra));

        return false;
    }
    ParcelFileDescriptor pfd = mOpenVPNService.openTun();
    if (pfd == null)
        return false;

    Method setInt;
    int fdint = pfd.getFd();
    try {
        setInt = FileDescriptor.class.getDeclaredMethod("setInt$", int.class);
        FileDescriptor fdtosend = new FileDescriptor();

        setInt.invoke(fdtosend, fdint);

        FileDescriptor[] fds = {fdtosend};
        mSocket.setFileDescriptorsForSend(fds);

        // Trigger a send so we can close the fd on our side of the channel
        // The API documentation fails to mention that it will not reset the file descriptor to
        // be send and will happily send the file descriptor on every write ...
        String cmd = String.format("needok '%s' %s\n", needed, "ok");
        managmentCommand(cmd);

        // Set the FileDescriptor to null to stop this mad behavior
        mSocket.setFileDescriptorsForSend(null);

        pfd.close();

        return true;
    } catch (NoSuchMethodException | IllegalArgumentException | InvocationTargetException |
            IOException | IllegalAccessException exp) {
        VpnStatus.logException("Could not send fd over socket", exp);
    }

    return false;
}