Revert "Fixing issue with old tasks being visible in Overview"

Bug: 28908500

This reverts commit b258f6a4bd685e5efcb36c02d5817f659e10479b.

Change-Id: I9b3c04358d6ca693ed3fdcd9220af13f81eb704e
diff --git a/services/core/java/com/android/server/am/TaskPersister.java b/services/core/java/com/android/server/am/TaskPersister.java
index 6cdabaa..43eb251 100644
--- a/services/core/java/com/android/server/am/TaskPersister.java
+++ b/services/core/java/com/android/server/am/TaskPersister.java
@@ -17,7 +17,6 @@
 package com.android.server.am;
 
 import android.annotation.NonNull;
-import android.content.ContentResolver;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.os.Debug;
@@ -25,7 +24,6 @@
 import android.os.FileUtils;
 import android.os.Process;
 import android.os.SystemClock;
-import android.provider.Settings;
 import android.util.ArraySet;
 import android.util.AtomicFile;
 import android.util.Slog;
@@ -82,7 +80,7 @@
     private static final String PERSISTED_TASK_IDS_FILENAME = "persisted_taskIds.txt";
     static final String IMAGE_EXTENSION = ".png";
 
-    @VisibleForTesting static final String TAG_TASK = "task";
+    private static final String TAG_TASK = "task";
 
     private final ActivityManagerService mService;
     private final ActivityStackSupervisor mStackSupervisor;
@@ -409,43 +407,18 @@
         return null;
     }
 
