Android Lollipop Patterns.WEB_URL wrong behavior - android

I am validating a URL before to open using Patterns.WEB_URL. It works perfect until lollipop that return true to "http://www.google" url for example.
final String registrationUrl = "http://www.google";
final Pattern urlPattern = Patterns.WEB_URL;
Boolean bool = urlPattern.matcher(registrationUrl).matches();
Any idea how to validate a URL? URLUtil.isValidUrl() dont work for me.

Related

Appcelerator WebView can´t load URL

i´am test an url (http://docker.itop.es:10101) and the WebView don´t load.
My Code:
var web = Titanium.UI.createWebView();
web.height = "100%";
web.width = "100%";
web.ignoreSslError = true;
web.scalesPageToFit = false;
web.enableZoomControls = false;
web.showScrollbars = false;
web.scrollsToTop = false;
web.pluginState = Titanium.UI.Android.WEBVIEW_PLUGINS_ON;
web.url = "http://docker.itop.es:10101/";
$.index.add(web);
$.index.open();
I tested Titanium SDK 6.1.1 6.1.2 and 5.1.1
Thanks for the help.
I had a similar issue where my webview did not load.
Ultimately the solution for me was urlencoding the json stringified data I was sending to it. This is not the solution to this particular issue, however it might help other visitors finding this question like I did.

Validate Specfic URL input in EditText for android

My application is about downloading an image from a specific website e.g. www.example.com/img-...
The user will input the url for the img to the EditText field. e.g. www.example.com/img-123
My problem is that when the user inputs a wrong URL, i.e. one with no no image, it is empty e.g. www.example.com/img-222
I want to detect this and tell the user their input does not link to an image and try again.
I'm using the isValidUrl() function to detect if the input is a WEB_URL only but what I want is that when the entered url has no image, the program should tell them it is an incorrect format for url.
I'm using Jsoup.connect(url).get(); to connect to the url and get the image and save it
private boolean isValidUrl(String url) {
Pattern p = Patterns.WEB_URL;
Matcher m = p.matcher(url);
if(m.matches())
return true;
else
return false;
}
We can use android native android.webkit.URLUtil class to validate any kind of url.
URLUtil.isValidUrl(downloadImaheEditText.getText().toString());
it will return true if valid else false.
String[] schemes = {"http","https"}; //DEFAULT schemes = "http", "https", "ftp"
UrlValidator urlValidator = new UrlValidator(schemes);
if (urlValidator.isValid("http://www.google.com")) {
//url is valid
}else{
//url is invalid
}
Use Apache commons-validator URLValidator class
I tried this and it worked for me. Please find the code snippet below:
public static boolean isURL(String url) {
Pattern p = Patterns.WEB_URL;
Matcher m = p.matcher(url.toLowerCase());
return m.matches();
}

android how to check if device supports Sview

How can we determine if given android device supports S-view or not ?
Is there any way programatically or through adb command ? So that we can check if device supports sview and to check its status when it will be shown.
Any guidelines or help would be greatly appreciated.
EDIT :
I added workaround below for this check.
Also if someone has different approach please let me know.
I have got workaround for this,
These are extra features added by samsung for the devices for which no constant values are added in Android api, so we need to add them manually.
private static final String SVIEW_FEATURE = "com.sec.feature.cover.sview";
private static final String HOVER_FEATURE = "com.sec.feature.hovering_ui";
private static final String SENSOR_HUB_FEATURE = "com.sec.feature.sensorhub";
private static final String SPEN_FEATURE = "com.sec.feature.spen_usp";
You can call below function as ,
hasFeature(SPEN_FEATURE );
Function :
private boolean hasFeature(String feature) {
FeatureInfo[] infos = getPackageManager().getSystemAvailableFeatures();
for (FeatureInfo info : infos) {
if (!TextUtils.isEmpty(info.name)) {
Log.d("TAG", info.name);
if (feature.equalsIgnoreCase(info.name)) {
Log.v("TAG", "Feature supported "+ info.name);
return true;
}
}
}
return false;
}
Check the link below, it could be helpful to you. As android doesn't support S view directly.
s-view functionality on any phone

Getting YouTube Video ID including parameters

In my Android application, I have an embedded YouTube video.
I'm getting the Youtube video ID from the original url as follows:
private String extractYoutubeId(String url) {
String video_id = "";
if (url != null && url.trim().length() > 0 && url.startsWith("http")) {
String expression = "^.*((youtu.be" + "\\/)"
+ "|(v\\/)|(\\/u\\/w\\/)|(embed\\/)|(watch\\?))\\??v?=?([^#\\&\\?]*).*";
CharSequence input = url;
Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(input);
if (matcher.matches()) {
String groupIndex1 = matcher.group(7);
if (groupIndex1 != null && groupIndex1.length() == 11)
video_id = groupIndex1;
}
}
return video_id;
}
This works just fine for an original url like this: http://www.youtube.com/watch?v=A7ry4cx6HfY
When I do that the video is loaded and played perfectly fine, but the quality is very bad. (240p or even worse, I think)
So from googling, I know that you just need to add a parameter like &vq=large or &vq=hd1080 to get 480p/1080p.
But when I use a url like this http://www.youtube.com/watch?v=A7ry4cx6HfY&vq=hd1080 the parameter is ignored and the quality is still bad.
How can I get the video in better quality? Of course, assuming that the video is available in that quality. Why is my parameter being ignored?
Try this code here.
// (?:youtube(?:-nocookie)?\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})
final static String reg = "(?:youtube(?:-nocookie)?\\.com\\/(?:[^\\/\\n\\s]+\\/\\S+\\/|(?:v|e(?:mbed)?)\\/|\\S*?[?&]v=)|youtu\\.be\\/)([a-zA-Z0-9_-]{11})";
public static String getVideoId(String videoUrl) {
if (videoUrl == null || videoUrl.trim().length() <= 0)
return null;
Pattern pattern = Pattern.compile(reg, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(videoUrl);
if (matcher.find())
return matcher.group(1);
return null;
}
You can find my whole parser code from here
https://github.com/TheFinestArtist/YouTubePlayerActivity/blob/master/library/src/main/java/com/thefinestartist/ytpa/utils/YoutubeUrlParser.java
This is useful open source I made to play Youtube Video.
https://github.com/TheFinestArtist/YouTubePlayerActivity
I solved this problem by using the official Google Youtube API for Android.
It's probably not the best idea to try implementing the getting of the videos by oneself. Using the API does that for you and it works just fine.
EDIT
Here the link to the API: https://developers.google.com/youtube/android/player/

how to get the default HTTP USER AGENT from the android device?

How to get the default HTTP USER AGENT and its default settings from the android device?
thanks
Nohsib
as Varundroid mentioned in his answer,
String userAgent = System.getProperty("http.agent");
is better way to do it for Android 2.1 and above.
====================
From android source code.
public static String getDefaultUserAgent() {
StringBuilder result = new StringBuilder(64);
result.append("Dalvik/");
result.append(System.getProperty("java.vm.version")); // such as 1.1.0
result.append(" (Linux; U; Android ");
String version = Build.VERSION.RELEASE; // "1.0" or "3.4b5"
result.append(version.length() > 0 ? version : "1.0");
// add the model for the release build
if ("REL".equals(Build.VERSION.CODENAME)) {
String model = Build.MODEL;
if (model.length() > 0) {
result.append("; ");
result.append(model);
}
}
String id = Build.ID; // "MASTER" or "M4-rc20"
if (id.length() > 0) {
result.append(" Build/");
result.append(id);
}
result.append(")");
return result.toString();
}
Edit: See Prakash's answer, which is better for 2.1+.
Try http://developer.android.com/reference/android/webkit/WebSettings.html#getUserAgentString
Note that this User Agent will only apply for the embedded WebKit browser that's used by default in Android. Unfortunately, you'll need to create a new WebView object to get the user agent. Fortunately, the user agent doesn't change often, so you should only need to run this code once in your application lifetime (unless don't care about performance). Just do:
String userAgent = new WebView(this).getSettings().getUserAgentString();
Alternatively, you can use the JavaScript method navigator.getUserAgent().
When you use web view to access the user-agent, make sure you run the
new WebView(this).getSettings().getUserAgentString();
on the UI thread.
If you want access the user agent on background thread.
use
System.getProperty("http.agent")
To check whether a user-agent is valid or not use this
https://deviceatlas.com/device-data/user-agent-tester
Android get user agent
An alternative
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
String userAgent = WebSettings.getDefaultUserAgent(context);
}

Categories

Resources