diff --git a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/process/ServiceUtils.java b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/process/ServiceUtils.java index ca9908b6be4b..c377fc3b1c05 100644 --- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/process/ServiceUtils.java +++ b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/process/ServiceUtils.java @@ -4,10 +4,9 @@ package org.mozilla.gecko.process; -import org.mozilla.gecko.util.ContextUtils; - import android.content.ComponentName; import android.content.Context; +import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.ServiceInfo; import android.support.annotation.NonNull; @@ -92,7 +91,14 @@ import android.support.annotation.NonNull; * Obtain the list of all services defined for |context|. */ private static ServiceInfo[] getServiceList(@NonNull final Context context) { - return ContextUtils.getCurrentPackageInfo(context, PackageManager.GET_SERVICES).services; + final PackageInfo packageInfo; + try { + packageInfo = context.getPackageManager() + .getPackageInfo(context.getPackageName(), PackageManager.GET_SERVICES); + } catch (PackageManager.NameNotFoundException e) { + throw new AssertionError("Should not happen: Can't get package info of own package"); + } + return packageInfo.services; } /** diff --git a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/ContextUtils.java b/mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/ContextUtils.java deleted file mode 100644 index 9d2b361e7b02..000000000000 --- a/mobile/android/geckoview/src/main/java/org/mozilla/gecko/util/ContextUtils.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * 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.ApplicationInfo; -import android.content.pm.PackageInfo; -import android.content.pm.PackageManager; -import android.os.Build; -import android.provider.Settings; -import android.support.annotation.NonNull; -import android.text.TextUtils; - -public class ContextUtils { - private static final String INSTALLER_GOOGLE_PLAY = "com.android.vending"; - - private ContextUtils() {} - - public static PackageInfo getCurrentPackageInfo(final Context context, final int flags) { - try { - return context.getPackageManager().getPackageInfo(context.getPackageName(), flags); - } catch (PackageManager.NameNotFoundException e) { - throw new AssertionError("Should not happen: Can't get package info of own package"); - } - } - - public static PackageInfo getCurrentPackageInfo(final Context context) { - return getCurrentPackageInfo(context, 0); - } - - public static boolean isApplicationDebuggable(final @NonNull Context context) { - final ApplicationInfo applicationInfo = context.getApplicationInfo(); - return (applicationInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0; - } - - public static boolean isApplicationCurrentDebugApp(final @NonNull Context context) { - final ApplicationInfo applicationInfo = context.getApplicationInfo(); - - final String currentDebugApp; - if (Build.VERSION.SDK_INT >= 17) { - currentDebugApp = Settings.Global.getString(context.getContentResolver(), - Settings.Global.DEBUG_APP); - } else { - currentDebugApp = Settings.System.getString(context.getContentResolver(), - Settings.System.DEBUG_APP); - } - return applicationInfo.packageName.equals(currentDebugApp); - } -} diff --git a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoRuntime.java b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoRuntime.java index 5d27b913eeb9..69a380335f48 100644 --- a/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoRuntime.java +++ b/mobile/android/geckoview/src/main/java/org/mozilla/geckoview/GeckoRuntime.java @@ -15,6 +15,7 @@ import android.app.ActivityManager; import android.content.ComponentName; import android.content.Context; import android.content.Intent; +import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.content.pm.ServiceInfo; import android.content.res.Configuration; @@ -22,6 +23,7 @@ import android.os.Build; import android.os.Parcel; import android.os.Parcelable; import android.os.Process; +import android.provider.Settings; import android.support.annotation.AnyThread; import android.support.annotation.NonNull; import android.support.annotation.Nullable; @@ -38,7 +40,6 @@ import org.mozilla.gecko.GeckoThread; import org.mozilla.gecko.PrefsHelper; import org.mozilla.gecko.annotation.WrapForJNI; import org.mozilla.gecko.util.BundleEventListener; -import org.mozilla.gecko.util.ContextUtils; import org.mozilla.gecko.util.DebugConfig; import org.mozilla.gecko.util.EventCallback; import org.mozilla.gecko.util.GeckoBundle; @@ -352,7 +353,7 @@ public final class GeckoRuntime implements Parcelable { // Default to /data/local/tmp/$PACKAGE-geckoview-config.yaml if android:debuggable="true" // or if this application is the current Android "debug_app", and to not read configuration // from a file otherwise. - if (ContextUtils.isApplicationDebuggable(context) || ContextUtils.isApplicationCurrentDebugApp(context)) { + if (isApplicationDebuggable(context) || isApplicationCurrentDebugApp(context)) { configFilePath = String.format(CONFIG_FILE_PATH_TEMPLATE, context.getApplicationInfo().packageName); } } @@ -394,6 +395,25 @@ public final class GeckoRuntime implements Parcelable { return true; } + private boolean isApplicationDebuggable(final @NonNull Context context) { + final ApplicationInfo applicationInfo = context.getApplicationInfo(); + return (applicationInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0; + } + + private boolean isApplicationCurrentDebugApp(final @NonNull Context context) { + final ApplicationInfo applicationInfo = context.getApplicationInfo(); + + final String currentDebugApp; + if (Build.VERSION.SDK_INT >= 17) { + currentDebugApp = Settings.Global.getString(context.getContentResolver(), + Settings.Global.DEBUG_APP); + } else { + currentDebugApp = Settings.System.getString(context.getContentResolver(), + Settings.System.DEBUG_APP); + } + return applicationInfo.packageName.equals(currentDebugApp); + } + /* package */ void setDefaultPrefs(final GeckoBundle prefs) { EventDispatcher.getInstance().dispatch("GeckoView:SetDefaultPrefs", prefs); } diff --git a/mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/TestContextUtils.java b/mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/TestContextUtils.java deleted file mode 100644 index f18fc2cb9294..000000000000 --- a/mobile/android/geckoview/src/test/java/org/mozilla/gecko/util/TestContextUtils.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * 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.robolectric.RobolectricTestRunner; -import org.robolectric.RuntimeEnvironment; - -import static org.junit.Assert.*; - -/** - * Unit test methods of the ContextUtils class. - */ -@RunWith(RobolectricTestRunner.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.getCurrentPackageInfo(context).firstInstallTime; - assertTrue("Package install time is positive", installTime >= 0); - assertTrue("Package install time is less than current time", installTime < System.currentTimeMillis()); - } -}