Wiki links menu and behavior improvements

This commit is contained in:
PaulStets 2018-04-23 14:18:42 +03:00
parent c4dc53f74d
commit 5446665d88
4 changed files with 128 additions and 219 deletions

View file

@ -1,160 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:osmand="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<View
android:id="@+id/upper_row_divider"
android:layout_width="wrap_content"
android:layout_height="1dp"
android:background="?attr/wikivoyage_card_divider_color"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/wikivoyage_card_bg_color">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="@dimen/bottom_sheet_content_padding_small"
android:paddingTop="@dimen/content_padding">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/selectableItemBackground"
android:paddingBottom="@dimen/content_padding"
android:id="@+id/download_wikimaps">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/content_padding"
android:layout_marginRight="@dimen/content_padding">
<ImageView
android:id="@+id/icon_download"
android:layout_width="28dp"
android:layout_height="28dp"
android:layout_marginEnd="@dimen/content_padding"
android:layout_marginRight="@dimen/content_padding"
tools:ignore="ContentDescription"
tools:src="@drawable/ic_action_import"
tools:visibility="visible"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<net.osmand.plus.widgets.TextViewEx
android:id="@+id/title_download"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/context_menu_padding_margin_medium"
android:ellipsize="end"
android:maxLines="1"
android:text="@string/download_wikipedia_label"
android:textColor="?android:textColorPrimary"
android:textSize="@dimen/empty_state_text_size"
osmand:typeface="@string/font_roboto_regular"/>
<TextView
android:id="@+id/content_download"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="3"
android:textAppearance="@style/TextAppearance.ContextMenuSubtitle"
tools:text="Download the Wikipedia articles for North Holland to read them offline."/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<View
android:id="@+id/row_divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?attr/wikivoyage_card_divider_color"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="@dimen/content_padding"
android:background="?attr/selectableItemBackground"
android:id="@+id/view_in_browser">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/content_padding"
android:layout_marginRight="@dimen/content_padding">
<ImageView
android:id="@+id/icon_browser"
android:layout_width="28dp"
android:layout_height="28dp"
android:layout_marginEnd="@dimen/content_padding"
android:layout_marginRight="@dimen/content_padding"
tools:ignore="ContentDescription"
tools:src="@drawable/ic_browse_map"
tools:visibility="visible"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<net.osmand.plus.widgets.TextViewEx
android:id="@+id/title_browser"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/context_menu_padding_margin_medium"
android:ellipsize="end"
android:maxLines="1"
android:text="@string/open_in_browser_wiki"
android:textColor="?android:textColorPrimary"
android:textSize="@dimen/empty_state_text_size"
osmand:typeface="@string/font_roboto_regular"/>
<TextView
android:id="@+id/content_browser"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="3"
android:text="@string/open_in_browser_wiki_description"
android:textAppearance="@style/TextAppearance.ContextMenuSubtitle"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</FrameLayout>
<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?attr/wikivoyage_card_divider_color"/>
<include
android:id="@+id/shadow"
layout="@layout/card_bottom_divider"
android:visibility="gone"
tools:visibility="visible"/>
</LinearLayout>

View file

@ -2856,4 +2856,7 @@
<string name="download_wikipedia_label">Download Wikipedia data</string>
<string name="open_in_browser_wiki">Open article online</string>
<string name="open_in_browser_wiki_description">View this article in a browser.</string>
<string name="download_wiki_region_placeholder">this region</string>
<string name="wiki_article_search_text">Searching for the necessary wiki article</string>
<string name="wiki_article_not_found">Article not found</string>
</resources>

View file

