Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/multiwindow/MultiWindowUtils.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/multiwindow/MultiWindowUtils.java b/chrome/android/java/src/org/chromium/chrome/browser/multiwindow/MultiWindowUtils.java |
| index 983145b737f6b0f70f0c9a98c415438ba83d1cc8..5d00032eaf7d56259b78820f753490be0a7ae9ee 100644 |
| --- a/chrome/android/java/src/org/chromium/chrome/browser/multiwindow/MultiWindowUtils.java |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/multiwindow/MultiWindowUtils.java |
| @@ -19,12 +19,15 @@ import org.chromium.base.ApplicationStatus; |
| import org.chromium.base.ApplicationStatus.ActivityStateListener; |
| import org.chromium.base.VisibleForTesting; |
| import org.chromium.chrome.browser.AppHooks; |
| +import org.chromium.chrome.browser.ChromeActivity; |
| import org.chromium.chrome.browser.ChromeTabbedActivity; |
| import org.chromium.chrome.browser.ChromeTabbedActivity2; |
| import org.chromium.chrome.browser.IntentHandler; |
| import org.chromium.chrome.browser.document.ChromeLauncherActivity; |
| +import org.chromium.chrome.browser.tab.Tab; |
| import org.chromium.chrome.browser.util.IntentUtils; |
| +import java.io.Serializable; |
| import java.lang.ref.WeakReference; |
| import java.lang.reflect.InvocationTargetException; |
| import java.lang.reflect.Method; |
| @@ -43,9 +46,10 @@ public class MultiWindowUtils implements ActivityStateListener { |
| // TODO(twellington): replace this with Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT once we're building |
| // against N. |
| public static final int FLAG_ACTIVITY_LAUNCH_ADJACENT = 0x00001000; |
| + public static final String EXTRA_FROM_ACTIVITY_INTENT = |
| + "org.chromium.chrome.browser.multiwindow.FromActivity"; |
|
Theresa
2017/04/12 22:29:11
Rather than introducing a new intent flag, I think
PEConn
2017/04/26 17:24:37
Done.
|
| - private static AtomicReference<MultiWindowUtils> sInstance = |
| - new AtomicReference<MultiWindowUtils>(); |
| + private static AtomicReference<MultiWindowUtils> sInstance = new AtomicReference<>(); |
| // Used to keep track of whether ChromeTabbedActivity2 is running. A tri-state Boolean is |
| // used in case both activities die in the background and MultiWindowUtils is recreated. |
| @@ -147,8 +151,7 @@ public class MultiWindowUtils implements ActivityStateListener { |
| @Override |
| public void onActivityStateChange(Activity activity, int newState) { |
| if (newState == ActivityState.RESUMED && activity instanceof ChromeTabbedActivity) { |
| - mLastResumedTabbedActivity = |
| - new WeakReference<ChromeTabbedActivity>((ChromeTabbedActivity) activity); |
| + mLastResumedTabbedActivity = new WeakReference<>((ChromeTabbedActivity) activity); |
| } |
| } |
| @@ -275,6 +278,52 @@ public class MultiWindowUtils implements ActivityStateListener { |
| return activityState == ActivityState.RESUMED || activityState == ActivityState.PAUSED; |
| } |
| + /** |
| + * Moves a Tab to the other MultiWindow window. |
| + * @param activity An {@link Activity} to use for context. |
| + * @param tab The {@link Tab} to move. |
| + */ |
| + public static void moveTabToOtherWindow(Activity activity, Tab tab) { |
| + moveTabToActivity( |
| + activity, tab, getInstance().getOpenInOtherWindowActivity(activity), null); |
| + } |
| + |
| + /** |
| + * Moves a Tab to a specific Activity. |
| + * @param activity An {@link Activity} for context. |
| + * @param tab The {@link Tab} to move. |
| + * @param targetActivity The class of the Activity to move the Tab to. |
| + * @param finalizeCallback A callback that will be called after the tab is attached to the new |
| + * host activity}. |
| + */ |
| + public static void moveTabToActivity(Activity activity, Tab tab, |
| + Class<? extends Activity> targetActivity, @Nullable Runnable finalizeCallback) { |
| + if (targetActivity == null) return; |
| + |
| + Intent intent = new Intent(activity, targetActivity); |
| + intent.putExtra(EXTRA_FROM_ACTIVITY_INTENT, tab.getActivity().getClass()); |
| + |
| + MultiWindowUtils.setOpenInOtherWindowIntentExtras(intent, activity, targetActivity); |
| + MultiWindowUtils.onMultiInstanceModeStarted(); |
|
Theresa
2017/04/12 22:29:11
"Multi-instance" mode has historically referred to
PEConn
2017/04/26 17:24:37
Acknowledged.
|
| + tab.detachAndStartReparenting(intent, null, finalizeCallback); |
| + } |
| + |
| + /** |
| + * Extracts the class of the {@link Activity} that sent a {@link Tab} to the given Activity. |
| + * May be null. |
| + */ |
| + @SuppressWarnings("unchecked") |
| + public static Class<? extends ChromeActivity> getSenderActivity(Activity activity) { |
|
Theresa
2017/04/12 22:29:11
Typically we use IntentUtils#safeGet*() to extract
PEConn
2017/04/26 17:24:37
Done.
|
| + Serializable extra = activity.getIntent().getSerializableExtra(EXTRA_FROM_ACTIVITY_INTENT); |
| + if (extra == null) return null; |
| + |
| + assert extra instanceof Class; |
| + Class<?> clazz = (Class<?>) extra; |
| + |
| + assert ChromeActivity.class.isAssignableFrom(clazz); |
| + return (Class<? extends ChromeActivity>) clazz; |
| + } |
| + |
| @VisibleForTesting |
| public Boolean getTabbedActivity2TaskRunning() { |
| return mTabbedActivity2TaskRunning; |