diff --git a/OsmAnd/res/layout/wikipedia_dialog_fragment.xml b/OsmAnd/res/layout/wikipedia_dialog_fragment.xml index 5bc645d6b2..db6b07919f 100644 --- a/OsmAnd/res/layout/wikipedia_dialog_fragment.xml +++ b/OsmAnd/res/layout/wikipedia_dialog_fragment.xml @@ -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" /> diff --git a/OsmAnd/src/net/osmand/plus/OsmandSettings.java b/OsmAnd/src/net/osmand/plus/OsmandSettings.java index fbe4f3e981..0b7f387a4a 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandSettings.java +++ b/OsmAnd/src/net/osmand/plus/OsmandSettings.java @@ -717,8 +717,8 @@ public class OsmandSettings { public final CommonPreference SHOW_LINES_TO_FIRST_MARKERS = new BooleanPreference("show_lines_to_first_markers", false).makeProfile(); public final CommonPreference SHOW_ARROWS_TO_FIRST_MARKERS = new BooleanPreference("show_arrows_to_first_markers", false).makeProfile(); - public final CommonPreference WIKIVOYAGE_SHOW_IMAGES_ASKED = new BooleanPreference("wikivoyage_show_images_asked", false).makeGlobal(); - public final CommonPreference WIKIVOYAGE_SHOW_IMAGES = new EnumIntPreference<>("wikivoyage_show_imgs", WikivoyageShowImages.OFF, WikivoyageShowImages.values()).makeGlobal(); + public final CommonPreference WIKI_ARTICLE_SHOW_IMAGES_ASKED = new BooleanPreference("wikivoyage_show_images_asked", false).makeGlobal(); + public final CommonPreference WIKI_ARTICLE_SHOW_IMAGES = new EnumIntPreference<>("wikivoyage_show_imgs", WikiArticleShowImages.OFF, WikiArticleShowImages.values()).makeGlobal(); public final CommonPreference 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; } } diff --git a/OsmAnd/src/net/osmand/plus/development/SettingsDevelopmentActivity.java b/OsmAnd/src/net/osmand/plus/development/SettingsDevelopmentActivity.java index 190452c22a..bece3ea19a 100644 --- a/OsmAnd/src/net/osmand/plus/development/SettingsDevelopmentActivity.java +++ b/OsmAnd/src/net/osmand/plus/development/SettingsDevelopmentActivity.java @@ -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; diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/AmenityMenuBuilder.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/AmenityMenuBuilder.java index f4259f7dd0..d4d74333e4 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/AmenityMenuBuilder.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/builders/AmenityMenuBuilder.java @@ -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; diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/AmenityMenuController.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/AmenityMenuController.java index ae4474d3a0..9c5d48894a 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/AmenityMenuController.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/controllers/AmenityMenuController.java @@ -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; diff --git a/OsmAnd/src/net/osmand/plus/wikipedia/WikiArticleBaseDialogFragment.java b/OsmAnd/src/net/osmand/plus/wikipedia/WikiArticleBaseDialogFragment.java new file mode 100644 index 0000000000..9bf4103a5a --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/wikipedia/WikiArticleBaseDialogFragment.java @@ -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 = "\n" + + "\n" + + "\n" + + "\n" + + "" + + ""; + protected static final String FOOTER_INNER = "" + + ""; + + 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(); +} diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/WikipediaDialogFragment.java b/OsmAnd/src/net/osmand/plus/wikipedia/WikipediaDialogFragment.java similarity index 58% rename from OsmAnd/src/net/osmand/plus/mapcontextmenu/WikipediaDialogFragment.java rename to OsmAnd/src/net/osmand/plus/wikipedia/WikipediaDialogFragment.java index cb00eb78f5..f78f23be54 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/WikipediaDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikipedia/WikipediaDialogFragment.java @@ -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 = "\n" + - "\n" + - "\n" + - "\n" + - ""; - private static final String FOOTER_INNER = ""; - 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("
\n"); + sb.append("

").append(title).append("

"); 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 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); } } diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/WikipediaWebViewClient.java b/OsmAnd/src/net/osmand/plus/wikipedia/WikipediaWebViewClient.java similarity index 97% rename from OsmAnd/src/net/osmand/plus/mapcontextmenu/WikipediaWebViewClient.java rename to OsmAnd/src/net/osmand/plus/wikipedia/WikipediaWebViewClient.java index a4c336ec70..7ea63b8184 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/WikipediaWebViewClient.java +++ b/OsmAnd/src/net/osmand/plus/wikipedia/WikipediaWebViewClient.java @@ -1,4 +1,4 @@ -package net.osmand.plus.mapcontextmenu; +package net.osmand.plus.wikipedia; import android.content.Context; import android.content.DialogInterface; diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageBaseDialogFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/WikiBaseDialogFragment.java similarity index 95% rename from OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageBaseDialogFragment.java rename to OsmAnd/src/net/osmand/plus/wikivoyage/WikiBaseDialogFragment.java index f76689c45e..8a24895151 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageBaseDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/WikiBaseDialogFragment.java @@ -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; diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageShowPicturesDialogFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageShowPicturesDialogFragment.java index 6811e23da9..4a15189776 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageShowPicturesDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageShowPicturesDialogFragment.java @@ -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(); diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageUtils.java b/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageUtils.java index 70f500d176..42bb7f8f5a 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageUtils.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageUtils.java @@ -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: diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageWebViewClient.java b/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageWebViewClient.java index 74982c22e1..f10c962121 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageWebViewClient.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageWebViewClient.java @@ -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; diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageWelcomeDialogFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageWelcomeDialogFragment.java index bef0832780..a69ff557ec 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageWelcomeDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/WikivoyageWelcomeDialogFragment.java @@ -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(); diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleDialogFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleDialogFragment.java index d110935b41..98cd31fe51 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleDialogFragment.java @@ -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 = "\n" + - "\n" + - "\n" + - "\n" + - "" + - ""; - private static final String FOOTER_INNER = "" - + ""; - 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 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("
"); } @@ -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, diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleWikiLinkFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleWikiLinkFragment.java index 1d30bdc1af..e53b346086 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleWikiLinkFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/article/WikivoyageArticleWikiLinkFragment.java @@ -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; diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/WikivoyageExploreDialogFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/WikivoyageExploreDialogFragment.java index fcc50fa190..8a35f9cb55 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/WikivoyageExploreDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/WikivoyageExploreDialogFragment.java @@ -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"; diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/WikivoyageOptionsBottomSheetDialogFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/WikivoyageOptionsBottomSheetDialogFragment.java index 60300873ab..acec066b68 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/explore/WikivoyageOptionsBottomSheetDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/explore/WikivoyageOptionsBottomSheetDialogFragment.java @@ -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 showImagesPref = app.getSettings().WIKIVOYAGE_SHOW_IMAGES; + final OsmandSettings.CommonPreference 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 diff --git a/OsmAnd/src/net/osmand/plus/wikivoyage/search/WikivoyageSearchDialogFragment.java b/OsmAnd/src/net/osmand/plus/wikivoyage/search/WikivoyageSearchDialogFragment.java index 25a7266da4..ed71622482 100644 --- a/OsmAnd/src/net/osmand/plus/wikivoyage/search/WikivoyageSearchDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/wikivoyage/search/WikivoyageSearchDialogFragment.java @@ -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";