Search in sources :

Example 1 with FileObserver

use of android.os.FileObserver in project glasquare by davidvavra.

the class ImageUtils method processPictureWhenReady.

public static void processPictureWhenReady(final Activity activity, final File picture, final OnPictureReadyListener listener) {
    if (picture.exists()) {
        // The picture is ready; process it.
        listener.onPictureReady();
    } else {
        // The file does not exist yet. Before starting the file observer, you
        // can update your UI to let the user know that the application is
        // waiting for the picture (for example, by displaying the thumbnail
        // image and a progress indicator).
        final File parentDirectory = picture.getParentFile();
        FileObserver observer = new FileObserver(parentDirectory.getPath()) {

            // Protect against additional pending events after CLOSE_WRITE is
            // handled.
            private boolean isFileWritten;

            @Override
            public void onEvent(int event, String path) {
                if (!isFileWritten) {
                    // For safety, make sure that the file that was created in
                    // the directory is actually the one that we're expecting.
                    File affectedFile = new File(parentDirectory, path);
                    isFileWritten = (event == FileObserver.CLOSE_WRITE && affectedFile.equals(picture));
                    if (isFileWritten) {
                        stopWatching();
                        // Now that the file is ready, recursively call
                        // processPictureWhenReady again (on the UI thread).
                        activity.runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                processPictureWhenReady(activity, picture, listener);
                            }
                        });
                    }
                }
            }
        };
        observer.startWatching();
    }
}
Also used : File(java.io.File) FileObserver(android.os.FileObserver)

Example 2 with FileObserver

use of android.os.FileObserver in project android_frameworks_base by ParanoidAndroid.

the class BootReceiver method logBootEvents.

private void logBootEvents(Context ctx) throws IOException {
    final DropBoxManager db = (DropBoxManager) ctx.getSystemService(Context.DROPBOX_SERVICE);
    final SharedPreferences prefs = ctx.getSharedPreferences("log_files", Context.MODE_PRIVATE);
    final String headers = new StringBuilder(512).append("Build: ").append(Build.FINGERPRINT).append("\n").append("Hardware: ").append(Build.BOARD).append("\n").append("Revision: ").append(SystemProperties.get("ro.revision", "")).append("\n").append("Bootloader: ").append(Build.BOOTLOADER).append("\n").append("Radio: ").append(Build.RADIO).append("\n").append("Kernel: ").append(FileUtils.readTextFile(new File("/proc/version"), 1024, "...\n")).append("\n").toString();
    String recovery = RecoverySystem.handleAftermath();
    if (recovery != null && db != null) {
        db.addText("SYSTEM_RECOVERY_LOG", headers + recovery);
    }
    if (SystemProperties.getLong("ro.runtime.firstboot", 0) == 0) {
        String now = Long.toString(System.currentTimeMillis());
        SystemProperties.set("ro.runtime.firstboot", now);
        if (db != null)
            db.addText("SYSTEM_BOOT", headers);
        // Negative sizes mean to take the *tail* of the file (see FileUtils.readTextFile())
        addFileToDropBox(db, prefs, headers, "/proc/last_kmsg", -LOG_SIZE, "SYSTEM_LAST_KMSG");
        addFileToDropBox(db, prefs, headers, "/cache/recovery/log", -LOG_SIZE, "SYSTEM_RECOVERY_LOG");
        addFileToDropBox(db, prefs, headers, "/data/dontpanic/apanic_console", -LOG_SIZE, "APANIC_CONSOLE");
        addFileToDropBox(db, prefs, headers, "/data/dontpanic/apanic_threads", -LOG_SIZE, "APANIC_THREADS");
        addAuditErrorsToDropBox(db, prefs, headers, -LOG_SIZE, "SYSTEM_AUDIT");
    } else {
        if (db != null)
            db.addText("SYSTEM_RESTART", headers);
    }
    // Scan existing tombstones (in case any new ones appeared)
    File[] tombstoneFiles = TOMBSTONE_DIR.listFiles();
    for (int i = 0; tombstoneFiles != null && i < tombstoneFiles.length; i++) {
        addFileToDropBox(db, prefs, headers, tombstoneFiles[i].getPath(), LOG_SIZE, "SYSTEM_TOMBSTONE");
    }
    // Start watching for new tombstone files; will record them as they occur.
    // This gets registered with the singleton file observer thread.
    sTombstoneObserver = new FileObserver(TOMBSTONE_DIR.getPath(), FileObserver.CLOSE_WRITE) {

        @Override
        public void onEvent(int event, String path) {
            try {
                String filename = new File(TOMBSTONE_DIR, path).getPath();
                addFileToDropBox(db, prefs, headers, filename, LOG_SIZE, "SYSTEM_TOMBSTONE");
            } catch (IOException e) {
                Slog.e(TAG, "Can't log tombstone", e);
            }
        }
    };
    sTombstoneObserver.startWatching();
}
Also used : DropBoxManager(android.os.DropBoxManager) SharedPreferences(android.content.SharedPreferences) IOException(java.io.IOException) File(java.io.File) FileObserver(android.os.FileObserver)

