Search in sources :

Example 41 with ParcelFileDescriptor

use of android.os.ParcelFileDescriptor in project platform_frameworks_base by android.

the class BasePrintTest method runShellCommand.

public static String runShellCommand(Instrumentation instrumentation, String cmd) throws IOException {
    ParcelFileDescriptor pfd = instrumentation.getUiAutomation().executeShellCommand(cmd);
    byte[] buf = new byte[512];
    int bytesRead;
    FileInputStream fis = new ParcelFileDescriptor.AutoCloseInputStream(pfd);
    StringBuffer stdout = new StringBuffer();
    while ((bytesRead = fis.read(buf)) != -1) {
        stdout.append(new String(buf, 0, bytesRead));
    }
    fis.close();
    return stdout.toString();
}
Also used : ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileInputStream(java.io.FileInputStream)

Example 42 with ParcelFileDescriptor

use of android.os.ParcelFileDescriptor in project platform_frameworks_base by android.

the class BasePrintTest method getEnabledImes.

private String[] getEnabledImes() throws IOException {
    List<String> imeList = new ArrayList<>();
    ParcelFileDescriptor pfd = getInstrumentation().getUiAutomation().executeShellCommand(COMMAND_LIST_ENABLED_IME_COMPONENTS);
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(pfd.getFileDescriptor())))) {
        String line;
        while ((line = reader.readLine()) != null) {
            imeList.add(line);
        }
    }
    String[] imeArray = new String[imeList.size()];
    imeList.toArray(imeArray);
    return imeArray;
}
Also used : InputStreamReader(java.io.InputStreamReader) ArrayList(java.util.ArrayList) ParcelFileDescriptor(android.os.ParcelFileDescriptor) BufferedReader(java.io.BufferedReader) FileInputStream(java.io.FileInputStream)

Example 43 with ParcelFileDescriptor

use of android.os.ParcelFileDescriptor in project platform_frameworks_base by android.

the class ProcessStatsService method getCurrentStats.

public byte[] getCurrentStats(List<ParcelFileDescriptor> historic) {
    mAm.mContext.enforceCallingOrSelfPermission(android.Manifest.permission.PACKAGE_USAGE_STATS, null);
    Parcel current = Parcel.obtain();
    synchronized (mAm) {
        long now = SystemClock.uptimeMillis();
        mProcessStats.mTimePeriodEndRealtime = SystemClock.elapsedRealtime();
        mProcessStats.mTimePeriodEndUptime = now;
        mProcessStats.writeToParcel(current, now, 0);
    }
    mWriteLock.lock();
    try {
        if (historic != null) {
            ArrayList<String> files = getCommittedFiles(0, false, true);
            if (files != null) {
                for (int i = files.size() - 1; i >= 0; i--) {
                    try {
                        ParcelFileDescriptor pfd = ParcelFileDescriptor.open(new File(files.get(i)), ParcelFileDescriptor.MODE_READ_ONLY);
                        historic.add(pfd);
                    } catch (IOException e) {
                        Slog.w(TAG, "Failure opening procstat file " + files.get(i), e);
                    }
                }
            }
        }
    } finally {
        mWriteLock.unlock();
    }
    return current.marshall();
}
Also used : Parcel(android.os.Parcel) ParcelFileDescriptor(android.os.ParcelFileDescriptor) IOException(java.io.IOException) AtomicFile(android.util.AtomicFile) File(java.io.File)

Example 44 with ParcelFileDescriptor

use of android.os.ParcelFileDescriptor in project platform_frameworks_base by android.

the class ProcessStatsService method getStatsOverTime.

