Wednesday 8 October 2014

Collection of adb commands


 Here is a collection of adb commands 



1 Navigate To Home Screen  -->  "adb shell am start -a android.intent.action.MAIN -c android.intent.category.HOME"


2 Application Install  -->  "adb install TPLTRCU.apk"


3 Application Uninstall   -->  "adb uninstall com.tpltrcu"


4 To view apk properties  -->  "aapt dump badging <path-to-apk>"


5 To Remove the Default Launcher   -->  "adb shell rm        /system/app/Launcher3.apk"


6 Android Running Services  -->  "adb shell dumpsys activity services"


7 Android Version  -->  "adb shell getprop ro.build.version.release"


8 Android SDK Version  -->  "adb shell getprop ro.build.version.sdk"


9 Android properties  --> "adb shell getprop"


10 Date & Time  -->  "adb shell date"


11 Application Force-Stop  -->  "adb shell am force-stop <PACKAGE>"


12 Running Processes  -->  "adb shell ps"


13 ADB shell   -->  "adb shell"


14 Network status  -->  "adb shell netcfg"


15 To get a file  -->  "adb pull path_to_file"


16 To put a file  -->  "adb push sourcePath destinationPath"


17 To Change time Zone  -->  "adb shell setprop persist.sys.timezone "Asia/Kolkata""

        To find Time-Zone [http://en.wikipedia.org/wiki/List_of_tz_database_time_zones]

18 To take backups  -->  “adb backup -apk -shared -all”


19 To Restore  -->  “adb restore backup.ab”


20 To save logs from android   -->  “adb shell logcat > log.txt”


21 To check CPU usage  -->  “adb shell dumpsys cpuinfo”


22 To CheckMemory usage  -->  “adb shell dumpsys meminfo”


23 To Change Settings  -->  “adb shell am start -n com.android.settings/.Settings”


24 Bug Report Via ADB  -->  “adb bugreport > bug_report.txt”


25 Bug Report Parsing  -->  “java -jar chkbugreport.jar bug_report.txt”

        To get chkbugreport.jar [https://github.com/sonyxperiadev/ChkBugReport/downloads]

26 To Reboot the OS  -->  "adb shell reboot"


27 ADB Version  -->  "adb version"


28 To get Help  -->  "adb help"


29 To get Serial Number   -->  "adb get-serialno"


30 List of devices  -->  "adb devices"


31 To get running process  -->  "adb shell ps"


32 To print dump  -->  "adb shell dumpsys"


33 To start an Activity  -->  "adb shell am start PACKAGE_NAME/.ACTIVITY_NAME"


34 To save screenshot  -->  "adb shell screencap -p /mnt/image_name.png"


35 List of installed packages  -->  "adb shell pm list packages -f"


36    To make key event   -->  "adb shell input keyevent"




Logcat Options


If you want to control the format of each log entry is dumped, you need to use the "adb logcat -v <format>" option, which supports the following formats:


37    Display priority/tag and the PID of process issuing the message (the default format)  -->  "adb logcat -v     brief"


38    Display PID only  -->  "adb logcat -v process"


39    Display the priority/tag only   -->  "adb logcat -v tag" 


40    Display the raw log message, with no other metadata fields  -->  "adb logcat -v raw" 


41    Display the date, invocation time, priority/tag, and PID of the process issuing the message  -->  "adb logcat -v time"


42    Display the priority, tag, and the PID and TID of the thread issuing the message  -->  "adb logcat -v thread"


43    Display the date, invocation time, priority, tag, and the PID and TID of the thread issuing the message  

  -->"adb logcat -v threadtime"

 44   Display all metadata fields and separate messages with a blank lines  -->  "adb logcat -v long"



Key codes for adb command "input keyevent"


0 -->  "KEYCODE_UNKNOWN" 

1 -->  "KEYCODE_MENU" 
2 -->  "KEYCODE_SOFT_RIGHT" 
3 -->  "KEYCODE_HOME" 
4 -->  "KEYCODE_BACK" 
5 -->  "KEYCODE_CALL" 
6 -->  "KEYCODE_ENDCALL" 
7 -->  "KEYCODE_0" 
8 -->  "KEYCODE_1" 
9 -->  "KEYCODE_2" 
10 -->  "KEYCODE_3" 
11 -->  "KEYCODE_4" 
12 -->  "KEYCODE_5" 
13 -->  "KEYCODE_6" 
14 -->  "KEYCODE_7" 
15 -->  "KEYCODE_8" 
16 -->  "KEYCODE_9" 
17 -->  "KEYCODE_STAR" 
18 -->  "KEYCODE_POUND" 
19 -->  "KEYCODE_DPAD_UP" 
20 -->  "KEYCODE_DPAD_DOWN" 
21 -->  "KEYCODE_DPAD_LEFT" 
22 -->  "KEYCODE_DPAD_RIGHT" 
23 -->  "KEYCODE_DPAD_CENTER" 
24 -->  "KEYCODE_VOLUME_UP" 
25 -->  "KEYCODE_VOLUME_DOWN" 
26 -->  "KEYCODE_POWER" 
27 -->  "KEYCODE_CAMERA" 
28 -->  "KEYCODE_CLEAR" 
29 -->  "KEYCODE_A" 
30 -->  "KEYCODE_B" 
31 -->  "KEYCODE_C" 
32 -->  "KEYCODE_D" 
33 -->  "KEYCODE_E" 
34 -->  "KEYCODE_F" 
35 -->  "KEYCODE_G" 
36 -->  "KEYCODE_H" 
37 -->  "KEYCODE_I" 
38 -->  "KEYCODE_J" 
39 -->  "KEYCODE_K" 
40 -->  "KEYCODE_L" 
41 -->  "KEYCODE_M" 
42 -->  "KEYCODE_N" 
43 -->  "KEYCODE_O" 
44 -->  "KEYCODE_P" 
45 -->  "KEYCODE_Q" 
46 -->  "KEYCODE_R" 
47 -->  "KEYCODE_S" 
48 -->  "KEYCODE_T" 
49 -->  "KEYCODE_U" 
50 -->  "KEYCODE_V" 
51 -->  "KEYCODE_W" 
52 -->  "KEYCODE_X" 
53 -->  "KEYCODE_Y" 
54 -->  "KEYCODE_Z" 
55 -->  "KEYCODE_COMMA" 
56 -->  "KEYCODE_PERIOD" 
57 -->  "KEYCODE_ALT_LEFT" 
58 -->  "KEYCODE_ALT_RIGHT" 
59 -->  "KEYCODE_SHIFT_LEFT" 
60 -->  "KEYCODE_SHIFT_RIGHT" 
61 -->  "KEYCODE_TAB" 
62 -->  "KEYCODE_SPACE" 
63 -->  "KEYCODE_SYM" 
64 -->  "KEYCODE_EXPLORER" 
65 -->  "KEYCODE_ENVELOPE" 
66 -->  "KEYCODE_ENTER" 
67 -->  "KEYCODE_DEL" 
68 -->  "KEYCODE_GRAVE" 
69 -->  "KEYCODE_MINUS" 
70 -->  "KEYCODE_EQUALS" 
71 -->  "KEYCODE_LEFT_BRACKET" 
72 -->  "KEYCODE_RIGHT_BRACKET" 
73 -->  "KEYCODE_BACKSLASH" 
74 -->  "KEYCODE_SEMICOLON" 
75 -->  "KEYCODE_APOSTROPHE" 
76 -->  "KEYCODE_SLASH" 
77 -->  "KEYCODE_AT" 
78 -->  "KEYCODE_NUM" 
79 -->  "KEYCODE_HEADSETHOOK" 
80 -->  "KEYCODE_FOCUS" 
81 -->  "KEYCODE_PLUS" 
82 -->  "KEYCODE_MENU" 
83 -->  "KEYCODE_NOTIFICATION" 
84 -->  "KEYCODE_SEARCH" 
85 -->  "TAG_LAST_KEYCODE"

Tuesday 11 February 2014

How to Detect Features, Objects [Face, Eye ,Ear ,Vehicle etc] in an imge using Java with the help of OpenCV & javaCV

 We can detect features like eyes, nose, face from an image using java with the help of opencv [it is an api provided by google to enable computer vision ] and javacv [java wrapper for opencv]..Here is a simple example to demonstrate this..

                                               STEPS FOR A SAMPLE APPLICATION

1 ] You need to download opencv [https://sourceforge.net/projects/opencvlibrary/files/opencv-win/2.4.8/opencv-2.4.8.exe/download] & extract it to "C" drive

2 ] You have to add  some necessary jar files ,which are available  from :          https://code.google.com/p/javacv/downloads/list

3 ] Use the following code   - FaceDetection.java