Example 3 with FileObserver

use of android.os.FileObserver in project android_frameworks_base by ParanoidAndroid.

the class ActivityManagerService method dumpStackTraces.

private static void dumpStackTraces(String tracesPath, ArrayList<Integer> firstPids, ProcessStats processStats, SparseArray<Boolean> lastPids, String[] nativeProcs) {
    // Use a FileObserver to detect when traces finish writing.
    // The order of traces is considered important to maintain for legibility.
    FileObserver observer = new FileObserver(tracesPath, FileObserver.CLOSE_WRITE) {

        public synchronized void onEvent(int event, String path) {
            notify();
        }
    };
    try {
        observer.startWatching();
        // First collect all of the stacks of the most important pids.
        if (firstPids != null) {
            try {
                int num = firstPids.size();
                for (int i = 0; i < num; i++) {
                    synchronized (observer) {
                        Process.sendSignal(firstPids.get(i), Process.SIGNAL_QUIT);
                        // Wait for write-close, give up after 200msec
                        observer.wait(200);
                    }
                }
            } catch (InterruptedException e) {
                Log.wtf(TAG, e);
            }
        }
        // Next measure CPU usage.
        if (processStats != null) {
            processStats.init();
            System.gc();
            processStats.update();
            try {
                synchronized (processStats) {
                    // measure over 1/2 second.
                    processStats.wait(500);
                }
            } catch (InterruptedException e) {
            }
            processStats.update();
            // We'll take the stack crawls of just the top apps using CPU.
            final int N = processStats.countWorkingStats();
            int numProcs = 0;
            for (int i = 0; i < N && numProcs < 5; i++) {
                ProcessStats.Stats stats = processStats.getWorkingStats(i);
                if (lastPids.indexOfKey(stats.pid) >= 0) {
                    numProcs++;
                    try {
                        synchronized (observer) {
                            Process.sendSignal(stats.pid, Process.SIGNAL_QUIT);
                            // Wait for write-close, give up after 200msec
                            observer.wait(200);
                        }
                    } catch (InterruptedException e) {
                        Log.wtf(TAG, e);
                    }
                }
            }
        }
    } finally {
        observer.stopWatching();
    }
    if (nativeProcs != null) {
        int[] pids = Process.getPidsForCommands(nativeProcs);
        if (pids != null) {
            for (int pid : pids) {
                Debug.dumpNativeBacktraceToFile(pid, tracesPath);
            }
        }
    }
}
Also used : ProcessStats(com.android.internal.os.ProcessStats) FileObserver(android.os.FileObserver)

Example 4 with FileObserver

use of android.os.FileObserver in project cornerstone by Onskreen.

the class ActivityManagerService method dumpStackTraces.

/**
     * If a stack trace dump file is configured, dump process stack traces.
     * @param clearTraces causes the dump file to be erased prior to the new
     *    traces being written, if true; when false, the new traces will be
     *    appended to any existing file content.
     * @param firstPids of dalvik VM processes to dump stack traces for first
     * @param lastPids of dalvik VM processes to dump stack traces for last
     * @return file containing stack traces, or null if no dump file is configured
     */
