Merge pull request #5326 from osmandapp/WikipediaImprovements

Wikipedia with webview
This commit is contained in:
Alexander Sytnyk 2018-05-02 11:53:25 +03:00 committed by GitHub
commit 94e53b8ecb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 76 additions and 75 deletions

View file

@ -1,9 +1,13 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:osmand="http://schemas.android.com/apk/res-auto" xmlns:osmand="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:background="?attr/wikivoyage_bg_color"
android:layout_height="match_parent"> android:orientation="vertical">
<android.support.design.widget.AppBarLayout <android.support.design.widget.AppBarLayout
android:id="@+id/app_bar" android:id="@+id/app_bar"
@ -35,7 +39,8 @@
style="@style/Widget.AppCompat.ActionButton" style="@style/Widget.AppCompat.ActionButton"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
tools:src="@drawable/ic_arrow_back"/> tools:src="@drawable/ic_arrow_back"
android:contentDescription="@string/shared_string_back" />
<View <View
android:layout_width="0dp" android:layout_width="0dp"
@ -78,29 +83,29 @@
</android.support.design.widget.AppBarLayout> </android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView <FrameLayout
osmand:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="0dp"
android:layout_weight="1">
<TextView <WebView
android:id="@+id/content" android:id="@+id/content_web_view"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent" />
android:padding="@dimen/content_padding"/>
</android.support.v4.widget.NestedScrollView>
<net.osmand.plus.widgets.TextViewEx <net.osmand.plus.widgets.TextViewEx
osmand:typeface="@string/font_roboto_medium"
android:layout_margin="@dimen/content_padding"
android:id="@+id/read_full_article" android:id="@+id/read_full_article"
android:layout_gravity="bottom|center_horizontal"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
tools:background="@drawable/bt_round_long_night" android:layout_gravity="bottom|center_horizontal"
android:text="@string/read_full_article" android:layout_margin="@dimen/content_padding"
android:gravity="center_vertical" android:gravity="center_vertical"
android:text="@string/read_full_article"
android:textAllCaps="true" android:textAllCaps="true"
osmand:typeface="@string/font_roboto_medium"
tools:background="@drawable/bt_round_long_night"
tools:textColor="?attr/ctx_menu_controller_text_color" /> tools:textColor="?attr/ctx_menu_controller_text_color" />
</android.support.design.widget.CoordinatorLayout> </FrameLayout>
</LinearLayout>

View file

@ -1,53 +1,60 @@
package net.osmand.plus.mapcontextmenu; package net.osmand.plus.mapcontextmenu;
import android.annotation.SuppressLint;
import android.app.Dialog; import android.app.Dialog;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.content.res.ColorStateList; import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.net.Uri; import android.net.Uri;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.customtabs.CustomTabsIntent; import android.support.customtabs.CustomTabsIntent;
import android.support.design.widget.AppBarLayout; import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.app.DialogFragment; import android.support.v4.app.DialogFragment;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.PopupMenu; import android.support.v7.widget.PopupMenu;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.RelativeSizeSpan;
import android.view.Gravity; import android.view.Gravity;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageButton; import android.widget.ImageButton;
import android.widget.TextView; import android.widget.TextView;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.IndexConstants;
import net.osmand.data.Amenity; import net.osmand.data.Amenity;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.helpers.FileNameTranslationHelper; import net.osmand.plus.helpers.FileNameTranslationHelper;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import java.io.File;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.TreeSet; import java.util.TreeSet;
public class WikipediaDialogFragment extends DialogFragment { public class WikipediaDialogFragment extends DialogFragment {
public static final String TAG = "WikipediaDialogFragment"; 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 View mainView; private View mainView;
private WebView contentWebView;
private boolean darkMode; private boolean darkMode;
private Amenity amenity; private Amenity amenity;
private String lang; private String lang;
@ -79,6 +86,7 @@ public class WikipediaDialogFragment extends DialogFragment {
return dialog; return dialog;
} }
@SuppressLint("SetJavaScriptEnabled")
@Nullable @Nullable
@Override @Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
@ -108,29 +116,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); 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); 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.setBackgroundResource(darkMode ? R.drawable.bt_round_long_night : R.drawable.bt_round_long_day);
readFullArticleButton.setTextColor(buttonColorStateList); readFullArticleButton.setTextColor(buttonColorStateList);
int paddingLeft = (int) getResources().getDimension(R.dimen.wikipedia_button_left_padding); int paddingLeft = (int) getResources().getDimension(R.dimen.wikipedia_button_left_padding);
@ -144,10 +130,34 @@ public class WikipediaDialogFragment extends DialogFragment {
selectLanguageTextView.setCompoundDrawablesWithIntrinsicBounds(getIcon(R.drawable.ic_action_map_language), null, null, null); 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.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); 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; 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("<div class=\"main");
sb.append(nightModeClass);
sb.append("\">\n");
sb.append(article);
sb.append(FOOTER_INNER);
return sb.toString();
}
@Override @Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
populateWiki(); populateWiki();
@ -196,29 +206,15 @@ public class WikipediaDialogFragment extends DialogFragment {
}); });
String content = amenity.getDescription(langSelected); String content = amenity.getDescription(langSelected);
contentWebView.loadDataWithBaseURL(getBaseUrl(), createHtmlContent(content), "text/html", "UTF-8", null);
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());
} }
} }
public static void showFullArticle(Context context, Uri uri, boolean nightMode) { 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() CustomTabsIntent customTabsIntent = new CustomTabsIntent.Builder()
.setToolbarColor(ContextCompat.getColor(context, nightMode ? R.color.actionbar_dark_color : R.color.actionbar_light_color)) .setToolbarColor(ContextCompat.getColor(context, nightMode ? R.color.actionbar_dark_color : R.color.actionbar_light_color))
.build(); .build();
customTabsIntent.launchUrl(context, uri); customTabsIntent.launchUrl(context, uri);
} else {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(uri);
context.startActivity(i);
}
} }
private void showPopupLangMenu(View view, final String langSelected) { private void showPopupLangMenu(View view, final String langSelected) {