-    @VisibleForTesting
     List<TaskRecord> restoreTasksForUserLocked(final int userId) {
         final ArrayList<TaskRecord> tasks = new ArrayList<TaskRecord>();
         ArraySet<Integer> recoveredTaskIds = new ArraySet<Integer>();
 
         File userTasksDir = getUserTasksDir(userId);
+
         File[] recentFiles = userTasksDir.listFiles();
         if (recentFiles == null) {
             Slog.e(TAG, "restoreTasksForUserLocked: Unable to list files from " + userTasksDir);
             return tasks;
         }
 
-        // Get the last persist uptime so we know how to adjust the first/last active times for each
-        // task
-        ContentResolver cr = mService.mContext.getContentResolver();
-        long lastPersistUptime = Settings.Secure.getLong(cr,
-                Settings.Secure.TASK_PERSISTER_LAST_WRITE_UPTIME, 0);
-        if (DEBUG) {
-            Slog.d(TaskPersister.TAG, "restoreTasksForUserLocked: lastPersistUptime=" +
-                    lastPersistUptime);
-        }
-
-        // Adjust the overview last visible task active time as we adjust the task active times when
-        // loading. See TaskRecord.restoreFromXml().  If we have not migrated yet, SystemUI will
-        // migrate the old value into the system setting.
-        if (lastPersistUptime > 0) {
-            long overviewLastActiveTime = Settings.Secure.getLongForUser(cr,
-                    Settings.Secure.OVERVIEW_LAST_VISIBLE_TASK_ACTIVE_UPTIME, 0, userId);
-            if (DEBUG) {
-                Slog.d(TaskPersister.TAG, "restoreTasksForUserLocked: overviewLastActiveTime=" +
-                        overviewLastActiveTime + " lastPersistUptime=" + lastPersistUptime);
-            }
-            Settings.Secure.putLongForUser(cr,
-                    Settings.Secure.OVERVIEW_LAST_VISIBLE_TASK_ACTIVE_UPTIME,
-                    -lastPersistUptime + overviewLastActiveTime, userId);
-        }
-
         for (int taskNdx = 0; taskNdx < recentFiles.length; ++taskNdx) {
             File taskFile = recentFiles[taskNdx];
             if (DEBUG) {
@@ -466,11 +439,15 @@
                     if (event == XmlPullParser.START_TAG) {
                         if (DEBUG) Slog.d(TAG, "restoreTasksForUserLocked: START_TAG name=" + name);
                         if (TAG_TASK.equals(name)) {
-                            final TaskRecord task = TaskRecord.restoreFromXml(in, mService,
-                                    mStackSupervisor, lastPersistUptime);
+                            final TaskRecord task = TaskRecord.restoreFromXml(in, mStackSupervisor);
                             if (DEBUG) Slog.d(TAG, "restoreTasksForUserLocked: restored task="
                                     + task);
                             if (task != null) {
+                                // 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));
+
                                 final int taskId = task.taskId;
                                 if (mStackSupervisor.anyTaskForIdLocked(taskId,
                                         /* restoreFromRecents= */ false, 0) != null) {
@@ -486,12 +463,6 @@
                                     task.isPersistable = true;
                                     tasks.add(task);
                                     recoveredTaskIds.add(taskId);
-
-                                    // We've shifted the first and last active times, so we need to
-                                    // persist the new task data to disk otherwise they will not
-                                    // have the updated values.  This is only done once whenever
-                                    // the recents are first loaded for the user.
-                                    wakeup(task, false);
                                 }
                             } else {
                                 Slog.e(TAG, "restoreTasksForUserLocked: Unable to restore taskFile="
@@ -780,15 +751,6 @@
                         }
                     }
                 }
-
-                // Always update the task persister uptime when updating any tasks
-                if (DEBUG) {
-                    Slog.d(TAG, "LazyTaskWriter: Updating last write uptime=" +
-                            SystemClock.elapsedRealtime());
-                }
-                Settings.Secure.putLong(mService.mContext.getContentResolver(),
-                        Settings.Secure.TASK_PERSISTER_LAST_WRITE_UPTIME,
-                        SystemClock.elapsedRealtime());
             }
         }
     }
diff --git a/services/core/java/com/android/server/am/TaskRecord.java b/services/core/java/com/android/server/am/TaskRecord.java
index b3d8027..3f6db99 100644
--- a/services/core/java/com/android/server/am/TaskRecord.java
+++ b/services/core/java/com/android/server/am/TaskRecord.java
@@ -39,14 +39,12 @@
 import android.os.Debug;
 import android.os.ParcelFileDescriptor;
 import android.os.RemoteException;
-import android.os.SystemClock;
 import android.os.UserHandle;
 import android.provider.Settings;
 import android.service.voice.IVoiceInteractionSession;
 import android.util.DisplayMetrics;
 import android.util.Slog;
 
-import com.android.internal.annotations.VisibleForTesting;
 import com.android.internal.app.IVoiceInteractor;
 import com.android.internal.util.XmlUtils;
 
@@ -152,10 +150,8 @@
     ComponentName realActivity; // The actual activity component that started the task.
     boolean realActivitySuspended; // True if the actual activity component that started the
                                    // task is suspended.
-    long firstActiveTime;   // First time this task was active, relative to boot time. This can be
-                            // negative if this task was last used prior to boot.
-    long lastActiveTime;    // Last time this task was active, relative to boot time. This can be
-                            // negative if this task was last used prior to boot.
+    long firstActiveTime;   // First time this task was active.
+    long lastActiveTime;    // Last time this task was active, including sleep.
     boolean inRecents;      // Actually in the recents list?
     boolean isAvailable;    // Is the activity available to be launched?
     boolean rootWasReset;   // True if the intent at the root of the task had
@@ -381,14 +377,14 @@
     }
 
     void touchActiveTime() {
-        lastActiveTime = SystemClock.elapsedRealtime();
+        lastActiveTime = System.currentTimeMillis();
         if (firstActiveTime == 0) {
             firstActiveTime = lastActiveTime;
         }
     }
 
     long getInactiveDuration() {
-        return SystemClock.elapsedRealtime() - lastActiveTime;
+        return System.currentTimeMillis() - lastActiveTime;
     }
 
     /** Sets the original intent, and the calling uid and package. */
@@ -459,9 +455,8 @@
             rootWasReset = true;
         }
         userId = UserHandle.getUserId(info.applicationInfo.uid);
