Wednesday, May 22, 2013

Example using Volley Google Framework (very simple)

First you can download the Git where I did create a small lib project that have OkHttp and Volley together, to use it as library project or export the .jar file to use it on your projects.

If you want to use the project where I did put both together to facilitate the life (at least for myself), you need, after you have opened the project in the Eclipse, right click on the project name –> Properties –> Android , then scroll down to the bottom and check the box where says isLibrary , then Apply and Ok.


After you have done that you can open your project, and going over your project name doing right click –> Properties –> Android, then scroll down to the bottom and click on add and select your library project that in this case is called GoogleHttpFramework, click Ok, then Apply and Ok.


Doing this you will have create the link just like you would do inserting the jar in your built path.

Now if you want to export the .jar file, it’s quite simple, it’s just right click over the project, in this case the GoogleHttpFramework then Export –> Java –> JAR File, uncheck all checkboxes like the image bellow, select the project and the path that you want have the .jar generated, then click finish.


Then you can copy the .jar to you /lib folder inside your project.


From now on you have access to your jar or of your library project.

Then you can open my very simple example that you have downloaded from the Git also. The example project is called RestFulClient.

This example is quite simple and you have only two buttons in the screen one will call a rest service that send us a bad formatted JSON object and consequently will generate an error, that will display it to you in a TextView on your screen, the second button will make a simple call to a Yahoo rest ws and will return a number that should be translated to a Date, however the number doesn’t seem come in a real long number, meaning that the date will basically be the same just having differences on the seconds.

I’m using two URLs that I got from the web, for don’t have to create a server side project that would take some time, as this is a quite simple example and we just need one working well and other failing.

Here is the method where the Volley makes the call to the Rest WS.