public ParcelFileDescriptor getStatsOverTime(long minTime) {
    mAm.mContext.enforceCallingOrSelfPermission(android.Manifest.permission.PACKAGE_USAGE_STATS, null);
    Parcel current = Parcel.obtain();
    long curTime;
    synchronized (mAm) {
        long now = SystemClock.uptimeMillis();
        mProcessStats.mTimePeriodEndRealtime = SystemClock.elapsedRealtime();
        mProcessStats.mTimePeriodEndUptime = now;
        mProcessStats.writeToParcel(current, now, 0);
        curTime = mProcessStats.mTimePeriodEndRealtime - mProcessStats.mTimePeriodStartRealtime;
    }
    mWriteLock.lock();
    try {
        if (curTime < minTime) {
            // Need to add in older stats to reach desired time.
            ArrayList<String> files = getCommittedFiles(0, false, true);
            if (files != null && files.size() > 0) {
                current.setDataPosition(0);
                ProcessStats stats = ProcessStats.CREATOR.createFromParcel(current);
                current.recycle();
                int i = files.size() - 1;
                while (i >= 0 && (stats.mTimePeriodEndRealtime - stats.mTimePeriodStartRealtime) < minTime) {
                    AtomicFile file = new AtomicFile(new File(files.get(i)));
                    i--;
                    ProcessStats moreStats = new ProcessStats(false);
                    readLocked(moreStats, file);
                    if (moreStats.mReadError == null) {
                        stats.add(moreStats);
                        StringBuilder sb = new StringBuilder();
                        sb.append("Added stats: ");
                        sb.append(moreStats.mTimePeriodStartClockStr);
                        sb.append(", over ");
                        TimeUtils.formatDuration(moreStats.mTimePeriodEndRealtime - moreStats.mTimePeriodStartRealtime, sb);
                        Slog.i(TAG, sb.toString());
                    } else {
                        Slog.w(TAG, "Failure reading " + files.get(i + 1) + "; " + moreStats.mReadError);
                        continue;
                    }
                }
                current = Parcel.obtain();
                stats.writeToParcel(current, 0);
            }
        }
        final byte[] outData = current.marshall();
        current.recycle();
        final ParcelFileDescriptor[] fds = ParcelFileDescriptor.createPipe();
        Thread thr = new Thread("ProcessStats pipe output") {

            public void run() {
                FileOutputStream fout = new ParcelFileDescriptor.AutoCloseOutputStream(fds[1]);
                try {
                    fout.write(outData);
                    fout.close();
                } catch (IOException e) {
                    Slog.w(TAG, "Failure writing pipe", e);
                }
            }
        };
        thr.start();
        return fds[0];
    } catch (IOException e) {
        Slog.w(TAG, "Failed building output pipe", e);
    } finally {
        mWriteLock.unlock();
    }
    return null;
}
Also used : IProcessStats(com.android.internal.app.procstats.IProcessStats) ProcessStats(com.android.internal.app.procstats.ProcessStats) Parcel(android.os.Parcel) IOException(java.io.IOException) BackgroundThread(com.android.internal.os.BackgroundThread) AtomicFile(android.util.AtomicFile) FileOutputStream(java.io.FileOutputStream) ParcelFileDescriptor(android.os.ParcelFileDescriptor) AtomicFile(android.util.AtomicFile) File(java.io.File)

Example 45 with ParcelFileDescriptor

use of android.os.ParcelFileDescriptor in project platform_frameworks_base by android.

the class Vpn method establish.

/**
     * Establish a VPN network and return the file descriptor of the VPN
     * interface. This methods returns {@code null} if the application is
     * revoked or not prepared.
     *
     * @param config The parameters to configure the network.
     * @return The file descriptor of the VPN interface.
     */
