Android: android:autoLink=“web” does not support query parameters after domains - android

I have a TextView with autoLink set as
<TextView
android:id="#+id/messageDetail_privateText_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="web|phone|email" />
But when I set a text with an url like http://www.test.com?p1=v1&p2=v2 the TextView's autolink doesn't recognize the query parameters after the domain.
I can understand that this kind of URL's doesn't have too much sense, but is there any workaround to this problem?
iOS is recognizing the parameters just fine.

Answering to my own question, what finally worked for me was to check the urls of the string and adding the slash manually. Not the coolest solution in the world but worked in this case.
Below the code:
protected String normalizeURLs(String html)
{
String[] pieces = html.split(" ");
ArrayList<String> textParts = new ArrayList<>();
for(String piece : pieces) {
try {
URL isURL = new URL(piece);
String protocol = isURL.getProtocol();
String host = isURL.getHost();
String query = isURL.getQuery();
String path = isURL.getPath();
String questionMark = "?";
if (path.equals("")) {
path = "/";
}
if (query == null) {
query = "";
questionMark = "";
}
String url = protocol + "://" + host + path + questionMark + query;
textParts.add(url);
} catch (MalformedURLException exception) {
textParts.add(piece);
}
}
String resultString = "";
for (String s : textParts)
{
resultString += s + " ";
}
return resultString;
}

Related

in email mutipart message body (with attachment) is comming as plain text instead html format

I am writing an email client android app. In it, I am using JavaMail. While reading emails from server everything is coming properly except for a few mails which have attachments. In those cases the email body is coming as plain text instead of HTML format.
My code is as follows:
String str; //=bodyPart.toString();//(bodyPart.getContent());
if (bodyPart instanceof Part) {
if (bodyPart.getContent() instanceof String) {
str = bodyPart.getContent().toString();
}
else {
MimeMultipart mimeMultipart = (MimeMultipart) bodyPart.getContent();
str = getTextFromMimeMultipart(mimeMultipart);
}
}
else
str = bodyPart.getContent().toString();
//
//*****************************
//
private String getTextFromMimeMultipart(MimeMultipart mimeMultipart) throws MessagingException, IOException {
String result = "";
int count = mimeMultipart.getCount();
for (int i = 0; i < count; i++) {
BodyPart bodyPart = mimeMultipart.getBodyPart(i);
if (bodyPart.isMimeType("text/plain")) {
result = result + "\n" + bodyPart.getContent();
break; // without break same text appears twice in my tests
}
else if (bodyPart.isMimeType("text/html")) {
String html = (String) bodyPart.getContent();
result = result + "\n" + Jsoup.parse(html).text();//html;// + org.Jsoup.parse(html).text();
}
else if (bodyPart.getContent() instanceof MimeMultipart) {
result = result + getTextFromMimeMultipart((MimeMultipart)bodyPart.getContent());
}
}
return result;
}

Problems splitting string at first empty space

I have a string which i need to split at the first empty space.
Somehow I can not get it to split, the string stays untouched.
Could somebody help?
String in question:
https://test.com/info/dsfs76/933/TT Maps 2015.12
https://test.com/info/dsfs76/933/TT and Maps 2015.12 need to be 2 seperate parts which are added to an arraylist.
My code attempt:
if (str.contains(linkElem.getLinkAddress() + " ")) {
String[] temp = str.split(" ");
for (String temp : Arrays.asList(temp)) {
arraytest.add(temp);
}
}
From the code you provided, there is no such String strtemp
Maybe you should be splitting str instead?
String[] temp = str.split(" ");
You can use String tokenizer to do that like this:
StringTokenizer st = new StringTokenizer(linkElem.getLinkAddress(), " ");
String a = st.nextToken();//string before " "
String b = st.nextToken();//string after " "
i know it is completely unelegant and dumb but it works %)
String string = "https://test.com/info/dsfs76/933/TT Maps 2015.12";
StringBuilder stringBuilder = new StringBuilder();
ArrayList<String> list = new ArrayList<>();
boolean whiteSpaceFound = false;
for (int i = 0; i < string.length(); i++) {
if (string.charAt(i) == ' ' && !whiteSpaceFound) {
list.add(stringBuilder.toString());
stringBuilder = new StringBuilder();
whiteSpaceFound = true;
} else {
stringBuilder.append(string.charAt(i));
}
}
if (whiteSpaceFound || list.isEmpty()) {
list.add(stringBuilder.toString());
}

Start Chrome as Web-App on Android start