private void getService(String url) {
//        JSONObject reqBody = new JSONObject();
        Map<String,String> params = new HashMap<String,String>();
        if (url.equals(URL_SUCCESS)) {
            try {
                params.put("appid", "YahooDemo");
                params.put("output", "json");
                url = addParamsToUrl(url, params);
//                reqBody.put("appid", "YahooDemo");
//                reqBody.put("output", "json");
                volleyRequestQueue = Volley.newRequestQueue(this);
                // GET
                volleyRequestQueue.add(new JsonObjectRequest(Method.GET, url, null, new MyResponseListener(), new MyErrorListener()));
                // POST 
                // Need to comment at least the line 86 where url receive the par1ameters and uncomment lines 79, 87 and 88
                // However this url doesn't seems accept post, you need to try a different URL or create your own for test purpose
//                volleyRequestQueue.add(new JsonObjectRequest(Method.POST, url, reqBody, new MyResponseListener(), new MyErrorListener()));
            } catch (Exception e) {
                Log.e("ErrorListener", e.getLocalizedMessage());
        } else {
            params.put("level", "1");
            url = addParamsToUrl(url, params);
            volleyRequestQueue = Volley.newRequestQueue(this);
            volleyRequestQueue.add(new JsonObjectRequest(Method.GET, url, null, new MyResponseListener(), new MyErrorListener()));

and here the piece of code for the class response.

class MyResponseListener implements Listener<JSONObject> {
        public void onResponse(JSONObject response) {
            MainActivity.this.jsonObj = response;
            try {
                JSONObject result = MainActivity.this.jsonObj.getJSONObject("Result");
                String timestamp = result.getString("Timestamp");
                MainActivity.this.resultTxt.setText(new Date(Long.parseLong(timestamp.trim())).toString());
            } catch (Exception e) {
                Log.e("ErrorListener", e.getLocalizedMessage());


and the class error.

class MyErrorListener implements ErrorListener {
        public void onErrorResponse(VolleyError error) {
            Log.e("ErrorListener", error.getCause() + "");
            if (error.getCause() != null) {
                Log.e("ErrorListener", error.getLocalizedMessage());

I didn’t have tested thoroughly the Volley framework yet and as I’m progressing will posting more examples, I hope that this small example will help you when using HTTP requests and response. As Volley is a very powerful tool where you can create multi-thread requests and responses and also it’s deal with all try/catch’s and many other features that make HTTP easier to use, and it’s also compatible from Android 2.2 (API 8) to the latest.

Here the link on You Tube to watch the speech at Google I/O 2013 from the Volley with Ficus Kirkpatrick.

If you have any doubt or something do add please feel free to send me a email or post a comment.

Friday, December 21, 2012

Be On Time – How’s To’s

Be On Time is a alarm that contain a different set of features then most regular alarm apps and in fact it’s a clock web radio. The minimum Android version accepted is ICS 4.0 and it’s made to be used with the latest 4.2. I also tested it in my Nexus 7 and it should work well on all tablets that have Wi-Fi and microphone than run from ICS 4.0 above.

This is the first screen that you will see, on the top left corner you can see the radio icon and a toggle button for on and off the radio. If you don’t have any radio the toggle button will be disabled. Touch on the radio icon and you will get to the radio selector spinner.

After you select you radio, touch the Select button that is at the right side of the spinner selector with this the popup will close and return to clock, after that if you wish turn on the radio it’s just hit the toggle button (Off/On), that the app will load the buffer and start play it as soon the buffer is full.
Touching on the clock numbers will bring a different pop up, this one is for you select the transparency background color, the font type and color also.

To select it you need touch the Select button.
For you go to create/update or delete alarms, just touch the clock icon, the if you have any alarm on you will have the icon in blue.

Once you hit the clock icon this next screen will load up, the main screen.

To create a new alarm just touch on the plus blue icon on the top left corner from the screen.

To set the hour and minutes, just touch over the time displayed under the alarm name entry, you can slide up and down or tap and entry the desired time in each field..

Here you can create the alarm with all settings that you want. The rules for create an alarm are quite few though there is a few:
You can’t set an alarm with less than 2 minutes from the actual time and to the same day using date, if you try it the app can if you are using week days, set the alarm to the exactly same time though to the next selected day, if using by date the alarm will not be created and for last if you don’t set a date or week days then the alarm will be setted to the next day at the same hour.

Screen for select week days (above), the rule to roll each column up or down and tapping to entry the value desired is also valid for Date,

Screen for select date (above),

Volume options (above),

Snooze options (above), the snooze interval numbers means minutes.

Select sound screens (above), the selected sound option will be in blue

Add Net Radio option (from main screen menu), if you had copied or know one URL that you want, it’s just type, or copy it into the second entry field and Save it. Clear will just clear both entry fields. 

The web part for you import all links that you want, please be aware that many links are broken or missing, also you can hit menu and insert an URL desired, if the URL doesn’t contain HTTPS you can only type part of it e.g. : from  just type that the browser will load it for you if the url is correct. To quit the browser you just need to hit back a few times or open the menu and touch on Exit browser.
Once you had touched a net radio link, and if it’s compatible (Windows Media format is not) the app will import it to you and you will get a message telling you that (below).

The settings screens is pretty simple, the last item is the one that will make the alarm keep playing for the desired time selected.

Widget, will always tell you the next alarm, the blue plus button takes you straight to create a new alarm and the second to the main screen where you can edit, create or delete any alarm that you already have.

To stop the alarm you must tap twice in less than 1 second.

When importing radios be aware that not all links would be working, and also that not always that not working link will not be working, it's a matter of try it sometimes.

So that’s all, if you have any doubt that it’s not in this post, please feel free to send me an email to that I’ll reply as soon as I can, and please rate the app if you like.

Saturday, November 17, 2012

Example for Encrypt and Decrypt using AES with Android 4.2

I'll put in here the test that I did:
As after test the code on my Nexus it's worked encrypting and decrypting (before just encrypt was working), I got a String from an other phone, from SQLite, copied and sent it via email client (GMAIL) to my Nexus, then I have copied it and replace with the one that had been created on 4.2 (the copied one, was from 4.0.4 SII 3G), then started the app again and debugging I could see the correct string returning from this method:
public static String decrypt(String seed, String encrypted) throws Exception {
            byte[] seedByte = seed.getBytes();
            System.arraycopy(seedByte, 0, key, 0, ((seedByte.length < 16) ? seedByte.length : 16));
            String base64 = new String(Base64.decode(encrypted, 0));
            byte[] rawKey = getRawKey(seedByte);
            byte[] enc = toByte(base64);
            byte[] result = decrypt(rawKey, enc);
            return new String(result);

I've made an example to show how it's works.

Activity_main.xml (layout):
<RelativeLayout xmlns:android=""
    tools:context=".MainActivity" >

        android:text="App name" />

        android:text="This is the seed." />
        android:text="This is the text to encrypt" />

        android:text="Encrypted text" />

        android:text="Decrypted text" />

    </RelativeLayout> :
  1: package com.example.encryptdecrittest;
  3:         import;
  4:         import android.os.Bundle;
  5:         import android.util.Log;
  6:         import android.view.Menu;
  7:         import android.widget.TextView;
  9:         public class MainActivity extends Activity {
 11:     private String seedWith16Chars = "This is my seed.";
 12:     private String textToEncrypt = "Hi, this is a test to check its gone work or not.";
 14:     private TextView seed;
 15:     private TextView text;
 16:     private TextView encryptedValue;
 17:     private TextView decryptedValue;
 20:     @Override
 21:     protected void onCreate(Bundle savedInstanceState) {
 22:         super.onCreate(savedInstanceState);
 23:         setContentView(R.layout.activity_main);
 25:         seed = (TextView) findViewById(;
 26:         seed.setText(seedWith16Chars);
 28:         text = (TextView) findViewById(;
 29:         text.setText(textToEncrypt);
 31:         encryptedValue = (TextView) findViewById(;
 32:         decryptedValue = (TextView) findViewById(;
 34:         try {
 35:             // This value was got when did run it from an 2.3.3 device a Galaxy SII running Android 4.0.4
 36:             String encrypted = "MzA3RDBCMjMxMjQzNzcxREUxMUYxNjg1NzgwOTU1MjU1M0FDOUZEN0M3Q0JGQ0Q5MTI2NEIyNTE2"
 39:             // Uncomment the line bellow and comment the line above to run it on an Android 4.1.2 or older.
 40:             // String encrypted = EncodeDecodeAES.encrypt(seedWith16Chars, textToEncrypt);
 41:             Log.e("Encrypt", encrypted);
 42:             encryptedValue.setText(encrypted);
 44:             String decrypted = EncodeDecodeAES.decrypt(seedWith16Chars, encrypted);
 45:             decryptedValue.setText(decrypted);
 46:             Log.e("Decrypt", decrypted);
 47:         } catch (Exception e) {
 48:             Log.e("Exception", e.getLocalizedMessage());
 49:         }
 51:     }
 54:     @Override
 55:     public boolean onCreateOptionsMenu(Menu menu) {
 56:         // Inflate the menu; this adds items to the action bar if it is present.
 57:         getMenuInflater().inflate(, menu);
 58:         return true;
 59:     }
 61: }
 63: :
  1: package com.example.encryptdecrittest;
  3: import;
  5: import javax.crypto.Cipher;
  6: import javax.crypto.KeyGenerator;
  7: import javax.crypto.SecretKey;
  8: import javax.crypto.spec.SecretKeySpec;
 10: import android.util.Base64;
 12: public class EncodeDecodeAES {
 14:  private final static String HEX = "0123456789ABCDEF";
 15:  private final static int JELLY_BEAN_4_2 = 17;
 16:  private final static byte[] key = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
 19:  // static {
 20:  // Security.addProvider(new BouncyCastleProvider());
 21:  // }
 23:  public static String encrypt(String seed, String cleartext) throws Exception {
 24:   byte[] rawKey = getRawKey(seed.getBytes());
 25:   byte[] result = encrypt(rawKey, cleartext.getBytes());
 26:   String fromHex = toHex(result);
 27:   String base64 = new String(Base64.encodeToString(fromHex.getBytes(), 0));
 28:   return base64;
 29:  }
 32:  public static String decrypt(String seed, String encrypted) throws Exception {
 33:   byte[] seedByte = seed.getBytes();
 34:   System.arraycopy(seedByte, 0, key, 0, ((seedByte.length < 16) ? seedByte.length : 16));
 35:   String base64 = new String(Base64.decode(encrypted, 0));
 36:   byte[] rawKey = getRawKey(seedByte);
 37:   byte[] enc = toByte(base64);
 38:   byte[] result = decrypt(rawKey, enc);
 39:   return new String(result);
 40:  }
 43:  public static byte[] encryptBytes(String seed, byte[] cleartext) throws Exception {
 44:   byte[] rawKey = getRawKey(seed.getBytes());
 45:   byte[] result = encrypt(rawKey, cleartext);
 46:   return result;
 47:  }
 50:  public static byte[] decryptBytes(String seed, byte[] encrypted) throws Exception {
 51:   byte[] rawKey = getRawKey(seed.getBytes());
 52:   byte[] result = decrypt(rawKey, encrypted);
 53:   return result;
 54:  }
 57:  private static byte[] getRawKey(byte[] seed) throws Exception {
 58:   KeyGenerator kgen = KeyGenerator.getInstance("AES"); // , "SC");
 59:   SecureRandom sr = null;
 60:   if (android.os.Build.VERSION.SDK_INT >= JELLY_BEAN_4_2) {
 61:    sr = SecureRandom.getInstance("SHA1PRNG", "Crypto");
 62:   } else {
 63:    sr = SecureRandom.getInstance("SHA1PRNG");
 64:   }
 65:   sr.setSeed(seed);
 66:   try {
 67:    kgen.init(256, sr);
 68:    // kgen.init(128, sr);
 69:   } catch (Exception e) {
 70:    // Log.w(LOG, "This device doesn't suppor 256bits, trying 192bits.");
 71:    try {
 72:     kgen.init(192, sr);
 73:    } catch (Exception e1) {
 74:     // Log.w(LOG, "This device doesn't suppor 192bits, trying 128bits.");
 75:     kgen.init(128, sr);
 76:    }
 77:   }
 78:   SecretKey skey = kgen.generateKey();
 79:   byte[] raw = skey.getEncoded();
 80:   return raw;
 81:  }
 84:  private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
 85:   SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
 86:   Cipher cipher = Cipher.getInstance("AES"); // /ECB/PKCS7Padding", "SC");
 87:   cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
 88:   byte[] encrypted = cipher.doFinal(clear);
 89:   return encrypted;
 90:  }
 93:  private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
 94:   SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
 95:   Cipher cipher = Cipher.getInstance("AES"); // /ECB/PKCS7Padding", "SC");
 96:   cipher.init(Cipher.DECRYPT_MODE, skeySpec);
 97:   byte[] decrypted = cipher.doFinal(encrypted);
 98:   return decrypted;
 99:  }
102:  public static String toHex(String txt) {
103:   return toHex(txt.getBytes());
104:  }
107:  public static String fromHex(String hex) {
108:   return new String(toByte(hex));
109:  }
112:  public static byte[] toByte(String hexString) {
113:   int len = hexString.length() / 2;
114:   byte[] result = new byte[len];
115:   for (int i = 0; i < len; i++)
116:    result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2), 16).byteValue();
117:   return result;
118:  }
121:  public static String toHex(byte[] buf) {
122:   if (buf == null)
123:    return "";
124:   StringBuffer result = new StringBuffer(2 * buf.length);
125:   for (int i = 0; i < buf.length; i++) {
126:    appendHex(result, buf[i]);
127:   }
128:   return result.toString();
129:  }
132:  private static void appendHex(StringBuffer sb, byte b) {
133:   sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));
134:  }
136: }

Then I did run it from a SII with that line that have a ciphered text commented,

        String encrypted =    "MzA3RDBCMjMxMjQzNzcxREUxMUYxNjg1NzgwOTU1MjU1M0FDOUZEN0M3Q0JGQ0Q5MTI2NEIyNTE2"

and the one that have

        String encrypted = EncodeDecodeAES.encrypt(seedWith16Chars, textToEncrypt);

Uncomented. Then the LogCat show me the encrypted value, remember that we are still running it from an Anroid 4.0.4 (SII). After that I've copied the text encrypted to be received by **encrypted** String, and unplugged the SII plugged my Nexus with 4.2 and ran the code and worked.
Maybe if you are using a seed (or password) with less than 16 digits it's gone fail. If it's the case try complete it from the text length (that is less then 16th position), with something like spaces or zeros, or even 1 and 0's - 10101..) up to the 16th position.
Also you need to pay attention if your previous code is initializing the keyGen with 128 bits and the class published above try first 256 and then if it's fail go downgrading up to 128bits, then you should comment all of them leaving only the one that has 128.
If you want to download the example project please go to 
I hope that it helps you.

Monday, March 12, 2012

Wallpaper Rotator

With this application you will be able to pick up to 30 pictures from your Gallery and make it rotate as your wallpaper, the minimum time interval it’s from 5 minutes, and the maximum of 7 days. This’s a pretty easy app. to work with and consume quite few battery also. It doesn’t let your CPU awake all the time.

On FREE version there is the maximum possibility of just 3 pictures and also just 3 time intervals (15min, 1Hr and 1 Day).

The first screen it’s the EULA, that will only be displayed once, and you will only be able to use the application if you agree with it.


Then you will be taken to a screen where it will display all your pictures (from your Gallery), all of them will have a left up corner for you hit and select it, if you hit the picture one dialog will ask you to to edit. If you choose yes will be taken to the gallery where you can rotate the picture, and perform some other things. Choosing cancel you will stay in the program.

After you select all pictures that you want, you should hit the Start button, that will take you to the Service page.


On the service page, if you have the service running, will see only the Stop Service button enabled. If not you can start hitting the checkbox “Activate Service”, once you have checked that box, all others components from the screen will be enabled, less “Stop service” button if you don’t have any service running.


After prepare the service enabling the checkbox, it’s time to select the time interval, that might be from 5 minutes up to 7 days, this will be the interval time that between one update and the next.


After that to in fact start the service, it’s just hit “Start Service”. After hit the button one notification on your notification bar will be displayed letting you know that the service it’s up and running. You can remove it opening the notification bar and clear.


Once you want to stop that service, it’s just open the application (if it’s not opened yet) select at least two images, hit start and then “Stop service” that will update your notification for the status Stopped.

The button Deselect All unmark all pictures that you had previously marked, and the Quit finish the application.

If you have started a service, you can finish the application, because the service will be running on the background.

Saturday, January 14, 2012

Ice Cream Sandwich 4.0.3 RC3–Update from RC2.11

Now it looks faster and more responsive than the last RC2.11 version, a few things got better also, one it’s that you have use the Samsung USB driver now and everything will be fine.

That means if you have RC3 flashed on your mobile, you need just the Samsung USB driver from my last post, everything works fine just with the Samsung USB driver, though has to be the one that it’s been used for the Nexus version (


Tuesday, January 10, 2012

Ice Cream Sandwich 4.0.3 RC2.11 for Samsung Galaxy S - Connecting the device on Windows 7/Vista


To get ICS for SGS main page ( )  or for the download page (

If you want instructions of how to to install can be found on this link :

If you want to root it, here are a couple of links : ,

Google USB driver link : , if you want to try a Samsung driver you can try that one from Samsung Nexus, in this

link : , for me it doesn’t have worked, though may it work for you.

After you flash it and if it can’t get connected with your PC, you can follow those steps to make it work.

1. Remove Samsung USB Driver;

2. Uninstall Kies and restart PC;

3. After restart, plug the USB cable in your PC and on your SGS;

4. Open your device manager (Control Panel –> Hardware and Sound –> Device Manager), then you will see the exclamation signal in yellow on the nod named Portable Devices, it will have the name GT-i9000;

5; Right click on it (assuming that you have already installed the USB driver), go to Update Driver, choose select the option Browse my computer for driver software  , then go to the place where you have installed the Android-SDK or the Google USB driver that should have a similar path as this one


and click ok, hold for a minute or two and Windows should install the driver, and it will recognize you SGS for the ADB, that means you can get connected on Eclipse (or similar IDE for development).

6. If you can’t get it connected as USB storage, and your handset is rooted, download the Terminal Emulator from the Android Market (it’s free) and insert those commands:

su <enter>

setprop persist.sys.usb.config mass_storage,adb <enter>

reboot <enter>

Your mobile will restart and then from notification bar you will have enabled the option to enable USB storage and will be able do copy/move/delete files/folders from your handset. If it doesn’t work, try those other commands :

su <enter>

setprop persist.sys.usb.config mtp,adb <enter>

reboot <enter>

This should make the final trick.

For my experience the ICS rocks even on a handset that has some age on the back as SGS, it’s fast, though not as with Gingerbread 2.3.5, this release candidate it’s pretty stable, after nearly 4 days (did have install it hours after they put it available to download), I still do have only one issue,  that’s I can’t access my external_sd card. This ICS doesn’t have the TouchWiz, what it’s quite good, due that I never liked of it, it’s too similar with IOS and quite few customisable. I’m enjoying it and hope that you also have joy with ICS on SGS.

For those guys that are doing the working on the ICS for SGS, my CONGRATULATIONS  for such good product (even on RC phase)  in a such short time. Well done.

Sunday, December 4, 2011

Wallpaper Star Trek and Wallpaper Star Trek HD - Paid version

As the previous version to browse through the gallery, just swipe your finger to left or right, and tap to choose, then you will be prompted with a dialog box asking if you want set that picture as wallpaper or not.
Once you hit the button to set the new wallpaper, the application will take some time to do it.
The sound when starting, and ending (via return/exit button) the application it’s the media volume, that you can set as you want.
If you have browsed through several wallpapers and set one to see if it’s what you want, and after that you want to come back to pick an other picture, just press the home button once you have chosen the wallpaper, and hold home button for a second and release it, then choose the application from the Android task manager, that you will back to the exact picture that you where before press the home button.
As new features, now the application has a menu:

Image above are from Star Trek wallpapers


Image above are from Star Trek wallpapers HD
Where exist the ordinary About, Service Settings and Gallery Settings.

Service Settings
Once you click on it will be prompted with this screen :

Image above from normal resolution

Image above is from the HD version.

Where to enable a background service, you will need to tap on the check-box labelled “Activate Service” to start select those settings that you want. Then you can tap at the button Choose Pictures, to be taken to a thumbnail screen, where will be displayed all wallpapers and you will be able to select up to 10 of it.

To make the selection, it’s pretty simple, just swipe up/down to scroll through those wallpapers thumbnails and tap on those check-boxes to enable the wallpaper that you want be rotate as your background. When finished just click select to come back.

After selected and came back to the previous screen, it’s time to select the interval time, that it’s the time between wallpaper changes, this can be from 5 minutes up to 7 days. Once selected the interval just hit the back button to come back to the main screen and activate the service.
After you have selected all those options, just hit the back button that it will activate the background service. Be aware that in this version the application doesn't save these settings. This means that if you go again from main screen to the service settings screen everything will be like the first time, with all thumbnails to be selected. Though doesn't mean that the service it’s not running in background, but once you hit the back button to come back, the service will stop straight away.
The background services is very savvy in memory and with the battery, of course if you choose update the wallpaper at each 5 min it'll consume more battery than each day option.
If you click back button (and not the select) from select screen, will come back without save the selected state, means a plain back to the previous screen.
If you want to switch off the service it’s just select the Service Setting and hit back, without enable the service, doing this the applications will stop the service.

Gallery Settings
You will be taken to an other screen where will be able to choose the orientation for the application between just portrait to have landscape also and to select the size in % from the X axis, from the gallery pictures. That’s the one you see when scrolling from choose a wallpaper. After you have insert your settings, just hit the back button to save your changes.
To enable landscape orientation you must enable the button where it’s write Deactivated Landscape, that will change to Activate Landscape.
If you want to come back to the default settings, hit the Reset to Default button and back button.

Now the background services is part of Star Trek Wallpapers and of HD version as well.
That’s all, I hope that you have good times with these new features, if have any issue, doubt or ideas, it’s just drop a email to me that I’ll answer it as soon as possible, and “Live longer and Prosper”.