Merge pull request #5333 from osmandapp/WikipediaImprovements

Wikipedia improvements
This commit is contained in:
Alexey 2018-05-03 15:52:24 +03:00 committed by GitHub
commit dac34e8a44
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 232 additions and 301 deletions

View file

@ -29,6 +29,7 @@
android:layout_height="match_parent"
android:layout_weight="1"
android:ellipsize="end"
android:maxLines="1"
android:gravity="center_vertical"
android:textSize="@dimen/dialog_header_text_size"
osmand:typeface="@string/font_roboto_medium" />

View file

@ -717,8 +717,8 @@ public class OsmandSettings {
public final CommonPreference<Boolean> SHOW_LINES_TO_FIRST_MARKERS = new BooleanPreference("show_lines_to_first_markers", false).makeProfile();
public final CommonPreference<Boolean> SHOW_ARROWS_TO_FIRST_MARKERS = new BooleanPreference("show_arrows_to_first_markers", false).makeProfile();
public final CommonPreference<Boolean> WIKIVOYAGE_SHOW_IMAGES_ASKED = new BooleanPreference("wikivoyage_show_images_asked", false).makeGlobal();
public final CommonPreference<WikivoyageShowImages> WIKIVOYAGE_SHOW_IMAGES = new EnumIntPreference<>("wikivoyage_show_imgs", WikivoyageShowImages.OFF, WikivoyageShowImages.values()).makeGlobal();
public final CommonPreference<Boolean> WIKI_ARTICLE_SHOW_IMAGES_ASKED = new BooleanPreference("wikivoyage_show_images_asked", false).makeGlobal();
public final CommonPreference<WikiArticleShowImages> WIKI_ARTICLE_SHOW_IMAGES = new EnumIntPreference<>("wikivoyage_show_imgs", WikiArticleShowImages.OFF, WikiArticleShowImages.values()).makeGlobal();
public final CommonPreference<Boolean> SELECT_MARKER_ON_SINGLE_TAP = new BooleanPreference("select_marker_on_single_tap", false).makeProfile();
@ -3315,14 +3315,14 @@ public class OsmandSettings {
EMPTY
}
public enum WikivoyageShowImages {
public enum WikiArticleShowImages {
ON(R.string.shared_string_on),
OFF(R.string.shared_string_off),
WIFI(R.string.shared_string_wifi_only);
public final int name;
WikivoyageShowImages(int name) {
WikiArticleShowImages(int name) {
this.name = name;
}
}

View file

@ -76,7 +76,7 @@ public class SettingsDevelopmentActivity extends SettingsBaseActivity {
settings.MAPILLARY_FIRST_DIALOG_SHOWN.set(false);
settings.WEBGL_SUPPORTED.set(true);
settings.METRIC_SYSTEM_CHANGED_MANUALLY.set(false);
settings.WIKIVOYAGE_SHOW_IMAGES_ASKED.set(false);
settings.WIKI_ARTICLE_SHOW_IMAGES_ASKED.set(false);
getMyApplication().showToastMessage(R.string.shared_string_ok);
return true;

View file

@ -34,7 +34,7 @@ import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.helpers.FontCache;
import net.osmand.plus.mapcontextmenu.MenuBuilder;
import net.osmand.plus.mapcontextmenu.WikipediaDialogFragment;
import net.osmand.plus.wikipedia.WikipediaDialogFragment;
import net.osmand.plus.osmedit.OsmEditingPlugin;
import net.osmand.plus.poi.PoiUIFilter;
import net.osmand.plus.views.POIMapLayer;

View file

@ -17,7 +17,7 @@ import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.mapcontextmenu.MenuBuilder;
import net.osmand.plus.mapcontextmenu.MenuController;
import net.osmand.plus.mapcontextmenu.WikipediaDialogFragment;
import net.osmand.plus.wikipedia.WikipediaDialogFragment;
import net.osmand.plus.mapcontextmenu.builders.AmenityMenuBuilder;
import net.osmand.plus.render.RenderingIcons;
import net.osmand.plus.resources.TransportIndexRepository;

View file

@ -0,0 +1,154 @@
package net.osmand.plus.wikipedia;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.ColorRes;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;
import net.osmand.AndroidUtils;
import net.osmand.IndexConstants;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.wikivoyage.WikiBaseDialogFragment;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public abstract class WikiArticleBaseDialogFragment extends WikiBaseDialogFragment {
protected static final String HEADER_INNER = "<html><head>\n" +
"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n" +
"<meta http-equiv=\"cleartype\" content=\"on\" />\n" +
"<link href=\"article_style.css\" type=\"text/css\" rel=\"stylesheet\"/>\n" +
"<script type=\"text/javascript\">" +
"function showNavigation() {" +
" Android.showNavigation();" +
"}" +
"</script>" +
"</head>";
protected static final String FOOTER_INNER = "<script>var coll = document.getElementsByTagName(\"H2\");" +
"var i;" +
"for (i = 0; i < coll.length; i++) {" +
" coll[i].addEventListener(\"click\", function() {" +
" this.classList.toggle(\"active\");" +
" var content = this.nextElementSibling;" +
" if (content.style.display === \"block\") {" +
" content.style.display = \"none\";" +
" } else {" +
" content.style.display = \"block\";" +
" }" +
" });" +
"}" +
"document.addEventListener(\"DOMContentLoaded\", function(event) {\n" +
" document.querySelectorAll('img').forEach(function(img) {\n" +
" img.onerror = function() {\n" +
" this.style.display = 'none';\n" +
" var caption = img.parentElement.nextElementSibling;\n" +
" if (caption.className == \"thumbnailcaption\") {\n" +
" caption.style.display = 'none';\n" +
" }\n" +
" };\n" +
" })\n" +
"});" +
"function scrollAnchor(id, title) {" +
"openContent(title);" +
"window.location.hash = id;}\n" +
"function openContent(id) {\n" +
" var doc = document.getElementById(id).parentElement;\n" +
" doc.classList.toggle(\"active\");\n" +
" var content = doc.nextElementSibling;\n" +
" content.style.display = \"block\";\n" +
" collapseActive(doc);" +
"}" +
"function collapseActive(doc) {" +
" var coll = document.getElementsByTagName(\"H2\");" +
" var i;" +
" for (i = 0; i < coll.length; i++) {" +
" var item = coll[i];" +
" if (item != doc && item.classList.contains(\"active\")) {" +
" item.classList.toggle(\"active\");" +
" var content = item.nextElementSibling;" +
" if (content.style.display === \"block\") {" +
" content.style.display = \"none\";" +
" }" +
" }" +
" }" +
"}</script>"
+ "</body></html>";
protected WebView contentWebView;
protected TextView selectedLangTv;
protected TextView articleToolbarText;
protected void updateWebSettings() {
OsmandSettings.WikiArticleShowImages showImages = getSettings().WIKI_ARTICLE_SHOW_IMAGES.get();
WebSettings webSettings = contentWebView.getSettings();
switch (showImages) {
case ON:
webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
break;
case OFF:
webSettings.setCacheMode(WebSettings.LOAD_CACHE_ONLY);
break;
case WIFI:
webSettings.setCacheMode(getMyApplication().getSettings().isWifiConnected() ?
WebSettings.LOAD_DEFAULT : WebSettings.LOAD_CACHE_ONLY);
break;
}
}
@NonNull
protected String getBaseUrl() {
File wikivoyageDir = getMyApplication().getAppPath(IndexConstants.WIKIVOYAGE_INDEX_DIR);
if (new File(wikivoyageDir, "article_style.css").exists()) {
return "file://" + wikivoyageDir.getAbsolutePath() + "/";
}
return "file:///android_asset/";
}
protected void writeOutHTML(StringBuilder sb, File file) {
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(file));
writer.write(sb.toString());
writer.close();
} catch (IOException e) {
Log.w("ArticleDialog", e.getMessage(), e);
}
}
protected void moveToAnchor(String id, String title) {
contentWebView.loadUrl("javascript:scrollAnchor(\"" + id + "\", \"" + title.trim() + "\")");
}
@NonNull
protected Drawable getSelectedLangIcon() {
Drawable normal = getContentIcon(R.drawable.ic_action_map_language);
if (Build.VERSION.SDK_INT >= 21) {
Drawable active = getActiveIcon(R.drawable.ic_action_map_language);
return AndroidUtils.createPressedStateListDrawable(normal, active);
}
return normal;
}
@Override
@ColorRes
protected int getStatusBarColor() {
return nightMode ? R.color.status_bar_wikivoyage_article_dark : R.color.status_bar_wikivoyage_article_light;
}
protected abstract void showPopupLangMenu(View view, final String langSelected);
protected abstract void populateArticle();
@NonNull
protected abstract String createHtmlContent();
}

View file

@ -1,4 +1,4 @@
package net.osmand.plus.mapcontextmenu;
package net.osmand.plus.wikipedia;
import android.annotation.SuppressLint;
import android.app.Dialog;
@ -6,10 +6,7 @@ import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.customtabs.CustomTabsIntent;
@ -24,51 +21,38 @@ import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;
import net.osmand.AndroidUtils;
import net.osmand.IndexConstants;
import net.osmand.data.Amenity;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.base.BaseOsmAndDialogFragment;
import net.osmand.plus.helpers.FileNameTranslationHelper;
import net.osmand.util.Algorithms;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
public class WikipediaDialogFragment extends BaseOsmAndDialogFragment {
public class WikipediaDialogFragment extends WikiArticleBaseDialogFragment {
public static final String TAG = "WikipediaDialogFragment";
private static final String HEADER_INNER = "<html><head>\n" +
"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n" +
"<meta http-equiv=\"cleartype\" content=\"on\" />\n" +
"<link href=\"article_style.css\" type=\"text/css\" rel=\"stylesheet\"/>\n" +
"</head>";
private static final String FOOTER_INNER = "</body></html>";
private static final int MENU_ITEM_NO_ID = 0;
private static final int MENU_ITEM_SHOW_ID = 1;
private static final int MENU_ITEM_WIFI_ID = 2;
private WebView contentWebView;
private TextView articleToolbarText;
private TextView readFullArticleButton;
private TextView selectLanguageTextView;
private boolean darkMode;
private Amenity amenity;
private String lang;
private String title;
private String article;
public void setAmenity(Amenity amenity) {
this.amenity = amenity;
@ -78,32 +62,6 @@ public class WikipediaDialogFragment extends BaseOsmAndDialogFragment {
this.lang = lang;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
OsmandApplication app = getMyApplication();
darkMode = app.getDaynightHelper().isNightMode() || !app.getSettings().isLightContent();
int themeId = darkMode ? R.style.OsmandDarkTheme : R.style.OsmandLightTheme;
setStyle(STYLE_NO_FRAME, themeId);
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
int themeId = darkMode ? R.style.OsmandDarkTheme : R.style.OsmandLightTheme_LightStatusBar;
Dialog dialog = new Dialog(getContext(), themeId);
Window window = dialog.getWindow();
if (window != null) {
if (!getSettings().DO_NOT_USE_ANIMATIONS.get()) {
window.getAttributes().windowAnimations = R.style.Animations_Alpha;
}
if (Build.VERSION.SDK_INT >= 21) {
window.setStatusBarColor(getResolvedColor(getStatusBarColor()));
}
}
return dialog;
}
@SuppressLint("SetJavaScriptEnabled")
@Nullable
@Override
@ -114,18 +72,18 @@ public class WikipediaDialogFragment extends BaseOsmAndDialogFragment {
articleToolbarText = (TextView) mainView.findViewById(R.id.title_text_view);
ColorStateList buttonColorStateList = AndroidUtils.createPressedColorStateList(getContext(), darkMode,
ColorStateList buttonColorStateList = AndroidUtils.createPressedColorStateList(getContext(), nightMode,
R.color.ctx_menu_controller_button_text_color_light_n, R.color.ctx_menu_controller_button_text_color_light_p,
R.color.ctx_menu_controller_button_text_color_dark_n, R.color.ctx_menu_controller_button_text_color_dark_p);
ColorStateList selectedLangColorStateList = AndroidUtils.createPressedColorStateList(
getContext(), darkMode,
getContext(), nightMode,
R.color.icon_color, R.color.wikivoyage_active_light,
R.color.icon_color, R.color.wikivoyage_active_dark
);
readFullArticleButton = (TextView) mainView.findViewById(R.id.read_full_article);
readFullArticleButton.setBackgroundResource(darkMode ? R.drawable.bt_round_long_night : R.drawable.bt_round_long_day);
readFullArticleButton.setBackgroundResource(nightMode ? R.drawable.bt_round_long_night : R.drawable.bt_round_long_day);
readFullArticleButton.setTextColor(buttonColorStateList);
readFullArticleButton.setCompoundDrawablesWithIntrinsicBounds(getIcon(R.drawable.ic_world_globe_dark), null, null, null);
readFullArticleButton.setCompoundDrawablePadding((int) getResources().getDimension(R.dimen.content_padding_small));
@ -133,47 +91,30 @@ public class WikipediaDialogFragment extends BaseOsmAndDialogFragment {
int paddingRight = (int) getResources().getDimension(R.dimen.dialog_content_margin);
readFullArticleButton.setPadding(paddingLeft, 0, paddingRight, 0);
selectLanguageTextView = (TextView) mainView.findViewById(R.id.select_language_text_view);
selectLanguageTextView.setTextColor(selectedLangColorStateList);
selectLanguageTextView.setCompoundDrawablesWithIntrinsicBounds(getSelectedLangIcon(), null, null, null);
selectLanguageTextView.setBackgroundResource(darkMode
selectedLangTv = (TextView) mainView.findViewById(R.id.select_language_text_view);
selectedLangTv.setTextColor(selectedLangColorStateList);
selectedLangTv.setCompoundDrawablesWithIntrinsicBounds(getSelectedLangIcon(), null, null, null);
selectedLangTv.setBackgroundResource(nightMode
? R.drawable.wikipedia_select_lang_bg_dark_n : R.drawable.wikipedia_select_lang_bg_light_n);
contentWebView = (WebView) mainView.findViewById(R.id.content_web_view);
WebSettings webSettings = contentWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
contentWebView.setWebViewClient(new WikipediaWebViewClient(getActivity(), darkMode));
contentWebView.setWebViewClient(new WikipediaWebViewClient(getActivity(), nightMode));
updateWebSettings();
return mainView;
}
@Override
@NonNull
private Drawable getSelectedLangIcon() {
Drawable normal = getIcon(R.drawable.ic_action_map_language, R.color.icon_color);
if (Build.VERSION.SDK_INT >= 21) {
Drawable active = getActiveIcon(R.drawable.ic_action_map_language);
return AndroidUtils.createPressedStateListDrawable(normal, active);
}
return normal;
}
@NonNull
private String getBaseUrl() {
File wikivoyageDir = getMyApplication().getAppPath(IndexConstants.WIKIVOYAGE_INDEX_DIR);
if (new File(wikivoyageDir, "article_style.css").exists()) {
return "file://" + wikivoyageDir.getAbsolutePath() + "/";
}
return "file:///android_asset/";
}
@NonNull
private String createHtmlContent(@NonNull String article) {
protected String createHtmlContent() {
StringBuilder sb = new StringBuilder(HEADER_INNER);
String nightModeClass = darkMode ? " nightmode" : "";
String nightModeClass = nightMode ? " nightmode" : "";
sb.append("<div class=\"main");
sb.append(nightModeClass);
sb.append("\">\n");
sb.append("<h1>").append(title).append("</h1>");
sb.append(article);
sb.append(FOOTER_INNER);
return sb.toString();
@ -181,7 +122,7 @@ public class WikipediaDialogFragment extends BaseOsmAndDialogFragment {
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
populateWiki();
populateArticle();
}
@Override
@ -193,7 +134,8 @@ public class WikipediaDialogFragment extends BaseOsmAndDialogFragment {
super.onDestroyView();
}
private void populateWiki() {
@Override
protected void populateArticle() {
if (amenity != null) {
String preferredLanguage = lang;
if (TextUtils.isEmpty(preferredLanguage)) {
@ -206,26 +148,25 @@ public class WikipediaDialogFragment extends BaseOsmAndDialogFragment {
}
final String langSelected = lng;
final String title = amenity.getName(langSelected);
article = amenity.getDescription(langSelected);
title = amenity.getName(langSelected);
articleToolbarText.setText(title);
readFullArticleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String article = "https://" + langSelected.toLowerCase() + ".wikipedia.org/wiki/" + title.replace(' ', '_');
showFullArticle(getContext(), Uri.parse(article), darkMode);
showFullArticle(getContext(), Uri.parse(article), nightMode);
}
});
selectLanguageTextView.setText(Algorithms.capitalizeFirstLetter(langSelected));
selectLanguageTextView.setOnClickListener(new View.OnClickListener() {
selectedLangTv.setText(Algorithms.capitalizeFirstLetter(langSelected));
selectedLangTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showPopupLangMenu(selectLanguageTextView, langSelected);
showPopupLangMenu(selectedLangTv, langSelected);
}
});
String content = amenity.getDescription(langSelected);
contentWebView.loadDataWithBaseURL(getBaseUrl(), createHtmlContent(content), "text/html", "UTF-8", null);
contentWebView.loadDataWithBaseURL(getBaseUrl(), createHtmlContent(), "text/html", "UTF-8", null);
}
}
@ -236,7 +177,8 @@ public class WikipediaDialogFragment extends BaseOsmAndDialogFragment {
customTabsIntent.launchUrl(context, uri);
}
private void showPopupLangMenu(View view, final String langSelected) {
@Override
protected void showPopupLangMenu(View view, final String langSelected) {
final PopupMenu optionsMenu = new PopupMenu(getContext(), view, Gravity.RIGHT);
Set<String> namesSet = new TreeSet<>();
namesSet.addAll(amenity.getNames("content", "en"));
@ -258,7 +200,7 @@ public class WikipediaDialogFragment extends BaseOsmAndDialogFragment {
@Override
public boolean onMenuItemClick(MenuItem item) {
setLanguage(langSelected);
populateWiki();
populateArticle();
return true;
}
});
@ -269,7 +211,7 @@ public class WikipediaDialogFragment extends BaseOsmAndDialogFragment {
@Override
public boolean onMenuItemClick(MenuItem item) {
setLanguage(e.getKey());
populateWiki();
populateArticle();
return true;
}
});
@ -278,12 +220,7 @@ public class WikipediaDialogFragment extends BaseOsmAndDialogFragment {
}
private Drawable getIcon(int resId) {
int colorId = darkMode ? R.color.ctx_menu_controller_button_text_color_dark_n : R.color.ctx_menu_controller_button_text_color_light_n;
return getIcon(resId, colorId);
}
private Drawable getActiveIcon(int resId) {
int colorId = darkMode ? R.color.wikivoyage_active_dark : R.color.wikivoyage_active_light;
int colorId = nightMode ? R.color.ctx_menu_controller_button_text_color_dark_n : R.color.ctx_menu_controller_button_text_color_light_n;
return getIcon(resId, colorId);
}
@ -306,32 +243,15 @@ public class WikipediaDialogFragment extends BaseOsmAndDialogFragment {
}
}
private void updateWebSettings() {
OsmandSettings.WikivoyageShowImages showImages = getSettings().WIKIVOYAGE_SHOW_IMAGES.get();
WebSettings webSettings = contentWebView.getSettings();
switch (showImages) {
case ON:
webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
break;
case OFF:
webSettings.setCacheMode(WebSettings.LOAD_CACHE_ONLY);
break;
case WIFI:
webSettings.setCacheMode(getMyApplication().getSettings().isWifiConnected() ?
WebSettings.LOAD_DEFAULT : WebSettings.LOAD_CACHE_ONLY);
break;
}
}
public static boolean showInstance(AppCompatActivity activity, Amenity amenity) {
return showInstance(activity, amenity, null);
}
protected void setupToolbar(Toolbar toolbar) {
toolbar.setNavigationIcon(getIcon(R.drawable.ic_arrow_back, R.color.icon_color));
toolbar.setNavigationContentDescription(R.string.access_shared_string_navigate_up);
Menu menu = toolbar.getMenu();
super.setupToolbar(toolbar);
toolbar.setOverflowIcon(getIcon(R.drawable.ic_overflow_menu_white, R.color.icon_color));
Menu menu = toolbar.getMenu();
MenuItem.OnMenuItemClickListener itemClickListener = new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
@ -339,13 +259,13 @@ public class WikipediaDialogFragment extends BaseOsmAndDialogFragment {
if (app != null) {
int itemId = item.getItemId();
if (itemId == MENU_ITEM_SHOW_ID) {
app.getSettings().WIKIVOYAGE_SHOW_IMAGES.set(OsmandSettings.WikivoyageShowImages.ON);
app.getSettings().WIKI_ARTICLE_SHOW_IMAGES.set(OsmandSettings.WikiArticleShowImages.ON);
return true;
} else if (itemId == MENU_ITEM_WIFI_ID) {
app.getSettings().WIKIVOYAGE_SHOW_IMAGES.set(OsmandSettings.WikivoyageShowImages.WIFI);
app.getSettings().WIKI_ARTICLE_SHOW_IMAGES.set(OsmandSettings.WikiArticleShowImages.WIFI);
return true;
} else if (itemId == MENU_ITEM_NO_ID) {
app.getSettings().WIKIVOYAGE_SHOW_IMAGES.set(OsmandSettings.WikivoyageShowImages.OFF);
app.getSettings().WIKI_ARTICLE_SHOW_IMAGES.set(OsmandSettings.WikiArticleShowImages.OFF);
return true;
}
}
@ -353,28 +273,11 @@ public class WikipediaDialogFragment extends BaseOsmAndDialogFragment {
}
};
MenuItem itemShow = menu.add(0,MENU_ITEM_SHOW_ID,0,R.string.shared_string_show);
MenuItem itemShow = menu.add(0, MENU_ITEM_SHOW_ID, 0, R.string.shared_string_show);
itemShow.setOnMenuItemClickListener(itemClickListener);
MenuItem itemWifi = menu.add(0,MENU_ITEM_WIFI_ID,0,R.string.shared_string_only_with_wifi);
MenuItem itemWifi = menu.add(0, MENU_ITEM_WIFI_ID, 0, R.string.shared_string_only_with_wifi);
itemWifi.setOnMenuItemClickListener(itemClickListener);
MenuItem itemNo = menu.add(0,MENU_ITEM_NO_ID,0,R.string.shared_string_dont);
MenuItem itemNo = menu.add(0, MENU_ITEM_NO_ID, 0, R.string.shared_string_dont);
itemNo.setOnMenuItemClickListener(itemClickListener);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
}
@ColorRes
protected int getStatusBarColor() {
return darkMode ? R.color.status_bar_wikivoyage_article_dark : R.color.status_bar_wikivoyage_article_light;
}
@ColorInt
protected int getResolvedColor(@ColorRes int colorId) {
return ContextCompat.getColor(getContext(), colorId);
}
}

View file

@ -1,4 +1,4 @@
package net.osmand.plus.mapcontextmenu;
package net.osmand.plus.wikipedia;
import android.content.Context;
import android.content.DialogInterface;

View file

@ -22,9 +22,8 @@ import android.view.Window;
import net.osmand.plus.R;
import net.osmand.plus.base.BaseOsmAndDialogFragment;
import net.osmand.plus.wikivoyage.explore.WikivoyageExploreDialogFragment;
public class WikivoyageBaseDialogFragment extends BaseOsmAndDialogFragment {
public class WikiBaseDialogFragment extends BaseOsmAndDialogFragment {
protected boolean nightMode;

View file

@ -16,7 +16,7 @@ import android.widget.TextView;
import net.osmand.AndroidUtils;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings.WikivoyageShowImages;
import net.osmand.plus.OsmandSettings.WikiArticleShowImages;
import net.osmand.plus.R;
import net.osmand.plus.base.BottomSheetDialogFragment;
import net.osmand.plus.helpers.AndroidUiHelper;
@ -38,7 +38,7 @@ public class WikivoyageShowPicturesDialogFragment extends BottomSheetDialogFragm
public void onClick(View v) {
OsmandApplication app = getMyApplication();
if (app != null) {
app.getSettings().WIKIVOYAGE_SHOW_IMAGES.set(WikivoyageShowImages.OFF);
app.getSettings().WIKI_ARTICLE_SHOW_IMAGES.set(WikiArticleShowImages.OFF);
}
sendResult();
dismiss();
@ -52,7 +52,7 @@ public class WikivoyageShowPicturesDialogFragment extends BottomSheetDialogFragm
public void onClick(View v) {
OsmandApplication app = getMyApplication();
if (app != null) {
app.getSettings().WIKIVOYAGE_SHOW_IMAGES.set(WikivoyageShowImages.WIFI);
app.getSettings().WIKI_ARTICLE_SHOW_IMAGES.set(WikiArticleShowImages.WIFI);
}
sendResult();
dismiss();
@ -65,7 +65,7 @@ public class WikivoyageShowPicturesDialogFragment extends BottomSheetDialogFragm
public void onClick(View v) {
OsmandApplication app = getMyApplication();
if (app != null) {
app.getSettings().WIKIVOYAGE_SHOW_IMAGES.set(WikivoyageShowImages.ON);
app.getSettings().WIKI_ARTICLE_SHOW_IMAGES.set(WikiArticleShowImages.ON);
}
sendResult();
dismiss();

View file

@ -8,7 +8,7 @@ import net.osmand.plus.OsmandSettings;
public class WikivoyageUtils {
public static void setupNetworkPolicy(OsmandSettings settings, RequestCreator rc) {
switch (settings.WIKIVOYAGE_SHOW_IMAGES.get()) {
switch (settings.WIKI_ARTICLE_SHOW_IMAGES.get()) {
case ON:
break;
case OFF:

View file

@ -25,7 +25,7 @@ import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.mapcontextmenu.WikipediaDialogFragment;
import net.osmand.plus.wikipedia.WikipediaDialogFragment;
import net.osmand.plus.resources.AmenityIndexRepositoryBinary;
import net.osmand.plus.wikivoyage.article.WikivoyageArticleDialogFragment;
import net.osmand.plus.wikivoyage.article.WikivoyageArticleWikiLinkFragment;

View file

@ -15,7 +15,7 @@ import net.osmand.plus.R;
import net.osmand.plus.helpers.AndroidUiHelper;
import net.osmand.plus.wikivoyage.explore.WikivoyageExploreDialogFragment;
public class WikivoyageWelcomeDialogFragment extends WikivoyageBaseDialogFragment {
public class WikivoyageWelcomeDialogFragment extends WikiBaseDialogFragment {
public static final String TAG = WikivoyageWelcomeDialogFragment.class.getSimpleName();

View file

@ -4,7 +4,6 @@ import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
@ -14,7 +13,6 @@ import android.support.v4.app.FragmentManager.BackStackEntry;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
@ -29,12 +27,11 @@ import net.osmand.IndexConstants;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandPlugin;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.OsmandSettings.WikivoyageShowImages;
import net.osmand.plus.R;
import net.osmand.plus.activities.TrackActivity;
import net.osmand.plus.development.OsmandDevelopmentPlugin;
import net.osmand.plus.helpers.FileNameTranslationHelper;
import net.osmand.plus.wikivoyage.WikivoyageBaseDialogFragment;
import net.osmand.plus.wikipedia.WikiArticleBaseDialogFragment;
import net.osmand.plus.wikivoyage.WikivoyageShowPicturesDialogFragment;
import net.osmand.plus.wikivoyage.WikivoyageWebViewClient;
import net.osmand.plus.wikivoyage.data.TravelArticle;
@ -42,22 +39,18 @@ import net.osmand.plus.wikivoyage.data.TravelDbHelper;
import net.osmand.plus.wikivoyage.data.TravelLocalDataHelper;
import net.osmand.util.Algorithms;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static net.osmand.plus.OsmandSettings.WikivoyageShowImages.OFF;
import static net.osmand.plus.OsmandSettings.WikiArticleShowImages.OFF;
public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragment {
public class WikivoyageArticleDialogFragment extends WikiArticleBaseDialogFragment {
public static final String TAG = "WikivoyageArticleDialogFragment";
@ -67,66 +60,6 @@ public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragmen
private static final String LANGS_KEY = "langs_key";
private static final String SELECTED_LANG_KEY = "selected_lang_key";
private static final String HEADER_INNER = "<html><head>\n" +
"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n" +
"<meta http-equiv=\"cleartype\" content=\"on\" />\n" +
"<link href=\"article_style.css\" type=\"text/css\" rel=\"stylesheet\"/>\n" +
"<script type=\"text/javascript\">" +
"function showNavigation() {" +
" Android.showNavigation();" +
"}" +
"</script>" +
"</head>";
private static final String FOOTER_INNER = "<script>var coll = document.getElementsByTagName(\"H2\");" +
"var i;" +
"for (i = 0; i < coll.length; i++) {" +
" coll[i].addEventListener(\"click\", function() {" +
" this.classList.toggle(\"active\");" +
" var content = this.nextElementSibling;" +
" if (content.style.display === \"block\") {" +
" content.style.display = \"none\";" +
" } else {" +
" content.style.display = \"block\";" +
" }" +
" });" +
"}" +
"document.addEventListener(\"DOMContentLoaded\", function(event) {\n" +
" document.querySelectorAll('img').forEach(function(img) {\n" +
" img.onerror = function() {\n" +
" this.style.display = 'none';\n" +
" var caption = img.parentElement.nextElementSibling;\n" +
" if (caption.className == \"thumbnailcaption\") {\n" +
" caption.style.display = 'none';\n" +
" }\n" +
" };\n" +
" })\n" +
"});" +
"function scrollAnchor(id, title) {" +
"openContent(title);" +
"window.location.hash = id;}\n" +
"function openContent(id) {\n" +
" var doc = document.getElementById(id).parentElement;\n" +
" doc.classList.toggle(\"active\");\n" +
" var content = doc.nextElementSibling;\n" +
" content.style.display = \"block\";\n" +
" collapseActive(doc);" +
"}" +
"function collapseActive(doc) {" +
" var coll = document.getElementsByTagName(\"H2\");" +
" var i;" +
" for (i = 0; i < coll.length; i++) {" +
" var item = coll[i];" +
" if (item != doc && item.classList.contains(\"active\")) {" +
" item.classList.toggle(\"active\");" +
" var content = item.nextElementSibling;" +
" if (content.style.display === \"block\") {" +
" content.style.display = \"none\";" +
" }" +
" }" +
" }" +
"}</script>"
+ "</body></html>";
private static final String EMPTY_URL = "https://upload.wikimedia.org/wikipedia/commons/thumb/d/d4//";
private long cityId = NO_VALUE;
@ -135,11 +68,8 @@ public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragmen
private TravelArticle article;
private TextView trackButton;
private TextView selectedLangTv;
private TextView saveBtn;
private WebView contentWebView;
private TextView articleToolbarText;
private WikivoyageWebViewClient webViewClient;
@SuppressLint({"SetJavaScriptEnabled", "AddJavascriptInterface"})
@ -174,7 +104,7 @@ public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragmen
selectedLangTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showPopupLangMenu(v);
showPopupLangMenu(v, selectedLang);
}
});
@ -277,40 +207,18 @@ public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragmen
public void onResume() {
super.onResume();
OsmandSettings settings = getMyApplication().getSettings();
if (!settings.WIKIVOYAGE_SHOW_IMAGES_ASKED.get()) {
if (!settings.WIKI_ARTICLE_SHOW_IMAGES_ASKED.get()) {
FragmentActivity activity = getActivity();
FragmentManager fm = getFragmentManager();
if (activity != null && fm != null) {
WikivoyageShowPicturesDialogFragment fragment = new WikivoyageShowPicturesDialogFragment();
fragment.setTargetFragment(this, WikivoyageShowPicturesDialogFragment.SHOW_PICTURES_CHANGED_REQUEST_CODE);
fragment.show(fm, WikivoyageShowPicturesDialogFragment.TAG);
settings.WIKIVOYAGE_SHOW_IMAGES_ASKED.set(true);
settings.WIKI_ARTICLE_SHOW_IMAGES_ASKED.set(true);
}
}
}
@Override
protected int getStatusBarColor() {
return nightMode ? R.color.status_bar_wikivoyage_article_dark : R.color.status_bar_wikivoyage_article_light;
}
private void updateWebSettings() {
WikivoyageShowImages showImages = getSettings().WIKIVOYAGE_SHOW_IMAGES.get();
WebSettings webSettings = contentWebView.getSettings();
switch (showImages) {
case ON:
webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
break;
case OFF:
webSettings.setCacheMode(WebSettings.LOAD_CACHE_ONLY);
break;
case WIFI:
webSettings.setCacheMode(getMyApplication().getSettings().isWifiConnected() ?
WebSettings.LOAD_DEFAULT : WebSettings.LOAD_CACHE_ONLY);
break;
}
}
private void updateSaveButton() {
if (article != null) {
final TravelLocalDataHelper helper = getMyApplication().getTravelDbHelper().getLocalDataHelper();
@ -335,7 +243,8 @@ public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragmen
}
}
private void showPopupLangMenu(View view) {
@Override
protected void showPopupLangMenu(View view, String langSelected) {
if (langs == null) {
return;
}
@ -362,7 +271,7 @@ public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragmen
popup.show();
}
private void populateArticle() {
protected void populateArticle() {
if (cityId == NO_VALUE || langs == null) {
Bundle args = getArguments();
if (args != null) {
@ -392,15 +301,11 @@ public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragmen
updateSaveButton();
selectedLangTv.setText(Algorithms.capitalizeFirstLetter(selectedLang));
contentWebView.loadDataWithBaseURL(getBaseUrl(), createHtmlContent(article), "text/html", "UTF-8", null);
}
private void moveToAnchor(String id, String title) {
contentWebView.loadUrl("javascript:scrollAnchor(\"" + id + "\", \"" + title.trim() + "\")");
contentWebView.loadDataWithBaseURL(getBaseUrl(), createHtmlContent(), "text/html", "UTF-8", null);
}
@NonNull
private String createHtmlContent(@NonNull TravelArticle article) {
protected String createHtmlContent() {
StringBuilder sb = new StringBuilder(HEADER_INNER);
String[] rtlLanguages = new String[]{"ar","dv","he","iw","fa","nqo","ps","sd","ug","ur","yi"};
Set<String> rtls = new HashSet<>(Arrays.asList(rtlLanguages));
@ -428,7 +333,7 @@ public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragmen
}
}
String url = TravelArticle.getImageUrl(imageTitle, false);
if (!TextUtils.isEmpty(imageTitle) && getSettings().WIKIVOYAGE_SHOW_IMAGES.get() != OFF &&
if (!TextUtils.isEmpty(imageTitle) && getSettings().WIKI_ARTICLE_SHOW_IMAGES.get() != OFF &&
!url.startsWith(EMPTY_URL)) {
sb.append("<div class=\"title-image"+nightModeClass+"\" style=\"background-image: url(").append(url).append(")\"></div>");
}
@ -440,42 +345,11 @@ public class WikivoyageArticleDialogFragment extends WikivoyageBaseDialogFragmen
sb.append(article.getContent());
sb.append(FOOTER_INNER);
if (OsmandPlugin.getEnabledPlugin(OsmandDevelopmentPlugin.class) != null) {
writeOutHTML(sb);
writeOutHTML(sb, new File(getMyApplication().getAppPath(IndexConstants.WIKIVOYAGE_INDEX_DIR), "page.html"));
}
return sb.toString();
}
private void writeOutHTML(StringBuilder sb) {
File file = new File(getMyApplication().getAppPath(IndexConstants.WIKIVOYAGE_INDEX_DIR), "page.html");
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter(file));
writer.write(sb.toString());
writer.close();
} catch (IOException e) {
Log.w("ArticleDialog", e.getMessage(), e);
}
}
@NonNull
private String getBaseUrl() {
File wikivoyageDir = getMyApplication().getAppPath(IndexConstants.WIKIVOYAGE_INDEX_DIR);
if (new File(wikivoyageDir, "article_style.css").exists()) {
return "file://" + wikivoyageDir.getAbsolutePath() + "/";
}
return "file:///android_asset/";
}
@NonNull
private Drawable getSelectedLangIcon() {
Drawable normal = getContentIcon(R.drawable.ic_action_map_language);
if (Build.VERSION.SDK_INT >= 21) {
Drawable active = getActiveIcon(R.drawable.ic_action_map_language);
return AndroidUtils.createPressedStateListDrawable(normal, active);
}
return normal;
}
public static boolean showInstance(@NonNull OsmandApplication app,
@NonNull FragmentManager fm,
long cityId,

View file

@ -18,7 +18,7 @@ import net.osmand.plus.base.bottomsheetmenu.simpleitems.DividerHalfItem;
import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleDividerItem;
import net.osmand.plus.base.bottomsheetmenu.simpleitems.TitleItem;
import net.osmand.plus.download.DownloadActivity;
import net.osmand.plus.mapcontextmenu.WikipediaDialogFragment;
import net.osmand.plus.wikipedia.WikipediaDialogFragment;
import static net.osmand.plus.download.ui.SearchDialogFragment.SHOW_WIKI_KEY;

View file

@ -26,7 +26,7 @@ import net.osmand.plus.LockableViewPager;
import net.osmand.plus.R;
import net.osmand.plus.base.BaseOsmAndFragment;
import net.osmand.plus.download.DownloadIndexesThread;
import net.osmand.plus.wikivoyage.WikivoyageBaseDialogFragment;
import net.osmand.plus.wikivoyage.WikiBaseDialogFragment;
import net.osmand.plus.wikivoyage.data.TravelDbHelper;
import net.osmand.plus.wikivoyage.search.WikivoyageSearchDialogFragment;
@ -35,7 +35,7 @@ import java.util.Arrays;
import java.util.List;
public class WikivoyageExploreDialogFragment extends WikivoyageBaseDialogFragment implements DownloadIndexesThread.DownloadEvents {
public class WikivoyageExploreDialogFragment extends WikiBaseDialogFragment implements DownloadIndexesThread.DownloadEvents {
public static final String TAG = "WikivoyageExploreDialogFragment";

View file

@ -15,7 +15,7 @@ import android.webkit.WebView;
import net.osmand.PicassoUtils;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings;
import net.osmand.plus.OsmandSettings.WikivoyageShowImages;
import net.osmand.plus.OsmandSettings.WikiArticleShowImages;
import net.osmand.plus.R;
import net.osmand.plus.base.MenuBottomSheetDialogFragment;
import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem;
@ -41,7 +41,7 @@ public class WikivoyageOptionsBottomSheetDialogFragment extends MenuBottomSheetD
@Override
public void createMenuItems(Bundle savedInstanceState) {
final OsmandApplication app = getMyApplication();
final OsmandSettings.CommonPreference<WikivoyageShowImages> showImagesPref = app.getSettings().WIKIVOYAGE_SHOW_IMAGES;
final OsmandSettings.CommonPreference<WikiArticleShowImages> showImagesPref = app.getSettings().WIKI_ARTICLE_SHOW_IMAGES;
final TravelDbHelper dbHelper = app.getTravelDbHelper();
items.add(new TitleItem(getString(R.string.shared_string_options)));
@ -76,7 +76,7 @@ public class WikivoyageOptionsBottomSheetDialogFragment extends MenuBottomSheetD
@Override
public void onClick(View v) {
final PopupMenu popup = new PopupMenu(v.getContext(), v, Gravity.END);
for (final WikivoyageShowImages showImages : WikivoyageShowImages.values()) {
for (final WikiArticleShowImages showImages : WikiArticleShowImages.values()) {
MenuItem item = popup.getMenu().add(getString(showImages.name));
item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override

View file

@ -20,7 +20,7 @@ import android.widget.ProgressBar;
import net.osmand.ResultMatcher;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.wikivoyage.WikivoyageBaseDialogFragment;
import net.osmand.plus.wikivoyage.WikiBaseDialogFragment;
import net.osmand.plus.wikivoyage.article.WikivoyageArticleDialogFragment;
import net.osmand.plus.wikivoyage.data.TravelLocalDataHelper;
import net.osmand.plus.wikivoyage.data.WikivoyageSearchHistoryItem;
@ -29,7 +29,7 @@ import net.osmand.plus.wikivoyage.data.WikivoyageSearchResult;
import java.util.ArrayList;
import java.util.List;
public class WikivoyageSearchDialogFragment extends WikivoyageBaseDialogFragment {
public class WikivoyageSearchDialogFragment extends WikiBaseDialogFragment {
public static final String TAG = "WikivoyageSearchDialogFragment";