i have a quite specific problem. I have realized a Web App on an Android tablet, which will be used on an exhibition (Outform iDisplay). For this reason, the Web App has to start directly after boot. The after-boot thing is no problem (Broadcast with "android.permission.RECEIVE_BOOT_COMPLETED"), but i have a problem to start Chrome as Web-App. For getting the Intent, i have read the Icons in the launcher favorites with this snippet:
//Kitkat, therefore launcher3
url = "content://com.android.launcher3.settings/favorites?Notify=true";
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(Uri.parse(url), null, null, null, null);
if (cursor != null && cursor.moveToFirst())
{
do
{
String ent1 = cursor.getString(0);
String ent2 = cursor.getString(1);
String ent3 = cursor.getString(2); //there is the Intent string
String ent4 = cursor.getString(3);
System.out.println("Test");
String ent5 = cursor.getString(4);
String ent6 = cursor.getString(5);
String ent7 = cursor.getString(6);
String ent8 = cursor.getString(7);
String ent9 = cursor.getString(8);
String ent10 = cursor.getString(9);
String ent11 = cursor.getString(10);
String ent12 = cursor.getString(11);
String ent14 = cursor.getString(13);
String ent15 = cursor.getString(14);
String ent17 = cursor.getString(16);
String ent18 = cursor.getString(17);
String ent19 = cursor.getString(18);
String ent20 = cursor.getString(19);
if(ent2.equals("History Book")) //Get the right intent
{
runAction = ent3;
}
System.out.println(ent3);
} while (cursor.moveToNext());
}
The Intent string contains something like this:
#Intent;action=com.google.android.apps.chrome.webapps.WebappManager.ACTION_START_WEBAPP;package=com.android.chrome;S.org.chromium.chrome.browser.webapp_title=History%20Book;S.org.chromium.chrome.browser.webapp_id=86e362e4-a25d-4142-8a32-c02ffcb176a9;i.org.chromium.content_public.common.orientation=6;S.org.chromium.chrome.browser.webapp_icon=;S.org.chromium.chrome.browser.webapp_mac=3ZaXFbyWnJQaqFFOuUj3OssNz7DrBaaiWfzO2Dd7VIU%3D%0A;S.org.chromium.chrome.browser.webapp_url=http%3A%2F%2F192.168.5.148%2Fstyria%2Fhistorybook%2Findex.html;end
This looks quite good, but how can i start an Intent like this in a small app, which just has the single purpose to start this intent?
Just a small note at the end: I have tried to pack this thing into a webview, but the webview died constantly because of an libc error, so this is no option for me.
Finally i got this thing working. I was on the right way, but some Chrome.apk reverse engineering helped me for the last mile.
I have created a dummy activity with the following code in onCreate:
Search for the right entry on the homescreen, in my case for the AOSP launcher 3:
//Search for the History Book Shortcut on the Homescreen
String url = "";
String runAction="";
final String AUTHORITY = "com.android.launcher3.settings";
final Uri CONTENT_URI = Uri.parse("content://" +
AUTHORITY + "/favorites?notify=true");
final ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(CONTENT_URI,null,null,null,null);
cursor.moveToFirst();
do {
String id = cursor.getString(cursor.getColumnIndex("_id"));
String title = cursor.getString(cursor.getColumnIndex("title"));
String intent = cursor.getString(cursor.getColumnIndex("intent"));
if(title.equals(getResources().getString(R.string.homescreen_link)))
{
runAction = intent;
}
} while (cursor.moveToNext());
At this point, i have hopefully the intent as string. So, parse the string and create a new intent:
Intent intent = new Intent();
intent.setAction("com.google.android.apps.chrome.webapps.WebappManager.ACTION_START_WEBAPP");
intent.setPackage("com.android.chrome");
intent.setClassName("com.android.chrome", "com.google.android.apps.chrome.webapps.WebappManager");
HashMap<String, String> intentVals = getIntentParams(runAction);
intent.putExtra("org.chromium.chrome.browser.webapp_title",intentVals.get("S.org.chromium.chrome.browser.webapp_title"));
intent.putExtra("org.chromium.chrome.browser.webapp_icon",intentVals.get("S.org.chromium.chrome.browser.webapp_icon"));
intent.putExtra("org.chromium.chrome.browser.webapp_id",intentVals.get("S.org.chromium.chrome.browser.webapp_id"));
intent.putExtra("org.chromium.chrome.browser.webapp_url",intentVals.get("S.org.chromium.chrome.browser.webapp_url"));
intent.putExtra("org.chromium.chrome.browser.webapp_mac",intentVals.get("S.org.chromium.chrome.browser.webapp_mac"));
int orientation = 6;
try
{
orientation = Integer.parseInt(intentVals.get("i.org.chromium.content_public.common.orientation"));
}
catch(NumberFormatException _nex)
{
Log.e(TAG, "Wrong format, using default (6)");
}
intent.putExtra("org.chromium.content_public.common.orientation", orientation);
try
{
byte[] abyte0 = Base64.decode(
intentVals.get("S.org.chromium.chrome.browser.webapp_mac"),
0);
System.out.println(new String(abyte0));
}
catch (IllegalArgumentException _iae)
{
Log.e(TAG,
"Wrong webapp_mac: "
+ intentVals
.get("S.org.chromium.chrome.browser.webapp_mac"));
}
startActivity(intent);
finish();
And this function parses the intent parameters out of the intent string:
private HashMap<String, String> getIntentParams(String _runAction)
{
HashMap<String, String> retMap = new HashMap<String, String>();
String[] pairs = _runAction.split(";");
for (int i = 0; i < pairs.length; i++)
{
String[] keyval = pairs[i].split("=");
if(keyval.length==2)
{
String key = keyval[0];
String value = "";
try
{
value = java.net.URLDecoder.decode(keyval[1], "UTF-8");
}
catch (UnsupportedEncodingException _uee)
{
Log.e(TAG, "Unsupported Encoding: " + _uee.getMessage());
}
retMap.put(key, value);
}
}
return retMap;
}
And the strings.xml in res/values:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">WebAppStarter</string>
<string name="homescreen_link">History Book</string>
</resources>
That's it. You can configure the Homescreen link name to search for in strings.xml. When the app finds the string, it parses the intent string and creates a new intent to start Chrome as a Full Screen Activity Web App.

