Search in sources :

Example 36 with ParcelFileDescriptor

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

the class AppFuseTest method testOpenFile.

public void testOpenFile() throws IOException {
    final StorageManager storageManager = getContext().getSystemService(StorageManager.class);
    final int INODE = 10;
    final AppFuse appFuse = new AppFuse("test", new TestCallback() {

        @Override
        public long getFileSize(int inode) throws FileNotFoundException {
            if (INODE == inode) {
                return 1024;
            }
            throw new FileNotFoundException();
        }
    });
    appFuse.mount(storageManager);
    final ParcelFileDescriptor fd = appFuse.openFile(INODE, ParcelFileDescriptor.MODE_READ_ONLY);
    fd.close();
    appFuse.close();
}
Also used : StorageManager(android.os.storage.StorageManager) FileNotFoundException(java.io.FileNotFoundException) ParcelFileDescriptor(android.os.ParcelFileDescriptor)

Example 37 with ParcelFileDescriptor

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

the class AppFuseTest method testReadFile.

public void testReadFile() throws IOException {
    final StorageManager storageManager = getContext().getSystemService(StorageManager.class);
    final int fileInode = 10;
    final byte[] fileBytes = new byte[] { 'a', 'b', 'c', 'd', 'e' };
    final AppFuse appFuse = new AppFuse("test", new TestCallback() {

        @Override
        public long getFileSize(int inode) throws FileNotFoundException {
            if (inode == fileInode) {
                return fileBytes.length;
            }
            return super.getFileSize(inode);
        }

        @Override
        public long readObjectBytes(int inode, long offset, long size, byte[] bytes) throws IOException {
            if (inode == fileInode) {
                int i = 0;
                while (i < size && i + offset < fileBytes.length) {
                    bytes[i] = fileBytes[(int) (i + offset)];
                    i++;
                }
                return i;
            }
            return super.readObjectBytes(inode, offset, size, bytes);
        }
    });
    appFuse.mount(storageManager);
    final ParcelFileDescriptor fd = appFuse.openFile(fileInode, ParcelFileDescriptor.MODE_READ_ONLY);
    try (final ParcelFileDescriptor.AutoCloseInputStream stream = new ParcelFileDescriptor.AutoCloseInputStream(fd)) {
        final byte[] buffer = new byte[1024];
        final int size = stream.read(buffer, 0, buffer.length);
        assertEquals(5, size);
    }
    appFuse.close();
}
Also used : StorageManager(android.os.storage.StorageManager) FileNotFoundException(java.io.FileNotFoundException) ParcelFileDescriptor(android.os.ParcelFileDescriptor) IOException(java.io.IOException)

Example 38 with ParcelFileDescriptor

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

the class AppFuseTest method testWriteFile.

public void testWriteFile() throws IOException {
    final StorageManager storageManager = getContext().getSystemService(StorageManager.class);
    final int INODE = 10;
    final byte[] resultBytes = new byte[5];
    final AppFuse appFuse = new AppFuse("test", new TestCallback() {

        @Override
        public long getFileSize(int inode) throws FileNotFoundException {
            if (inode != INODE) {
                throw new FileNotFoundException();
            }
            return resultBytes.length;
        }

        @Override
        public int writeObjectBytes(long fileHandle, int inode, long offset, int size, byte[] bytes) {
            for (int i = 0; i < size; i++) {
                resultBytes[(int) (offset + i)] = bytes[i];
            }
            return size;
        }
    });
    appFuse.mount(storageManager);
    final ParcelFileDescriptor fd = appFuse.openFile(INODE, ParcelFileDescriptor.MODE_WRITE_ONLY | ParcelFileDescriptor.MODE_TRUNCATE);
    try (final ParcelFileDescriptor.AutoCloseOutputStream stream = new ParcelFileDescriptor.AutoCloseOutputStream(fd)) {
        stream.write('a');
        stream.write('b');
        stream.write('c');
        stream.write('d');
        stream.write('e');
    }
    final byte[] BYTES = new byte[] { 'a', 'b', 'c', 'd', 'e' };
    assertTrue(Arrays.equals(BYTES, resultBytes));
    appFuse.close();
}
Also used : StorageManager(android.os.storage.StorageManager) FileNotFoundException(java.io.FileNotFoundException) ParcelFileDescriptor(android.os.ParcelFileDescriptor)

Example 39 with ParcelFileDescriptor

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

the class ActivityManagerService method recordPssSampleLocked.

/**
     * Record new PSS sample for a process.
     */
