Monthly Archives: March 2013

Re-using xml layouts with

as simple as :

   <include layout="@layout/header"/>

you can also overwrite all layout parameters:

 <include android:id=”@+id/activity1_header”
         android:layout_width=”match_parent”
         android:layout_height=”match_parent”
         layout=”@layout/header”/>

more info: http://developer.android.com/training/improving-layouts/reusing-layouts.html

Handy c# snippets

string to byte[]

static byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}

byte[] to string
static string GetString(byte[] bytes)
{
char[] chars = new char[bytes.Length / sizeof(char)];
System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
return new string(chars);
}

byte[] = byte[] concat byte[]

 

private byte[] Combine(byte[] a, byte[] b)
{
byte[] rv = new byte[a.Length + b.Length];
System.Buffer.BlockCopy(a, 0, rv, 0, a.Length);
System.Buffer.BlockCopy(b, 0, rv, a.Length, b.Length);
return rv;
}

private byte[] Combine( params byte[][] arrays )
{
byte[] rv = new byte[ arrays.Sum( a => a.Length ) ];
int offset = 0;
foreach ( byte[] array in arrays ) {
System.Buffer.BlockCopy( array, 0, rv, offset, array.Length );
offset += array.Length;
}
return rv;
}

Using adb over wifi

Here is a small tip if you need to use adb over wifi instead of usb.

You need to have initial usb-adb connection

1)  in commnad prompt type:
adb tcpip 7777

This will make adb listen on port 7777

2) check your device ip. let’s say it is 192.168.1.100

3) then you can disconnect your device from usb and type:

adb connect 192.168.1.100:7777

voila :)

Stock/Factory Android ROMs/images, insecure adb, deodexed system

I would like to share a collection of factory images for nexus devices and a few others. It contains the full packages, also the extracted .img files (in case you need only the boot image for example). I also included a insecure adb boot image and deodexed system image for most of them.

full image packages are “tgz, tar or gz” files.
image files extension is .img
isecure boot images are *.unsec.img
deodexed system images are *.deodex.img

Currently by device there are avalable for:

Nexus 10 (mantaray) factory images
Nexus 7 (Wi-Fi)(nakasi) factory images
Nexus 7 GSM (nakasig) factory images
Nexus 4 (occam) factory images
Galaxy Nexus “maguro”(takju) factory images
Galaxy Nexus “toro”(mysid) factory images
Galaxy Nexus “toroplus”(mysidspr) factory images
Nexus Q (tungsten) factory images
Galaxy Nexus “maguro” (yakju) factory images
Nexus S(soju) factory images
Nexus S(sojua) factory images
Nexus S(sojuk) factory images
Nexus S(sojus) factory images

The full up-to-date list can be found here: Stock/Factory Android ROMs/images

If you need anything specific drop me a mail, I might include it as well.

Change Market App Country

When you search for an app from your phone’s Market App and you cannot find the app there and when you check on the web at https://market.android.com and you see that:

This item cannot be installed in your device’s country, 

then it means that you need to change the country the Market App is recognizing in order to be able to install it that way.

Info: basically you have several parameters that describes where you are located. These are:

gsm.operator.alpha, gsm.operator.iso-country, gsm.operator.numeric, gsm.sim.operator.alpha, gsm.operator.iso-country, gsm.operator.numeric.

The iportant ones for the Market app are the ‘.numeric’ properties.

 Switch the Market:(you will need a root access)

1) Backup your original properties:

2) Find the NumericCode of the desired network provider. You can find a full list from http://en.wikipedia.org/wiki/Mobile_Network_Code. The NumericCode = “MCC” + “MNC”. For exmple for AT&T it is “310” + “410” = 310410.

3) Connect you device to the usb then in command prompt execute:
adb shell getprop > c:\backups.txt

adb shell

su

setprop gsm.operator.numeric NumericCode

setprop gsm.sim.operator.numeric NumericCode

4) From the phone go to Settings->Applications->Manage Applications -> Market

5) force stop the Market App

6) Clear data

7) All set 🙂

Start the Market app again, search for your application and this time if you’ve put the right country you will be able to find it.

adb sideload

I would like to introduce a small but handy improvement in Android recovery as well as in the adb client. (CWM had it for some time though).

The update is that now you can use the command:

adb sideload update.zip|

to push and flash an update to your android device.

What you need to do  is:

  1. to update you Android SDK, adb in particularshould be version 1.0.31
  2. Reboot the device in recovery
  3. Select “apply package from ADB” from the recovery menu
  4. Execute from the PC “adb sideload update.zip”

What happens is that  a “minimal adb daemon” is started on the device which is responsible only for receiving the update package so you do not need to upload the update.zip anywhere but just sideload it to the device. The zip will be then saved into the \tmp ramdisk, verified and loaded from there.

This is really handy and at the same time quite useful if you have for example an unbootable device.

resource: https://android.googlesource.com/platform/bootable/recovery/

Install CWM recovery on HTC

This is a general tutorial to install ClockworkMod (CWM) to HTC device.