-        mUserSetupComplete = mService != null &&
-                Settings.Secure.getIntForUser(mService.mContext.getContentResolver(),
-                        USER_SETUP_COMPLETE, 0, userId) != 0;
+        mUserSetupComplete = Settings.Secure.getIntForUser(mService.mContext.getContentResolver(),
+                USER_SETUP_COMPLETE, 0, userId) != 0;
         if ((info.flags & ActivityInfo.FLAG_AUTO_REMOVE_FROM_RECENTS) != 0) {
             // If the activity itself has requested auto-remove, then just always do it.
             autoRemoveRecents = true;
@@ -1173,9 +1168,7 @@
         if (lastTaskDescription != null) {
             lastTaskDescription.saveToXml(out);
         }
-        if (mLastThumbnailInfo != null) {
-            mLastThumbnailInfo.saveToXml(out);
-        }
+        mLastThumbnailInfo.saveToXml(out);
         out.attribute(null, ATTR_TASK_AFFILIATION_COLOR, String.valueOf(mAffiliatedTaskColor));
         out.attribute(null, ATTR_TASK_AFFILIATION, String.valueOf(mAffiliatedTaskId));
         out.attribute(null, ATTR_PREV_AFFILIATION, String.valueOf(mPrevAffiliateTaskId));
@@ -1197,11 +1190,9 @@
             out.endTag(null, TAG_AFFINITYINTENT);
         }
 
-        if (intent != null) {
-            out.startTag(null, TAG_INTENT);
-            intent.saveToXml(out);
-            out.endTag(null, TAG_INTENT);
-        }
+        out.startTag(null, TAG_INTENT);
+        intent.saveToXml(out);
+        out.endTag(null, TAG_INTENT);
 
         final ArrayList<ActivityRecord> activities = mActivities;
         final int numActivities = activities.size();
@@ -1220,9 +1211,8 @@
         }
     }
 
-    static TaskRecord restoreFromXml(XmlPullParser in, ActivityManagerService service,
-            ActivityStackSupervisor stackSupervisor, long lastPersistUptime)
-                    throws IOException, XmlPullParserException {
+    static TaskRecord restoreFromXml(XmlPullParser in, ActivityStackSupervisor stackSupervisor)
+            throws IOException, XmlPullParserException {
         Intent intent = null;
         Intent affinityIntent = null;
         ArrayList<ActivityRecord> activities = new ArrayList<>();
@@ -1335,31 +1325,6 @@
             }
         }
 
-        if (lastPersistUptime > 0) {
-            if (TaskPersister.DEBUG) {
-                Slog.d(TaskPersister.TAG, "TaskRecord: Adjust firstActiveTime=" + firstActiveTime +
-                        " lastPersistUptime=" + lastPersistUptime);
-                Slog.d(TaskPersister.TAG, "TaskRecord: Migrate lastActiveTime=" + lastActiveTime +
-                        " lastActiveTime=" + lastPersistUptime);
-            }
-            // The first and last task active times are relative to the last boot time, so offset
-            // them to be prior to the current boot time
-            firstActiveTime = -lastPersistUptime + firstActiveTime;
-            lastActiveTime = -lastPersistUptime + lastActiveTime;
-        } else {
-            // The first/last active times are still absolute clock times, so offset them to be
-            // relative to the current boot time
-            long currentTime = System.currentTimeMillis();
-            if (TaskPersister.DEBUG) {
-                Slog.d(TaskPersister.TAG, "TaskRecord: Migrate firstActiveTime=" + firstActiveTime +
-                                " currentTime=" + currentTime);
-                Slog.d(TaskPersister.TAG, "TaskRecord: Migrate lastActiveTime=" + lastActiveTime +
-                                " currentTime=" + currentTime);
-            }
-            firstActiveTime = -Math.max(0, currentTime - firstActiveTime);
-            lastActiveTime = -Math.max(0, currentTime - lastActiveTime);
-        }
-
         int event;
         while (((event = in.next()) != XmlPullParser.END_DOCUMENT) &&
                 (event != XmlPullParser.END_TAG || in.getDepth() >= outerDepth)) {
@@ -1409,7 +1374,7 @@
                     + ": effectiveUid=" + effectiveUid);
         }
 
