Bug 717841 part 1 - Replace Date().getTime() calls with SystemClock.uptimeMillis or System.currentTimeMillis(). r=kats f=dougt a=dougt
authorChris Peterson <cpeterson@mozilla.com>
Fri, 13 Jan 2012 11:08:39 -0800
changeset 85979 46328da0936d021a130e6fea17f051db8eb3eb39
parent 85978 339d1d51af7a14302fdd49e1e6f94ca6387de91e
child 85980 e312a306ed0150aa9541e875b6628233eddd0695
push id805
push userakeybl@mozilla.com
push dateWed, 01 Feb 2012 18:17:35 +0000
treeherdermozilla-aurora@6fb3bf232436 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskats, dougt
bugs717841
milestone12.0a1
Bug 717841 part 1 - Replace Date().getTime() calls with SystemClock.uptimeMillis or System.currentTimeMillis(). r=kats f=dougt a=dougt
mobile/android/base/AwesomeBarTabs.java
mobile/android/base/DoorHanger.java
mobile/android/base/GeckoApp.java
mobile/android/base/GeckoBatteryManager.java
mobile/android/base/GeckoThread.java
mobile/android/base/db/AndroidBrowserDB.java
mobile/android/base/db/LocalBrowserDB.java
--- a/mobile/android/base/AwesomeBarTabs.java
+++ b/mobile/android/base/AwesomeBarTabs.java
@@ -42,16 +42,17 @@ import android.content.Context;
 import android.content.res.Resources;
 import android.database.Cursor;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.graphics.Color;
 import android.graphics.LightingColorFilter;
 import android.graphics.drawable.Drawable;
 import android.os.AsyncTask;
+import android.os.SystemClock;
 import android.provider.Browser;
 import android.util.AttributeSet;
 import android.util.Base64;
 import android.util.Log;
 import android.util.Pair;
 import android.view.LayoutInflater;
 import android.view.MotionEvent;
 import android.view.View;
@@ -617,22 +618,22 @@ public class AwesomeBarTabs extends TabH
             new int[] { R.id.title, R.id.url, R.id.favicon }
         );
 
         mAllPagesCursorAdapter.setViewBinder(new AwesomeCursorViewBinder());
 
         mAllPagesCursorAdapter.setFilterQueryProvider(new FilterQueryProvider() {
             public Cursor runQuery(CharSequence constraint) {
                 ContentResolver resolver = mContext.getContentResolver();
-                long start = new Date().getTime();
+                long start = SystemClock.uptimeMillis();
 
                 Cursor c = BrowserDB.filter(resolver, constraint, MAX_RESULTS);
                 c.getCount(); // ensure the query runs at least once
 
-                long end = new Date().getTime();
+                long end = SystemClock.uptimeMillis();
                 Log.i(LOGTAG, "Got cursor in " + (end - start) + "ms");
 
                 return c;
             }
         });
 
         final ListView allPagesList = (ListView) findViewById(R.id.all_pages_list);
 
--- a/mobile/android/base/DoorHanger.java
+++ b/mobile/android/base/DoorHanger.java
@@ -33,18 +33,16 @@
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 package org.mozilla.gecko;
 
-import java.util.Date;
-
 import android.content.Context;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.widget.Button;
 import android.widget.LinearLayout;
 import android.widget.LinearLayout.LayoutParams;
 import android.widget.TextView;
 
@@ -151,15 +149,15 @@ public class DoorHanger extends LinearLa
     public boolean shouldRemove() {
         // If persistence is set to -1, the doorhanger will never be
         // automatically removed.
         if (mPersistence != 0) {
             mPersistence--;
             return false;
         }
 
-        if (new Date().getTime() <= mTimeout) {
+        if (System.currentTimeMillis() <= mTimeout) {
             return false;
         }
 
         return true;
     }
 }
--- a/mobile/android/base/GeckoApp.java
+++ b/mobile/android/base/GeckoApp.java
@@ -226,17 +226,17 @@ abstract public class GeckoApp
     private static final String TYPE_NATIVE = "native";
     public ArrayList<PackageInfo> mPackageInfoCache = new ArrayList<PackageInfo>();
 
     String[] getPluginDirectories() {
         // we don't support Honeycomb and later
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
             return new String[0];
 
-        Log.w(LOGTAG, "zerdatime " + new Date().getTime() + " - start of getPluginDirectories");
+        Log.w(LOGTAG, "zerdatime " + SystemClock.uptimeMillis() + " - start of getPluginDirectories");
 
         ArrayList<String> directories = new ArrayList<String>();
         PackageManager pm = this.mAppContext.getPackageManager();
         List<ResolveInfo> plugins = pm.queryIntentServices(new Intent(PLUGIN_ACTION),
                 PackageManager.GET_SERVICES | PackageManager.GET_META_DATA);
 
         synchronized(mPackageInfoCache) {
 
@@ -352,17 +352,17 @@ abstract public class GeckoApp
 
                 // if all checks have passed then make the plugin available
                 mPackageInfoCache.add(pkgInfo);
                 directories.add(directory);
             }
         }
 
         String [] result = directories.toArray(new String[directories.size()]);
