Merge "Block modification to ID and package name" into oc-dev
diff --git a/src/com/android/providers/tv/TransientRowHelper.java b/src/com/android/providers/tv/TransientRowHelper.java
index 42e2499..27d38db 100644
--- a/src/com/android/providers/tv/TransientRowHelper.java
+++ b/src/com/android/providers/tv/TransientRowHelper.java
@@ -22,8 +22,8 @@
 import android.media.tv.TvContract.Channels;
 import android.media.tv.TvContract.PreviewPrograms;
 import android.media.tv.TvContract.WatchNextPrograms;
-import android.os.SystemClock;
 import android.preference.PreferenceManager;
+import android.provider.Settings;
 
 import com.android.internal.annotations.VisibleForTesting;
 import com.android.providers.tv.TvProvider.DatabaseHelper;
@@ -33,8 +33,8 @@
  * once after boot.
  */
 public class TransientRowHelper {
-    private static final String PREF_KEY_LAST_TRANSIENT_ROWS_DELETED_TIME =
-            "pref_key_last_transient_rows_deleted_time";
+    private static final String PREF_KEY_LAST_DELETION_BOOT_COUNT =
+            "pref_key_last_deletion_boot_count";
     private static TransientRowHelper sInstance;
 
     private Context mContext;
@@ -70,7 +70,7 @@
             return;
         }
         mTransientRowsDeleted = true;
-        if (getLastTransientRowsDeletedTime() > getBootCompletedTimeMillis()) {
+        if (getLastDeletionBootCount() >= getBootCount()) {
             // This can be the second execution of TvProvider after boot since system kills
             // TvProvider in low memory conditions. If this is the case, we shouldn't delete
             // transient rows.
@@ -82,25 +82,26 @@
         db.delete(TvProvider.CHANNELS_TABLE, Channels.COLUMN_TRANSIENT + "=1", null);
         db.delete(TvProvider.WATCH_NEXT_PROGRAMS_TABLE, WatchNextPrograms.COLUMN_TRANSIENT + "=1",
                 null);
-        setLastTransientRowsDeletedTime();
+        setLastDeletionBootCount();
     }
 
     @VisibleForTesting
-    protected long getLastTransientRowsDeletedTime() {
+    protected int getBootCount() {
+        return Settings.Global.getInt(mContext.getContentResolver(), Settings.Global.BOOT_COUNT,
+                -1);
+    }
+
+    @VisibleForTesting
+    protected int getLastDeletionBootCount() {
         SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
-        return prefs.getLong(PREF_KEY_LAST_TRANSIENT_ROWS_DELETED_TIME, 0);
+        return prefs.getInt(PREF_KEY_LAST_DELETION_BOOT_COUNT, -1);
     }
 
     @VisibleForTesting
-    protected void setLastTransientRowsDeletedTime() {
+    protected void setLastDeletionBootCount() {
         SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(mContext)
                 .edit();
-        editor.putLong(PREF_KEY_LAST_TRANSIENT_ROWS_DELETED_TIME, System.currentTimeMillis());
+        editor.putInt(PREF_KEY_LAST_DELETION_BOOT_COUNT, getBootCount());
         editor.apply();
     }
-
-    @VisibleForTesting
-    protected long getBootCompletedTimeMillis() {
-        return System.currentTimeMillis() - SystemClock.elapsedRealtime();
-    }
 }
diff --git a/tests/src/com/android/providers/tv/TransientRowHelperTests.java b/tests/src/com/android/providers/tv/TransientRowHelperTests.java
index 7576497..ae3a36d 100644
--- a/tests/src/com/android/providers/tv/TransientRowHelperTests.java
+++ b/tests/src/com/android/providers/tv/TransientRowHelperTests.java
@@ -29,7 +29,6 @@
 import android.media.tv.TvContract.PreviewPrograms;
 import android.net.Uri;
 import android.os.Bundle;
-import android.os.SystemClock;
 import android.provider.Settings;
 import android.test.AndroidTestCase;
 import android.test.mock.MockContentProvider;
@@ -133,6 +132,7 @@
         ContentValues values = new ContentValues();
         values.put(PreviewPrograms.COLUMN_CHANNEL_ID, channelId);
         for (PreviewProgram program : programs) {
+            values.put(PreviewPrograms.COLUMN_TYPE, PreviewPrograms.TYPE_MOVIE);
             values.put(PreviewPrograms.COLUMN_TRANSIENT, program.isTransient ? 1 : 0);
             Uri uri = mResolver.insert(PreviewPrograms.CONTENT_URI, values);
             assertNotNull(uri);
@@ -205,34 +205,31 @@
     }
 
     private class RebootSimulatingTransientRowHelper extends TransientRowHelper {
-        private long mLastTransientRowsRemoveTime;
-        private Long mBootTime;
+        private int mLastDeletionBootCount;
+        private int mBootCount = 1;
 
         private RebootSimulatingTransientRowHelper(Context context) {
             super(context);
         }
 
         @Override
-        protected long getLastTransientRowsDeletedTime() {
-            return mLastTransientRowsRemoveTime;
+        protected int getBootCount() {
+            return mBootCount;
         }
 
         @Override
-        protected void setLastTransientRowsDeletedTime() {
-            mLastTransientRowsRemoveTime = System.currentTimeMillis();
+        protected int getLastDeletionBootCount() {
+            return mLastDeletionBootCount;
         }
 
         @Override
-        protected long getBootCompletedTimeMillis() {
-            if (mBootTime != null) {
-                return mBootTime;
-            }
-            return System.currentTimeMillis() - SystemClock.elapsedRealtime();
+        protected void setLastDeletionBootCount() {
+            mLastDeletionBootCount = mBootCount;
         }
 
         private void simulateReboot() {
             mTransientRowsDeleted = false;
-            mBootTime = System.currentTimeMillis();
+            mBootCount++;
         }
     }
 }