Android: Never miss on GCM ID tracking

I have come across an excellent blog regarding GCM tracking and in the mobile world, push is the only gateway for you to reach the user after the installation.So, we can’t afford to mess it up by not following some easy methods which ensure successful tracking of GCM ids. Please visit the following link for the entire details …

http://blog.pushbullet.com/2014/02/12/keeping-google-cloud-messaging-for-android-working-reliably-techincal-post/

Happy Coding…

Advertisements

LBS – How to find User’s Home and Work location

LBS (location based services) for mobiles can be improved by knowing user’s work and home location, for eg. you don’t want to disturb the user with a push notification while he is busy working. There can be many uses around knowing this sensitive data. As for android devices google is able to predict the same and providing you with alerts at the right time. Mobile OS doesn’t track this information in general and hence doesn’t have an API for the app to access this information.

So, the only way for the app to know about user’s location is by detecting the location at various intervals of time. Hypothetically lets say the app get’s the user location every hour, but the challenge is how do you find home and work location using this location data.

Method 1

Not considering extreme cases, we know that a typical user would definitely be in office from 10.00 AM to 5.00 PM, so during these period the app receives 6 locations according to the assumptions made above. It’s hard to find which of these 6 locations is the exact work location, so geometric centre would be right solution, but what if the user has gone out for lunch during the work for a couple of hours, then the geometric centre will give us the wrong work location. Right method would be to cluster the locations based on the maximum distance between 2 locations and pick the cluster with maximum locations to find the geometric center which would be the user’s work location.

I think estimating work location for 1 day will not be enough, you have to observe the user over a period of time to get the exact work location. 10 days is a good period to get a right estimate, which would mean we have to track the user’s location for 14 days(5 working days per week). Each day we get a rough estimate of user’s work location and by clustering these day locations obtained based on the maximum distance between 2 locations and picking the cluster with maximum locations to find the geometric center will give us the exact work location.

The app needs to keep track of the user’s last 14 days locations and calculate the work location with the data over past 2 weeks to keep the data accurate and fresh, this way if user starts working at new location, we can get the new work location in 7 days.

Finding home location uses the same algorithm except that the locations used would be from 11.00PM to 6.00AM, as user stays at home during midnight and the 14 days period handles the variations.

Method2

In this method, make clusters of the 24 locations based on the distance between locations. Pull out the top 2 clusters with maximum locations and identify the time periods. One of them must be work location and other is home location. Do the same over a period of 2 weeks to get a proper estimate of user’s home and work location. But to find which of these locations is home location, observe the locations over the weekend assuming user doesn’t go to work on these days :P.

Happy Coding…

New Torrent Alerter

It’s time consuming to go to torrentz.eu and search for a tv series new episode, or some new movie. It becomes even harder if you follow a lot of series and dying to wait for a new movie torrent to come up. For eg. I don’t know when “Hunger games 720p” comes up on torrents, i can’t keep checking for the torrent everyday or i may forget to watch the movie unless i see the name somewhere probably on IMDB.

So, this calls for a torrent alerter for a string, I want to get an alert on my email, when a new torrent comes up for a tv series I am interested or a movie, or a song, or anything. So the idea is that the tool should take in a list of strings for the torrents i am interested and should alert me when something relevant comes up.

I searched for something like this over the internet but couldn’t find a service/tool, which does what i need. I guess in theory it’s hard to provide such service, for free at least. Imagine millions of users subscribing to millions of unique torrents, it takes a lot of infrastructure to handle this.

So I decided to write my own alerter. It was easy to achieve this.. the steps involved a briefed as follows.

1. Understand how search works on torrentz.eu and see if it has an API to get the results. We can use http://torrentz.eu/search?f=query to get a normal html response or http://torrentz.eu/feed?q=query to get a rss feed. query being the torrent you are interested in , could be the name of a movie or documentary or tv series.

2. Now, i only need latest torrents which were added may be 12 hours old or may be less than that.So getting the results by time sorted is the way to go, so i found that http://torrentz.eu/searchA?f=query, does what i need, instead of search we use searchA.

