FileDocCategorySizeDatePackage
TaskPersister.javaAPI DocAndroid 5.1 API53244Thu Mar 12 22:22:42 GMT 2015com.android.server.am

TaskPersister

public class TaskPersister extends Object

Fields Summary
static final String
TAG
static final boolean
DEBUG_PERSISTER
static final boolean
DEBUG_RESTORER
private static final long
INTER_WRITE_DELAY_MS
When not flushing don't write out files faster than this
private static final long
PRE_TASK_DELAY_MS
When not flushing delay this long before writing the first file out. This gives the next task being launched a chance to load its resources without this occupying IO bandwidth.
private static final int
MAX_WRITE_QUEUE_LENGTH
The maximum number of entries to keep in the queue before draining it automatically.
private static final long
FLUSH_QUEUE
Special value for mWriteTime to mean don't wait, just write
private static final String
RECENTS_FILENAME
private static final String
TASKS_DIRNAME
private static final String
TASK_EXTENSION
private static final String
IMAGES_DIRNAME
static final String
IMAGE_EXTENSION
private static final String
RESTORED_TASKS_DIRNAME
private static final long
MAX_INSTALL_WAIT_TIME
private static final String
TAG_TASK
static File
sImagesDir
static File
sTasksDir
static File
sRestoredTasksDir
private final ActivityManagerService
mService
private final ActivityStackSupervisor
mStackSupervisor
private long
mNextWriteTime
Value determines write delay mode as follows: < 0 We are Flushing. No delays between writes until the image queue is drained and all tasks needing persisting are written to disk. There is no delay between writes. == 0 We are Idle. Next writes will be delayed by #PRE_TASK_DELAY_MS. > 0 We are Actively writing. Next write will be at this time. Subsequent writes will be delayed by #INTER_WRITE_DELAY_MS.
private final LazyTaskWriterThread
mLazyTaskWriterThread
ArrayList
mWriteQueue
private android.util.ArrayMap
mOtherDeviceTasksMap
private android.util.ArrayMap
mPackageUidMap
private long
mExpiredTasksCleanupTime
Constructors Summary
TaskPersister(File systemDir, ActivityStackSupervisor stackSupervisor)


        
        sTasksDir = new File(systemDir, TASKS_DIRNAME);
        if (!sTasksDir.exists()) {
            if (DEBUG_PERSISTER) Slog.d(TAG, "Creating tasks directory " + sTasksDir);
            if (!sTasksDir.mkdir()) {
                Slog.e(TAG, "Failure creating tasks directory " + sTasksDir);
            }
        }

        sImagesDir = new File(systemDir, IMAGES_DIRNAME);
        if (!sImagesDir.exists()) {
            if (DEBUG_PERSISTER) Slog.d(TAG, "Creating images directory " + sTasksDir);
            if (!sImagesDir.mkdir()) {
                Slog.e(TAG, "Failure creating images directory " + sImagesDir);
            }
        }

        sRestoredTasksDir = new File(systemDir, RESTORED_TASKS_DIRNAME);

        mStackSupervisor = stackSupervisor;
        mService = stackSupervisor.mService;

        mLazyTaskWriterThread = new LazyTaskWriterThread("LazyTaskWriterThread");
    
Methods Summary
private voidaddOtherDeviceTasksToRecentsLocked()
Tries to add all backed-up tasks from another device to this device recent's list.

        synchronized (mOtherDeviceTasksMap) {
            for (int i = mOtherDeviceTasksMap.size() - 1; i >= 0; i--) {
                addOtherDeviceTasksToRecentsLocked(mOtherDeviceTasksMap.keyAt(i));
            }
        }
    
