Monday, May 23, 2011

Android Shell Commands

Here you can find the available command set in the shell by typing this at the android shell prompt:

dumpcrash, am, dumpstate, input, itr, monkey, pm, svc, ssltest,
debuggerd, dhcpcd, hostapd_cli, fillup, linker, logwrapper, telnetd, iftop, mkdosfs, mount,
mv, notify, netstat, printenv, reboot, ps, renice, rm, rmdir,rmmod, sendevent, schedtop,
ping, sh, hciattach, sdptool, logcat, servicemanager, dbus-daemon, debug_tool, flash_image, installd,
dvz, hostapd, htclogkernel, mountd, qemud, radiooptions, toolbox, hcid,
route, setprop, sleep, setconsole, smd, stop, top, start, umount,
vmstat, wipe, watchprops, sync, netcfg, Chmod, date, dd, cmp, cat, dmesg, df,
getevent, getprop, hd, id, ifconfig, insmod, ioctl, kill,
ln, log, lsmod, ls, mkdir, dumpsys, service, playmp3, sdutil,
rild, dalvikvm, dexopt, surfaceflinger, app_process, mediaserver, system_server,

Sunday, May 22, 2011

Combine two images in android java


In this blog we are combine two images and we have two images stored locally on an SD card or drawble folder in android.

Steps:

Read the image from Source
InputStream istream = context.getResources().openRawResource(R.drawable.img1);
try {
image1 = BitmapFactory.decodeStream(istream);
istream = context.getResources().openRawResource(R.drawable.img2);
image2 = BitmapFactory.decodeStream(istream);
} finally {
try {
istream.close();
} catch (IOException e) {
}
}

Define the Image property
int width = 0, height = 0;
if (c.getWidth() > s.getWidth()) {
width = c.getWidth();
height = c.getHeight() + s.getHeight();
} else {
width = s.getWidth();
height = c.getHeight() + s.getHeight();
}

Create your target Bitmap,
Bitmap combinedImages = Bitmap.createBitmap(width * 2, height, Bitmap.Config.ARGB_8888);

Create a Canvas for it,
Canvas comboImage = new Canvas(combinedImages);
Use Canvas.drawBitmap to blit each source bitmap into your target bitmap
comboImage.drawBitmap(image1, 0f, 0f, null);
comboImage.drawBitmap(image2, 0f, image1.getHeight()+1, null);

Example:
public Bitmap combineImages(Context context, int img1, int img2) {
// Bitmap[] mBitmap = new Bitmap[6];
Bitmap image1, image2;

InputStream istream = context.getResources().openRawResource(img1);
try {
image1 = BitmapFactory.decodeStream(istream);
istream = context.getResources().openRawResource(img2);
image2 = BitmapFactory.decodeStream(istream);
} finally {
try {
istream.close();
} catch (IOException e) {
}
}
int width = 0, height = 0;
if (c.getWidth() > s.getWidth()) {
width = c.getWidth();
height = c.getHeight() + s.getHeight();
} else {
width = s.getWidth();
height = c.getHeight() + s.getHeight();
}
Bitmap combinedImages = null;
combinedImages = Bitmap
.createBitmap(width * 2, height, Bitmap.Config.ARGB_8888);
Canvas comboImage = new Canvas(combinedImages);
comboImage.drawBitmap(c, 0f, 0f, null);
comboImage.drawBitmap(s, 0f, c.getHeight()+1, null);
return cs;
}

Sunday, May 15, 2011

Scrollable Text View in andriod -2


This article helps to making Scrollable TextView using TextView with dispatchKeyEvent

we can make a scrollable textview while implement the setMovementMethod and dispatchKeyEvent.

1. private TextView mTextView;

2. Set the Required data in the TextView
mTextView = (TextView) findViewById(R.id.textView1);
mTextView.setText("this is for testing \nthis is for testing \nthis is for testing
\nthis is for testing \nthis is for testing \nthis is for testing \n");

3. mTextView.setMovementMethod(new ScrollingMovementMethod(){.....})

4. Override the methods in ScrollingMovementMethod

new ScrollingMovementMethod() {
public void onTakeFocus(TextView widget, Spannable text, int dir) {}
@Override
public boolean onKeyDown(TextView widget, Spannable buffer,
int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_DOWN:
for (int i = 0, scrollAmount = getScrollAmount(widget); i < scrollAmount; i++) {
down(widget, buffer);
}
return true;
case KeyEvent.KEYCODE_DPAD_UP:
for (int i = 0, scrollAmount = getScrollAmount(widget); i < scrollAmount; i++) {
up(widget, buffer);
}
return true;
default:
return super.onKeyDown(widget, buffer, keyCode, event);
}
}

private int getScrollAmount(TextView widget) {
final int visibleLineCount = (int) ((1f * widget.getHeight()) / widget
.getLineHeight());
int scrollAmount = visibleLineCount - 1;
if (scrollAmount < 1) {
scrollAmount = 1;
}
return scrollAmount;
}
}