void recordPssSampleLocked(ProcessRecord proc, int procState, long pss, long uss, long swapPss, long now) {
    EventLogTags.writeAmPss(proc.pid, proc.uid, proc.processName, pss * 1024, uss * 1024, swapPss * 1024);
    proc.lastPssTime = now;
    proc.baseProcessTracker.addPss(pss, uss, true, proc.pkgList);
    if (DEBUG_PSS)
        Slog.d(TAG_PSS, "PSS of " + proc.toShortString() + ": " + pss + " lastPss=" + proc.lastPss + " state=" + ProcessList.makeProcStateString(procState));
    if (proc.initialIdlePss == 0) {
        proc.initialIdlePss = pss;
    }
    proc.lastPss = pss;
    proc.lastSwapPss = swapPss;
    if (procState >= ActivityManager.PROCESS_STATE_HOME) {
        proc.lastCachedPss = pss;
        proc.lastCachedSwapPss = swapPss;
    }
    final SparseArray<Pair<Long, String>> watchUids = mMemWatchProcesses.getMap().get(proc.processName);
    Long check = null;
    if (watchUids != null) {
        Pair<Long, String> val = watchUids.get(proc.uid);
        if (val == null) {
            val = watchUids.get(0);
        }
        if (val != null) {
            check = val.first;
        }
    }
    if (check != null) {
        if ((pss * 1024) >= check && proc.thread != null && mMemWatchDumpProcName == null) {
            boolean isDebuggable = "1".equals(SystemProperties.get(SYSTEM_DEBUGGABLE, "0"));
            if (!isDebuggable) {
                if ((proc.info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
                    isDebuggable = true;
                }
            }
            if (isDebuggable) {
                Slog.w(TAG, "Process " + proc + " exceeded pss limit " + check + "; reporting");
                final ProcessRecord myProc = proc;
                final File heapdumpFile = DumpHeapProvider.getJavaFile();
                mMemWatchDumpProcName = proc.processName;
                mMemWatchDumpFile = heapdumpFile.toString();
                mMemWatchDumpPid = proc.pid;
                mMemWatchDumpUid = proc.uid;
                BackgroundThread.getHandler().post(new Runnable() {

                    @Override
                    public void run() {
                        revokeUriPermission(ActivityThread.currentActivityThread().getApplicationThread(), DumpHeapActivity.JAVA_URI, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION, UserHandle.myUserId());
                        ParcelFileDescriptor fd = null;
                        try {
                            heapdumpFile.delete();
                            fd = ParcelFileDescriptor.open(heapdumpFile, ParcelFileDescriptor.MODE_CREATE | ParcelFileDescriptor.MODE_TRUNCATE | ParcelFileDescriptor.MODE_WRITE_ONLY | ParcelFileDescriptor.MODE_APPEND);
                            IApplicationThread thread = myProc.thread;
                            if (thread != null) {
                                try {
                                    if (DEBUG_PSS)
                                        Slog.d(TAG_PSS, "Requesting dump heap from " + myProc + " to " + heapdumpFile);
                                    thread.dumpHeap(true, heapdumpFile.toString(), fd);
                                } catch (RemoteException e) {
                                }
                            }
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        } finally {
                            if (fd != null) {
                                try {
                                    fd.close();
                                } catch (IOException e) {
                                }
                            }
                        }
                    }
                });
            } else {
                Slog.w(TAG, "Process " + proc + " exceeded pss limit " + check + ", but debugging not enabled");
            }
        }
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) IApplicationThread(android.app.IApplicationThread) AtomicLong(java.util.concurrent.atomic.AtomicLong) ParcelFileDescriptor(android.os.ParcelFileDescriptor) RemoteException(android.os.RemoteException) File(java.io.File) AtomicFile(android.util.AtomicFile) Pair(android.util.Pair)

Example 40 with ParcelFileDescriptor

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

the class ActivityManagerService method profileControl.

public boolean profileControl(String process, int userId, boolean start, ProfilerInfo profilerInfo, int profileType) throws RemoteException {
    try {
        synchronized (this) {
            // its own permission.
            if (checkCallingPermission(android.Manifest.permission.SET_ACTIVITY_WATCHER) != PackageManager.PERMISSION_GRANTED) {
                throw new SecurityException("Requires permission " + android.Manifest.permission.SET_ACTIVITY_WATCHER);
            }
            if (start && (profilerInfo == null || profilerInfo.profileFd == null)) {
                throw new IllegalArgumentException("null profile info or fd");
            }
            ProcessRecord proc = null;
            if (process != null) {
                proc = findProcessLocked(process, userId, "profileControl");
            }
            if (start && (proc == null || proc.thread == null)) {
                throw new IllegalArgumentException("Unknown process: " + process);
            }
            if (start) {
                stopProfilerLocked(null, 0);
                setProfileApp(proc.info, proc.processName, profilerInfo);
                mProfileProc = proc;
                mProfileType = profileType;
                ParcelFileDescriptor fd = profilerInfo.profileFd;
                try {
                    fd = fd.dup();
                } catch (IOException e) {
                    fd = null;
                }
                profilerInfo.profileFd = fd;
                proc.thread.profilerControl(start, profilerInfo, profileType);
                fd = null;
                mProfileFd = null;
            } else {
                stopProfilerLocked(proc, profileType);
                if (profilerInfo != null && profilerInfo.profileFd != null) {
                    try {
                        profilerInfo.profileFd.close();
                    } catch (IOException e) {
                    }
                }
            }
            return true;
        }
    } catch (RemoteException e) {
        throw new IllegalStateException("Process disappeared");
    } finally {
        if (profilerInfo != null && profilerInfo.profileFd != null) {
            try {
                profilerInfo.profileFd.close();
            } catch (IOException e) {
            }
        }
    }
}
Also used : ParcelFileDescriptor(android.os.ParcelFileDescriptor) IOException(java.io.IOException) 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