import static com.googlecode.javacv.cpp.opencv_core.CV_AA;
import static com.googlecode.javacv.cpp.opencv_core.IPL_DEPTH_8U;
import static com.googlecode.javacv.cpp.opencv_core.cvGetSeqElem;
import static com.googlecode.javacv.cpp.opencv_core.cvLoad;
import static com.googlecode.javacv.cpp.opencv_core.cvPoint;
import static com.googlecode.javacv.cpp.opencv_core.cvRectangle;
import static com.googlecode.javacv.cpp.opencv_highgui.cvLoadImage;
import static com.googlecode.javacv.cpp.opencv_highgui.cvSaveImage;
import static com.googlecode.javacv.cpp.opencv_imgproc.CV_BGR2GRAY;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvCvtColor;
import static com.googlecode.javacv.cpp.opencv_objdetect.cvHaarDetectObjects;

import com.googlecode.javacv.cpp.opencv_core.CvMemStorage;
import com.googlecode.javacv.cpp.opencv_core.CvRect;
import com.googlecode.javacv.cpp.opencv_core.CvScalar;
import com.googlecode.javacv.cpp.opencv_core.CvSeq;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import com.googlecode.javacv.cpp.opencv_objdetect.CvHaarClassifierCascade;

public class FaceDetection {

