Polaris, a New Mapping Library for Android

Maps on mobile devices are one of the most useful tools. Adding maps to your apps can sometimes be difficult especially due to the slow speed Google’s mapping framework has developed lately. Fortunately we have amazing people in Android community who are not afraid to step in and contribute. Cyril Mottier is on of the most active contributors to the common good of Android. Today, Cyril announced Polaris, a free library for building maps for Android.

Polaris is a flexible and easy to use Mapping library that allows developers to build annotated maps on Android. For more info read Cyril’s blog post about the library. You can also see the library in action by installing the example app from Google Play.

Cyril lists following features of the Polaris library in his blog post:

  • Gesture support
  • Effortless map annotating
  • Map callout variable anchor positioning
  • Built-in “user tracking” mode
  • Automatic built-in zoom controls
  • Natural map callouts transitions
  • Additional listeners

You can find the library from Github: https://github.com/cyrilmottier/Polaris

Advertisements

How to Create QuickAction Dialog in Android

Official Twitter application for Android  has introduced new Android UI features and behavior patterns such as Dashboard, Search Bar, QuickAction and Action Bar. One of the interesting pattern is QuickActions that displays contextual actions in a list view. This pattern actually already exists in QuickContact dialog/bar in default Contact application (since Android 2.0).

QuickContact QuickContact

The QuickActions dialog is not included in standard Android SDK, so we have to create it manually. At first, i had no idea on how to create it so i decided to download and read the Contact app source code from  Android git. I found that the QuickContact dialog  uses private API call (com.android.internal.policy.PolicyManager) that does not exists in standard SDK. After posting question about it on google groups and stack overflow, i got the solution for it from Qberticus (thanx Qberticus!).

Qberticus’s QuickActions uses simple/plain layout so i have to create a custom layout so it will look like QuickContact in Contact app or QuickActions in Twitter app. Based on QuickContact source code, i made a slight modification on Qberticus’s BetterPopupWindow class and extended it to implement custom layout. I also made it customizeable, so the icon and text in action list can be customized.

Here are the screenshoots of QuickActions demo:

QuickContact / Twitter-like QuickActions

 

Code snippet

Create action items:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Add action item
ActionItem addAction = new ActionItem();
addAction.setTitle("Add");
addAction.setIcon(getResources().getDrawable(R.drawable.ic_add));
//Accept action item
ActionItem accAction = new ActionItem();
accAction.setTitle("Accept");
accAction.setIcon(getResources().getDrawable(R.drawable.ic_accept));
//Upload action item
ActionItem upAction = new ActionItem();
upAction.setTitle("Upload");
upAction.setIcon(getResources().getDrawable(R.drawable.ic_up));

Line 02: Create new action item
Line 04: Set action title
Line 05: Set action icon

Create quickaction instance and setup listener:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
final QuickAction mQuickAction  = new QuickAction(this);
mQuickAction.addActionItem(addAction);
mQuickAction.addActionItem(accAction);
mQuickAction.addActionItem(upAction);
//setup the action item click listener
mQuickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
    @Override
        public void onItemClick(int pos) {
        if (pos == 0) { //Add item selected
           Toast.makeText(Example1Activity.this, "Add item selected", Toast.LENGTH_SHORT).show();
        } else if (pos == 1) { //Accept item selected
           Toast.makeText(Example1Activity.this, "Accept item selected", Toast.LENGTH_SHORT).show();
        } else if (pos == 2) { //Upload item selected
           Toast.makeText(Example1Activity.this, "Upload items selected", Toast.LENGTH_SHORT).show();
        }
    }
});

Line 1: Create quickaction instance
Line 3-5: Add the action items into quikaction
Line 8: Setup listener for action item clicked, the pos argument on onItemClick method shows which action item is clicked.

Show quickAction dialog:

1
2
3
4
5
6
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    mQuickAction.show(v);
}
});

Line 04: Show quickaction dialog, the v argument used as the anchor where the quickaction displayed.

Gallery3D-like QuickActions

 

How to Implement Google Cloud Messaging?

Hi guys, I will explain and show you a simple GCM Implementation.

gcm

Because C2DM has been officially deprecated as of June 26, 2012 :p

Refer: https://developers.google.com/android/c2dm/

Requirements:

1. Apache Ant – http://ant.apache.org/bindownload.cgi

2. Apache Tomcat – http://tomcat.apache.org/download-70.cgi

First of all you need to get an API key from Google API Console.

Getting your Sender ID

  • STEP 1.  Register Here .
  • STEP 2.  Click Create project. Your browser URL will change to something like:
    " https://code.google.com/apis/console/#project:4815162342 "

    Take note of the value after#project: (4815162342 in this example). This is your project ID, and it will be used later on as the GCM sender ID. This Id will be used by the Android Device while Registering for Push Notification.

  • STEP 3. Choose Service tab from the left side menu on the web page. and turn on “ Google Cloud Messaging for Android “
  • STEP 4. Go to API Access tab from the left menu of web page.

Note: If you going to use a static IP, provide it. If u don’t have, don worry.

