Tuesday, October 7, 2014

Android Widget Creation and facebook messenger like Chathead

I recently developed an Android widget which pops up an Facebook messenger like image when clicked. This pop-up is similar to the Facebook messenger. Here is a screenshot of the app.

What did I learn from this app:
* How to create a widget
* How the facebook messenger and Button savior app is implemented

The icon can be dragged by touching within the WindowManager (except the Notification bar on top and SoftKey at bottom if any). Now i can create a bunch of wonderful apps which can run on top of all the apps and the whole UI. Wonderful. :-)





The MainActivity.java looks like this:

package com.app.samplewidget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
import android.widget.Toast;

public class MainActivity extends AppWidgetProvider {

    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        super.onDeleted(context, appWidgetIds);
        Toast.makeText(context, "onDeleted called",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
            int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        Intent in = new Intent(context,ChatThread.class);
        PendingIntent pi = PendingIntent.getService(context, 0, in, 0);
        
        final int N = appWidgetIds.length;
        for(int i=0; i<N ; i++){
            int awId = appWidgetIds[i]; 
            RemoteViews v = new RemoteViews(context.getPackageName(), R.layout.widget);
            v.setOnClickPendingIntent(R.id.bwidgetOpen, pi);
            appWidgetManager.updateAppWidget(awId, v);
        }
        
    }
}
The ChatThread.java looks like this:

package com.app.samplewidget;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

public class ChatThread extends Service {

    public static final String TAG = "ChatThread";
    Context c;
    public static boolean status = false;
    private WindowManager windowManager;
    private ImageView chatHead;
    WindowManager.LayoutParams params;

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        chatHead = new ImageView(this);
        chatHead.setImageResource(R.drawable.ic_launcher);

        params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);

        params.gravity = Gravity.TOP | Gravity.LEFT;
        params.x = 0;
        params.y = 100;

        windowManager.addView(chatHead, params);
        chatHead.setOnTouchListener(new View.OnTouchListener() {
            private int initialX;
            private int initialY;
            private float initialTouchX;
            private float initialTouchY;

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    initialX = params.x;
                    initialY = params.y;
                    initialTouchX = event.getRawX();
                    initialTouchY = event.getRawY();
                    return true;
                case MotionEvent.ACTION_UP:
                    return true;
                case MotionEvent.ACTION_MOVE:
                    params.x = initialX
                            + (int) (event.getRawX() - initialTouchX);
                    params.y = initialY
                            + (int) (event.getRawY() - initialTouchY);
                    windowManager.updateViewLayout(chatHead, params);
                    return true;
                }
                return false;
            }
        });

    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Log.i(TAG, "onDestroy Called");
        if (chatHead != null)
            windowManager.removeView(chatHead);
    }

    @Override
    public boolean onUnbind(Intent intent) {
        // TODO Auto-generated method stub
        return super.onUnbind(intent);

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        if (status) {
            status = false;
            this.stopSelf();
        } else {
            status = true;
        }
        Log.i(TAG, "onStartCommand - " + status);
        return super.onStartCommand(intent, flags, startId);

    }

}


