2012-09-11 10:51:43 -07:00
|
|
|
/* 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;
|
|
|
|
|
2014-02-20 21:27:04 -08:00
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
2013-12-17 22:43:31 -08:00
|
|
|
import org.mozilla.gecko.util.StringUtils;
|
|
|
|
|
2012-09-11 10:51:43 -07:00
|
|
|
import android.net.Uri;
|
2014-02-20 21:27:04 -08:00
|
|
|
import android.util.Log;
|
2012-09-11 10:51:43 -07:00
|
|
|
|
|
|
|
public class ReaderModeUtils {
|
|
|
|
private static final String LOGTAG = "ReaderModeUtils";
|
|
|
|
|
2012-09-11 10:51:44 -07:00
|
|
|
public static String getUrlFromAboutReader(String aboutReaderUrl) {
|
2013-12-17 22:43:31 -08:00
|
|
|
return StringUtils.getQueryParameter(aboutReaderUrl, "url");
|
2012-09-11 10:51:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean isEnteringReaderMode(String currentUrl, String newUrl) {
|
2013-11-21 23:33:28 -08:00
|
|
|
if (currentUrl == null || newUrl == null) {
|
2012-09-11 10:51:44 -07:00
|
|
|
return false;
|
2013-11-21 23:33:28 -08:00
|
|
|
}
|
2012-09-11 10:51:44 -07:00
|
|
|
|
2013-11-21 23:33:28 -08:00
|
|
|
if (!AboutPages.isAboutReader(newUrl)) {
|
2012-09-11 10:51:44 -07:00
|
|
|
return false;
|
2013-11-21 23:33:28 -08:00
|
|
|
}
|
2012-09-11 10:51:44 -07:00
|
|
|
|
|
|
|
String urlFromAboutReader = getUrlFromAboutReader(newUrl);
|
2013-11-21 23:33:28 -08:00
|
|
|
if (urlFromAboutReader == null) {
|
2012-09-11 10:51:44 -07:00
|
|
|
return false;
|
2013-11-21 23:33:28 -08:00
|
|
|
}
|
2012-09-11 10:51:44 -07:00
|
|
|
|
|
|
|
return urlFromAboutReader.equals(currentUrl);
|
|
|
|
}
|
|
|
|
|
2013-10-14 06:58:16 -07:00
|
|
|
public static String getAboutReaderForUrl(String url) {
|
|
|
|
return getAboutReaderForUrl(url, -1);
|
2012-09-11 10:51:43 -07:00
|
|
|
}
|
|
|
|
|
2013-10-14 06:58:16 -07:00
|
|
|
public static String getAboutReaderForUrl(String url, int tabId) {
|
2013-11-21 23:33:28 -08:00
|
|
|
String aboutReaderUrl = AboutPages.READER + "?url=" + Uri.encode(url);
|
2012-09-11 10:51:43 -07:00
|
|
|
|
2013-11-21 23:33:28 -08:00
|
|
|
if (tabId >= 0) {
|
2012-09-11 10:51:43 -07:00
|
|
|
aboutReaderUrl += "&tabId=" + tabId;
|
2013-11-21 23:33:28 -08:00
|
|
|
}
|
2012-09-11 10:51:43 -07:00
|
|
|
|
|
|
|
return aboutReaderUrl;
|
|
|
|
}
|
2014-02-20 21:27:04 -08:00
|
|
|
|
|
|
|
public static void addToReadingList(Tab tab) {
|
|
|
|
if (!tab.getReaderEnabled()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSONObject json = new JSONObject();
|
|
|
|
try {
|
|
|
|
json.put("tabID", String.valueOf(tab.getId()));
|
|
|
|
} catch (JSONException e) {
|
|
|
|
Log.e(LOGTAG, "JSON error - failing to add to reading list", e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GeckoEvent e = GeckoEvent.createBroadcastEvent("Reader:Add", json.toString());
|
|
|
|
GeckoAppShell.sendEventToGecko(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void removeFromReadingList(String url) {
|
|
|
|
if (url == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GeckoEvent e = GeckoEvent.createBroadcastEvent("Reader:Remove", url);
|
|
|
|
GeckoAppShell.sendEventToGecko(e);
|
|
|
|
}
|
2012-09-11 10:51:43 -07:00
|
|
|
}
|