Bug 1363167 - Remove GeckoInterface.getActivity; r=snorp

Use available Context or GeckoActivityMonitor to derive an Activity
instance, instead of using GeckoInterface.getActivity().

MozReview-Commit-ID: GHLMtnQkr2l
This commit is contained in:
Jim Chen 2017-05-15 23:11:11 -04:00
Родитель 7f18399feb
Коммит c0ecba00ed
14 изменённых файлов: 105 добавлений и 75 удалений

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

@ -21,10 +21,6 @@ public class ActivityHandlerHelper {
return mActivityResultHandlerMap.put(aHandler);
}
public static void startIntent(Intent intent, ActivityResultHandler activityResultHandler) {
startIntentForActivity(GeckoAppShell.getGeckoInterface().getActivity(), intent, activityResultHandler);
}
/**
* Starts the Activity, catching & logging if the Activity fails to start.
*

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

@ -3047,7 +3047,7 @@ public class BrowserApp extends GeckoApp
//
// We do this here because there are glitches when unlocking a device with
// BrowserSearch in the foreground if we use BrowserSearch.onStart/Stop.
getActivity().getWindow().setBackgroundDrawableResource(android.R.color.white);
getWindow().setBackgroundDrawableResource(android.R.color.white);
}
private void hideBrowserSearch() {

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

@ -21,7 +21,7 @@ public class DevToolsAuthHelper {
private static final String LOGTAG = "GeckoDevToolsAuthHelper";
public static void scan(Context context, final EventCallback callback) {
public static void scan(final Activity context, final EventCallback callback) {
final Intent intent = InputOptionsUtils.createQRCodeReaderIntent();
intent.putExtra("PROMPT_MESSAGE", context.getString(R.string.devtools_auth_scan_header));
@ -36,7 +36,8 @@ public class DevToolsAuthHelper {
return;
}
ActivityHandlerHelper.startIntent(intent, new ActivityResultHandler() {
ActivityHandlerHelper.startIntentForActivity(
context, intent, new ActivityResultHandler() {
@Override
public void onActivityResult(int resultCode, Intent intent) {
if (resultCode == Activity.RESULT_OK) {

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

@ -5,12 +5,14 @@
package org.mozilla.gecko;
import org.mozilla.gecko.GeckoAppShell;
import org.mozilla.gecko.permissions.PermissionBlock;
import org.mozilla.gecko.permissions.Permissions;
import org.mozilla.gecko.util.BundleEventListener;
import org.mozilla.gecko.util.EventCallback;
import org.mozilla.gecko.util.GeckoBundle;
import android.Manifest;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
@ -43,7 +45,7 @@ public class FilePicker implements BundleEventListener {
}
}
protected FilePicker(Context context) {
private FilePicker(Context context) {
this.context = context;
EventDispatcher.getInstance().registerUiThreadListener(this, "FilePicker:Show");
}
@ -65,26 +67,35 @@ public class FilePicker implements BundleEventListener {
final String[] requiredPermission = getPermissionsForMimeType(mimeType);
final String finalMimeType = mimeType;
// Use activity context cause we want to prompt for runtime permission. (bug 1337692)
Permissions.from(GeckoAppShell.getGeckoInterface().getActivity())
.withPermissions(requiredPermission)
.andFallback(new Runnable() {
@Override
public void run() {
callback.sendError(null);
}
})
.run(new Runnable() {
@Override
public void run() {
showFilePickerAsync(title, finalMimeType, new ResultHandler() {
@Override
public void gotFile(final String filename) {
callback.sendSuccess(filename);
}
}, tabId);
}
});
// Use activity context because we want to prompt for runtime permission.
final Activity currentActivity =
GeckoActivityMonitor.getInstance().getCurrentActivity();
final PermissionBlock perm;
if (currentActivity != null) {
perm = Permissions.from(currentActivity);
} else {
perm = Permissions.from(context).doNotPrompt();
}
perm.withPermissions(requiredPermission)
.andFallback(new Runnable() {
@Override
public void run() {
callback.sendError(null);
}
})
.run(new Runnable() {
@Override
public void run() {
showFilePickerAsync(title, finalMimeType, new ResultHandler() {
@Override
public void gotFile(final String filename) {
callback.sendSuccess(filename);
}
}, tabId);
}
});
}
}
@ -228,12 +239,14 @@ public class FilePicker implements BundleEventListener {
final FilePickerResultHandler fileHandler =
new FilePickerResultHandler(handler, context, tabId);
final Intent intent = getFilePickerIntent(title, mimeType, fileHandler);
final Activity currentActivity =
GeckoActivityMonitor.getInstance().getCurrentActivity();
if (intent == null) {
if (intent == null || currentActivity == null) {
handler.gotFile("");
return;
}
ActivityHandlerHelper.startIntent(intent, fileHandler);
ActivityHandlerHelper.startIntentForActivity(currentActivity, intent, fileHandler);
}
}

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

@ -24,8 +24,6 @@ import android.os.Environment;
import android.os.Process;
import android.provider.MediaStore;
import android.provider.OpenableColumns;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
@ -58,6 +56,18 @@ class FilePickerResultHandler implements ActivityResultHandler {
}
}
private <T> void initLoader(final LoaderCallbacks<T> callbacks) {
final Loader<T> loader = callbacks.onCreateLoader(/* id */ 0, /* args */ null);
loader.registerListener(/* id */ 0, new Loader.OnLoadCompleteListener<T>() {
@Override
public void onLoadComplete(final Loader<T> loader, final T data) {
callbacks.onLoadFinished(loader, data);
loader.unregisterListener(this);
}
});
loader.startLoading();
}
@Override
public void onActivityResult(int resultCode, Intent intent) {
if (resultCode != Activity.RESULT_OK) {
@ -90,18 +100,17 @@ class FilePickerResultHandler implements ActivityResultHandler {
return;
}
final FragmentActivity fa = (FragmentActivity) GeckoAppShell.getGeckoInterface().getActivity();
final LoaderManager lm = fa.getSupportLoaderManager();
final Context context = GeckoAppShell.getApplicationContext();
// Finally, Video pickers and some file pickers may return a content provider.
final ContentResolver cr = fa.getContentResolver();
final ContentResolver cr = context.getContentResolver();
final Cursor cursor = cr.query(uri, new String[] { MediaStore.Video.Media.DATA }, null, null, null);
if (cursor != null) {
try {
// Try a query to make sure the expected columns exist
int index = cursor.getColumnIndex(MediaStore.Video.Media.DATA);
if (index >= 0) {
lm.initLoader(intent.hashCode(), null, new VideoLoaderCallbacks(uri));
initLoader(new VideoLoaderCallbacks(uri));
return;
}
} catch (Exception ex) {
@ -111,7 +120,7 @@ class FilePickerResultHandler implements ActivityResultHandler {
}
}
lm.initLoader(uri.hashCode(), null, new FileLoaderCallbacks(uri, cacheDir, tabId));
initLoader(new FileLoaderCallbacks(uri, cacheDir, tabId));
}
public String generateImageName() {
@ -129,8 +138,8 @@ class FilePickerResultHandler implements ActivityResultHandler {
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
final FragmentActivity fa = (FragmentActivity) GeckoAppShell.getGeckoInterface().getActivity();
return new CursorLoader(fa,
final Context context = GeckoAppShell.getApplicationContext();
return new CursorLoader(context,
uri,
new String[] { MediaStore.Video.Media.DATA },
null, // selection
@ -157,9 +166,7 @@ class FilePickerResultHandler implements ActivityResultHandler {
}
private void tryFileLoaderCallback() {
final FragmentActivity fa = (FragmentActivity) GeckoAppShell.getGeckoInterface().getActivity();
final LoaderManager lm = fa.getSupportLoaderManager();
lm.initLoader(uri.hashCode(), null, new FileLoaderCallbacks(uri, cacheDir, tabId));
initLoader(new FileLoaderCallbacks(uri, cacheDir, tabId));
}
@Override
@ -184,8 +191,8 @@ class FilePickerResultHandler implements ActivityResultHandler {
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
final FragmentActivity fa = (FragmentActivity) GeckoAppShell.getGeckoInterface().getActivity();
return new CursorLoader(fa,
final Context context = GeckoAppShell.getApplicationContext();
return new CursorLoader(context,
uri,
new String[] { OpenableColumns.DISPLAY_NAME },
null, // selection
@ -198,8 +205,8 @@ class FilePickerResultHandler implements ActivityResultHandler {
if (cursor.moveToFirst()) {
String fileName = cursor.getString(0);
final FragmentActivity fa = (FragmentActivity) GeckoAppShell.getGeckoInterface().getActivity();
final ContentResolver cr = fa.getContentResolver();
final Context context = GeckoAppShell.getApplicationContext();
final ContentResolver cr = context.getContentResolver();
// Generate an extension if we don't already have one
if (fileName == null || fileName.lastIndexOf('.') == -1) {

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

@ -419,11 +419,6 @@ public abstract class GeckoApp extends GeckoActivity
return GeckoSharedPrefs.forProfile(this);
}
@Override
public Activity getActivity() {
return this;
}
@Override
public void addAppStateListener(GeckoAppShell.AppStateListener listener) {
mAppStateListeners.add(listener);

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

@ -108,9 +108,10 @@ public final class IntentHelper implements BundleEventListener {
String action,
String title,
final boolean showPromptInPrivateBrowsing) {
final GeckoAppShell.GeckoInterface gi = GeckoAppShell.getGeckoInterface();
final Context activityContext = gi != null ? gi.getActivity() : null;
final Context context = activityContext != null ? activityContext : GeckoAppShell.getApplicationContext();
final Context activityContext =
GeckoActivityMonitor.getInstance().getCurrentActivity();
final Context context = (activityContext != null) ?
activityContext : GeckoAppShell.getApplicationContext();
final Intent intent = getOpenURIIntent(context, targetURI,
mimeType, action, title);

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

@ -5,6 +5,7 @@
package org.mozilla.gecko.notifications;
import android.app.Activity;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.ComponentName;
@ -19,6 +20,7 @@ import android.util.Log;
import java.util.HashMap;
import org.mozilla.gecko.AppConstants;
import org.mozilla.gecko.GeckoActivityMonitor;
import org.mozilla.gecko.GeckoApp;
import org.mozilla.gecko.GeckoAppShell;
import org.mozilla.gecko.GeckoService;
@ -75,13 +77,13 @@ public final class NotificationClient implements NotificationListener {
String persistentData) {
// Put the strings into the intent as an URI
// "alert:?name=<name>&cookie=<cookie>"
String packageName = AppConstants.ANDROID_PACKAGE_NAME;
String packageName = mContext.getPackageName();
String className = AppConstants.MOZ_ANDROID_BROWSER_INTENT_CLASS;
if (GeckoAppShell.getGeckoInterface() != null) {
final ComponentName comp = GeckoAppShell.getGeckoInterface()
.getActivity().getComponentName();
packageName = comp.getPackageName();
className = comp.getClassName();
final Activity currentActivity =
GeckoActivityMonitor.getInstance().getCurrentActivity();
if (currentActivity != null) {
className = currentActivity.getClass().getName();
}
final Uri dataUri = (new Uri.Builder())
.scheme("moz-notification")

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

@ -13,6 +13,7 @@ import java.util.List;
import org.mozilla.gecko.AppConstants;
import org.mozilla.gecko.EventDispatcher;
import org.mozilla.gecko.GeckoActivityMonitor;
import org.mozilla.gecko.GeckoAppShell;
import org.mozilla.gecko.R;
import org.mozilla.gecko.gfx.BitmapUtils;
@ -22,6 +23,7 @@ import org.mozilla.gecko.util.EventCallback;
import org.mozilla.gecko.util.GeckoBundle;
import org.mozilla.gecko.util.ThreadUtils;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
@ -189,8 +191,14 @@ public final class NotificationHelper implements BundleEventListener {
notificationIntent.putExtra(COOKIE_ATTR, message.getString(COOKIE_ATTR, ""));
// All intents get routed through the notificationReceiver. That lets us bail if we don't want to start Gecko
final ComponentName name = new ComponentName(
mContext, GeckoAppShell.getGeckoInterface().getActivity().getClass());
final Activity currentActivity =
GeckoActivityMonitor.getInstance().getCurrentActivity();
final ComponentName name;
if (currentActivity != null) {
name = new ComponentName(mContext, currentActivity.getClass());
} else {
name = new ComponentName(mContext, AppConstants.MOZ_ANDROID_BROWSER_INTENT_CLASS);
}
notificationIntent.putExtra(ORIGINAL_EXTRA_COMPONENT, name);
return notificationIntent;

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

@ -9,6 +9,7 @@ import org.mozilla.gecko.util.GeckoBundle;
import org.mozilla.gecko.util.ThreadUtils;
import org.mozilla.gecko.widget.GeckoActionProvider;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
@ -134,7 +135,8 @@ public class IntentChooserPrompt {
private ArrayList<PromptListItem> getItemsForIntent(Context context, Intent intent) {
ArrayList<PromptListItem> items = new ArrayList<PromptListItem>();
PackageManager pm = context.getPackageManager();
List<ResolveInfo> lri = pm.queryIntentActivityOptions(GeckoAppShell.getGeckoInterface().getActivity().getComponentName(), null, intent, 0);
List<ResolveInfo> lri = pm.queryIntentActivityOptions(
((Activity) context).getComponentName(), null, intent, 0);
// If we didn't find any activities, just return the empty list
if (lri == null) {

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

@ -25,10 +25,12 @@ import org.mozilla.gecko.toolbar.BrowserToolbar.OnDismissListener;
import org.mozilla.gecko.toolbar.BrowserToolbar.OnFilterListener;
import org.mozilla.gecko.toolbar.BrowserToolbar.TabEditingState;
import org.mozilla.gecko.util.ActivityResultHandler;
import org.mozilla.gecko.util.ActivityUtils;
import org.mozilla.gecko.util.DrawableUtil;
import org.mozilla.gecko.util.HardwareUtils;
import org.mozilla.gecko.util.StringUtils;
import org.mozilla.gecko.util.InputOptionsUtils;
import org.mozilla.gecko.util.StringUtils;
import org.mozilla.gecko.util.ThreadUtils;
import org.mozilla.gecko.widget.themed.ThemedLinearLayout;
import android.content.Context;
@ -186,8 +188,7 @@ public class ToolbarEditLayout extends ThemedLinearLayout {
if (showKeyboardOnFocus) {
showKeyboardOnFocus = false;
Activity activity = GeckoAppShell.getGeckoInterface().getActivity();
activity.runOnUiThread(new Runnable() {
ThreadUtils.postToUiThread(new Runnable() {
public void run() {
mEditText.requestFocus();
showSoftInput();
@ -289,7 +290,7 @@ public class ToolbarEditLayout extends ThemedLinearLayout {
Telemetry.sendUIEvent(TelemetryContract.Event.ACTION, TelemetryContract.Method.ACTIONBAR, "voice_input_launch");
final Intent intent = InputOptionsUtils.createVoiceRecognizerIntent(getResources().getString(R.string.voicesearch_prompt));
Activity activity = GeckoAppShell.getGeckoInterface().getActivity();
final Activity activity = ActivityUtils.getActivityFromContext(getContext());
ActivityHandlerHelper.startIntentForActivity(activity, intent, new ActivityResultHandler() {
@Override
public void onActivityResult(int resultCode, Intent data) {
@ -326,7 +327,7 @@ public class ToolbarEditLayout extends ThemedLinearLayout {
Telemetry.sendUIEvent(TelemetryContract.Event.ACTION, TelemetryContract.Method.ACTIONBAR, "qrcode_input_launch");
final Intent intent = InputOptionsUtils.createQRCodeReaderIntent();
Activity activity = GeckoAppShell.getGeckoInterface().getActivity();
final Activity activity = ActivityUtils.getActivityFromContext(getContext());
ActivityHandlerHelper.startIntentForActivity(activity, intent, new ActivityResultHandler() {
@Override
public void onActivityResult(int resultCode, Intent intent) {

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

@ -27,12 +27,6 @@ public class BaseGeckoInterface implements GeckoAppShell.GeckoInterface {
return eventDispatcher;
}
@Override
public Activity getActivity() {
// By default, GeckoView consumers do not have a distinguished current foreground Activity.
return null;
}
@Override
public String getDefaultUAString() {
return HardwareUtils.isTablet() ? BuildConfig.USER_AGENT_GECKOVIEW_TABLET :

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

@ -1661,7 +1661,6 @@ public class GeckoAppShell
public interface GeckoInterface {
public @NonNull EventDispatcher getAppEventDispatcher();
public Activity getActivity();
public String getDefaultUAString();
public void addPluginView(View view);

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

@ -6,6 +6,8 @@
package org.mozilla.gecko.util;
import android.app.Activity;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.os.Build;
import android.view.View;
@ -66,6 +68,15 @@ public class ActivityUtils {
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(intent);
}
public static Activity getActivityFromContext(Context context) {
while (context instanceof ContextWrapper) {
if (context instanceof Activity) {
return (Activity) context;
}
context = ((ContextWrapper) context).getBaseContext();
}
return null;
}
}