public static File dumpStackTraces(boolean clearTraces, ArrayList<Integer> firstPids, ProcessStats processStats, SparseArray<Boolean> lastPids) {
    String tracesPath = SystemProperties.get("dalvik.vm.stack-trace-file", null);
    if (tracesPath == null || tracesPath.length() == 0) {
        return null;
    }
    File tracesFile = new File(tracesPath);
    try {
        File tracesDir = tracesFile.getParentFile();
        if (!tracesDir.exists())
            tracesFile.mkdirs();
        // drwxrwxr-x
        FileUtils.setPermissions(tracesDir.getPath(), 0775, -1, -1);
        if (clearTraces && tracesFile.exists())
            tracesFile.delete();
        tracesFile.createNewFile();
        // -rw-rw-rw-
        FileUtils.setPermissions(tracesFile.getPath(), 0666, -1, -1);
    } catch (IOException e) {
        Slog.w(TAG, "Unable to prepare ANR traces file: " + tracesPath, e);
        return null;
    }
    // Use a FileObserver to detect when traces finish writing.
    // The order of traces is considered important to maintain for legibility.
    FileObserver observer = new FileObserver(tracesPath, FileObserver.CLOSE_WRITE) {

        public synchronized void onEvent(int event, String path) {
            notify();
        }
    };
    try {
        observer.startWatching();
        // First collect all of the stacks of the most important pids.
        try {
            int num = firstPids.size();
            for (int i = 0; i < num; i++) {
                synchronized (observer) {
                    Process.sendSignal(firstPids.get(i), Process.SIGNAL_QUIT);
                    // Wait for write-close, give up after 200msec
                    observer.wait(200);
                }
            }
        } catch (InterruptedException e) {
            Log.wtf(TAG, e);
        }
        // Next measure CPU usage.
        if (processStats != null) {
            processStats.init();
            System.gc();
            processStats.update();
            try {
                synchronized (processStats) {
                    // measure over 1/2 second.
                    processStats.wait(500);
                }
            } catch (InterruptedException e) {
            }
            processStats.update();
            // We'll take the stack crawls of just the top apps using CPU.
            final int N = processStats.countWorkingStats();
            int numProcs = 0;
            for (int i = 0; i < N && numProcs < 5; i++) {
                ProcessStats.Stats stats = processStats.getWorkingStats(i);
                if (lastPids.indexOfKey(stats.pid) >= 0) {
                    numProcs++;
                    try {
                        synchronized (observer) {
                            Process.sendSignal(stats.pid, Process.SIGNAL_QUIT);
                            // Wait for write-close, give up after 200msec
                            observer.wait(200);
                        }
                    } catch (InterruptedException e) {
                        Log.wtf(TAG, e);
                    }
                }
            }
        }
        return tracesFile;
    } finally {
        observer.stopWatching();
    }
}
Also used : ProcessStats(com.android.internal.os.ProcessStats) IOException(java.io.IOException) File(java.io.File) FileObserver(android.os.FileObserver)

Example 5 with FileObserver

use of android.os.FileObserver in project cornerstone by Onskreen.

the class ActivityManagerService method dumpStackTraces.

private static void dumpStackTraces(String tracesPath, ArrayList<Integer> firstPids, ProcessStats processStats, SparseArray<Boolean> lastPids, String[] nativeProcs) {
    // Use a FileObserver to detect when traces finish writing.
    // The order of traces is considered important to maintain for legibility.
    FileObserver observer = new FileObserver(tracesPath, FileObserver.CLOSE_WRITE) {

        public synchronized void onEvent(int event, String path) {
            notify();
        }
    };
    try {
        observer.startWatching();
        // First collect all of the stacks of the most important pids.
        if (firstPids != null) {
            try {
                int num = firstPids.size();
                for (int i = 0; i < num; i++) {
                    synchronized (observer) {
                        Process.sendSignal(firstPids.get(i), Process.SIGNAL_QUIT);
                        // Wait for write-close, give up after 200msec
                        observer.wait(200);
                    }
                }
            } catch (InterruptedException e) {
                Log.wtf(TAG, e);
            }
        }
        // Next measure CPU usage.
        if (processStats != null) {
            processStats.init();
            System.gc();
            processStats.update();
            try {
                synchronized (processStats) {
                    // measure over 1/2 second.
                    processStats.wait(500);
                }
            } catch (InterruptedException e) {
            }
            processStats.update();
            // We'll take the stack crawls of just the top apps using CPU.
            final int N = processStats.countWorkingStats();
            int numProcs = 0;
            for (int i = 0; i < N && numProcs < 5; i++) {
                ProcessStats.Stats stats = processStats.getWorkingStats(i);
                if (lastPids.indexOfKey(stats.pid) >= 0) {
                    numProcs++;
                    try {
                        synchronized (observer) {
                            Process.sendSignal(stats.pid, Process.SIGNAL_QUIT);
                            // Wait for write-close, give up after 200msec
                            observer.wait(200);
                        }
                    } catch (InterruptedException e) {
                        Log.wtf(TAG, e);
                    }
                }
            }
        }
    } finally {
        observer.stopWatching();
    }
    if (nativeProcs != null) {
        int[] pids = Process.getPidsForCommands(nativeProcs);
        if (pids != null) {
            for (int pid : pids) {
                Debug.dumpNativeBacktraceToFile(pid, tracesPath);
            }
        }
    }
}
Also used : ProcessStats(com.android.internal.os.ProcessStats) FileObserver(android.os.FileObserver)

Aggregations

FileObserver (android.os.FileObserver)22 File (java.io.File)15 DropBoxManager (android.os.DropBoxManager)12 IOException (java.io.IOException)7 AtomicFile (android.util.AtomicFile)5 HashMap (java.util.HashMap)5 ProcessStats (com.android.internal.os.ProcessStats)3 Point (android.graphics.Point)2 ProcessCpuTracker (com.android.internal.os.ProcessCpuTracker)2 SharedPreferences (android.content.SharedPreferences)1 Stack (java.util.Stack)1