voidaddOtherDeviceTasksToRecentsLocked(java.lang.String packageName)
Tries to add backed-up tasks that are associated with the input package from another device to this device recent's list.

        synchronized (mOtherDeviceTasksMap) {
            List<List<OtherDeviceTask>> chains = mOtherDeviceTasksMap.get(packageName);
            if (chains == null) {
                return;
            }

            for (int i = chains.size() - 1; i >= 0; i--) {
                List<OtherDeviceTask> chain = chains.get(i);
                if (!canAddOtherDeviceTaskChain(chain)) {
                    if (DEBUG_RESTORER) Slog.d(TAG, "Can't add task chain at index=" + i
                            + " for package=" + packageName);
                    continue;
                }

                // Generate task records for this chain.
                List<TaskRecord> tasks = new ArrayList<>();
                TaskRecord prev = null;
                for (int j = chain.size() - 1; j >= 0; j--) {
                    TaskRecord task = createTaskRecordLocked(chain.get(j));
                    if (task == null) {
                        // There was a problem in creating one of this task records in this chain.
                        // There is no way we can continue...
                        if (DEBUG_RESTORER) Slog.d(TAG, "Can't create task record for file="
                                + chain.get(j).mFile + " for package=" + packageName);
                        break;
                    }

                    // Wire-up affiliation chain.
                    if (prev == null) {
                        task.mPrevAffiliate = null;
                        task.mPrevAffiliateTaskId = INVALID_TASK_ID;
                        task.mAffiliatedTaskId = task.taskId;
                    } else {
                        prev.mNextAffiliate = task;
                        prev.mNextAffiliateTaskId = task.taskId;
                        task.mAffiliatedTaskId = prev.mAffiliatedTaskId;
                        task.mPrevAffiliate = prev;
                        task.mPrevAffiliateTaskId = prev.taskId;
                    }
                    prev = task;
                    tasks.add(0, task);
                }

                // Add tasks to recent's if we were able to create task records for all the tasks
                // in the chain.
                if (tasks.size() == chain.size()) {
                    // Make sure there is space in recent's to add the new task. If there is space
                    // to the to the back.
                    // TODO: Would be more fancy to interleave the new tasks into recent's based on
                    // {@link TaskRecord.mLastTimeMoved} and drop the oldest recent's vs. just
                    // adding to the back of the list.
                    int spaceLeft =
                            ActivityManager.getMaxRecentTasksStatic()
                            - mService.mRecentTasks.size();
                    if (spaceLeft >= tasks.size()) {
                        mService.mRecentTasks.addAll(mService.mRecentTasks.size(), tasks);
                        for (int k = tasks.size() - 1; k >= 0; k--) {
                            // Persist new tasks.
                            wakeup(tasks.get(k), false);
                        }

                        if (DEBUG_RESTORER) Slog.d(TAG, "Added " + tasks.size()
                                    + " tasks to recent's for" + " package=" + packageName);
                    } else {
                        if (DEBUG_RESTORER) Slog.d(TAG, "Didn't add to recents. tasks.size("
                                    + tasks.size() + ") != chain.size(" + chain.size()
                                    + ") for package=" + packageName);
                    }
                } else {
                    if (DEBUG_RESTORER) Slog.v(TAG, "Unable to add restored tasks to recents "
                            + tasks.size() + " tasks for package=" + packageName);
                }

                // Clean-up structures
                for (int j = chain.size() - 1; j >= 0; j--) {
                    chain.get(j).mFile.delete();
                }
                chains.remove(i);
                if (chains.isEmpty()) {
                    // The fate of all backed-up tasks associated with this package has been
                    // determine. Go ahead and remove it from the to-process list.
                    mOtherDeviceTasksMap.remove(packageName);
                    if (DEBUG_RESTORER)
                            Slog.d(TAG, "Removed package=" + packageName + " from restore map");
                }
            }
        }
    
