Search in sources :

Example 1 with WikivoyageWebViewClient

use of net.osmand.plus.wikivoyage.WikivoyageWebViewClient in project Osmand by osmandapp.

the class WikivoyageArticleDialogFragment method onCreateView.

@SuppressLint({ "SetJavaScriptEnabled", "AddJavascriptInterface" })
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    if (savedInstanceState != null) {
        selectedLang = savedInstanceState.getString(SELECTED_LANG_KEY);
    } else {
        Bundle args = getArguments();
        if (args != null) {
            selectedLang = args.getString(SELECTED_LANG_KEY);
        }
    }
    final View mainView = inflate(R.layout.fragment_wikivoyage_article_dialog, container);
    setupToolbar((Toolbar) mainView.findViewById(R.id.toolbar));
    int appBarTextColor = nightMode ? R.color.wikivoyage_app_bar_text_dark : R.color.wikivoyage_app_bar_text_light;
    articleToolbarText = (TextView) mainView.findViewById(R.id.article_toolbar_text);
    articleToolbarText.setTextColor(ContextCompat.getColor(getContext(), appBarTextColor));
    ColorStateList selectedLangColorStateList = AndroidUtils.createPressedColorStateList(getContext(), nightMode, R.color.icon_color_default_light, R.color.wikivoyage_active_light, R.color.icon_color_default_dark, R.color.wikivoyage_active_dark);
    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);
    selectedLangTv.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            showPopupLangMenu(v, selectedLang);
        }
    });
    TextView contentsBtn = (TextView) mainView.findViewById(R.id.contents_button);
    contentsBtn.setCompoundDrawablesWithIntrinsicBounds(getActiveIcon(R.drawable.ic_action_contents), null, null, null);
    contentsBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            FragmentManager fm = getFragmentManager();
            if (article == null || fm == null) {
                return;
            }
            Bundle args = new Bundle();
            args.putString(WikivoyageArticleContentsFragment.CONTENTS_JSON_KEY, article.getContentsJson());
            WikivoyageArticleContentsFragment fragment = new WikivoyageArticleContentsFragment();
            fragment.setUsedOnMap(false);
            fragment.setArguments(args);
            fragment.setTargetFragment(WikivoyageArticleDialogFragment.this, WikivoyageArticleContentsFragment.SHOW_CONTENT_ITEM_REQUEST_CODE);
            fragment.show(fm, WikivoyageArticleContentsFragment.TAG);
        }
    });
    trackButton = (TextView) mainView.findViewById(R.id.gpx_button);
    trackButton.setCompoundDrawablesWithIntrinsicBounds(getActiveIcon(R.drawable.ic_action_markers_dark), null, null, null);
    trackButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            FragmentActivity activity = getActivity();
            FragmentManager fm = getFragmentManager();
            if (article == null || activity == null || fm == null) {
                return;
            }
            if (activity instanceof WikivoyageExploreActivity) {
                WikivoyageExploreActivity exploreActivity = (WikivoyageExploreActivity) activity;
                exploreActivity.setArticle(article);
            }
            TravelHelper travelHelper = getMyApplication().getTravelHelper();
            File file = travelHelper.createGpxFile(article);
            openTrack(activity, new File(file.getAbsolutePath()), null, getString(R.string.icon_group_travel), TrackMenuType.POINTS);
        }
    });
    trackButton.setVisibility(View.GONE);
    gpxProgress = mainView.findViewById(R.id.gpx_progress);
    gpxProgress.setVisibility(View.GONE);
    saveBtn = (TextView) mainView.findViewById(R.id.save_button);
    contentWebView = (WebView) mainView.findViewById(R.id.content_web_view);
    WebSettings webSettings = contentWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setTextZoom((int) (getResources().getConfiguration().fontScale * 100f));
    updateWebSettings();
    contentWebView.addJavascriptInterface(new WikivoyageArticleWebAppInterface(), "Android");
    FragmentActivity activity = requireActivity();
    FragmentManager fragmentManager = requireFragmentManager();
    webViewClient = new WikivoyageWebViewClient(activity, fragmentManager, nightMode);
    contentWebView.setWebViewClient(webViewClient);
    contentWebView.setBackgroundColor(ContextCompat.getColor(getMyApplication(), nightMode ? R.color.wiki_webview_background_dark : R.color.wiki_webview_background_light));
    return mainView;
}
Also used : Bundle(android.os.Bundle) ColorStateList(android.content.res.ColorStateList) View(android.view.View) WebView(android.webkit.WebView) TextView(android.widget.TextView) SuppressLint(android.annotation.SuppressLint) FragmentManager(androidx.fragment.app.FragmentManager) FragmentActivity(androidx.fragment.app.FragmentActivity) WikivoyageWebViewClient(net.osmand.plus.wikivoyage.WikivoyageWebViewClient) WikivoyageExploreActivity(net.osmand.plus.wikivoyage.explore.WikivoyageExploreActivity) WebSettings(android.webkit.WebSettings) TextView(android.widget.TextView) GPXFile(net.osmand.GPXUtilities.GPXFile) File(java.io.File) TravelHelper(net.osmand.plus.wikivoyage.data.TravelHelper) SuppressLint(android.annotation.SuppressLint) Nullable(androidx.annotation.Nullable)

Aggregations

SuppressLint (android.annotation.SuppressLint)1 ColorStateList (android.content.res.ColorStateList)1 Bundle (android.os.Bundle)1 View (android.view.View)1 WebSettings (android.webkit.WebSettings)1 WebView (android.webkit.WebView)1 TextView (android.widget.TextView)1 Nullable (androidx.annotation.Nullable)1 FragmentActivity (androidx.fragment.app.FragmentActivity)1 FragmentManager (androidx.fragment.app.FragmentManager)1 File (java.io.File)1 GPXFile (net.osmand.GPXUtilities.GPXFile)1 WikivoyageWebViewClient (net.osmand.plus.wikivoyage.WikivoyageWebViewClient)1 TravelHelper (net.osmand.plus.wikivoyage.data.TravelHelper)1 WikivoyageExploreActivity (net.osmand.plus.wikivoyage.explore.WikivoyageExploreActivity)1