remove unused fields in DiscountHelper and add decodeTitleFromTravelUrl

This commit is contained in:
Chumva 2018-05-25 14:11:47 +03:00
parent 22592206c7
commit ea157a2d9c
3 changed files with 29 additions and 28 deletions

View file

@ -18,7 +18,6 @@ import net.osmand.plus.chooseplan.ChoosePlanDialogFragment;
import net.osmand.plus.inapp.InAppPurchaseHelper; import net.osmand.plus.inapp.InAppPurchaseHelper;
import net.osmand.plus.views.mapwidgets.MapInfoWidgetsFactory.TopToolbarController; import net.osmand.plus.views.mapwidgets.MapInfoWidgetsFactory.TopToolbarController;
import net.osmand.plus.views.mapwidgets.MapInfoWidgetsFactory.TopToolbarControllerType; import net.osmand.plus.views.mapwidgets.MapInfoWidgetsFactory.TopToolbarControllerType;
import net.osmand.plus.wikipedia.WikiArticleHelper;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import org.json.JSONObject; import org.json.JSONObject;
@ -38,12 +37,10 @@ public class DiscountHelper {
private static String mDescription; private static String mDescription;
private static String mIcon; private static String mIcon;
private static String mUrl; private static String mUrl;
private static String mTravelUrl;
private static String mLang;
private static boolean mBannerVisible; private static boolean mBannerVisible;
private static final String URL = "https://osmand.net/api/motd"; private static final String URL = "https://osmand.net/api/motd";
private static final String INAPP_PREFIX = "osmand-in-app:"; private static final String INAPP_PREFIX = "osmand-in-app:";
private static final String TRAVEL_PREFIX = "travel:"; private static final String TRAVEL_PREFIX = "https://osmand.net/travel";
public static void checkAndDisplay(final MapActivity mapActivity) { public static void checkAndDisplay(final MapActivity mapActivity) {
@ -53,7 +50,7 @@ public class DiscountHelper {
return; return;
} }
if (mBannerVisible) { if (mBannerVisible) {
showDiscountBanner(mapActivity, mTitle, mDescription, mIcon, mUrl, mTravelUrl, mLang); showDiscountBanner(mapActivity, mTitle, mDescription, mIcon, mUrl);
} }
if (System.currentTimeMillis() - mLastCheckTime < 1000 * 60 * 60 * 24 if (System.currentTimeMillis() - mLastCheckTime < 1000 * 60 * 60 * 24
|| !settings.isInternetConnectionAvailable() || !settings.isInternetConnectionAvailable()
@ -104,8 +101,6 @@ public class DiscountHelper {
String description = obj.getString("description"); String description = obj.getString("description");
String icon = obj.getString("icon"); String icon = obj.getString("icon");
String url = parseUrl(app, obj.getString("url")); String url = parseUrl(app, obj.getString("url"));
String travelUrl = parseUrl(app, obj.getString("travel"));
String lang = parseUrl(app, obj.getString("lang"));
SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm"); SimpleDateFormat df = new SimpleDateFormat("dd-MM-yyyy HH:mm");
Date start = df.parse(obj.getString("start")); Date start = df.parse(obj.getString("start"));
Date end = df.parse(obj.getString("end")); Date end = df.parse(obj.getString("end"));
@ -142,7 +137,7 @@ public class DiscountHelper {
settings.DISCOUNT_TOTAL_SHOW.set(settings.DISCOUNT_TOTAL_SHOW.get() + 1); settings.DISCOUNT_TOTAL_SHOW.set(settings.DISCOUNT_TOTAL_SHOW.get() + 1);
settings.DISCOUNT_SHOW_NUMBER_OF_STARTS.set(app.getAppInitializer().getNumberOfStarts()); settings.DISCOUNT_SHOW_NUMBER_OF_STARTS.set(app.getAppInitializer().getNumberOfStarts());
settings.DISCOUNT_SHOW_DATETIME_MS.set(System.currentTimeMillis()); settings.DISCOUNT_SHOW_DATETIME_MS.set(System.currentTimeMillis());
showDiscountBanner(mapActivity, message, description, icon, url, travelUrl, lang); showDiscountBanner(mapActivity, message, description, icon, url);
} }
} }
} }
@ -172,8 +167,8 @@ public class DiscountHelper {
return result; return result;
} }
private static void showDiscountBanner(final MapActivity mapActivity, final String title, final String description, private static void showDiscountBanner(final MapActivity mapActivity, final String title,
final String icon, final String url, final String travelUrl, final String lang) { final String description, final String icon, final String url) {
final DiscountBarController toolbarController = new DiscountBarController(); final DiscountBarController toolbarController = new DiscountBarController();
toolbarController.setTitle(title); toolbarController.setTitle(title);
toolbarController.setDescription(description); toolbarController.setDescription(description);
@ -186,7 +181,7 @@ public class DiscountHelper {
mapActivity.getMyApplication().logEvent(mapActivity, "motd_click"); mapActivity.getMyApplication().logEvent(mapActivity, "motd_click");
mBannerVisible = false; mBannerVisible = false;
mapActivity.hideTopToolbar(toolbarController); mapActivity.hideTopToolbar(toolbarController);
openUrl(mapActivity, url, lang); openUrl(mapActivity, url);
} }
}); });
toolbarController.setOnTitleClickListener(new View.OnClickListener() { toolbarController.setOnTitleClickListener(new View.OnClickListener() {
@ -195,7 +190,7 @@ public class DiscountHelper {
mapActivity.getMyApplication().logEvent(mapActivity, "motd_click"); mapActivity.getMyApplication().logEvent(mapActivity, "motd_click");
mBannerVisible = false; mBannerVisible = false;
mapActivity.hideTopToolbar(toolbarController); mapActivity.hideTopToolbar(toolbarController);
openUrl(mapActivity, url, lang); openUrl(mapActivity, url);
} }
}); });
} }
@ -212,14 +207,12 @@ public class DiscountHelper {
mDescription = description; mDescription = description;
mIcon = icon; mIcon = icon;
mUrl = url; mUrl = url;
mTravelUrl = travelUrl;
mLang = lang;
mBannerVisible = true; mBannerVisible = true;
mapActivity.showTopToolbar(toolbarController); mapActivity.showTopToolbar(toolbarController);
} }
private static void openUrl(final MapActivity mapActivity, String url, String lang) { private static void openUrl(final MapActivity mapActivity, String url) {
if (url.startsWith(INAPP_PREFIX)) { if (url.startsWith(INAPP_PREFIX)) {
if (url.contains(InAppPurchaseHelper.SKU_FULL_VERSION_PRICE)) { if (url.contains(InAppPurchaseHelper.SKU_FULL_VERSION_PRICE)) {
OsmandApplication app = mapActivity.getMyApplication(); OsmandApplication app = mapActivity.getMyApplication();
@ -233,7 +226,7 @@ public class DiscountHelper {
} }
} else if (url.startsWith(TRAVEL_PREFIX)) { } else if (url.startsWith(TRAVEL_PREFIX)) {
Intent intent = new Intent(Intent.ACTION_SEND); Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, WikiArticleHelper.buildTravelUrl(url, lang)); intent.putExtra(Intent.EXTRA_TEXT, url);
intent.setType("text/plain"); intent.setType("text/plain");
mapActivity.startActivity(Intent.createChooser(intent, mapActivity.getString(R.string.shared_string_share))); mapActivity.startActivity(Intent.createChooser(intent, mapActivity.getString(R.string.shared_string_share)));
} else { } else {

View file

@ -335,4 +335,17 @@ public class WikiArticleHelper {
} }
return "https://osmand.net/travel?title=" + title + "&lang=" + lang; return "https://osmand.net/travel?title=" + title + "&lang=" + lang;
} }
public static String decodeTitleFromTravelUrl(String url) {
String title = "";
try {
if (!Algorithms.isEmpty(url)) {
title = url.replace("_", " ");
title = URLDecoder.decode(title, "UTF-8");
}
} catch (UnsupportedEncodingException e) {
System.err.println(e.getMessage());
}
return title;
}
} }

