From 75598c7118bedddb3baa197b1aa4865f04d83645 Mon Sep 17 00:00:00 2001 From: Chumva Date: Tue, 1 May 2018 19:04:42 +0300 Subject: [PATCH 1/3] change textview to webview --- .../res/layout/wikipedia_dialog_fragment.xml | 55 +++++++------ .../WikipediaDialogFragment.java | 79 +++++++++++-------- 2 files changed, 77 insertions(+), 57 deletions(-) diff --git a/OsmAnd/res/layout/wikipedia_dialog_fragment.xml b/OsmAnd/res/layout/wikipedia_dialog_fragment.xml index 6164f93d9f..0c52bdaa95 100644 --- a/OsmAnd/res/layout/wikipedia_dialog_fragment.xml +++ b/OsmAnd/res/layout/wikipedia_dialog_fragment.xml @@ -1,9 +1,14 @@ - + + + - + android:layout_height="0dp" + android:layout_weight="1"> - + + - + android:layout_gravity="bottom|center_horizontal" + android:layout_margin="@dimen/content_padding" + android:gravity="center_vertical" + android:text="@string/read_full_article" + android:textAllCaps="true" + osmand:typeface="@string/font_roboto_medium" + tools:background="@drawable/bt_round_long_night" + tools:textColor="?attr/ctx_menu_controller_text_color" /> - + - \ No newline at end of file + \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/mapcontextmenu/WikipediaDialogFragment.java b/OsmAnd/src/net/osmand/plus/mapcontextmenu/WikipediaDialogFragment.java index 6a1b3958b7..4c819e5e03 100644 --- a/OsmAnd/src/net/osmand/plus/mapcontextmenu/WikipediaDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapcontextmenu/WikipediaDialogFragment.java @@ -1,5 +1,6 @@ package net.osmand.plus.mapcontextmenu; +import android.annotation.SuppressLint; import android.app.Dialog; import android.content.Context; import android.content.Intent; @@ -28,30 +29,49 @@ import android.view.LayoutInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; +import android.webkit.WebSettings; +import android.webkit.WebView; import android.widget.ImageButton; +import android.widget.LinearLayout; 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.OsmandPlugin; import net.osmand.plus.R; +import net.osmand.plus.development.OsmandDevelopmentPlugin; import net.osmand.plus.helpers.FileNameTranslationHelper; +import net.osmand.plus.wikivoyage.data.TravelArticle; import net.osmand.util.Algorithms; +import java.io.File; +import java.util.Arrays; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.TreeSet; +import static net.osmand.plus.OsmandSettings.WikivoyageShowImages.OFF; + public class WikipediaDialogFragment extends DialogFragment { public static final String TAG = "WikipediaDialogFragment"; + private static final String HEADER_INNER = "\n" + + "\n" + + "\n" + + "\n" + + ""; + private static final String FOOTER_INNER = ""; + private View mainView; private boolean darkMode; private Amenity amenity; private String lang; - + WebView contentWebView; public void setAmenity(Amenity amenity) { this.amenity = amenity; } @@ -79,6 +99,7 @@ public class WikipediaDialogFragment extends DialogFragment { return dialog; } + @SuppressLint("SetJavaScriptEnabled") @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { @@ -108,29 +129,7 @@ public class WikipediaDialogFragment extends DialogFragment { R.color.ctx_menu_controller_button_text_color_dark_n, R.color.ctx_menu_controller_button_text_color_dark_p); final TextView readFullArticleButton = (TextView) mainView.findViewById(R.id.read_full_article); - CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) readFullArticleButton.getLayoutParams(); - params.setBehavior(new CoordinatorLayout.Behavior() { - @Override - public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) { - return dependency instanceof AppBarLayout; - } - @Override - public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) { - if (dependency instanceof AppBarLayout) { - int readFullArticleButtonHeight = child.getMeasuredHeight(); - if (readFullArticleButtonHeight != 0) { - CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) child.getLayoutParams(); - int viewBottomMargin = lp.bottomMargin; - int distanceToScroll = child.getHeight() + viewBottomMargin; - float ratio = dependency.getY() / readFullArticleButtonHeight; - child.setTranslationY(-distanceToScroll * ratio); - } - } - return true; - } - }); - readFullArticleButton.setLayoutParams(params); readFullArticleButton.setBackgroundResource(darkMode ? R.drawable.bt_round_long_night : R.drawable.bt_round_long_day); readFullArticleButton.setTextColor(buttonColorStateList); int paddingLeft = (int) getResources().getDimension(R.dimen.wikipedia_button_left_padding); @@ -144,10 +143,34 @@ public class WikipediaDialogFragment extends DialogFragment { selectLanguageTextView.setCompoundDrawablesWithIntrinsicBounds(getIcon(R.drawable.ic_action_map_language), null, null, null); selectLanguageTextView.setCompoundDrawablePadding((int) getResources().getDimension(R.dimen.context_menu_padding_margin_small)); selectLanguageTextView.setBackgroundResource(darkMode ? R.drawable.wikipedia_select_lang_bg_dark : R.drawable.wikipedia_select_lang_bg_light); + contentWebView = (WebView) mainView.findViewById(R.id.content_web_view); + WebSettings webSettings = contentWebView.getSettings(); + webSettings.setJavaScriptEnabled(true); return mainView; } + @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) { + StringBuilder sb = new StringBuilder(HEADER_INNER); + + String nightModeClass = darkMode ? " nightmode" : ""; + + sb.append("
\n"); + sb.append(article); + sb.append(FOOTER_INNER); + return sb.toString(); + } + @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { populateWiki(); @@ -196,15 +219,7 @@ public class WikipediaDialogFragment extends DialogFragment { }); String content = amenity.getDescription(langSelected); - - TextView articleTextView = (TextView) mainView.findViewById(R.id.content); - Spannable spannableContent = new SpannableString(Html.fromHtml(content)); - int length = spannableContent.length(); - spannableContent.setSpan(new RelativeSizeSpan(1.2f), 0, length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); - int contentTextColor = ContextCompat.getColor(getContext(), darkMode ? R.color.ctx_menu_bottom_view_text_color_dark : R.color.ctx_menu_bottom_view_text_color_light); - articleTextView.setTextColor(contentTextColor); - articleTextView.setText(spannableContent); - articleTextView.setMovementMethod(LinkMovementMethod.getInstance()); + contentWebView.loadDataWithBaseURL(getBaseUrl(), createHtmlContent(content), "text/html", "UTF-8", null); } } From 9ff75223353893d40175f59e814535d0f0523a69 Mon Sep 17 00:00:00 2001 From: Chumva Date: Wed, 2 May 2018 11:21:55 +0300 Subject: [PATCH 2/3] removed unused imports and refactored showFullArticle --- .../res/layout/wikipedia_dialog_fragment.xml | 4 +- .../WikipediaDialogFragment.java | 39 +++++-------------- 2 files changed, 12 insertions(+), 31 deletions(-) diff --git a/OsmAnd/res/layout/wikipedia_dialog_fragment.xml b/OsmAnd/res/layout/wikipedia_dialog_fragment.xml index 0c52bdaa95..cef5e87ece 100644 --- a/OsmAnd/res/layout/wikipedia_dialog_fragment.xml +++ b/OsmAnd/res/layout/wikipedia_dialog_fragment.xml @@ -9,7 +9,6 @@ android:background="?attr/wikivoyage_bg_color" android:orientation="vertical"> - + tools:src="@drawable/ic_arrow_back" + android:contentDescription="@string/shared_string_back" /> \n"); + sb.append("
\n"); sb.append(article); sb.append(FOOTER_INNER); return sb.toString(); @@ -224,16 +211,10 @@ public class WikipediaDialogFragment extends DialogFragment { } public static void showFullArticle(Context context, Uri uri, boolean nightMode) { - if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH) { - CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder() - .setToolbarColor(ContextCompat.getColor(context, nightMode ? R.color.actionbar_dark_color : R.color.actionbar_light_color)) - .build(); - customTabsIntent.launchUrl(context, uri); - } else { - Intent i = new Intent(Intent.ACTION_VIEW); - i.setData(uri); - context.startActivity(i); - } + CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder() + .setToolbarColor(ContextCompat.getColor(context, nightMode ? R.color.actionbar_dark_color : R.color.actionbar_light_color)) + .build(); + customTabsIntent.launchUrl(context, uri); } private void showPopupLangMenu(View view, final String langSelected) { From 394206b5eb29b575cc47d8f77368d80708a1e7d9 Mon Sep 17 00:00:00 2001 From: Chumva Date: Wed, 2 May 2018 11:38:55 +0300 Subject: [PATCH 3/3] fix empty space in xml --- OsmAnd/res/layout/wikipedia_dialog_fragment.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OsmAnd/res/layout/wikipedia_dialog_fragment.xml b/OsmAnd/res/layout/wikipedia_dialog_fragment.xml index cef5e87ece..ea3267d555 100644 --- a/OsmAnd/res/layout/wikipedia_dialog_fragment.xml +++ b/OsmAnd/res/layout/wikipedia_dialog_fragment.xml @@ -40,7 +40,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" tools:src="@drawable/ic_arrow_back" - android:contentDescription="@string/shared_string_back" /> + android:contentDescription="@string/shared_string_back" />