the supported devices are (check http://www.clockworkmod.com/rommanager for up-to-date information):

HTC Amaze
HTC Aria
HTC Buzz (Wildfire)
HTC Click/Tattoo
HTC Desire
HTC Desire CDMA
HTC Desire HD
HTC Desire S
HTC EVO 3D
HTC EVO 3D (GSM)
HTC EVO LTE
HTC EVO View 4G
HTC Evo
HTC Evo Shift
HTC G1/Dream
HTC G2
HTC HD2
HTC Hero (CDMA)
HTC Hero (GSM)
HTC Holiday
HTC Incredible (CDMA)
HTC Incredible 2
HTC Incredible S
HTC Legend
HTC Magic
HTC MyTouch 3G
HTC MyTouch 4G
HTC MyTouch 4G Slide
HTC MyTouch Slide
HTC One S
HTC One V
HTC One X (GSM)
HTC Pico
HTC Rezound
HTC Sensation
HTC Thunderbolt
HTC Wildfire S
HTC Wildfire S CDMA

Prerequisites:

1) CWM Recovery image(we will call this CWM.img). Download the one for your phone from http://www.clockworkmod.com/rommanager

2) fastboot.exe . included in Android SDK. available here http://web.djodjo.org/?a=download:android:tools:win:bootloadertools

3) You have to unlock your device.To do that follow the instructions on http://htcdev.com/bootloader/

Procedure:

  • boot in bootloader (hold [vol down] + hold [power])
  • make sure you are in HBOOT mode (you should see “HBOOT” after the ROM information)
  • make sure the Bootloader is unlocked (you should see “UNLOCKED” on the top of the screen)
  • Select [FASTBOOT] using [volume up/down] to move and [power] to select (when you select it you should see FASTBOOT, instead of HBOOT)
  • connect the device to the pc via usb
  • check if the device is available by:
    fastboot devices
  • (note that if the device is not there and you are pretty sure that the usb is connected. you may have driver problems. in that case i would recommend to install PdaNet from http://junefabrics.com/android/download.php, just complete the first part of its installation. it will be enough to get the drivers working)
  • execute:
    fastboot flash recovery CWM.img (the image you've downloaded)
  • reboot you device, you can use  fastboot reboot also

Enjoy 🙂

ps command on Android

The “ps” command is widely use on Linux systems and have quite a few arguments which can be very handy in some occasions. On Android however you do not have the same “ps” command. Also it does not have much info on supported options, basically none 🙂

So lets look at the source: https://github.com/android/platform_system_core/blob/master/toolbox/ps.c

We can see the following available options:

	-t: show threads
	-x: show time
	-P: show policy
	-p: show process priorities
	-c: show CPU #
	<number>: filter on PID
	<string>: filter on command name

Remark 1: The last two options are not cumulative. If you exec “ps 1 2 3” or “ps com. org. net.” It will filter only on the last one i.e. PID 3 and NAME (apk packages) starting with net.

Remark 2: Another strange thing is that you filter by name it is not exactly as you expect:

root@android:/ # ps | grep com.android.
ps | grep com.android.
u0_a48    529   130   506552 63324 ffffffff 401e7ee4 S com.android.systemui
radio     670   130   516804 40524 ffffffff 401e7ee4 S com.android.phone
nfc       679   130   487492 21080 ffffffff 401e7ee4 S com.android.nfc
nfc       733   130   469508 18732 ffffffff 401e7ee4 S com.android.nfc:handover
u0_a99    841   130   466124 18608 ffffffff 401e7ee4 S com.android.location.fused
u0_a0     2519  130   501248 48084 ffffffff 401e7ee4 S com.android.contacts
u0_a29    15967 130   531532 86044 ffffffff 401e7ee4 S com.android.launcher
system    27700 130   480876 33924 ffffffff 401e7ee4 S com.android.settings

But when we filter using “ps” itself without “grep”:

root@android:/ # ps com.android
ps com.android
USER     PID   PPID  VSIZE  RSS     WCHAN    PC         NAME
nfc       679   130   487492 21080 ffffffff 401e7ee4 S com.android.nfc
root@android:/ #

Not exactly as expected. Let’s see in the code why:

we check:

 if(!namefilter || !strncmp(name, namefilter, strlen(namefilter))) {

but we show

 printf(" %08x %08x %s %s", wchan, eip, state, cmdline[0] ? cmdline : name);

So we actually show the cmdline if it is there but we actually check only by the name.

Lets see where cmdline and name are coming from:

sprintf(statline, "/proc/%d/stat", pid);
sprintf(cmdline, "/proc/%d/cmdline", pid);
...
...
ptr = statline;
    nexttok(&ptr); // skip pid
    ptr++; // skip "("

    name = ptr;

Lets check:

root@android:/ # ps | grep  com.android.phone
ps | grep  com.android.phone
radio     670   130   516804 40528 ffffffff 401e7ee4 S com.android.phone
root@android:/ # ps com.android.phone
ps com.android.phone
USER     PID   PPID  VSIZE  RSS     WCHAN    PC         NAME

As expected no luck with that, apparently cmdname. Let’s check the name.

root@android:/ # cat /proc/670/stat
cat /proc/670/stat
670 (m.android.phone) S 130 130 0 0 -1 4194624 78333 0 236 0 7260 3152 0 0 20 0 43 0 2829 529207296
10132 4294967295 1074626560 1074630815 3203537792 3203536088 1075740388 0 4612 0 38120 4294967295 0
0 17 0 0 0 0 0 0
root@android:/ # ps m.android.phone
ps m.android.phone
USER     PID   PPID  VSIZE  RSS     WCHAN    PC         NAME
radio     670   130   516804 40528 ffffffff 401e7ee4 S com.android.phone
root@android:/ #

Wow! What a surprise 🙂