View file

@ -32,15 +32,14 @@ import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.activities.TabActivity; import net.osmand.plus.activities.TabActivity;
import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents; import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents;
import net.osmand.plus.wikipedia.WikiArticleHelper;
import net.osmand.plus.wikivoyage.article.WikivoyageArticleDialogFragment; import net.osmand.plus.wikivoyage.article.WikivoyageArticleDialogFragment;
import net.osmand.plus.wikivoyage.data.TravelArticle; import net.osmand.plus.wikivoyage.data.TravelArticle;
import net.osmand.plus.wikivoyage.data.TravelDbHelper; import net.osmand.plus.wikivoyage.data.TravelDbHelper;
import net.osmand.plus.wikivoyage.search.WikivoyageSearchDialogFragment; import net.osmand.plus.wikivoyage.search.WikivoyageSearchDialogFragment;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import java.io.UnsupportedEncodingException;
import java.lang.ref.WeakReference; import java.lang.ref.WeakReference;
import java.net.URLDecoder;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -182,17 +181,13 @@ public class WikivoyageExploreActivity extends TabActivity implements DownloadEv
&& path != null && path != null
&& host.contains("osmand.net") && host.contains("osmand.net")
&& path.startsWith("/travel")) { && path.startsWith("/travel")) {
String query = data.getQueryParameter("title"); String title = WikiArticleHelper.decodeTitleFromTravelUrl(data.getQueryParameter("title"));
try {
if (!Algorithms.isEmpty(query)) {
query = URLDecoder.decode(query, "UTF-8");
}
} catch (UnsupportedEncodingException e) {
System.err.println(e.getMessage());
}
String selectedLang = data.getQueryParameter("lang"); String selectedLang = data.getQueryParameter("lang");
if (!Algorithms.isEmpty(query) && !Algorithms.isEmpty(selectedLang)) { if (!Algorithms.isEmpty(title) && !Algorithms.isEmpty(selectedLang)) {
WikivoyageArticleDialogFragment.showInstance(app, getSupportFragmentManager(), query, selectedLang); long articleId = app.getTravelDbHelper().getArticleId(title, selectedLang);
if (articleId != -1) {
WikivoyageArticleDialogFragment.showInstance(app, getSupportFragmentManager(), articleId, selectedLang);
}
} }
} }
} else { } else {