Just press Create new Server key and note down the generated API key.

Refer: http://developer.android.com/guide/google/gcm/gs.html

Google has already some things to implement this. Just grab it from

http://code.google.com/p/gcm/source/checkout (You need to have git installed in ur PC)

if you don’t have git, get it from here – https://rapidshare.com/files/1274880597/gcm.zip

Server Implementation:

1. After downloading or else gitting, just extract and/or open up

gcm\samples\gcm-demo-server\WebContent\WEB-INF\classes

2. Open “api.key” with some text editor, some sample text will be there. Replace it with your API key.

3. open cmd there and navigate to the path(root of gcm-demo-server), you could find a build.xml.

Just give “ant war” in cmd. you will get a “gcm-demo.war” inside “dist” folder (gcm-demo-server\dist)

4. Copy that war file and paste it to “webapps” folder of tomcat.

Just check it whether its working “http://localhost:8080/gcm-demo”

It should give like the thing below.

Great your server implementation is over now.

Client Implementation:

Download GCMDemo (Client side) from here – https://rapidshare.com/files/3183517979/GCMDemo.zip

Extract and add it to IDE

If you have ADT or some issues with tools, follow the below things:

For this just create android project in your IDE with some name, eg: “GCMDemo”

Goto “gcm\samples\gcm-demo-client”, do the following:

1. Open up “AndroidManifest.xml” and copy it to the same xml which is in android project.

2.Open up “res” folder, you could find some folders. Just drag and drop it to “res”  folder in your android project.

3.Open up “src” folder,you will find a folder named as “src”. Just drag and drop it to “src” folder in your android project.

4.Open up “libs” folder,you will find a file named as “gcm.jar”. Just drag and drop it to “libs” folder in your android project.

For both of the steps, you need to add some spice ups for client implementation.

Open up android project in IDE. Goto “src  >> com.google.android.gcm.demo.app >> CommonUtilities.java” and do the following.

1. Replace “your_gcm-demo_url” with your server url, static IP users give your static IP.

static final String SERVER_URL = “your_gcm-demo_url”;  In my case “http://192.168.1.9/gcm-demo”

For non-static IP users:

Instead of giving “localhost, 127.0.0.1”, you should give system internal IP which u have mentioned in your network config. Because both of them are giving SocketException. So use system IPv4 Address.

To get it , XP and vista  users type “ipconfig” in your cmd. In W7, Goto Control panel>>Networking and sharing center>> Change adapter settings>>Local Area Connection”

Double click and click details where you could find your IP named as “Ipv4 Address”

2. Replace “your_sender_id_from_api_console_url” with your sender ID, like 4815162342 in this example

static final String SENDER_ID = “your_sender_id_from_api_console_url”;

Uffff! Just run your application in emulator now. Wait! wait! Just add an google account in your emulator. And then run your application. you can get following result.

Now refresh your server URL. you should get whats shown below.

Press “Send Message”. You will get a notification in your emulator.

Voila!! You did it!

If u have any doubts catch me on – https://twitter.com/satheesh420

Using NTLM authentication with HttpClient

Sometimes the web service you want to consume is locked behind Windows (NTLM) authentication. I struggled for a bit trying to figure out how to use NTLM authentication with an HttpClient request in a sample Android application. It looked like I had everything setup properly, but it just wasn’t working. Then I found the blog post, Android: Authenticating via NTLM, that started me down the proper path. Ah-ha! NTLM authentication is not supported out of the box with HttpClient for various reasons. There is a Java library that supports NTLM authentication, the Java CIFS Client Library, but it is not bundled with Apache’s HttpClient.

Ntlm

According to the Apache Http Components documentation, this is due to two concerns; Microsoft only just made public the specification for NTLM in February 2008 and for whatever reason it is still not clear whether parts of the protocol are protected by patents. Secondly JCIFS is licensed under the Lesser General Public License and so is not necessarily compatible with the Apache License.

So instead of providing an actual implementation, we get a set of interfaces for clients to provide this functionality them self’s (sic): org.apache.http.auth.AuthScheme, org.apache.http.auth.AuthSchemeFactory, org.apache.http.impl.auth.NTLMScheme, org.apache.http.impl.auth.NTLMEngine, org.apache.http.impl.auth.NTLMEngineException;

So how do these classes go together? The HttpClient needs an AuthScemeFactory that will return the AuthScheme descendant NTLMScheme. NTLMScheme requires an implementation of the NTLMEngine interface to operate on.

Apache provides the interfaces but you have to provide the implementation. Now what? Well, it turns out that Apache does actually provide the code implementation but it is “out of band” and not distributed with HttpClient. You can find the details in the HttpClient documentation, NTLM Support in HttpClient. Read the linked article for details, but basically you download and import the JCIFS library from the Samba site and then copy and paste the code for two classes (from the NTLM Support in HttpClient page) to implement the NTLMEngine and AuthSchemeFactory interfaces. There are even a couple of example how to use it. Once you’ve completed those couple of steps you will have support for using NTLM authentication with your HttpClient requests from your Android application.