    // The cascade definition to be used for detection.
    private static final String CASCADE_FILE = "./haarcascade_frontalface_alt.xml";

    public static void main(String arg[]) throws Exception {
       
     
        // Load the original image.
        IplImage originalImage = cvLoadImage("c:\\Users\\Desktop\\Test\\input.jpg",1);

        // We need a grayscale image in order to do the recognition, so we
        // create a new image of the same size as the original one.
        IplImage grayImage = IplImage.create(originalImage.width(),
                originalImage.height(), IPL_DEPTH_8U, 1);

        // We convert the original image to grayscale.
         cvCvtColor(originalImage, grayImage, CV_BGR2GRAY);

        CvMemStorage storage = CvMemStorage.create();

        // We instantiate a classifier cascade to be used for detection, using
        // the cascade definition.
        CvHaarClassifierCascade cascade = new CvHaarClassifierCascade(
                cvLoad(CASCADE_FILE));

        // We detect the faces.
        CvSeq faces = cvHaarDetectObjects(grayImage, cascade, storage, 1.1, 1,0);

        // We iterate over the discovered faces and draw yellow rectangles
        // around them.
        for (int i = 0; i < faces.total(); i++) {
            CvRect r = new CvRect(cvGetSeqElem(faces, i));
            cvRectangle(originalImage, cvPoint(r.x(), r.y()),
                    cvPoint(r.x() + r.width(), r.y() + r.height()),
                    CvScalar.YELLOW, 1, CV_AA, 0);

        }
        // Save the image to a new file.
        cvSaveImage("c:\\Users\\Desktop\\Output\\output.jpg", originalImage);

    }

}

4 ]  Code contains a  "haarcascade_frontalface_alt.xml" file , which is available from
 C:\opencv\data\haarcascades  directory .Just copy that file into your project's folder

5 ] Haarcascade file is generated by training, with lot of similiar images .Opencv provide so many   Haarcascade files to detect left ear,right eye ,nose etc. This code will work for any case [just replace the xml file & its name in code]

6 ]  We can generate our own Haarcascade file [So , we can identify things as we wish , like number plate, vehicle, biological cell ,road lane etc.]. That will be publish soon.

Sunday 9 February 2014

How to integrate Google's Custom Search Engine Service to your own Website

  Sometimes integration of  google's custom search engine into our own website will  enhance ease of  use,    better search results , customisation in searching areas [we can specify the websites to take results] etc. This  is a service provided by Google. This custom search engine will be look like the following in our website


1 ]  Login into this url   "https://www.google.co.in/cse/"  with your Gmail account
      The Login Page will be look like this,,,,
   
  2 ]  Then the you can see the following page
   

