Get a map (dictionnary) of arguments->value from an URI in Android with any API Level requirements

If someone wants to use the URI class to parse an URL in Android, and get the given parameters the normal way to do it is :

String url = "http://www.example.com/?argument=value&argument2=value2&...";
Uri uri = Uri.parse(url);

//To get the value of known parameters
String argument = uri.getQueryParameter("argument");
String argument2 = uri.getQueryParameter("argument2");
...

//To look at all parameters if you don't know what you're waiting for
for (String key : uri.getQueryParameterNames()) {
   String value = uri.getQueryParameter(key);
   //Do something with value  key, like using a switch/case
}


The problem with the first method is that :

  • You need to know the arguments, and you cannot check if there is “unknow” arguments, like a malicious client trying to pass “include=X”
  • The query part of the URI is parsed each time you call setQueryParameter from the beginning !

The problem with the second method  is that :

  • When you call getQueryParameterNames() the whole query is parsed once
  • The query part of the URI is parsed each time you call setQueryParameter from the beginning !
  • getQueryParameterNames() is only available starting with Android 3.0 (API Level 11)

So I wrote the code below, which gives you a map of argument->value in one reading. You can then call any map-related function like containsKey(key) to know if an argument is in the URL, entrySet() to iterate through all argument/value, keySet() to get all the arguments, and values() to get all values. This only parse the URL once and is much more convenient. This code is in distributed in any GPL variant, take the one you prefer.

	/**
	 * Return a map of argument->value from a query in a URI
	 * @param uri The URI
	 */
	private Map<String,String> getQueryParameter(Uri uri) {
	    if (uri.isOpaque()) {
	    	return Collections.emptyMap();
	    }

	    String query = uri.getEncodedQuery();
	    if (query == null) {
	        return Collections.emptyMap();
	    }

	    Map<String,String> parameters = new LinkedHashMap<String,String>();
	    int start = 0;
	    do {
	        int next = query.indexOf('&', start);
	        int end = (next == -1) ? query.length() : next;

	        int separator = query.indexOf('=', start);
	        if (separator > end || separator == -1) {
	            separator = end;
	        }

	        String name = query.substring(start, separator);
	        String value;
	        if (separator < end)
	        	value = query.substring(separator + 1, end);
	        else
	        	value = "";
	        
	        parameters.put(Uri.decode(name),Uri.decode(value));

	        // Move start to end of name.
	        start = end + 1;
	    } while (start < query.length());

	    return Collections.unmodifiableMap(parameters);
	}

Huawei Honor 3 root + gapps + multilang

From chinese to international version (with root, multilang and gapps)

Just a little tuto about how I managed to re-flash the ROM of the Huawei Honor 3 bought in China with an international ROM from needrom.com. As you may know, all chinese smartphones may include an English version, but never the Google Play store, Gmail, … And of course, are not rooted.

(On windows)

  1. Put your phone in USB debugging mode (see below if you don’t know how)
  2. Go to http://www.needrom.com/mobile/huawei-honor-3/ (disable adblock if you have it)
  3. Download the first file, not the updates. The others are original, un-rooter version without gapps and so on.
  4. Run the .cmd file. (Without the chinese characters it’s called 3__HN3-U01_.cmd). It’s in chinese, but you just have to type “enter”.
  5. After 2 or 3 minutes it will reboot in recovery mode.
  6. Choose “Wipe data/factory reset”, and choose “yes – delete all data”. This step is very important. If you don’t do that, you’ll mix the two roms… Choose also “wipe cache”.
  7. Choose “reboot now”.

Change language

 

 

 

 

 

Go in settings. It’s the icon on the main screen with a gear :

20140211_155545

Click on the language menu. It’s the one with an icon of letter”A” :

20140211_155615

Click on the first item and choose your preferred language :

20140211_155648

 

 

 

 

 

 

Enable USB debugging

Like for the language, go in settings and choose “about phone” :

20140211_155706

 

Type 7 times on the “build number menu”. I know it’s weard but it’s the way to do it !

20140211_155718

Now, click on the new menu that appeared “developer options”

20140211_155732

And finally, check usb debugging :

20140211_155745

 

Troubleshooting

  • Error at startup : your phone can tell you twice that a process has stop. It won’t come a third time, so just ignore that. You screen may be black a little too, just lock your phone, unlock in message modes and it won’t happen again.
  • Get rid of the Huawei “home” : you cannot really remove it, but you can install “Apex launcher” or “Nova launcher” which arethe normal google home but slightly enhanced. After installing press the home button, choose the apex or nova launcher and type “always”. For them to work properly, you should install google search from the playstore too.
  • If nothing happen after running the .cmd file, maybe you have a driver problem. Install the software of your phone (HiSuite for the Huawei Honor 3.
  • I woudn’t advise doing system updates. If it’s working, don’t try… If you want to try other roms like the ones of needrom.com, remember always to do a full wipe ! If the installer don’t reboot you in recovery, whhen the system has rebooted, go in the windows command line (type “cmd” in the windows search prompt), go to the folder where your rom installer is and type “adb reboot recovery” to reboot in recovery. I tried a lot of rom on this website and this was the best at time of writing… The only one removing the chinese apps and dooing everything I said is this one. And the changelog between the 113 and 119 version doesn’t seem that important…

Grignoux Cinema

This post is in French as it concerns only people from my area

Grignoux Cinema Grignoux Cinema

Je suis l’auteur de l’application “Grignoux Cinéma” qui permet de voir les horaires des cinémas grignoux à Liège (Churchill, Parc et Sauvenière) sur les terminaux Android. J’ai déposé le code sur Github, et rend donc open source cette application (licence GPL). N’hésitez pas à contribuer, je manque un peu de temps… J’aimerais rajouter certains fonctions comme la recherche par cinéma, une légende, etc…

Lien github

Lien Google Play Store