In addition to the above two file, AndroidManifest.xml is also important:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.app.samplewidget"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="14" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <service 
            android:name=".ChatThread">
        </service>`
        
        <receiver android:name=".MainActivity"
            android:label="@string/app_name">
            <intent-filter >
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
            </intent-filter>
            <meta-data android:name="android.appwidget.provider"
                android:resource="@xml/widget_stuff"/>
        </receiver>
    </application>
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    
</manifest>




Code is shared on github SampleWidget.

Reference:
Widget Creation Thenewboston Android videos (160-164)
Chathead basics: http://www.piwai.info/chatheads-basics/

Wednesday, September 3, 2014

Adding Timeline to Google Site

I came across a Timeline which can be embedded in the Google site. Check the video here.


Reference: http://timeline.knightlab.com/

I followed the exact steps as per the video. However the timeline was not being displayed in the Google site. On digging further in the issue I found out that Google doesn't allow the http pages in the iFrame.

The solution is to convert the http to https page as follows:


Yeyy.. it works for me...!!! :-)

Sunday, July 13, 2014

Socket Server to transfer Accelerometer Sensor data to PC

Hi Guys,

Recently, I have been trying to get the accelerometer data on PC in real time. Here is how to make it work.

The idea basically is to create a socket server in the Android device and a socket client in local PC to transfer data over wifi. Now lets create a socket server in the Android device which transmits the linear acceleration data.

Create an android Project in Eclipse:
package com.example.myserver;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

public class MainActivity extends Activity implements SensorEventListener {
    String mClientMsg = "";
    Thread myCommsThread = null;
    public static final String TAG = "SocketServer";
    private CommsThread commsThread = null;
    TextView tv;
    PrintWriter out;
    private float ax, ay, az;
    private long timenow = 0, timeprev = 0, timestamp =0 ;

    private SensorManager sm;
    private Sensor sensor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.textView1);
        sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        sensor = sm.getSensorList(Sensor.TYPE_LINEAR_ACCELERATION).get(0);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        sm.registerListener(this, sensor, SensorManager.SENSOR_DELAY_UI);
        this.commsThread = new CommsThread();
        this.myCommsThread = new Thread(this.commsThread);
        this.myCommsThread.start();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        sm.unregisterListener(this);
        if (commsThread != null) {
            commsThread.stopComms();
        }
    }    

    Handler myHandler = new Handler(){
        public void handleMessage(Message msg){
            TextView status = (TextView) findViewById(R.id.textView3);
            status.setText("Status: Streaming Now!");
        }
    };

    class CommsThread implements Runnable {
        private volatile boolean stopFlag = false;
        private ServerSocket ss = null;
        private static final int SERVERPORT = 6000;
        public void run() {
            Socket s = null;
            try {
                ss = new ServerSocket(SERVERPORT);
            } catch (IOException e) {
                e.printStackTrace();
            }
            
            try {
                s = ss.accept();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            myHandler.sendEmptyMessage(0);
            
            while(!stopFlag){
                try {
                    out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(s.getOutputStream())), true);
                    out.printf("*#%3.2f#%3.2f#%3.2f#%2d#*\n",ax,ay,az,(int)timestamp );
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }

        public void stopComms() {
            // TODO Auto-generated method stub
            this.stopFlag = true;
            if(ss != null){
                try {
                    ss.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }         
        }
    }

    @Override
    public void onAccuracyChanged(Sensor arg0, int arg1) {
        // TODO Auto-generated method stub       
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        // TODO Auto-generated method stub
        ax = event.values[0];
        ay = event.values[1];
        az = event.values[2];
        timenow = event.timestamp;
        timestamp = (timenow - timeprev)/1000000;
        refreshDisplay();
    }

    private void refreshDisplay() {
        // TODO Auto-generated method stub
        String output = String.format("time: %d -- x:%03.2f | Y:%03.2f | Z:%03.2f", timestamp, ax,ay,az);
        timeprev = timenow;
        tv.setText(output);
    }
}


In the AndroidManifest.xml add the following lines:
    <uses-permission android:maxSdkVersion="19" android:name="android.permission.INTERNET"/>
    <uses-permission android:maxSdkVersion="19" android:name="android.permission.ACCESS_NETWORK_STATE"/>

Now install the socket server on the Android device and launch the program. The server is up and running. It now waits for the client to connect and then transmit the real-time linear acceleration data to the client.

Now lets write a simple client to read the data on Ubuntu PC.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h> 

void error(const char *msg)
{
    perror(msg);
    exit(0);
}

int main(int argc, char *argv[])
{
    int sockfd, portno, n;
    struct sockaddr_in serv_addr;
    struct hostent *server;

    char buffer[256] = "\0";
    if (argc < 3) {
       fprintf(stderr,"usage %s hostname port\n", argv[0]);
       exit(0);
    }

    portno = atoi(argv[2]);
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) 
        error("ERROR opening socket");
    server = gethostbyname(argv[1]);

    if (server == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }

    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server->h_addr, 
         (char *)&serv_addr.sin_addr.s_addr,
         server->h_length);
    serv_addr.sin_port = htons(portno);
    if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) 
        error("ERROR connecting");

    //printf("Please enter the message: ");
    bzero(buffer,256);

    //fgets(buffer,255,stdin);
    n = write(sockfd,buffer,strlen(buffer));
    if (n < 0) 
         error("ERROR writing to socket");
    while(1){
        bzero(buffer,256);
        n = read(sockfd,buffer,255);
        if (n < 0) 
            error("ERROR reading from socket");
        printf("%s\n",buffer);
    }
    close(sockfd);
    return 0;
}



compile the above code using
gcc -o socketclient client.c
To connect to the server over wifi network:
./socketclient 192.168.1.2 6000
to connect to the server via USB cable:
adb forward tcp:1234 tcp:6000
./socketclient localhost 1234
Now the data will be streaming on the Ubuntu PC. I'll try to plot this data in my next blog.

References:
C socket programming:
1. http://www.linuxhowtos.org/C_C++/socket.htm 
USB port forwarding:
2. http://www.anothem.net/archives/2010/02/15/communicating-over-the-usb-cable/
Android Socket Programming:
3. http://thinkandroid.wordpress.com/2010/03/27/incorporating-socket-programming-into-your-applications/
4. http://www.edumobile.org/android/android-development/socket-programming/ 

Sunday, July 6, 2014

adb port forwarding via USB



Here is a way to create a socket connection between Android phone and Ubuntu 14.04 desktop.

Pre-requites:
1. Android Terminal Emulator App (On Android Device)
2. Android SDK installed on host computer

create a socket on the device using nc command:
jai@jai-server:~$ adb shell
shell@mako:/ $ nc -l -p 1234
---//
The above command could be executed on the terminal Emulator as follows:



On the host machine:
jai@jai-server:~$ adb forward tcp:1235 tcp:1234
jai@jai-server:~$ nc localhost 1235
--//
Connection is established and you are good to go. Start writing on any of the device. It should be reflected on other device.

Reference: http://bharathisubramanian.wordpress.com/2012/03/10/android-socket-communication-thru-adb/

Wednesday, April 24, 2013

Running programs on server using Screens command

In my last blog I have discussed a way of connecting to your server PC at home from anywhere over the internet either on another desktop or on mobile device. I faced a problem of getting disconnected from the server when the phone signals were week on the move. So tasks like compiling of android source code was halted as soon as I get disconnected from the ssh client. Here is a way to overcome this problem which i found on the internet:

Login to the server using the ssh client and start a screen on the server. Now the shell will actually be running on the server rather than the client.
$ screen
Any other ssh client can now get connected to this screen and can view a copy the screen on their local shell. Even if the client gets disconnected due to any reason, the shell will still be running on the server.

To display the screens running on server:

jaiprakash@jaiprakash-Inspiron-1525:~$ screen -ls

There are screens on:

    2941.pts-4.jaiprakash-Inspiron-1525    (Wednesday 24 April 2013 11:15:52  IST)    (Attached)

    2811.pts-0.jaiprakash-Inspiron-1525    (Wednesday 24 April 2013 11:14:33  IST)    (Attached)

2 Sockets in /var/run/screen/S-jaiprakash.

To connect to a particular screen:
$ screen -x 2941.pts-4.jaiprakash-Inspiron-1525        --> to get a copy of the screen i.e. the shell running on the server.

if only one screen is running the simply use the command
$ screen -x

To disconnect from the screen (but process should be running on the server, in other words only to disconnect the client) use ctrl+A and ctrl+D or ctrl+A and type :detach
You would now be disconnected from the screen. but the process would still be running on the server.

I found it very useful for myself while using the ssh over internet. The screen command is very useful when the client gets disconnected abruptly. Hope this helps to remote developers :-P.

Reference: http://www.howtogeek.com/howto/ubuntu/keep-your-ssh-session-running-when-you-disconnect/

Sunday, April 21, 2013

Accessing my computer from anywhere over internet using ssh and port forwarding

1. Creating a free account on noip.com
Create an account on noip.com. You'll receive a confirmation mail from noip.com. Confirm the link.

Login to your account on noip.com and goto
My Account > Hosts/Redirects > Manage Hosts > Add a host
Chose a proper host name (ex: myserver) and a free domain (ex: zapto.org) from the available list and click on "Update host" when completed.

2. Installing the noip client on device to be accessed
Now Download the noip2 tool from the site under Hosts/Redirects > Download Client

Unzip the noip-duc-linux.tar.gz and install it
$ tar -xzvf noip-duc-linux.tar.gz
$ cd noip-2.1.9-1
$ sudo make install
$ sudo noip2 -C
[sudo] password for user: 

Auto configuration for Linux client of no-ip.com.

Please enter the login/email string for no-ip.com youremail@gmail.com
Please enter the password for user 'youremail@gmail.com'  ********

2 hosts are registered to this account.
Do you wish to have them all updated?[N] (y/N)  n
Do you wish to have host [myserver.no-ip.biz] updated?[N] (y/N)  n
Do you wish to have host [myserver.zapto.org] updated?[N] (y/N)  y
Please enter an update interval:[30]  
Do you wish to run something at successful update?[N] (y/N)  n

New configuration file '/usr/local/etc/no-ip2.conf' created.

To check the status:
 $ sudo noip2 -S

No noip2 processes active.
 
Configuration data from /usr/local/etc/no-ip2.conf.
Account youremail@gmail.com
configured for:
    host  myserver.zapto.org
Updating every 30 minutes via /dev/wlan0 with NAT enabled.

3. Port Forwarding of local wifi network to be accessible from anywhere over internet
Now we need to give access to wifi router to access ssh port from anywhere in the internet. I followed the tutorial from this site and it worked well for me. For my beetel wifi router on airtel network on the browser, login to wifi http://192.168.1.1
user: admin
password: password
http://portforward.com/english/routers/port_forwarding/Beetel/450TC1/defaultguide.htm

4. Changing default ssh port
So we are almost done now. However its recommended to change the ssh port from default (default port 22) settings while forwarding it to internet for security reasons.

To change the ssh port:
sudo vi /etc/ssh/sshd_config and change the following line to custom port address

 ListenAddress 0.0.0.0:44812 

--> make sure that this port number matches the port address described in the portforward.com tutorial.

Also you can make your device ip static. For Raspberry pi, i have mentioned the modifications in the comments of previous post.

5. Accessing over internet using ssh client
Now, to access the device from anywhere from the internet. I am using the ConnectBot client in Android to access my device remotely.

 ssh username@myserver.zapto.org:44812

--> you should be connected to your device now. Cheers, now I can access my device from anywhere using my android phone :-).

Friday, March 15, 2013

Setting up wifi on Raspberry Pi

I recently bought a "NETGEAR wifi dongle" fo my Raspberry Pi so free it from the wired ethernet connections. The details of the wifi dongle is as follows:
$lsusb
Bus 001 Device 004: ID 0846:9041 NetGear, Inc. WNA1000M 802.11bgn [Realtek RTL8188CUS]

To configure it to the local wifi network:

modify /etc/network/interfaces as follows
For example if my wifi network's name is "~Jaguar~" and password is "Password123"
auto lo

iface lo inet loopback
iface eth0 inet dhcp
allow-hotplug wlan0
auto wlan0
iface wlan0 inet dhcp
        wpa-ssid "~Jaguar~"
        wpa-psk "Password123"

Softreboot the system and wifi should be working.
$ sudo shutdown "now" -r

To re-confirm
pi@raspberrypi ~ $ ifconfig
eth0      Link encap:Ethernet  HWaddr b8:27:eb:ae:b2:aa  
          UP BROADCAST MULTICAST  MTU:1500  Metric:1
          RX packets:95 errors:0 dropped:0 overruns:0 frame:0
          TX packets:84 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:9356 (9.1 KiB)  TX bytes:13962 (13.6 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:6402 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6402 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:19282157 (18.3 MiB)  TX bytes:19282157 (18.3 MiB)

wlan0     Link encap:Ethernet  HWaddr 84:1b:5e:92:bf:e8  
          inet addr:192.168.1.8  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:2182 errors:0 dropped:2359 overruns:0 frame:0
          TX packets:2001 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:289214 (282.4 KiB)  TX bytes:1055742 (1.0 MiB)
and
 pi@raspberrypi ~ $ iwconfig  
lo        no wireless extensions. 
 
wlan0     IEEE 802.11bg  ESSID:"~Jaguar~"  Nickname:"<WIFI@REALTEK>"
          Mode:Managed  Frequency:2.412 GHz  Access Point: 80:A1:D7:8D:A3:24   
          Bit Rate:54 Mb/s   Sensitivity:0/0  
          Retry:off   RTS thr:off   Fragment thr:off
          Power Management:off
          Link Quality=100/100  Signal level=100/100  Noise level=0/100
          Rx invalid nwid:0  Rx invalid crypt:0  Rx invalid frag:0
          Tx excessive retries:0  Invalid misc:0   Missed beacon:0  
 
eth0      no wireless extensions.  
For Internet to work: use
$ sudo /sbin/route add -net 0.0.0.0 -gw 192.168.1.1 wlan0
where 192.168.1.1 is gateway of my router.

Hope this post helps. :-)