private booleancanAddOtherDeviceTaskChain(java.util.List chain)
Returns true if the input task chain backed-up from another device can be restored on this device. Also, sets the {@link OtherDeviceTask#mUid} on the input tasks if they can be restored.


        final ArraySet<ComponentName> validComponents = new ArraySet<>();
        final IPackageManager pm = AppGlobals.getPackageManager();
        for (int i = 0; i < chain.size(); i++) {

            OtherDeviceTask task = chain.get(i);
            // Quick check, we can't add the task chain if any of its task files don't exist.
            if (!task.mFile.exists()) {
                if (DEBUG_RESTORER) Slog.d(TAG,
                        "Can't add chain due to missing file=" + task.mFile);
                return false;
            }

            // Verify task package is installed.
            if (!isPackageInstalled(task.mComponentName.getPackageName())) {
                return false;
            }
            // Verify that all the launch packages are installed.
            if (task.mLaunchPackages != null) {
                for (int j = task.mLaunchPackages.size() - 1; j >= 0; --j) {
                    if (!isPackageInstalled(task.mLaunchPackages.valueAt(j))) {
                        return false;
                    }
                }
            }

            if (validComponents.contains(task.mComponentName)) {
                // Existance of component has already been verified.
                continue;
            }

            // Check to see if the specific component is installed.
            try {
                if (pm.getActivityInfo(task.mComponentName, 0, UserHandle.USER_OWNER) == null) {
                    // Component isn't installed...
                    return false;
                }
                validComponents.add(task.mComponentName);
            } catch (RemoteException e) {
                // Should not happen???
                return false;
            }
        }

        return true;
    
private TaskRecordcreateTaskRecordLocked(com.android.server.am.TaskPersister$OtherDeviceTask other)
Creates and returns {@link TaskRecord} for the task from another device that can be used on this device. Returns null if the operation failed.

        File file = other.mFile;
        BufferedReader reader = null;
        TaskRecord task = null;
        if (DEBUG_RESTORER) Slog.d(TAG, "createTaskRecordLocked: file=" + file.getName());

        try {
            reader = new BufferedReader(new FileReader(file));
            final XmlPullParser in = Xml.newPullParser();
            in.setInput(reader);

            int event;
            while (((event = in.next()) != XmlPullParser.END_DOCUMENT)
                    && event != XmlPullParser.END_TAG) {
                final String name = in.getName();
                if (event == XmlPullParser.START_TAG) {

                    if (TAG_TASK.equals(name)) {
                        // Create a task record using a task id that is valid for this device.
                        task = TaskRecord.restoreFromXml(
                                in, mStackSupervisor, mStackSupervisor.getNextTaskId());
                        if (DEBUG_RESTORER)
                                Slog.d(TAG, "createTaskRecordLocked: restored task=" + task);

                        if (task != null) {
                            task.isPersistable = true;
                            task.inRecents = true;
                            // Task can/should only be backed-up/restored for device owner.
                            task.userId = UserHandle.USER_OWNER;
                            // Clear out affiliated ids that are no longer valid on this device.
                            task.mAffiliatedTaskId = INVALID_TASK_ID;
                            task.mPrevAffiliateTaskId = INVALID_TASK_ID;
                            task.mNextAffiliateTaskId = INVALID_TASK_ID;
                            // Set up uids valid for this device.
                            Integer uid = mPackageUidMap.get(task.realActivity.getPackageName());
                            if (uid == null) {
                                // How did this happen???
                                Slog.wtf(TAG, "Can't find uid for task=" + task
                                        + " in mPackageUidMap=" + mPackageUidMap);
                                return null;
                            }
                            task.effectiveUid = task.mCallingUid = uid;
                            for (int i = task.mActivities.size() - 1; i >= 0; --i) {
                                final ActivityRecord activity = task.mActivities.get(i);
                                uid = mPackageUidMap.get(activity.launchedFromPackage);
                                if (uid == null) {
                                    // How did this happen??
                                    Slog.wtf(TAG, "Can't find uid for activity=" + activity
                                            + " in mPackageUidMap=" + mPackageUidMap);
                                    return null;
                                }
                                activity.launchedFromUid = uid;
                            }

                        } else {
                            Slog.e(TAG, "Unable to create task for backed-up file=" + file + ": "
                                        + fileToString(file));
                        }
                    } else {
                        Slog.wtf(TAG, "createTaskRecordLocked Unknown xml event=" + event
                                    + " name=" + name);
                    }
                }
                XmlUtils.skipCurrentTag(in);
            }
        } catch (Exception e) {
            Slog.wtf(TAG, "Unable to parse " + file + ". Error ", e);
            Slog.e(TAG, "Failing file: " + fileToString(file));
        } finally {
            IoUtils.closeQuietly(reader);
        }

        return task;
    
private java.lang.StringfileToString(java.io.File file)

        final String newline = System.lineSeparator();
        try {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            StringBuffer sb = new StringBuffer((int) file.length() * 2);
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line + newline);
            }
            reader.close();
            return sb.toString();
        } catch (IOException ioe) {
            Slog.e(TAG, "Couldn't read file " + file.getName());
            return null;
        }
    