5. Call the dispatch method in your code
mTextView.dispatchKeyEvent(event);


SAMPLE

public class MainActivity extends Activity {
/** Called when the activity is first created. */

private TextView mTextView;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView = (TextView) findViewById(R.id.textView1);
mTextView
.setText("this is for testing \nthis is for testing \nthis is for testing \nthis is for testing \nthis is for testing \nthis is for testing \n");

mTextView.setMovementMethod(new ScrollingMovementMethod() {
public void onTakeFocus(TextView widget, Spannable text, int dir) {

}

@Override
public boolean onKeyDown(TextView widget, Spannable buffer,
int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_DOWN:
for (int i = 0, scrollAmount = getScrollAmount(widget); i < scrollAmount; i++) {
down(widget, buffer);
}
return true;
case KeyEvent.KEYCODE_DPAD_UP:
for (int i = 0, scrollAmount = getScrollAmount(widget); i < scrollAmount; i++) {
up(widget, buffer);
}
return true;
default:
return super.onKeyDown(widget, buffer, keyCode, event);
}
}
private int getScrollAmount(TextView widget) {
final int visibleLineCount = (int) ((1f * widget.getHeight()) / widget
.getLineHeight());
int scrollAmount = visibleLineCount - 1;
if (scrollAmount < 1) {
scrollAmount = 1;
}
return scrollAmount;
}
});
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_DOWN:

return true;
case KeyEvent.KEYCODE_DPAD_UP:
mTextView.dispatchKeyEvent(event);
return true;
default:
return super.onKeyDown(keyCode, event);
}
}
}

Scrollable Text View in andriod


This article helps to making Scrollable TextView in Android.

We can do it two ways. 1.Create a TextView inside the ScrollView and 2. TextView with dispatch Events.


Create a TextView inside the ScrollView
< ScrollView android:layout_width="fill_parent" android:layout_height="wrap_content"
android:fillViewport="true">
< LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent">
< TextView android:text="@+id/TextView01"
android:id="@+id/mTextView"
android:layout_width="fill_parent"
android:layout_height="fill_parent">< / TextView>

< / LinearLayout>
< / ScrollView>

Activity Implementation
mTextView = (TextView) findViewById(R.id.logTextView);
mTextView .setText("");
mTextView .setMovementMethod(ScrollingMovementMethod.getInstance());

Thursday, May 12, 2011

How to Obfuscated Android Code

Obfuscated code is source or machine code that has been made difficult to understand for humans. Programmers may deliberately obfuscate code to conceal its purpose or its logic to prevent tampering, deter reverse engineering, or as a puzzle or recreational challenge for someone reading the source code.
Programs known as obfuscators transform readable code into obfuscated code using various techniques. Code obfuscation is different in essence from hardware obfuscation, where description and/or structure of a circuit is modified to hide its functionality.


The following steps to describe the android obfuscation


1) Setup the obfuscated environment Download the follwing file before you starts

ProGuard

ProGuard is a free Java class file shrinker, optimizer, obfuscator, and preverifier. It detects and removes unused classes, fields, methods, and attributes. It optimizes bytecode and removes unused instructions. It renames the remaining classes, fields, and methods using short meaningless names. Finally, it preverifies the processed code for Java 6 or for Java Micro Edition.

Download From Sourceforge : http://sourceforge.net/projects/proguard/files/

Reference : http://proguard.sourceforge.net/


Download files from internet

  • add-proguard-release.xml
  • procfg.txt

1) To update an existing Android project, open a command-line and navigate to the tools/ directory of your SDK. Now run:

android update project --name --target --path

2) So, now you have a signed build from the command line, but still no obfuscated build.

To make things easy, you’re going to want to get two helper files:


add-proguard-release.xml and procfg.txt

Copy these files into your root directory (where the build.xml file found).

3) To add Proguard to your build, you first need to edit your local properties file to add the location of the directory that Proguard is installed in:


proguard.dir=/Directory/Proguard/Is/Installed/In

3) Finally... you need to add our script to your build file and have it override a few targets. To do this, we use the XML “entity” construct. At the top of your build.xml file, add an entity that references our script file:

]

>

4) By default ADT creates a proguard.cfg file with every new project, so if you have an existing project just copy it over from a new dummy project. The next step is to enable ProGuard, you do this by adding the following to your default.properties file:

proguard.config=proguard.cfg

(assuming proguard.cfg is the ProGuard configuration file created for you, or copied from a new project, into the project root folder.)