3 ]  Select the Add option to add  a website [Search engine will take results from this website]
4 ] By clicking create you will redirect to the following page
 


5 ] Click on Reeive Code [you can also edit the current settings / click on Control panel]
6 ]  Completed ! ! ! just copy the code snippet & paste it where you wish to add the search tool
           


Friday 7 February 2014

Use of Thread Pool In Java - Easy way to handle multiple independent tasks [Set of threads will work "automatically" in an interleaved manner]

When you have to run multiple tasks simultaneously in java , ThreadPool is the best option...Here provided is a simple implementation of ThreadPool... Each thread will work asynchronously  , so we don't need to wait to complete a thread to start another

1 . Add the following Thread Pool class to your project  [ThreadPool.java]
------------------------------------------------------------------------------------------------------------

class ThreadPool extends Object {
  private ObjectFIFO idleWorkers;

  private ThreadPoolWorker[] workerList;

  static ThreadPool pool=new ThreadPool(5);
  public ThreadPool(int numberOfThreads) {
    // make sure that it's at least one
    numberOfThreads = Math.max(1, numberOfThreads);

    idleWorkers = new ObjectFIFO(numberOfThreads);
    workerList = new ThreadPoolWorker[numberOfThreads];

    for (int i = 0; i < workerList.length; i++) {
      workerList[i] = new ThreadPoolWorker(idleWorkers);
    }
  }

  public void execute(Runnable target) throws InterruptedException {
    // block (forever) until a worker is available
    ThreadPoolWorker worker = (ThreadPoolWorker) idleWorkers.remove();
    worker.process(target);
  }

  public void stopRequestIdleWorkers() {
    try {
      Object[] idle = idleWorkers.removeAll();
      for (int i = 0; i < idle.length; i++) {
        ((ThreadPoolWorker) idle[i]).stopRequest();
      }
    } catch (InterruptedException x) {
      Thread.currentThread().interrupt(); // re-assert
    }
  }

  public void stopRequestAllWorkers() {
    stopRequestIdleWorkers();

    try {
      Thread.sleep(250);
    } catch (InterruptedException x) {
    }

    for (int i = 0; i < workerList.length; i++) {
      if (workerList[i].isAlive()) {
        workerList[i].stopRequest();
      }
    }
  }
}

class ThreadPoolWorker extends Object {
  private static int nextWorkerID = 0;

  private ObjectFIFO idleWorkers;

  private int workerID;

  private ObjectFIFO handoffBox;

  private Thread internalThread;

  private volatile boolean noStopRequested;

  public ThreadPoolWorker(ObjectFIFO idleWorkers) {
    this.idleWorkers = idleWorkers;

    workerID = getNextWorkerID();
    handoffBox = new ObjectFIFO(1); // only one slot

    // just before returning, the thread should be created and started.
    noStopRequested = true;

    Runnable r = new Runnable() {
      public void run() {
        try {
          runWork();
        } catch (Exception x) {
          // in case ANY exception slips through
          x.printStackTrace();
        }
      }
    };

    internalThread = new Thread(r);
    internalThread.start();
  }

  public static synchronized int getNextWorkerID() {
    // notice: synchronized at the class level to ensure uniqueness
    int id = nextWorkerID;
    nextWorkerID++;
    return id;
  }

  public void process(Runnable target) throws InterruptedException {
    handoffBox.add(target);
  }

  private void runWork() {
    while (noStopRequested) {
      try {
        System.out.println("workerID=" + workerID + ", ready for work");

        idleWorkers.add(this);

        Runnable r = (Runnable) handoffBox.remove();

        System.out.println("workerID=" + workerID
            + ", starting execution of new Runnable: " + r);
        runIt(r);
      } catch (InterruptedException x) {
        Thread.currentThread().interrupt(); // re-assert
      }
    }
  }

  private void runIt(Runnable r) {
    try {
      r.run();
    } catch (Exception runex) {
      System.err.println("Uncaught exception fell through from run()");
      runex.printStackTrace();
    } finally {
      Thread.interrupted();
    }
  }

  public void stopRequest() {
    System.out
        .println("workerID=" + workerID + ", stopRequest() received.");
    noStopRequested = false;
    internalThread.interrupt();
  }