Android parsing string to url to InetAddress results in a error

I am trying to get the host and IP address from a URL. This URL is given by a text field, when the button is clicked a want the given text to be stored in a string, and I want to convert that string to a URL. This is my code:
String value;
String ip;
URL url;
public void btnOnClick(View v){
EditText text = (EditText)findViewById(R.id.txtWebsite);
value = "http://" + text.getText().toString();
TextView output = (TextView)findViewById(R.id.txtViewOutput);
if (value.length() < 3){
output.setText("At least 3 characters are required ");
}else{
try {
url = new URL(value);
//Here is where I get my error: (look below for the given error)
InetAddress address = InetAddress.getByName(new URL(***url***).getHost());
ip = address.getHostAddress();
}catch (MalformedURLException e){
System.out.println("Error: " + e.getMessage());
e.printStackTrace();
}
}
}
The error:
http://oi60.tinypic.com/b6vpxy.jpg
So it seems to me that it doesn't convert the string to a URL, and I don't know how I can fix this. If someone could point me in the right direction and tell me what I do wrong, I would appreciate it.
The variable url is already of type URL. no need to do new URL(url), just do this instead:
InetAddress address = InetAddress.getByName(url.getHost());
you can use this to parse url from string:
ArrayList retrieveLinks(String text) {
ArrayList links = new ArrayList();
String regex = "\\(?\\b(http://|www[.])[-A-Za-z0-9+&##/%?=~_()|!:,.;]*[-A-Za-z0-9+&##/%=~_()|]";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(text);
while(m.find()) {
String urlStr = m.group();
char[] stringArray1 = urlStr.toCharArray();
if (urlStr.startsWith("(") && urlStr.endsWith(")"))
{
char[] stringArray = urlStr.toCharArray();
char[] newArray = new char[stringArray.length-2];
System.arraycopy(stringArray, 1, newArray, 0, stringArray.length-2);
urlStr = new String(newArray);
System.out.println("Finally Url ="+newArray.toString());
}
System.out.println("...Url..."+urlStr);
links.add(urlStr);
}
return links;
}
hope this helps.

get url from String in android

Currently I am using the following method to get a URL from a string and insert into a TextView. It works fine, but does see anybody any issues with this approach?
public String StringURL(String args) {
String s = args;
String [] parts = s.split("\\s");
String withURL = "";
for( String item : parts ){
if (Patterns.WEB_URL.matcher(item).matches()) {
if (!item.startsWith("http://") && !item.startsWith("https://")){
item = "http://" + item;
}
withURL += ""+ item + " ";
}
else {
withURL += item + " ";
}
}
return withURL;
}
In the TextView I set the returned String the following way:
TextView.setText(Html.fromHtml(StringURL(P)));
TextView.setMovementMethod(LinkMovementMethod.getInstance());
This method works only for SdkVersions 8+ since Patterns.WEB_URL was introduced as of SdkVersion 8.

Categories

Resources