Bug 1246816 - Add ContextUtils.getPackageInstallTime. r=sebastian a=ritu
authorMichael Comella <michael.l.comella@gmail.com>
Thu, 31 Mar 2016 14:32:12 -0700
changeset 325907 940fa79188fc1cf919ff8b0f3a4bb306d378df25
parent 325906 2aa1ab2957926a6e0ef0e1e9fe369da30f65bf0b
child 325908 6eb56cc6d62fc03c8129db68a4ade7b3ebc2e012
push id1128
push userjlund@mozilla.com
push dateWed, 01 Jun 2016 01:31:59 +0000
treeherdermozilla-release@fe0d30de989d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssebastian, ritu
bugs1246816
milestone47.0a2
Bug 1246816 - Add ContextUtils.getPackageInstallTime. r=sebastian a=ritu MozReview-Commit-ID: JjGcoSwahUF
mobile/android/base/java/org/mozilla/gecko/util/ContextUtils.java
mobile/android/base/moz.build
mobile/android/tests/background/junit4/src/org/mozilla/gecko/util/TestContextUtils.java
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/java/org/mozilla/gecko/util/ContextUtils.java
@@ -0,0 +1,27 @@
+/*
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+package org.mozilla.gecko.util;
+
+import android.content.Context;
+import android.content.pm.PackageManager;
+
+public class ContextUtils {
+    private ContextUtils() {}
+
+    /**
+     * @return {@link android.content.pm.PackageInfo#firstInstallTime} for the context's package.
+     * @throws PackageManager.NameNotFoundException Unexpected - we get the package name from the context so
+     *         it's expected to be found.
+     */
+    public static long getPackageInstallTime(final Context context) {
+        try {
+            return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).firstInstallTime;
+        } catch (final PackageManager.NameNotFoundException e) {
+            throw new AssertionError("Should not happen: could not get package info for own package");
+        }
+    }
+}
--- a/mobile/android/base/moz.build
+++ b/mobile/android/base/moz.build
@@ -92,16 +92,17 @@ mgjar.javac_flags += ['-Xlint:all']
 gujar = add_java_jar('gecko-util')
 gujar.sources += ['java/org/mozilla/gecko/' + x for x in [
     'util/ActivityResultHandler.java',
     'util/ActivityResultHandlerMap.java',
     'util/ActivityUtils.java',
     'util/BundleEventListener.java',
     'util/Clipboard.java',
     'util/ColorUtils.java',
+    'util/ContextUtils.java',
     'util/DrawableUtil.java',
     'util/EventCallback.java',
     'util/FileUtils.java',
     'util/FloatUtils.java',
     'util/GamepadUtils.java',
     'util/GeckoBackgroundThread.java',
     'util/GeckoEventListener.java',
     'util/GeckoJarReader.java',
new file mode 100644
--- /dev/null
+++ b/mobile/android/tests/background/junit4/src/org/mozilla/gecko/util/TestContextUtils.java
@@ -0,0 +1,38 @@
+/*
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+package org.mozilla.gecko.util;
+
+import android.content.Context;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mozilla.gecko.background.testhelpers.TestRunner;
+import org.robolectric.RuntimeEnvironment;
+
+import static org.junit.Assert.*;
+
+/**
+ * Unit test methods of the ContextUtils class.
+ */
+@RunWith(TestRunner.class)
+public class TestContextUtils {
+
+    private Context context;
+
+    @Before
+    public void setUp() {
+        context = RuntimeEnvironment.application;
+    }
+
+    @Test
+    public void testGetPackageInstallTimeReturnsReasonableValue() throws Exception {
+        // At the time of writing, Robolectric's value is 0, which is reasonable.
+        final long installTime = ContextUtils.getPackageInstallTime(context);
+        assertTrue("Package install time is positive", installTime >= 0);
+        assertTrue("Package install time is less than current time", installTime < System.currentTimeMillis());
+    }
+}