@ -1,12 +1,15 @@
package net.osmand.plus.wikivoyage;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AlertDialog;
@ -14,21 +17,20 @@ import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import net.osmand.binary.BinaryMapDataObject;
import net.osmand.binary.BinaryMapIndexReader;
import net.osmand.data.Amenity;
import net.osmand.data.PointDescription;
import net.osmand.data.QuadRect;
import net.osmand.map.OsmandRegions;
import net.osmand.osm.PoiCategory;
import net.osmand.plus.GPXUtilities;
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.dialogs.ProgressDialogFragment;
import net.osmand.plus.mapcontextmenu.WikipediaDialogFragment;
import net.osmand.plus.resources.AmenityIndexRepository;
import net.osmand.plus.quickaction.actions.FavoriteAction;
import net.osmand.plus.resources.AmenityIndexRepositoryBinary;
import net.osmand.plus.wikivoyage.article.WikivoyageArticleDialogFragment;
import net.osmand.plus.wikivoyage.article.WikivoyageArticleWikiLinkFragment;
@ -39,10 +41,9 @@ import net.osmand.util.MapUtils;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.ref.WeakReference;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import static android.support.v4.app.FragmentManager.POP_BACK_STACK_INCLUSIVE;
@ -54,7 +55,6 @@ import static android.support.v4.app.FragmentManager.POP_BACK_STACK_INCLUSIVE;
public class WikivoyageWebViewClient extends WebViewClient {
private static final String TAG = WikivoyageWebViewClient.class.getSimpleName();
private static final String REGIONS_OCBF = "regions.ocbf";
private OsmandApplication app;
private FragmentManager fragmentManager;
@ -172,7 +172,6 @@ public class WikivoyageWebViewClient extends WebViewClient {
}
protected void getWikiArticle(String name, String url) {
List<Amenity> found = new ArrayList<>();
List<AmenityIndexRepositoryBinary> indexes = app.getResourceManager()
.getWikiAmenityRepository(article.getLat(), article.getLon());
if (indexes.isEmpty()) {
@ -184,27 +183,77 @@ public class WikivoyageWebViewClient extends WebViewClient {
}
WikivoyageArticleWikiLinkFragment.showInstance(fragmentManager, reg, url);
} else {
for (AmenityIndexRepositoryBinary repo : indexes) {
found.addAll(repo.searchAmenitiesByName(0, 0, 0, 0,
Integer.MAX_VALUE, Integer.MAX_VALUE, name, null));
}
WikipediaDialogFragment.showInstance((AppCompatActivity) context, found.get(0));
new WikiArticleSearchTask(name, indexes, (MapActivity) context).execute();
}
}
private String getRegion(double lat, double lon) throws IOException {
OsmandRegions regions = new OsmandRegions();
regions.prepareFile(app.getAppPath(REGIONS_OCBF).getAbsolutePath());
regions.cacheAllCountries();
int x31 = MapUtils.get31TileNumberX(lon);
int y31 = MapUtils.get31TileNumberY(lat);
List<BinaryMapDataObject> cs = regions.query(x31, y31);
for (BinaryMapDataObject b : cs) {
if(regions.contain(b, x31, y31)) {
return regions.getFullName(b);
OsmandRegions osmandRegions = app.getRegions();
if (osmandRegions != null) {
int x31 = MapUtils.get31TileNumberX(lon);
int y31 = MapUtils.get31TileNumberY(lat);
List<BinaryMapDataObject> cs = osmandRegions.query(x31, y31);
for (BinaryMapDataObject b : cs) {
if(osmandRegions.contain(b, x31, y31)) {
return osmandRegions.getLocaleName(osmandRegions.getDownloadName(b), false);
}
}
}
return "";
}
private static class WikiArticleSearchTask extends AsyncTask<Void, Void, List<Amenity>> {
private ProgressDialog dialog;
private String name;
private List<AmenityIndexRepositoryBinary> indexes;
private WeakReference<MapActivity> weakContext;
WikiArticleSearchTask(String articleName, List<AmenityIndexRepositoryBinary> indexes, MapActivity context) {
name = articleName;
this.indexes = indexes;
weakContext = new WeakReference<>(context);
dialog = createProgressDialog();
}
@Override
protected void onPreExecute() {
if (dialog != null) {
dialog.show();
}
}
@Override
protected List<Amenity> doInBackground(Void... voids) {
List<Amenity> found = new ArrayList<>();
for (AmenityIndexRepositoryBinary repo : indexes) {
found.addAll(repo.searchAmenitiesByName(0, 0, 0, 0,
Integer.MAX_VALUE, Integer.MAX_VALUE, name, null));
}
return found;
}
@Override
protected void onPostExecute(List<Amenity> found) {
if (!weakContext.get().isActivityDestroyed()) {
dialog.dismiss();
if (!found.isEmpty()) {
WikipediaDialogFragment.showInstance((AppCompatActivity) weakContext.get(), found.get(0));
} else {
Toast.makeText(weakContext.get(), R.string.wiki_article_not_found, Toast.LENGTH_LONG).show();
}
}
}
private ProgressDialog createProgressDialog() {
MapActivity activity = weakContext.get();
if (!activity.isActivityDestroyed()) {
ProgressDialog dialog = new ProgressDialog(activity);
dialog.setCancelable(false);
dialog.setMessage(activity.getString(R.string.wiki_article_search_text));
return dialog;
}
return null;
}
}
}

View file

@ -2,15 +2,12 @@ package net.osmand.plus.wikivoyage.article;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
@ -19,9 +16,15 @@ import android.widget.TextView;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.base.MenuBottomSheetDialogFragment;
import net.osmand.plus.base.bottomsheetmenu.BaseBottomSheetItem;
import net.osmand.plus.base.bottomsheetmenu.BottomSheetItemWithDescription;
import net.osmand.plus.base.bottomsheetmenu.SimpleBottomSheetItem;
import net.osmand.plus.base.bottomsheetmenu.simpleitems.DescriptionItem;
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.mapcontextmenu.WikipediaDialogFragment;
import net.osmand.plus.osmedit.OsmEditsFragment;
public class WikivoyageArticleWikiLinkFragment extends MenuBottomSheetDialogFragment {
@ -49,44 +52,58 @@ public class WikivoyageArticleWikiLinkFragment extends MenuBottomSheetDialogFrag
wikiRegion = args.getString(WIKI_REGION);
}
}
items.add(new TitleItem("How to open Wikipedia articles?\n" + articleUrl));
final int themeRes = nightMode ? R.style.OsmandDarkTheme : R.style.OsmandLightTheme;
final View wikiLinkDialog = View.inflate(new ContextThemeWrapper(getContext(), themeRes),
R.layout.wikivoyage_wikipedia_link_dialog, null);
items.add(new TitleItem("How to open Wikipedia articles?"));
LinearLayout downloadWikiSection = wikiLinkDialog.findViewById(R.id.download_wikimaps);
LinearLayout openOnlineSection = wikiLinkDialog.findViewById(R.id.view_in_browser);
BaseBottomSheetItem wikiLinkitem = new TitleItem.Builder().setTitle(articleUrl)
.setTitleColorId(nightMode
? R.color.wikivoyage_contents_parent_icon_dark : R.color.wikivoyage_contents_parent_icon_light)
.setLayoutId(R.layout.bottom_sheet_item_title)
.create();
items.add(wikiLinkitem);
items.add(new TitleDividerItem(getContext()));
downloadWikiSection.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MapActivity mapActivity = (MapActivity) getActivity();
Intent newIntent = new Intent(mapActivity, mapActivity.getMyApplication().getAppCustomization()
.getDownloadActivity());
newIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
mapActivity.startActivity(newIntent);
}
});
Drawable downloadIcon = getIcon(R.drawable.ic_action_import, nightMode
? R.color.wikivoyage_contents_parent_icon_dark : R.color.wikivoyage_contents_parent_icon_light);
openOnlineSection.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
WikipediaDialogFragment.showFullArticle(getContext(), Uri.parse(articleUrl), nightMode);
}
});
Drawable viewOnlineIcon = getIcon(R.drawable.ic_world_globe_dark, nightMode
? R.color.wikivoyage_contents_parent_icon_dark : R.color.wikivoyage_contents_parent_icon_light);
TextView tw = wikiLinkDialog.findViewById(R.id.content_download);
tw.setText(ctx.getString(R.string.download_wikipedia_description, wikiRegion));
BaseBottomSheetItem wikiDownloadItem = new BottomSheetItemWithDescription.Builder()
.setDescription(getString(R.string.download_wikipedia_description, wikiRegion.isEmpty() ?
getString(R.string.download_wiki_region_placeholder) : wikiRegion))
.setIcon(downloadIcon)
.setTitle(getString(R.string.download_wikipedia_label))
.setLayoutId(R.layout.bottom_sheet_item_with_descr_56dp)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MapActivity mapActivity = (MapActivity) getActivity();
Intent newIntent = new Intent(mapActivity, mapActivity.getMyApplication().getAppCustomization()
.getDownloadActivity());
newIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
mapActivity.startActivity(newIntent);
dismiss();
}
})
.create();
items.add(wikiDownloadItem);
ImageView downloadWiki = wikiLinkDialog.findViewById(R.id.icon_download);
ImageView openInBrowser = wikiLinkDialog.findViewById(R.id.icon_browser);
items.add(new DividerHalfItem(getContext()));
downloadWiki.setImageDrawable(getIcon(R.drawable.ic_action_import, nightMode
? R.color.wikivoyage_contents_parent_icon_dark : R.color.wikivoyage_contents_parent_icon_light));
openInBrowser.setImageDrawable(getIcon(R.drawable.ic_browse_map, nightMode
? R.color.wikivoyage_contents_parent_icon_dark : R.color.wikivoyage_contents_parent_icon_light));
items.add(new SimpleBottomSheetItem.Builder().setCustomView(wikiLinkDialog).create());
BaseBottomSheetItem wikiArticleOnlineItem = new BottomSheetItemWithDescription.Builder()
.setDescription(getString(R.string.open_in_browser_wiki_description))
.setIcon(viewOnlineIcon)
.setTitle(getString(R.string.open_in_browser_wiki))
.setLayoutId(R.layout.bottom_sheet_item_with_descr_56dp)
.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
WikipediaDialogFragment.showFullArticle(getContext(), Uri.parse(articleUrl), nightMode);
dismiss();
}
})
.create();
items.add(wikiArticleOnlineItem);
}
@Override