-        final TaskRecord task = new TaskRecord(service, taskId, intent,
+        final TaskRecord task = new TaskRecord(stackSupervisor.mService, taskId, intent,
                 affinityIntent, affinity, rootAffinity, realActivity, origActivity, rootHasReset,
                 autoRemoveRecents, askedCompatMode, taskType, userId, effectiveUid, lastDescription,
                 activities, firstActiveTime, lastActiveTime, lastTimeOnTop, neverRelinquishIdentity,
@@ -1812,7 +1777,7 @@
         pw.print(prefix + "hasBeenVisible=" + hasBeenVisible);
                 pw.print(" mResizeMode=" + ActivityInfo.resizeModeToString(mResizeMode));
                 pw.print(" isResizeable=" + isResizeable());
-                pw.print(" firstActiveTime=" + firstActiveTime);
+                pw.print(" firstActiveTime=" + lastActiveTime);
                 pw.print(" lastActiveTime=" + lastActiveTime);
                 pw.println(" (inactive for " + (getInactiveDuration() / 1000) + "s)");
     }
diff --git a/services/tests/servicestests/src/com/android/server/am/TaskPersisterTest.java b/services/tests/servicestests/src/com/android/server/am/TaskPersisterTest.java
index 7571f79..984a484 100644
--- a/services/tests/servicestests/src/com/android/server/am/TaskPersisterTest.java
+++ b/services/tests/servicestests/src/com/android/server/am/TaskPersisterTest.java
@@ -16,36 +16,19 @@
 
 package com.android.server.am;
 
-import android.app.ActivityManager;
-import android.content.ContentResolver;
-import android.content.pm.ActivityInfo;
 import android.content.pm.UserInfo;
 import android.os.Environment;
-import android.os.SystemClock;
 import android.os.UserHandle;
 import android.os.UserManager;
-import android.provider.Settings;
 import android.test.AndroidTestCase;
 import android.util.Log;
 import android.util.SparseBooleanArray;
-import android.util.Xml;
 
-import com.android.internal.util.FastXmlSerializer;
-import com.android.internal.util.XmlUtils;
 import com.android.server.am.TaskPersister;
 
 import java.io.File;
-import java.io.IOException;
-import java.io.StringReader;
-import java.io.StringWriter;
 import java.util.Random;
 
-import libcore.io.IoUtils;
-
-import org.xmlpull.v1.XmlPullParser;
-import org.xmlpull.v1.XmlPullParserException;
-import org.xmlpull.v1.XmlSerializer;
-
 public class TaskPersisterTest extends AndroidTestCase {
     private static final String TEST_USER_NAME = "AM-Test-User";
 
@@ -86,140 +69,6 @@
                 taskIdsOnFile.equals(newTaskIdsOnFile));
     }
 
