Bug 1624675 - Remove ContextUtils and inline only caller. r=snorp

Differential Revision: https://phabricator.services.mozilla.com/D68440

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Agi Sferro 2020-03-27 22:03:44 +00:00
Родитель 7918d390b6
Коммит bf487f84b3
4 изменённых файлов: 31 добавлений и 96 удалений

Просмотреть файл

@ -4,10 +4,9 @@
package org.mozilla.gecko.process; package org.mozilla.gecko.process;
import org.mozilla.gecko.util.ContextUtils;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.pm.ServiceInfo; import android.content.pm.ServiceInfo;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
@ -92,7 +91,14 @@ import android.support.annotation.NonNull;
* Obtain the list of all services defined for |context|. * Obtain the list of all services defined for |context|.
*/ */
private static ServiceInfo[] getServiceList(@NonNull final Context 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;
} }
/** /**

Просмотреть файл

@ -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);
}
}

Просмотреть файл

@ -15,6 +15,7 @@ import android.app.ActivityManager;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.pm.ServiceInfo; import android.content.pm.ServiceInfo;
import android.content.res.Configuration; import android.content.res.Configuration;
@ -22,6 +23,7 @@ import android.os.Build;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import android.os.Process; import android.os.Process;
import android.provider.Settings;
import android.support.annotation.AnyThread; import android.support.annotation.AnyThread;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
@ -38,7 +40,6 @@ import org.mozilla.gecko.GeckoThread;
import org.mozilla.gecko.PrefsHelper; import org.mozilla.gecko.PrefsHelper;
import org.mozilla.gecko.annotation.WrapForJNI; import org.mozilla.gecko.annotation.WrapForJNI;
import org.mozilla.gecko.util.BundleEventListener; import org.mozilla.gecko.util.BundleEventListener;
import org.mozilla.gecko.util.ContextUtils;
import org.mozilla.gecko.util.DebugConfig; import org.mozilla.gecko.util.DebugConfig;
import org.mozilla.gecko.util.EventCallback; import org.mozilla.gecko.util.EventCallback;
import org.mozilla.gecko.util.GeckoBundle; 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" // 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 // or if this application is the current Android "debug_app", and to not read configuration
// from a file otherwise. // 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); configFilePath = String.format(CONFIG_FILE_PATH_TEMPLATE, context.getApplicationInfo().packageName);
} }
} }
@ -394,6 +395,25 @@ public final class GeckoRuntime implements Parcelable {
return true; 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) { /* package */ void setDefaultPrefs(final GeckoBundle prefs) {
EventDispatcher.getInstance().dispatch("GeckoView:SetDefaultPrefs", prefs); EventDispatcher.getInstance().dispatch("GeckoView:SetDefaultPrefs", prefs);
} }

Просмотреть файл

@ -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());
}
}