  public boolean isAlive() {
    return internalThread.isAlive();
  }
}

class ObjectFIFO extends Object {
  private Object[] queue;

  private int capacity;

  private int size;

  private int head;

  private int tail;

  public ObjectFIFO(int cap) {
    capacity = (cap > 0) ? cap : 1; // at least 1
    queue = new Object[capacity];
    head = 0;
    tail = 0;
    size = 0;
  }

  public int getCapacity() {
    return capacity;
  }

  public synchronized int getSize() {
    return size;
  }

  public synchronized boolean isEmpty() {
    return (size == 0);
  }

  public synchronized boolean isFull() {
    return (size == capacity);
  }

  public synchronized void add(Object obj) throws InterruptedException {

    waitWhileFull();

    queue[head] = obj;
    head = (head + 1) % capacity;
    size++;

    notifyAll();
  }

  public synchronized void addEach(Object[] list) throws InterruptedException {

    for (int i = 0; i < list.length; i++) {
      add(list[i]);
    }
  }

  public synchronized Object remove() throws InterruptedException {

    waitWhileEmpty();

    Object obj = queue[tail];

    queue[tail] = null;

    tail = (tail + 1) % capacity;
    size--;

    notifyAll();

    return obj;
  }

  public synchronized Object[] removeAll() throws InterruptedException {

     Object[] list = new Object[size];

    for (int i = 0; i < list.length; i++) {
      list[i] = remove();
    }

    return list;
  }

  public synchronized Object[] removeAtLeastOne() throws InterruptedException {

    waitWhileEmpty();
    return removeAll();
  }

  public synchronized boolean waitUntilEmpty(long msTimeout)
      throws InterruptedException {

    if (msTimeout == 0L) {
      waitUntilEmpty();
      return true;
    }

    long endTime = System.currentTimeMillis() + msTimeout;
    long msRemaining = msTimeout;

    while (!isEmpty() && (msRemaining > 0L)) {
      wait(msRemaining);
      msRemaining = endTime - System.currentTimeMillis();
    }

    return isEmpty();
  }

  public synchronized void waitUntilEmpty() throws InterruptedException {

    while (!isEmpty()) {
      wait();
    }
  }

  public synchronized void waitWhileEmpty() throws InterruptedException {

    while (isEmpty()) {
      wait();
    }
  }

  public synchronized void waitUntilFull() throws InterruptedException {

    while (!isFull()) {
      wait();
    }
  }

  public synchronized void waitWhileFull() throws InterruptedException {

    while (isFull()) {
      wait();
    }
  }
}

------------------------------------------------------------------------------------------------------------

2 .  Write your tasks as Runnable instance , as follows

        Runnable runnable1 = new Runnable() {

            public void run() {
                try {
                    System.out.println("Starting up");
                    Thread.sleep(1000);
                    System.out.println("Doing some stuff");
                    Thread.sleep(2000);
                    // Your Desired Tasks Here 
                } catch (Exception e) {
                }

            }
        };


     Runnable runnable2 = new Runnable() {

            public void run() {
                try {
                    System.out.println("Starting up");
                    Thread.sleep(1000);
                    System.out.println("Doing some stuff");
                    Thread.sleep(2000);
                    // Your Desired Tasks Here 
                } catch (Exception e) {
                }

            }
        };
------------------------------------------------------------------------------------------------------------

3 . Simply add all runnable instance [threads] to thread pool..... 


            ThreadPool.pool.execute(runnable1);
            ThreadPool.pool.execute(runnable2);
            ThreadPool.pool.stopRequestIdleWorkers();
            Thread.sleep(2000);
            ThreadPool.pool.stopRequestIdleWorkers();
            Thread.sleep(5000);
            ThreadPool.pool.stopRequestAllWorkers();

------------------------------------------------------------------------------------------------------------

The runnabel1 & runnable2 [you can add any number of runnable instance] will work in an interleaved manner..If 2 or more threads simultaneously access the same resource , a deadlock situation occurs......So, threadpool will work good for independent tasks....The number of threads in the pool can specify in the ThreaPool.java[@ object creation line ; as constructor argument]

------------------------------------------------------------------------------------------------------------