-    public void testActiveTimeMigration() {
-        // Simulate a migration scenario by setting the last write uptime to zero
-        ContentResolver cr = getContext().getContentResolver();
-        Settings.Secure.putLong(cr,
-                Settings.Secure.TASK_PERSISTER_LAST_WRITE_UPTIME, 0);
-
-        // Create a dummy task record with an absolute time 1s before now
-        long pastOffset = 1000;
-        long activeTime = System.currentTimeMillis() - pastOffset;
-        TaskRecord tr0 = createDummyTaskRecordWithActiveTime(activeTime, activeTime);
-
-        // Save and load the tasks with no last persist uptime (0)
-        String tr0XmlStr = serializeTaskRecordToXmlString(tr0);
-        TaskRecord xtr0 = unserializeTaskRecordFromXmlString(tr0XmlStr, 0);
-
-        // Ensure that the absolute time has been migrated to be relative to the current elapsed
-        // time
-        assertTrue("Expected firstActiveTime to be migrated from: " + tr0.firstActiveTime +
-                " instead found: " + xtr0.firstActiveTime,
-                        xtr0.firstActiveTime <= -pastOffset);
-        assertTrue("Expected lastActiveTime to be migrated from: " + tr0.lastActiveTime +
-                " instead found: " + xtr0.lastActiveTime,
-                        xtr0.lastActiveTime <= -pastOffset);
-
-        // Ensure that the last active uptime is not set so that SystemUI can migrate it itself
-        // assuming that the last persist time is zero
-        Settings.Secure.putLongForUser(cr,
-                Settings.Secure.OVERVIEW_LAST_VISIBLE_TASK_ACTIVE_UPTIME, 0, testUserId);
-        mTaskPersister.restoreTasksForUserLocked(testUserId);
-        long lastVisTaskActiveTime = Settings.Secure.getLongForUser(cr,
-                Settings.Secure.OVERVIEW_LAST_VISIBLE_TASK_ACTIVE_UPTIME, -1, testUserId);
-        assertTrue("Expected last visible task active time is zero", lastVisTaskActiveTime == 0);
-    }
-
-    public void testActiveTimeOffsets() {
-        // Simulate a normal boot scenario by setting the last write uptime
-        long lastWritePastOffset = 1000;
-        long lastVisActivePastOffset = 500;
-        ContentResolver cr = getContext().getContentResolver();
-        Settings.Secure.putLong(cr,
-                Settings.Secure.TASK_PERSISTER_LAST_WRITE_UPTIME, lastWritePastOffset);
-
-        // Create a dummy task record with an absolute time 1s before now
-        long activeTime = 250;
-        TaskRecord tr0 = createDummyTaskRecordWithActiveTime(activeTime, activeTime);
-
-        // Save and load the tasks with the last persist time
-        String tr0XmlStr = serializeTaskRecordToXmlString(tr0);
-        TaskRecord xtr0 = unserializeTaskRecordFromXmlString(tr0XmlStr, lastWritePastOffset);
-
-        // Ensure that the prior elapsed time has been offset to be relative to the current boot
-        // time
-        assertTrue("Expected firstActiveTime to be offset from: " + tr0.firstActiveTime +
-                " instead found: " + xtr0.firstActiveTime,
-                        xtr0.firstActiveTime <= (-lastWritePastOffset + activeTime));
-        assertTrue("Expected lastActiveTime to be offset from: " + tr0.lastActiveTime +
-                " instead found: " + xtr0.lastActiveTime,
-                        xtr0.lastActiveTime <= (-lastWritePastOffset + activeTime));
-
-        // Ensure that we update the last active uptime as well by simulating a restoreTasks call
-        Settings.Secure.putLongForUser(cr,
-                Settings.Secure.OVERVIEW_LAST_VISIBLE_TASK_ACTIVE_UPTIME, lastVisActivePastOffset,
-                        testUserId);
-        mTaskPersister.restoreTasksForUserLocked(testUserId);
-        long lastVisTaskActiveTime = Settings.Secure.getLongForUser(cr,
-                Settings.Secure.OVERVIEW_LAST_VISIBLE_TASK_ACTIVE_UPTIME, Long.MAX_VALUE,
-                        testUserId);
-        assertTrue("Expected last visible task active time to be offset", lastVisTaskActiveTime <=
-                (-lastWritePastOffset + lastVisActivePastOffset));
-    }
-
-    private TaskRecord createDummyTaskRecordWithActiveTime(long firstActiveTime,
-            long lastActiveTime) {
-        ActivityInfo info = createDummyActivityInfo();
-        ActivityManager.TaskDescription td = new ActivityManager.TaskDescription();
-        TaskRecord t = new TaskRecord(null, 0, info, null, td, null);
-        t.firstActiveTime = firstActiveTime;
-        t.lastActiveTime = lastActiveTime;
-        return t;
-    }
-
-    private ActivityInfo createDummyActivityInfo() {
-        ActivityInfo info = new ActivityInfo();
-        info.applicationInfo = getContext().getApplicationInfo();
-        return info;
-    }
-
-    private String serializeTaskRecordToXmlString(TaskRecord tr) {
-        StringWriter stringWriter = new StringWriter();
-
-        try {
-            final XmlSerializer xmlSerializer = new FastXmlSerializer();
-            xmlSerializer.setOutput(stringWriter);
-
-            xmlSerializer.startDocument(null, true);
-            xmlSerializer.startTag(null, TaskPersister.TAG_TASK);
-            tr.saveToXml(xmlSerializer);
-            xmlSerializer.endTag(null, TaskPersister.TAG_TASK);
-            xmlSerializer.endDocument();
-            xmlSerializer.flush();
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-
-        return stringWriter.toString();
-    }
-
-    private TaskRecord unserializeTaskRecordFromXmlString(String xmlStr, long lastPersistUptime) {
-        StringReader reader = null;
-        TaskRecord task = null;
-        try {
-            reader = new StringReader(xmlStr);
-            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 (TaskPersister.TAG_TASK.equals(name)) {
-                        task = TaskRecord.restoreFromXml(in, null, null, lastPersistUptime);
-                    }
-                }
-                XmlUtils.skipCurrentTag(in);
-            }
-        } catch (Exception e) {
-            return null;
-        } finally {
-            IoUtils.closeQuietly(reader);
-        }
-        return task;
-    }
-
     private int createUser(String name, int flags) {
         UserInfo user = mUserManager.createUser(name, flags);
         if (user == null) {