3. BeatuifulSoup comes to the rescue to parse the html response given by the above query, Now it’s up to me to extract the torrents that were added with in the time limit. I had to find the time the torrent was uploaded and compare it to the current time and check if it’s relatively fresh for me.

5. BeatifulSoup helps you to extract name, link, time of the torrents listed.

6. After finding the torrents i need, I use SES to send a mail to my email address with the torrents i need.

7. I put the whole process in the scheduler to check for updates regularly.

So, I don’t have to search for torrents anymore, they drop in my mail every day. Bye bye searching, the current world is all about notifications … 😀

Happy Coding..

Android: Center aligning a TextView with drawableLeft

Instead of using a TextView and ImageView, we can use drawables of TextView to reduce the no.of views used.

So, the problem at hand is, how do you align a TextView text and it’s left drawable to the center horizontally in a given width.

Fail Method: 

Put the textview width to “match_parent” and use “android:gravity” as center, the expected result is that both drawable and text should be center aligned horizontally, but to my surprise, i see only text horizontally aligned and the drawable is aligned to the textview left.

Working Method But Not Efficient:

Take a horizontal LinearLayout, put ImageView and TextView(no drawableLeft since we are using ImageView) in it with each of the view’s width set to “wrap_content”, and use “android:layout_gravity” to center on the LinearLayout. This works but we are using extra ImageView and LinearLayout.

Best Method:

Set TextView’s width to “wrap_content”, use drawableLeft,  and set “layout_gravity” to center for the TextView.

Happy Coding…

Android: How to find if a user has uninstalled the application

In the apps world, uninstall is a very common phenomenon, especially in Android, where there are no checks before an app gets published.

So how do you find if a particular user has uninstalled your application, a quick answer is GCM. Lets look at what happens when you send a push to a device where your application has been uninstalled.. (picked up from GCM docs)

  1. The end user uninstalls the application.
  2. Your server sends a message to GCM server.
  3. The GCM server sends the message to the device.
  4. The GCM client receives the message and queries Package Manager about whether there are broadcast receivers configured to receive it, which returns false.
  5. The GCM client informs the GCM server that the application was uninstalled.
  6. The GCM server marks the registration ID for deletion.
  7. The 3rd-party server sends a message to GCM.
  8. The GCM returns a NotRegistered error message to your server.
  9. Your server should delete the registration ID.

So from the above steps, it’s easy to understand that if your server gets “NotRegistered” error message, then the app has been uninstalled on the user’s device. So all you need to do is, send a silent push to your application, to check if it exists on user’s device.

Though from my personal experience, GCM server doesn’t return the error message even the app has been uninstalled, at least for the first few days of uninstallation.My guess is that GCM server takes some time to update the information of app uninstallation.

A more accurate approach will be to receive an ack from the application for the push message, So, if you receive an ack then the app still exists, if you don’t then the app doesn’t exist on the user’s device. But, there is a catch here, what if the app exists on the user’s device but the message is not delivered because the user isn’t connected with internet. How long will you wait to receive the ack, here is where your conscience comes to the rescue, you have to decide the time you will wait for the ack and decide if the app exists on user’s device. If your app is dependent on internet for the app usage and you don’t receive an ack with in 3-4 days, it’s safe to assume the user is useless to you whether he has the app or not, you can safely put him under uninstall category.

Identifying the uninstallation details can help you find some insights about the app if you can map user’s information with device information.

Happy Coding…

MongoDB: mongorestore invalid object size

I had to take a back up of a collection few months back, and dumped the data in to a file with name backup.json. Today i tried to use mongorestore to put the data back in to the database only to realise that it’s not possible and was getting the error ..

“invalid object size”.

After searching on the internet and thanks to SO, i found that i have used mongoexport to take a dump of the back up file and can import the data back to database only using mongoimport.

So use

mongoimport if you have exported data using mongoexport

mongorestore if you have exported data using mongodump

Happy Coding..