public synchronized ParcelFileDescriptor establish(VpnConfig config) {
    // Check if the caller is already prepared.
    UserManager mgr = UserManager.get(mContext);
    if (Binder.getCallingUid() != mOwnerUID) {
        return null;
    }
    // Check to ensure consent hasn't been revoked since we were prepared.
    if (!isVpnUserPreConsented(mPackage)) {
        return null;
    }
    // Check if the service is properly declared.
    Intent intent = new Intent(VpnConfig.SERVICE_INTERFACE);
    intent.setClassName(mPackage, config.user);
    long token = Binder.clearCallingIdentity();
    try {
        // Restricted users are not allowed to create VPNs, they are tied to Owner
        UserInfo user = mgr.getUserInfo(mUserHandle);
        if (user.isRestricted()) {
            throw new SecurityException("Restricted users cannot establish VPNs");
        }
        ResolveInfo info = AppGlobals.getPackageManager().resolveService(intent, null, 0, mUserHandle);
        if (info == null) {
            throw new SecurityException("Cannot find " + config.user);
        }
        if (!BIND_VPN_SERVICE.equals(info.serviceInfo.permission)) {
            throw new SecurityException(config.user + " does not require " + BIND_VPN_SERVICE);
        }
    } catch (RemoteException e) {
        throw new SecurityException("Cannot find " + config.user);
    } finally {
        Binder.restoreCallingIdentity(token);
    }
    // Save the old config in case we need to go back.
    VpnConfig oldConfig = mConfig;
    String oldInterface = mInterface;
    Connection oldConnection = mConnection;
    NetworkAgent oldNetworkAgent = mNetworkAgent;
    mNetworkAgent = null;
    Set<UidRange> oldUsers = mVpnUsers;
    // Configure the interface. Abort if any of these steps fails.
    ParcelFileDescriptor tun = ParcelFileDescriptor.adoptFd(jniCreate(config.mtu));
    try {
        updateState(DetailedState.CONNECTING, "establish");
        String interfaze = jniGetName(tun.getFd());
        // TEMP use the old jni calls until there is support for netd address setting
        StringBuilder builder = new StringBuilder();
        for (LinkAddress address : config.addresses) {
            builder.append(" " + address);
        }
        if (jniSetAddresses(interfaze, builder.toString()) < 1) {
            throw new IllegalArgumentException("At least one address must be specified");
        }
        Connection connection = new Connection();
        if (!mContext.bindServiceAsUser(intent, connection, Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE, new UserHandle(mUserHandle))) {
            throw new IllegalStateException("Cannot bind " + config.user);
        }
        mConnection = connection;
        mInterface = interfaze;
        // Fill more values.
        config.user = mPackage;
        config.interfaze = mInterface;
        config.startTime = SystemClock.elapsedRealtime();
        mConfig = config;
        // Set up forwarding and DNS rules.
        agentConnect();
        if (oldConnection != null) {
            mContext.unbindService(oldConnection);
        }
        // Remove the old tun's user forwarding rules
        // The new tun's user rules have already been added so they will take over
        // as rules are deleted. This prevents data leakage as the rules are moved over.
        agentDisconnect(oldNetworkAgent);
        if (oldInterface != null && !oldInterface.equals(interfaze)) {
            jniReset(oldInterface);
        }
        try {
            IoUtils.setBlocking(tun.getFileDescriptor(), config.blocking);
        } catch (IOException e) {
            throw new IllegalStateException("Cannot set tunnel's fd as blocking=" + config.blocking, e);
        }
    } catch (RuntimeException e) {
        IoUtils.closeQuietly(tun);
        agentDisconnect();
        // restore old state
        mConfig = oldConfig;
        mConnection = oldConnection;
        mVpnUsers = oldUsers;
        mNetworkAgent = oldNetworkAgent;
        mInterface = oldInterface;
        throw e;
    }
    Log.i(TAG, "Established by " + config.user + " on " + mInterface);
    return tun;
}
Also used : LinkAddress(android.net.LinkAddress) NetworkAgent(android.net.NetworkAgent) VpnConfig(com.android.internal.net.VpnConfig) UidRange(android.net.UidRange) ServiceConnection(android.content.ServiceConnection) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) UserInfo(android.content.pm.UserInfo) IOException(java.io.IOException) ResolveInfo(android.content.pm.ResolveInfo) UserManager(android.os.UserManager) UserHandle(android.os.UserHandle) ParcelFileDescriptor(android.os.ParcelFileDescriptor) RemoteException(android.os.RemoteException)

Aggregations

ParcelFileDescriptor (android.os.ParcelFileDescriptor)526 IOException (java.io.IOException)199 FileNotFoundException (java.io.FileNotFoundException)136 RemoteException (android.os.RemoteException)127 File (java.io.File)127 FileDescriptor (java.io.FileDescriptor)58 FileOutputStream (java.io.FileOutputStream)58 AssetFileDescriptor (android.content.res.AssetFileDescriptor)44 FileInputStream (java.io.FileInputStream)36 Parcel (android.os.Parcel)35 Uri (android.net.Uri)33 Intent (android.content.Intent)30 Bundle (android.os.Bundle)29 Cursor (android.database.Cursor)27 StorageManager (android.os.storage.StorageManager)25 Request (android.app.DownloadManager.Request)24 Bitmap (android.graphics.Bitmap)22 InputStream (java.io.InputStream)18 ProfilerInfo (android.app.ProfilerInfo)17 Binder (android.os.Binder)17