voidflush()

        synchronized (this) {
            mNextWriteTime = FLUSH_QUEUE;
            notifyAll();
            do {
                try {
                    wait();
                } catch (InterruptedException e) {
                }
            } while (mNextWriteTime == FLUSH_QUEUE);
        }
    
android.graphics.BitmapgetImageFromWriteQueue(java.lang.String filename)

        synchronized (this) {
            for (int queueNdx = mWriteQueue.size() - 1; queueNdx >= 0; --queueNdx) {
                final WriteQueueItem item = mWriteQueue.get(queueNdx);
                if (item instanceof ImageWriteQueueItem) {
                    ImageWriteQueueItem imageWriteQueueItem = (ImageWriteQueueItem) item;
                    if (imageWriteQueueItem.mFilename.equals(filename)) {
                        return imageWriteQueueItem.mImage;
                    }
                }
            }
            return null;
        }
    
android.graphics.BitmapgetTaskDescriptionIcon(java.lang.String filename)

        // See if it is in the write queue
        final Bitmap icon = getImageFromWriteQueue(filename);
        if (icon != null) {
            return icon;
        }
        return restoreImage(filename);
    
private booleanisPackageInstalled(java.lang.String packageName)
Returns true if the input package name is installed. If the package is installed, an entry for the package is added to {@link #mPackageUidMap}.

        if (mPackageUidMap != null && mPackageUidMap.containsKey(packageName)) {
            return true;
        }
        try {
            int uid = AppGlobals.getPackageManager().getPackageUid(
                    packageName, UserHandle.USER_OWNER);
            if (uid == -1) {
                // package doesn't exist...
                return false;
            }
            if (mPackageUidMap == null) {
                mPackageUidMap = new ArrayMap<>();
            }
            mPackageUidMap.put(packageName, uid);
            return true;
        } catch (RemoteException e) {
            // Should not happen???
            return false;
        }
    
private voidreadOtherDeviceTasksFromDisk()
Read the tasks that were backed-up on a different device and can be restored to this device from disk and populated {@link #mOtherDeviceTasksMap} with the information. Also sets up time to clear out other device tasks that have not been restored on this device within the allotted time.

        synchronized (mOtherDeviceTasksMap) {
            // Clear out current map and expiration time.
            mOtherDeviceTasksMap.clear();
            mExpiredTasksCleanupTime = Long.MAX_VALUE;

            final File[] taskFiles;
            if (!sRestoredTasksDir.exists()
                    || (taskFiles = sRestoredTasksDir.listFiles()) == null) {
                // Nothing to do if there are no tasks to restore.
                return;
            }

            long earliestMtime = System.currentTimeMillis();
            SparseArray<List<OtherDeviceTask>> tasksByAffiliateIds =
                        new SparseArray<>(taskFiles.length);

            // Read new tasks from disk
            for (int i = 0; i < taskFiles.length; ++i) {
                final File taskFile = taskFiles[i];
                if (DEBUG_RESTORER) Slog.d(TAG, "readOtherDeviceTasksFromDisk: taskFile="
                            + taskFile.getName());

                final OtherDeviceTask task = OtherDeviceTask.createFromFile(taskFile);

                if (task == null) {
                    // Go ahead and remove the file on disk if we are unable to create a task from
                    // it.
                    if (DEBUG_RESTORER) Slog.e(TAG, "Unable to create task for file="
                                + taskFile.getName() + "...deleting file.");
                    taskFile.delete();
                    continue;
                }

                List<OtherDeviceTask> tasks = tasksByAffiliateIds.get(task.mAffiliatedTaskId);
                if (tasks == null) {
                    tasks = new ArrayList<>();
                    tasksByAffiliateIds.put(task.mAffiliatedTaskId, tasks);
                }
                tasks.add(task);
                final long taskMtime = taskFile.lastModified();
                if (earliestMtime > taskMtime) {
                    earliestMtime = taskMtime;
                }
            }

            if (tasksByAffiliateIds.size() > 0) {
                // Sort each affiliated tasks chain by taskId which is the order they were created
                // that should always be correct...Then add to task map.
                for (int i = 0; i < tasksByAffiliateIds.size(); i++) {
                    List<OtherDeviceTask> chain = tasksByAffiliateIds.valueAt(i);
                    Collections.sort(chain);
                    // Package name of the root task in the affiliate chain.
                    final String packageName =
                            chain.get(chain.size()-1).mComponentName.getPackageName();
                    List<List<OtherDeviceTask>> chains = mOtherDeviceTasksMap.get(packageName);
                    if (chains == null) {
                        chains = new ArrayList<>();
                        mOtherDeviceTasksMap.put(packageName, chains);
                    }
                    chains.add(chain);
                }

                // Set expiration time.
                mExpiredTasksCleanupTime = earliestMtime + MAX_INSTALL_WAIT_TIME;
                if (DEBUG_RESTORER) Slog.d(TAG, "Set Expiration time to "
                            + DateUtils.formatDateTime(mService.mContext, mExpiredTasksCleanupTime,
                            DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_TIME));
            }
        }
    
private voidremoveExpiredTasksIfNeeded()
Removed any expired tasks from {@link #mOtherDeviceTasksMap} and disk if their expiration time is less than or equal to {@link #mExpiredTasksCleanupTime}.

        synchronized (mOtherDeviceTasksMap) {
            final long now = System.currentTimeMillis();
            final boolean noMoreTasks = mOtherDeviceTasksMap.isEmpty();
            if (noMoreTasks || now < mExpiredTasksCleanupTime) {
                if (noMoreTasks && mPackageUidMap != null) {
                    // All done! package->uid map no longer needed.
                    mPackageUidMap = null;
                }
                return;
            }

            long earliestNonExpiredMtime = now;
            mExpiredTasksCleanupTime = Long.MAX_VALUE;

            // Remove expired backed-up tasks that have not been restored. We only want to
            // remove task if it is safe to remove all tasks in the affiliation chain.
            for (int i = mOtherDeviceTasksMap.size() - 1; i >= 0 ; i--) {

                List<List<OtherDeviceTask>> chains = mOtherDeviceTasksMap.valueAt(i);
                for (int j = chains.size() - 1; j >= 0 ; j--) {

                    List<OtherDeviceTask> chain = chains.get(j);
                    boolean removeChain = true;
                    for (int k = chain.size() - 1; k >= 0 ; k--) {
                        OtherDeviceTask task = chain.get(k);
                        final long taskLastModified = task.mFile.lastModified();
                        if ((taskLastModified + MAX_INSTALL_WAIT_TIME) > now) {
                            // File has not expired yet...but we keep looping to get the earliest
                            // mtime.
                            if (earliestNonExpiredMtime > taskLastModified) {
                                earliestNonExpiredMtime = taskLastModified;
                            }
                            removeChain = false;
                        }
                    }
                    if (removeChain) {
                        for (int k = chain.size() - 1; k >= 0; k--) {
                            final File file = chain.get(k).mFile;
                            if (DEBUG_RESTORER) Slog.d(TAG, "Deleting expired file="
                                    + file.getName() + " mapped to not installed component="
                                    + chain.get(k).mComponentName);
                            file.delete();
                        }
                        chains.remove(j);
                    }
                }
                if (chains.isEmpty()) {
                    final String packageName = mOtherDeviceTasksMap.keyAt(i);
                    mOtherDeviceTasksMap.removeAt(i);
                    if (DEBUG_RESTORER) Slog.d(TAG, "Removed package=" + packageName
                                + " from task map");
                }
            }

            // Reset expiration time if there is any task remaining.
            if (!mOtherDeviceTasksMap.isEmpty()) {
                mExpiredTasksCleanupTime = earliestNonExpiredMtime + MAX_INSTALL_WAIT_TIME;
                if (DEBUG_RESTORER) Slog.d(TAG, "Reset expiration time to "
                            + DateUtils.formatDateTime(mService.mContext, mExpiredTasksCleanupTime,
                            DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_TIME));
            } else {
                // All done! package->uid map no longer needed.
                mPackageUidMap = null;
            }
        }
    
voidremoveFromPackageCache(java.lang.String packageName)
Removes the input package name from the local package->uid map.

        synchronized (mOtherDeviceTasksMap) {
            if (mPackageUidMap != null) {
                mPackageUidMap.remove(packageName);
            }
        }
    
private static voidremoveObsoleteFiles(android.util.ArraySet persistentTaskIds, java.io.File[] files)

        if (DEBUG_PERSISTER) Slog.d(TAG, "removeObsoleteFile: persistentTaskIds="
                    + persistentTaskIds + " files=" + files);
        if (files == null) {
            Slog.e(TAG, "File error accessing recents directory (too many files open?).");
            return;
        }
        for (int fileNdx = 0; fileNdx < files.length; ++fileNdx) {
            File file = files[fileNdx];
            String filename = file.getName();
            final int taskIdEnd = filename.indexOf('_");
            if (taskIdEnd > 0) {
                final int taskId;
                try {
                    taskId = Integer.valueOf(filename.substring(0, taskIdEnd));
                    if (DEBUG_PERSISTER) Slog.d(TAG, "removeObsoleteFile: Found taskId=" + taskId);
                } catch (Exception e) {
                    Slog.wtf(TAG, "removeObsoleteFile: Can't parse file=" + file.getName());
                    file.delete();
                    continue;
                }
                if (!persistentTaskIds.contains(taskId)) {
                    if (true || DEBUG_PERSISTER) Slog.d(TAG, "removeObsoleteFile: deleting file=" +
                            file.getName());
                    file.delete();
                }
            }
        }
    
private voidremoveObsoleteFiles(android.util.ArraySet persistentTaskIds)

        removeObsoleteFiles(persistentTaskIds, sTasksDir.listFiles());
        removeObsoleteFiles(persistentTaskIds, sImagesDir.listFiles());
    
private voidremoveThumbnails(TaskRecord task)

        final String taskString = Integer.toString(task.taskId);
        for (int queueNdx = mWriteQueue.size() - 1; queueNdx >= 0; --queueNdx) {
            final WriteQueueItem item = mWriteQueue.get(queueNdx);
            if (item instanceof ImageWriteQueueItem &&
                    ((ImageWriteQueueItem) item).mFilename.startsWith(taskString)) {
                if (DEBUG_PERSISTER) Slog.d(TAG, "Removing "
                        + ((ImageWriteQueueItem) item).mFilename + " from write queue");
                mWriteQueue.remove(queueNdx);
            }
        }
    
static android.graphics.BitmaprestoreImage(java.lang.String filename)

        if (DEBUG_PERSISTER) Slog.d(TAG, "restoreImage: restoring " + filename);
        return BitmapFactory.decodeFile(sImagesDir + File.separator + filename);
    
voidrestoreTasksFromOtherDeviceLocked()
Tries to restore task that were backed-up on a different device onto this device.

        readOtherDeviceTasksFromDisk();
        addOtherDeviceTasksToRecentsLocked();
    
java.util.ArrayListrestoreTasksLocked()

        final ArrayList<TaskRecord> tasks = new ArrayList<TaskRecord>();
        ArraySet<Integer> recoveredTaskIds = new ArraySet<Integer>();

        File[] recentFiles = sTasksDir.listFiles();
        if (recentFiles == null) {
            Slog.e(TAG, "Unable to list files from " + sTasksDir);
            return tasks;
        }

        for (int taskNdx = 0; taskNdx < recentFiles.length; ++taskNdx) {
            File taskFile = recentFiles[taskNdx];
            if (DEBUG_PERSISTER) Slog.d(TAG, "restoreTasksLocked: taskFile=" + taskFile.getName());
            BufferedReader reader = null;
            boolean deleteFile = false;
            try {
                reader = new BufferedReader(new FileReader(taskFile));
                final XmlPullParser in = Xml.newPullParser();
                in.setInput(reader);

                int event;
                while (((event = in.next()) != XmlPullParser.END_DOCUMENT) &&
                        event != XmlPullParser.END_TAG) {
                    final String name = in.getName();
                    if (event == XmlPullParser.START_TAG) {
                        if (DEBUG_PERSISTER)
                                Slog.d(TAG, "restoreTasksLocked: START_TAG name=" + name);
                        if (TAG_TASK.equals(name)) {
                            final TaskRecord task =
                                    TaskRecord.restoreFromXml(in, mStackSupervisor);
                            if (DEBUG_PERSISTER) Slog.d(TAG, "restoreTasksLocked: restored task=" +
                                    task);
                            if (task != null) {
                                task.isPersistable = true;
                                // XXX Don't add to write queue... there is no reason to write
                                // out the stuff we just read, if we don't write it we will
                                // read the same thing again.
                                //mWriteQueue.add(new TaskWriteQueueItem(task));
                                tasks.add(task);
                                final int taskId = task.taskId;
                                recoveredTaskIds.add(taskId);
                                mStackSupervisor.setNextTaskId(taskId);
                            } else {
                                Slog.e(TAG, "Unable to restore taskFile=" + taskFile + ": " +
                                        fileToString(taskFile));
                            }
                        } else {
                            Slog.wtf(TAG, "restoreTasksLocked Unknown xml event=" + event +
                                    " name=" + name);
                        }
                    }
                    XmlUtils.skipCurrentTag(in);
                }
            } catch (Exception e) {
                Slog.wtf(TAG, "Unable to parse " + taskFile + ". Error ", e);
                Slog.e(TAG, "Failing file: " + fileToString(taskFile));
                deleteFile = true;
            } finally {
                IoUtils.closeQuietly(reader);
                if (!DEBUG_PERSISTER && deleteFile) {
                    if (true || DEBUG_PERSISTER)
                            Slog.d(TAG, "Deleting file=" + taskFile.getName());
                    taskFile.delete();
                }
            }
        }

        if (!DEBUG_PERSISTER) {
            removeObsoleteFiles(recoveredTaskIds);
        }

        // Fixup task affiliation from taskIds
        for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
            final TaskRecord task = tasks.get(taskNdx);
            task.setPrevAffiliate(taskIdToTask(task.mPrevAffiliateTaskId, tasks));
            task.setNextAffiliate(taskIdToTask(task.mNextAffiliateTaskId, tasks));
        }

        TaskRecord[] tasksArray = new TaskRecord[tasks.size()];
        tasks.toArray(tasksArray);
        Arrays.sort(tasksArray, new Comparator<TaskRecord>() {
            @Override
            public int compare(TaskRecord lhs, TaskRecord rhs) {
                final long diff = rhs.mLastTimeMoved - lhs.mLastTimeMoved;
                if (diff < 0) {
                    return -1;
                } else if (diff > 0) {
                    return +1;
                } else {
                    return 0;
                }
            }
        });

        return new ArrayList<TaskRecord>(Arrays.asList(tasksArray));
    
voidsaveImage(android.graphics.Bitmap image, java.lang.String filename)

        synchronized (this) {
            int queueNdx;
            for (queueNdx = mWriteQueue.size() - 1; queueNdx >= 0; --queueNdx) {
                final WriteQueueItem item = mWriteQueue.get(queueNdx);
                if (item instanceof ImageWriteQueueItem) {
                    ImageWriteQueueItem imageWriteQueueItem = (ImageWriteQueueItem) item;
                    if (imageWriteQueueItem.mFilename.equals(filename)) {
                        // replace the Bitmap with the new one.
                        imageWriteQueueItem.mImage = image;
                        break;
                    }
                }
            }
            if (queueNdx < 0) {
                mWriteQueue.add(new ImageWriteQueueItem(filename, image));
            }
            if (mWriteQueue.size() > MAX_WRITE_QUEUE_LENGTH) {
                mNextWriteTime = FLUSH_QUEUE;
            } else if (mNextWriteTime == 0) {
                mNextWriteTime = SystemClock.uptimeMillis() + PRE_TASK_DELAY_MS;
            }
            if (DEBUG_PERSISTER) Slog.d(TAG, "saveImage: filename=" + filename + " now=" +
                    SystemClock.uptimeMillis() + " mNextWriteTime=" +
                    mNextWriteTime + " Callers=" + Debug.getCallers(4));
            notifyAll();
        }

        yieldIfQueueTooDeep();
    
private java.io.StringWritersaveToXml(TaskRecord task)

        if (DEBUG_PERSISTER) Slog.d(TAG, "saveToXml: task=" + task);
        final XmlSerializer xmlSerializer = new FastXmlSerializer();
        StringWriter stringWriter = new StringWriter();
        xmlSerializer.setOutput(stringWriter);

        if (DEBUG_PERSISTER) xmlSerializer.setFeature(
                    "http://xmlpull.org/v1/doc/features.html#indent-output", true);

        // save task
        xmlSerializer.startDocument(null, true);

        xmlSerializer.startTag(null, TAG_TASK);
        task.saveToXml(xmlSerializer);
        xmlSerializer.endTag(null, TAG_TASK);

        xmlSerializer.endDocument();
        xmlSerializer.flush();

        return stringWriter;
    
voidstartPersisting()

        mLazyTaskWriterThread.start();
    
private TaskRecordtaskIdToTask(int taskId, java.util.ArrayList tasks)

        if (taskId < 0) {
            return null;
        }
        for (int taskNdx = tasks.size() - 1; taskNdx >= 0; --taskNdx) {
            final TaskRecord task = tasks.get(taskNdx);
            if (task.taskId == taskId) {
                return task;
            }
        }
        Slog.e(TAG, "Restore affiliation error looking for taskId=" + taskId);
        return null;
    
voidwakeup(TaskRecord task, boolean flush)

        synchronized (this) {
            if (task != null) {
                int queueNdx;
                for (queueNdx = mWriteQueue.size() - 1; queueNdx >= 0; --queueNdx) {
                    final WriteQueueItem item = mWriteQueue.get(queueNdx);
                    if (item instanceof TaskWriteQueueItem &&
                            ((TaskWriteQueueItem) item).mTask == task) {
                        if (!task.inRecents) {
                            // This task is being removed.
                            removeThumbnails(task);
                        }
                        break;
                    }
                }
                if (queueNdx < 0 && task.isPersistable) {
                    mWriteQueue.add(new TaskWriteQueueItem(task));
                }
            } else {
                // Dummy.
                mWriteQueue.add(new WriteQueueItem());
            }
            if (flush || mWriteQueue.size() > MAX_WRITE_QUEUE_LENGTH) {
                mNextWriteTime = FLUSH_QUEUE;
            } else if (mNextWriteTime == 0) {
                mNextWriteTime = SystemClock.uptimeMillis() + PRE_TASK_DELAY_MS;
            }
            if (DEBUG_PERSISTER) Slog.d(TAG, "wakeup: task=" + task + " flush=" + flush
                    + " mNextWriteTime=" + mNextWriteTime + " mWriteQueue.size="
                    + mWriteQueue.size() + " Callers=" + Debug.getCallers(4));
            notifyAll();
        }

        yieldIfQueueTooDeep();
    
private voidyieldIfQueueTooDeep()

        boolean stall = false;
        synchronized (this) {
            if (mNextWriteTime == FLUSH_QUEUE) {
                stall = true;
            }
        }
        if (stall) {
            Thread.yield();
        }