Bug 1359531 - Part 2 - Move some tab switch functionality to the tab object. r?sebastian,walkingice
These methods already take a tab object as their argument, so we just turn them into Tab member functions.
MozReview-Commit-ID: 7fp7bzDbRja
--- a/mobile/android/base/java/org/mozilla/gecko/SingleTabActivity.java
+++ b/mobile/android/base/java/org/mozilla/gecko/SingleTabActivity.java
@@ -71,17 +71,17 @@ public abstract class SingleTabActivity
return;
}
final Tabs tabs = Tabs.getInstance();
final Tab tabToSelect = tabs.getTab(mLastSelectedTabId);
// If the tab we've stored is still existing and valid select it...
if (tabToSelect != null && GeckoApplication.getSessionUUID().equals(mLastSessionUUID) &&
- tabs.currentActivityMatchesTab(tabToSelect)) {
+ tabToSelect.matchesActivity(this)) {
tabs.selectTab(mLastSelectedTabId);
} else {
// ... otherwise fall back to the intent data and open a new tab.
loadTabFromIntent(new SafeIntent(getIntent()));
}
}
private void loadTabFromIntent(final SafeIntent intent) {
@@ -93,17 +93,17 @@ public abstract class SingleTabActivity
* @return True if we're going to select an existing tab, false if we want to load a new tab.
*/
private boolean decideTabAction(@NonNull final SafeIntent intent,
@Nullable final Bundle savedInstanceState) {
final Tabs tabs = Tabs.getInstance();
if (hasGeckoTab(intent)) {
final Tab tabToSelect = tabs.getTab(intent.getIntExtra(INTENT_EXTRA_TAB_ID, INVALID_TAB_ID));
- if (tabs.currentActivityMatchesTab(tabToSelect)) {
+ if (tabToSelect.matchesActivity(this)) {
// Nothing further to do here, GeckoApp will select the correct
// tab from the intent.
return true;
}
}
// The intent doesn't refer to a valid tab, so don't pass that data on.
intent.getUnsafe().removeExtra(INTENT_EXTRA_TAB_ID);
intent.getUnsafe().removeExtra(INTENT_EXTRA_SESSION_UUID);
@@ -120,17 +120,17 @@ public abstract class SingleTabActivity
lastSessionUUID = savedInstanceState.getString(LAST_SESSION_UUID);
} else {
lastSelectedTabId = mLastSelectedTabId;
lastSessionUUID = mLastSessionUUID;
}
final Tab tabToSelect = tabs.getTab(lastSelectedTabId);
if (tabToSelect != null && GeckoApplication.getSessionUUID().equals(lastSessionUUID) &&
- tabs.currentActivityMatchesTab(tabToSelect)) {
+ tabToSelect.matchesActivity(this)) {
intent.getUnsafe().putExtra(INTENT_EXTRA_TAB_ID, lastSelectedTabId);
intent.getUnsafe().putExtra(INTENT_EXTRA_SESSION_UUID, lastSessionUUID);
return true;
}
// If we end up here, this means that there's no suitable tab we can take over.
// Instead, we'll just open a new tab from the data specified in the intent.
return false;
--- a/mobile/android/base/java/org/mozilla/gecko/Tab.java
+++ b/mobile/android/base/java/org/mozilla/gecko/Tab.java
@@ -7,32 +7,35 @@ package org.mozilla.gecko;
import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.Future;
import java.util.regex.Pattern;
import org.json.JSONObject;
import org.mozilla.gecko.annotation.RobocopTarget;
+import org.mozilla.gecko.customtabs.CustomTabsActivity;
import org.mozilla.gecko.db.BrowserDB;
import org.mozilla.gecko.db.URLMetadata;
import org.mozilla.gecko.gfx.BitmapUtils;
import org.mozilla.gecko.icons.IconCallback;
import org.mozilla.gecko.icons.IconDescriptor;
import org.mozilla.gecko.icons.IconRequestBuilder;
import org.mozilla.gecko.icons.IconResponse;
import org.mozilla.gecko.icons.Icons;
import org.mozilla.gecko.mozglue.SafeIntent;
import org.mozilla.gecko.reader.ReaderModeUtils;
import org.mozilla.gecko.reader.ReadingListHelper;
import org.mozilla.gecko.toolbar.BrowserToolbar.TabEditingState;
import org.mozilla.gecko.util.GeckoBundle;
import org.mozilla.gecko.util.ThreadUtils;
+import org.mozilla.gecko.webapps.WebAppIndexer;
import org.mozilla.gecko.widget.SiteLogins;
+import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
@@ -796,16 +799,42 @@ public class Tab {
public enum TabType {
BROWSING,
CUSTOMTAB,
WEBAPP
}
/**
+ * @return False if the tab is not matching the activity passed as argument.
+ */
+ public boolean matchesActivity(final Activity activity) {
+ final String activityName = activity.getClass().getName();
+ return activityName.equals(getTargetClassNameForTab());
+ }
+
+ /**
+ * @return The class name of the activity that should preferably be displaying this tab.
+ */
+ public String getTargetClassNameForTab() {
+ final TabType type = getType();
+
+ switch (type) {
+ case CUSTOMTAB:
+ return CustomTabsActivity.class.getName();
+ case WEBAPP:
+ final int index = WebAppIndexer.getInstance().getIndexForManifest(
+ getManifestPath(), mAppContext);
+ return WebAppIndexer.WEBAPP_CLASS + index;
+ default:
+ return AppConstants.MOZ_ANDROID_BROWSER_INTENT_CLASS;
+ }
+ }
+
+ /**
* Sets the tab load progress to the given percentage.
*
* @param progressPercentage Percentage to set progress to (0-100)
*/
void setLoadProgress(int progressPercentage) {
mLoadProgress = progressPercentage;
}
--- a/mobile/android/base/java/org/mozilla/gecko/Tabs.java
+++ b/mobile/android/base/java/org/mozilla/gecko/Tabs.java
@@ -19,33 +19,31 @@ import android.content.Intent;
import android.content.SharedPreferences;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import org.json.JSONException;
import org.json.JSONObject;
import org.mozilla.gecko.annotation.JNITarget;
import org.mozilla.gecko.annotation.RobocopTarget;
-import org.mozilla.gecko.customtabs.CustomTabsActivity;
import org.mozilla.gecko.db.BrowserContract;
import org.mozilla.gecko.db.BrowserDB;
import org.mozilla.gecko.distribution.PartnerBrowserCustomizationsClient;
import org.mozilla.gecko.gfx.LayerView;
import org.mozilla.gecko.mozglue.SafeIntent;
import org.mozilla.gecko.notifications.WhatsNewReceiver;
import org.mozilla.gecko.preferences.GeckoPreferences;
import org.mozilla.gecko.reader.ReaderModeUtils;
import org.mozilla.gecko.util.BundleEventListener;
import org.mozilla.gecko.util.EventCallback;
import org.mozilla.gecko.util.FileUtils;
import org.mozilla.gecko.util.GeckoBundle;
import org.mozilla.gecko.util.JavaUtil;
import org.mozilla.gecko.util.ThreadUtils;
import org.mozilla.gecko.webapps.WebAppActivity;
-import org.mozilla.gecko.webapps.WebAppIndexer;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.OnAccountsUpdateListener;
import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.database.sqlite.SQLiteException;
@@ -355,22 +353,17 @@ public class Tabs implements BundleEvent
return tab;
}
/**
* Check whether the currently active activity matches the tab type of the passed tab.
*/
public boolean currentActivityMatchesTab(Tab tab) {
final Activity currentActivity = GeckoActivityMonitor.getInstance().getCurrentActivity();
-
- if (currentActivity == null) {
- return false;
- }
- String currentActivityName = currentActivity.getClass().getName();
- return currentActivityName.equals(getClassNameForTab(tab));
+ return currentActivity != null && tab.matchesActivity(currentActivity);
}
private void launchActivityForTab(Tab tab) {
final Intent intent;
switch (tab.getType()) {
case CUSTOMTAB:
if (tab.getCustomTabIntent() != null) {
intent = tab.getCustomTabIntent().getUnsafe();
@@ -390,17 +383,17 @@ public class Tabs implements BundleEvent
}
intent.putExtra(WebAppActivity.MANIFEST_PATH, manifestPath);
break;
default:
intent = new Intent(GeckoApp.ACTION_SWITCH_TAB);
break;
}
- intent.setClassName(AppConstants.ANDROID_PACKAGE_NAME, getClassNameForTab(tab));
+ intent.setClassName(AppConstants.ANDROID_PACKAGE_NAME, tab.getTargetClassNameForTab());
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(BrowserContract.SKIP_TAB_QUEUE_FLAG, true);
intent.putExtra(INTENT_EXTRA_TAB_ID, tab.getId());
intent.putExtra(INTENT_EXTRA_SESSION_UUID, GeckoApplication.getSessionUUID());
mAppContext.startActivity(intent);
}
// TODO: When things have settled down a bit, we should split this and everything similar
@@ -408,34 +401,16 @@ public class Tabs implements BundleEvent
private Uri getStartUriFromManifest(String manifestPath) throws IOException, JSONException {
File manifestFile = new File(manifestPath);
final JSONObject manifest = FileUtils.readJSONObjectFromFile(manifestFile);
final JSONObject manifestField = manifest.getJSONObject("manifest");
return Uri.parse(manifestField.getString("start_url"));
}
- /**
- * Get the class name of the activity that should be displaying this tab.
- */
- private String getClassNameForTab(Tab tab) {
- TabType type = tab.getType();
-
- switch (type) {
- case CUSTOMTAB:
- return CustomTabsActivity.class.getName();
- case WEBAPP:
- final int index = WebAppIndexer.getInstance().getIndexForManifest(
- tab.getManifestPath(), mAppContext);
- return WebAppIndexer.WEBAPP_CLASS + index;
- default:
- return AppConstants.MOZ_ANDROID_BROWSER_INTENT_CLASS;
- }
- }
-
public synchronized boolean selectLastTab() {
if (mOrder.isEmpty()) {
return false;
}
selectTab(mOrder.get(mOrder.size() - 1).getId());
return true;
}