Added full screen browser support for context menu card

This commit is contained in:
Alexey Kulish 2017-05-11 10:31:34 +03:00
parent c3452f3bbb
commit 0930a72819
3 changed files with 126 additions and 11 deletions

View file

@ -1,10 +1,23 @@
package net.osmand.plus.mapcontextmenu.builders.cards;
import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.Toolbar;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.LinearLayout;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
public abstract class AbstractCard {
@ -35,4 +48,107 @@ public abstract class AbstractCard {
public OsmandApplication getMyApplication() {
return app;
}
@SuppressLint("SetJavaScriptEnabled")
@SuppressWarnings("deprecation")
protected static void openUrl(Context ctx, OsmandApplication app,
String title, String url, boolean externalLink) {
if (externalLink) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
ctx.startActivity(intent);
return;
}
final Dialog dialog = new Dialog(ctx,
app.getSettings().isLightContent() ?
R.style.OsmandLightTheme :
R.style.OsmandDarkTheme);
LinearLayout ll = new LinearLayout(ctx);
ll.setOrientation(LinearLayout.VERTICAL);
final Toolbar topBar = new Toolbar(ctx);
topBar.setClickable(true);
Drawable back = app.getIconsCache().getIcon(R.drawable.ic_action_remove_dark);
topBar.setNavigationIcon(back);
topBar.setNavigationContentDescription(R.string.shared_string_close);
topBar.setTitle(title);
topBar.setBackgroundColor(ContextCompat.getColor(ctx, getResIdFromAttribute(ctx, R.attr.pstsTabBackground)));
topBar.setTitleTextColor(ContextCompat.getColor(ctx, getResIdFromAttribute(ctx, R.attr.pstsTextColor)));
topBar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
dialog.dismiss();
}
});
final WebView wv = new WebView(ctx);
WebSettings settings = wv.getSettings();
/*
settings.setDefaultTextEncodingName("utf-8");
settings.setBuiltInZoomControls(true);
settings.setDisplayZoomControls(false);
settings.setSupportZoom(true);
//Scale web view font size with system font size
float scale = ctx.getResources().getConfiguration().fontScale;
if (android.os.Build.VERSION.SDK_INT >= 14) {
settings.setTextZoom((int) (scale * 100f));
} else {
if (scale <= 0.7f) {
settings.setTextSize(WebSettings.TextSize.SMALLEST);
} else if (scale <= 0.85f) {
settings.setTextSize(WebSettings.TextSize.SMALLER);
} else if (scale <= 1.0f) {
settings.setTextSize(WebSettings.TextSize.NORMAL);
} else if (scale <= 1.15f) {
settings.setTextSize(WebSettings.TextSize.LARGER);
} else {
settings.setTextSize(WebSettings.TextSize.LARGEST);
}
}
*/
wv.setBackgroundColor(Color.argb(1, 0, 0, 0));
//wv.setScrollContainer(false);
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl(url);
ll.addView(topBar);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0);
lp.weight = 1;
ll.addView(wv, lp);
dialog.setContentView(ll);
/*
wv.setFocusable(true);
wv.setFocusableInTouchMode(true);
wv.requestFocus(View.FOCUS_DOWN);
wv.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
if (!v.hasFocus()) {
v.requestFocus();
}
break;
}
return false;
}
});
*/
dialog.setCancelable(true);
dialog.show();
}
private static int getResIdFromAttribute(Context ctx, int attr) {
if (attr == 0) {
return 0;
}
TypedValue typedvalueattr = new TypedValue();
ctx.getTheme().resolveAttribute(attr, typedvalueattr, true);
return typedvalueattr.resourceId;
}
}

View file

@ -59,6 +59,8 @@ public abstract class ImageCard extends AbstractCard {
private String imageUrl;
// Image high resolution bitmap url
private String imageHiresUrl;
// true if external browser should to be opened, open webview otherwise
private boolean externalLink;
protected int topIconId;
protected int buttonIconId;
@ -117,6 +119,9 @@ public abstract class ImageCard extends AbstractCard {
if (imageObject.has("imageHiresUrl")) {
this.imageHiresUrl = imageObject.getString("imageHiresUrl");
}
if (imageObject.has("externalLink") && !imageObject.isNull("externalLink")) {
this.externalLink = imageObject.getBoolean("externalLink");
}
if (imageObject.has("topIcon") && !imageObject.isNull("topIcon")) {
this.topIconId = getDrawableId(imageObject.getString("topIcon"));
}
@ -215,6 +220,10 @@ public abstract class ImageCard extends AbstractCard {
return imageHiresUrl;
}
public boolean isExternalLink() {
return externalLink;
}
public int getTopIconId() {
return topIconId;
}

View file

@ -1,17 +1,9 @@
package net.osmand.plus.mapcontextmenu.builders.cards;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.net.Uri;
import android.support.v7.widget.AppCompatButton;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.mapillary.MapillaryPlugin;
import net.osmand.util.Algorithms;
import org.json.JSONObject;
@ -24,9 +16,7 @@ public class UrlImageCard extends ImageCard {
OnClickListener onClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(getUrl()));
v.getContext().startActivity(intent);
openUrl(getMapActivity(), getMyApplication(), "", getUrl(), isExternalLink());
}
};
if (!Algorithms.isEmpty(buttonText)) {