-        Log.w(LOGTAG, "zerdatime " + new Date().getTime() + " - end of getPluginDirectories");
+        Log.w(LOGTAG, "zerdatime " + SystemClock.uptimeMillis() + " - end of getPluginDirectories");
         return result;
     }
 
     String getPluginPackage(String pluginLib) {
 
         if (pluginLib == null || pluginLib.length() == 0) {
             return null;
         }
@@ -1391,17 +1391,17 @@ abstract public class GeckoApp
     {
         // StrictMode is set by defaults resource flag |enableStrictMode|.
         if (getResources().getBoolean(R.bool.enableStrictMode)) {
             enableStrictMode();
         }
 
         System.loadLibrary("mozglue");
         mMainHandler = new Handler();
-        Log.w(LOGTAG, "zerdatime " + new Date().getTime() + " - onCreate");
+        Log.w(LOGTAG, "zerdatime " + SystemClock.uptimeMillis() + " - onCreate");
         if (savedInstanceState != null) {
             mLastUri = savedInstanceState.getString(SAVED_STATE_URI);
             mLastTitle = savedInstanceState.getString(SAVED_STATE_TITLE);
             mLastViewport = savedInstanceState.getString(SAVED_STATE_VIEWPORT);
             mLastScreen = savedInstanceState.getByteArray(SAVED_STATE_SCREEN);
             mRestoreSession = savedInstanceState.getBoolean(SAVED_STATE_SESSION);
         }
 
@@ -1507,17 +1507,17 @@ abstract public class GeckoApp
             mPlaceholderLayerClient = PlaceholderLayerClient.createInstance(this);
             mLayerController.setLayerClient(mPlaceholderLayerClient);
 
             mGeckoLayout.addView(mLayerController.getView(), 0);
         }
 
         mPluginContainer = (AbsoluteLayout) findViewById(R.id.plugin_container);
 
-        Log.w(LOGTAG, "zerdatime " + new Date().getTime() + " - UI almost up");
+        Log.w(LOGTAG, "zerdatime " + SystemClock.uptimeMillis() + " - UI almost up");
 
         if (!sTryCatchAttached) {
             sTryCatchAttached = true;
             mMainHandler.post(new Runnable() {
                 public void run() {
                     try {
                         Looper.loop();
                     } catch (Exception e) {
@@ -1575,36 +1575,35 @@ abstract public class GeckoApp
         }
 
         GeckoNetworkManager.getInstance().init();
 
         final GeckoApp self = this;
  
         GeckoAppShell.getHandler().postDelayed(new Runnable() {
             public void run() {
-                
-                Log.w(LOGTAG, "zerdatime " + new Date().getTime() + " - pre checkLaunchState");
+                Log.w(LOGTAG, "zerdatime " + SystemClock.uptimeMillis() + " - pre checkLaunchState");
 
                 /*
                   XXXX see bug 635342
                    We want to disable this code if possible.  It is about 145ms in runtime
                 SharedPreferences settings = getPreferences(Activity.MODE_PRIVATE);
                 String localeCode = settings.getString(getPackageName() + ".locale", "");
                 if (localeCode != null && localeCode.length() > 0)
                     GeckoAppShell.setSelectedLocale(localeCode);
                 */
 
                 if (!checkLaunchState(LaunchState.Launched)) {
                     return;
                 }
 
-                // it would be good only to do this if MOZ_UPDATER was defined 
-                long startTime = new Date().getTime();
+                // it would be good only to do this if MOZ_UPDATER was defined
+                long startTime = SystemClock.uptimeMillis();
                 checkAndLaunchUpdate();
-                Log.w(LOGTAG, "checking for an update took " + (new Date().getTime() - startTime) + "ms");
+                Log.w(LOGTAG, "checking for an update took " + (SystemClock.uptimeMillis() - startTime) + "ms");
                 checkMigrateProfile();
             }
         }, 50);
 
         mOrientation = getResources().getConfiguration().orientation;
     }
 
     /**
@@ -1634,17 +1633,17 @@ abstract public class GeckoApp
     }
 
     public void disableCameraView() {
         mMainLayout.removeView(cameraView);
     }
 
     @Override
     protected void onNewIntent(Intent intent) {
-        Log.w(LOGTAG, "zerdatime " + new Date().getTime() + " - onNewIntent");
+        Log.w(LOGTAG, "zerdatime " + SystemClock.uptimeMillis() + " - onNewIntent");
 
         if (checkLaunchState(LaunchState.GeckoExiting)) {
             // We're exiting and shouldn't try to do anything else just incase
             // we're hung for some reason we'll force the process to exit
             System.exit(0);
             return;
         }
         final String action = intent.getAction();
@@ -1754,17 +1753,17 @@ abstract public class GeckoApp
     {
         Log.i(LOGTAG, "restart");
         super.onRestart();
     }
 
     @Override
     public void onStart()
     {
-        Log.w(LOGTAG, "zerdatime " + new Date().getTime() + " - onStart");
+        Log.w(LOGTAG, "zerdatime " + SystemClock.uptimeMillis() + " - onStart");
 
         Log.i(LOGTAG, "start");
         GeckoAppShell.sendEventToGecko(new GeckoEvent(GeckoEvent.ACTIVITY_START));
         super.onStart();
     }
 
     @Override
     public void onDestroy()
--- a/mobile/android/base/GeckoBatteryManager.java
+++ b/mobile/android/base/GeckoBatteryManager.java
@@ -42,30 +42,31 @@ import java.util.Date;
 
 import android.util.Log;
 
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 
 import android.os.BatteryManager;
+import android.os.SystemClock;
 
 public class GeckoBatteryManager
   extends BroadcastReceiver
 {
     private static final String LOGTAG = "GeckoBatteryManager";
 
   // Those constants should be keep in sync with the ones in:
   // dom/battery/Constants.h
   private final static double  kDefaultLevel         = 1.0;
   private final static boolean kDefaultCharging      = true;
   private final static double  kDefaultRemainingTime = -1.0;
   private final static double  kUnknownRemainingTime = -1.0;
 
-  private static Date    sLastLevelChange            = new Date(0);
+  private static long    sLastLevelChange            = 0;
   private static boolean sNotificationsEnabled       = false;
   private static double  sLevel                      = kDefaultLevel;
   private static boolean sCharging                   = kDefaultCharging;
   private static double  sRemainingTime              = kDefaultRemainingTime;;
 
   @Override
   public void onReceive(Context context, Intent intent) {
     if (!intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {
@@ -86,36 +87,37 @@ public class GeckoBatteryManager
         // isn't clear about that.
         sCharging = plugged != 0;
       }
 
       if (sCharging != previousCharging) {
         sRemainingTime = kUnknownRemainingTime;
         // The new remaining time is going to take some time to show up but
         // it's the best way to show a not too wrong value.
-        sLastLevelChange = new Date(0);
+        sLastLevelChange = 0;
       }
 
       // We need two doubles because sLevel is a double.
       double current =  (double)intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
       double max = (double)intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
       if (current == -1 || max == -1) {
         Log.e(LOGTAG, "Failed to get battery level!");
         sLevel = kDefaultLevel;
       } else {
         sLevel = current / max;
       }
 
       if (sLevel == 1.0 && sCharging) {
         sRemainingTime = 0.0;
       } else if (sLevel != previousLevel) {
         // Estimate remaining time.
-        if (sLastLevelChange.getTime() != 0) {
-          Date currentTime = new Date();
-          long dt = (currentTime.getTime() - sLastLevelChange.getTime()) / 1000;
+        if (sLastLevelChange != 0) {
+          // Use elapsedRealtime() because we want to track time across device sleeps.
+          long currentTime = SystemClock.elapsedRealtime();
+          long dt = (currentTime - sLastLevelChange) / 1000;
           double dLevel = sLevel - previousLevel;
 
           if (sCharging) {
             if (dLevel < 0) {
               Log.w(LOGTAG, "When charging, level should increase!");
               sRemainingTime = kUnknownRemainingTime;
             } else {
               sRemainingTime = Math.round(dt / dLevel * (1.0 - sLevel));
@@ -127,17 +129,17 @@ public class GeckoBatteryManager
             } else {
               sRemainingTime = Math.round(dt / -dLevel * sLevel);
             }
           }
 
           sLastLevelChange = currentTime;
         } else {
           // That's the first time we got an update, we can't do anything.
-          sLastLevelChange = new Date();
+          sLastLevelChange = SystemClock.elapsedRealtime();
         }
       }
     } else {
       sLevel = kDefaultLevel;
       sCharging = kDefaultCharging;
       sRemainingTime = kDefaultRemainingTime;
     }
 
--- a/mobile/android/base/GeckoThread.java
+++ b/mobile/android/base/GeckoThread.java
@@ -35,18 +35,20 @@
  *
  * ***** END LICENSE BLOCK ***** */
 
 package org.mozilla.gecko;
 
 import android.content.Intent;
 import android.content.res.Resources;
 import android.content.res.Configuration;
+import android.os.SystemClock;
 import android.util.Log;
 import android.widget.AbsoluteLayout;
+
 import java.io.File;
 import java.io.FilenameFilter;
 import java.io.PrintWriter;
 import java.io.StringWriter;
 import java.util.Date;
 import java.util.Locale;
 
 public class GeckoThread extends Thread {
@@ -87,17 +89,17 @@ public class GeckoThread extends Thread 
         GeckoAppShell.loadGeckoLibs(
             app.getApplication().getPackageResourcePath());
         Locale.setDefault(locale);
         Resources res = app.getBaseContext().getResources();
         Configuration config = res.getConfiguration();
         config.locale = locale;
         res.updateConfiguration(config, res.getDisplayMetrics());
 
-        Log.w(LOGTAG, "zerdatime " + new Date().getTime() + " - runGecko");
+        Log.w(LOGTAG, "zerdatime " + SystemClock.uptimeMillis() + " - runGecko");
 
         // and then fire us up
         try {
             Log.w(LOGTAG, "RunGecko - URI = " + mUri);
 
             GeckoAppShell.runGecko(app.getApplication().getPackageResourcePath(),
                                    mIntent.getStringExtra("args"),
                                    mUri,
--- a/mobile/android/base/db/AndroidBrowserDB.java
+++ b/mobile/android/base/db/AndroidBrowserDB.java
@@ -33,17 +33,16 @@
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 package org.mozilla.gecko.db;
 
 import java.io.ByteArrayOutputStream;
-import java.util.Date;
 
 import android.content.ContentResolver;
 import android.content.ContentValues;
 import android.database.Cursor;
 import android.database.CursorWrapper;
 import android.database.sqlite.SQLiteConstraintException;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
@@ -77,17 +76,17 @@ public class AndroidBrowserDB implements
                             "(" + Browser.BookmarkColumns.URL + " LIKE ? OR " + Browser.BookmarkColumns.TITLE + " LIKE ?)"
                             + " AND LENGTH(" + Browser.BookmarkColumns.URL + ") > 0",
                             urlFilter == null ? new String[] {"%" + constraint.toString() + "%", "%" + constraint.toString() + "%"} :
                             new String[] {urlFilter.toString(), "%" + constraint.toString() + "%", "%" + constraint.toString() + "%"},
                             // ORDER BY is number of visits times a multiplier from 1 - 120 of how recently the site
                             // was accessed with a site accessed today getting 120 and a site accessed 119 or more
                             // days ago getting 1
                             Browser.BookmarkColumns.VISITS + " * MAX(1, (" +
-                            Browser.BookmarkColumns.DATE + " - " + new Date().getTime() + ") / 86400000 + 120) DESC LIMIT " + limit);
+                            Browser.BookmarkColumns.DATE + " - " + System.currentTimeMillis() + ") / 86400000 + 120) DESC LIMIT " + limit);
 
         return new AndroidDBCursor(c);
     }
 
     public void updateVisitedHistory(ContentResolver cr, String uri) {
         Browser.updateVisitedHistory(cr, uri, true);
     }
 
--- a/mobile/android/base/db/LocalBrowserDB.java
+++ b/mobile/android/base/db/LocalBrowserDB.java
@@ -33,17 +33,16 @@
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 package org.mozilla.gecko.db;
 
 import java.io.ByteArrayOutputStream;
-import java.util.Date;
 
 import org.mozilla.gecko.db.BrowserContract.Bookmarks;
 import org.mozilla.gecko.db.BrowserContract.History;
 import org.mozilla.gecko.db.BrowserContract.ImageColumns;
 import org.mozilla.gecko.db.BrowserContract.Images;
 import org.mozilla.gecko.db.BrowserContract.URLColumns;
 
 import android.content.ContentResolver;
@@ -91,17 +90,17 @@ public class LocalBrowserDB implements B
                             (urlFilter != null ? "(" + History.URL + " NOT LIKE ? ) AND " : "" ) + 
                             "(" + History.URL + " LIKE ? OR " + History.TITLE + " LIKE ?)",
                             urlFilter == null ? new String[] {"%" + constraint.toString() + "%", "%" + constraint.toString() + "%"} :
                             new String[] {urlFilter.toString(), "%" + constraint.toString() + "%", "%" + constraint.toString() + "%"},
                             // ORDER BY is number of visits times a multiplier from 1 - 120 of how recently the site
                             // was accessed with a site accessed today getting 120 and a site accessed 119 or more
                             // days ago getting 1
                             History.VISITS + " * MAX(1, (" +
-                            History.DATE_LAST_VISITED + " - " + new Date().getTime() + ") / 86400000 + 120) DESC");
+                            History.DATE_LAST_VISITED + " - " + System.currentTimeMillis() + ") / 86400000 + 120) DESC");
 
         return new LocalDBCursor(c);
     }
 
     private void truncateHistory(ContentResolver cr) {
         Cursor cursor = null;
 
         try {