Showing posts with label Android. Show all posts
Showing posts with label Android. Show all posts

Friday, 9 March 2012

Java Multipart Upload Code (Android Friendly)



This is a quick function to do a multipart upload of a file in android (and Java in general). It is adapted from Adrian Smith's code here: http://www.17od.com/2010/02/18/multipart-form-upload-on-android/.

 The differences are:
  1. This function will not try to read the file into a string (which imposes size restrictions).
  2. You can pass the content type of the file upon execution
  3. It returns whether or not it was successful and the HTTP response code.
  4. It does not read the response to the upload (if you want to add it back in, the code is in Adrian Smith's page above).

Thursday, 29 December 2011

Decompiling and displaying the source of an Android APK

This is a Windows-centric post, but it should be fairly straight-forward to make this work in Linux/MacOS by porting the script file and downloading the platform-correct version of JD-GUI.

Why do it
  1. You are curious to see how an application does what it does by reading its code.
  2.  You want to make sure that your application is properly obfuscated before making it public (i.e. you don't want people doing #1 and/or ProGuard is giving you grief).
You will need
What to do
  • Create a directory and name it something appropriate (for example apk-decompile).
  • Download and extract dex2jar and JD-GUI in the directory you created.
  • Make sure you rename the folders to match the file structure below:
  • Create a new text file in the root directory (apk-decompile in our example) and name it "go.bat".
  • Open it in notepad and paste the following:
echo off

REM Make sure we are working off the local directory.
set _=%~dp0
chdir /d "%_%"
echo Working directory is: "%_%"

IF "%~1"=="" GOTO BAD_SYNTAX

echo Converting dex to jar:
call dex2jar\dex2jar %1
echo.

echo Opening Class Viewer:
start "" jd-gui\jd-gui.exe %1.dex2jar.jar
echo.

echo DONT CLOSE THIS DIALOGUE UNTIL YOU'VE FINSHED CHECKING THE FILE AS IT WILL AUTOMATICALLY BE DELETED.
pause

del %1.dex2jar.jar

exit 0

:BAD_SYNTAX
echo Error! Incorrect syntax.
echo The correct syntax is:
echo  %0 ^<path_to_apk^>;
pause
exit 1

How to use this
  • You can either use the command line and type the following: go.bat <path_to_apk>
  • You can drag and drop an apk file onto go.bat.
Notes
 I'm certain that there are other, probably more efficient ways to do this but this suits my purposes (for now).

Tuesday, 20 September 2011

Two ListViews Side by Side in Android

The following XML shows how to place two ListViews side by side, add some seperator views and button bars for any actions needed.
Even though fragment now make the creation and maintenance this type of layout much easier, simple application might benefit from this without having to include the compatibility library.

The result will look like this:


(The layout file is after the break)

Monday, 19 September 2011

Getting a Bitmap out of a Zip file in Android (or Java in general)

The following function will extract an image from a zip file stored anywhere on the filesystem (for as long as the application has access to it).

It will return a bitmap object if the image is found or null otherwise. This function should work in pure java as well with minor alterations (replacing the Log calls with system.outs).


public Bitmap getBtimapFromZip(final String zipFilePath, final String imageFileInZip){
	Log.d(TAG, "Getting image '" + imageFileInZip + "' from '" + zipFilePath +"'");
	Bitmap result = null;
	try {
		FileInputStream fis = new FileInputStream(zipFilePath);
		ZipInputStream zis = new ZipInputStream(fis);
		ZipEntry ze = null;
		while ((ze = zis.getNextEntry()) != null) {
			if (ze.getName().equals(imageFileInZip)) {
				result = BitmapFactory.decodeStream(zis);
				break;
			}
		}
	} catch (FileNotFoundException e) {
		Log.e(Constants.TAG, "Extracting file: Error opening zip file - FileNotFoundException: ", e);
		e.printStackTrace();
	} catch (IOException e) {
		Log.e(Constants.TAG, "Extracting file: Error opening zip file - IOException: ", e);
		e.printStackTrace();
	}
	return result;
}

If you you use a wrapper function like this:
private void loadImage(ImageButton button, String image){
	// Use this drawable by default
	Drawable d = context.getResources().getDrawable(R.drawable.no_image);
	Bitmap b = getBtimapFromZip("path_to_zip_file", image);
	
	if(b != null){ // if the bitmap is not null, load that instead.
		d = new BitmapDrawable(b);
	}
	
	button.setImageDrawable(d);
}

you can have a fallback drawable to display if something goes wrong (the above bit is Android specific).

A few things to keep in mind:
  1. To get the image out of the zip file you are scanning through all records in the archive sequentially. This could be a performance hit if you have many images. 
  2. If the function returns null but you know that the image is in there, check that you havent accidentally archived the top directory along with the images when creating the zip.

Monday, 27 June 2011

Fixing the "Error getting final archive: Debug certificate expired on xx/xx/xxxx" problem

If you are developing Android applications for a while on the same computer (specifically for more than 365 days) you should be familiar with the "Error getting final archive: Debug certificate expired" error which will stop you from testing your applications.

When the Android SDK is installed, it creates a Debug Key which is used to automatically sign applications when you hit the run button and upload them to the Emulator/Test device. The certificate is only valid for 365 days after which you get the error above.

There are two ways to tackle this issue:
  1. You can either get the Android SDK to recreate a debug key - and repeat this every 1 year.
  2. You can create your own key and set the expiry date yourself.

Tuesday, 21 June 2011

Checking if an Intent handler or Activity is present

Quite often in Android you need to call a 3rd party Intent or activity.
For example, to scan a barcode using the Barcode Scanner application, or to start the "hidden" Field Test application in HTC phones.

In order to make the life of a user easier, it is a good idea to check if the Intent Receiver/Activity exists before you attempt to invoke it. Some of the reasons being:
  1. If a non-existent intent, you application will force close.
  2. If the intent receiver is not present, you might want to redirect the user to the market to download the necessary application.
  3. Depending on the existence of an intent you might want to make menu options appear/disappear.
This post contains two functions (isIntentAvailable() and isActivityAvailable())which can perform the checking and return a boolean accordingly.

Sunday, 17 April 2011

Orange UK APN Settings

I've been flashing my phone quite often recently, and some ROMs do not contain the settings for the Orange UK APNs, so I am writing them here.

For the record, they are for Pay Monthly.

APN 1: Orange Internet
  • Name: Orange Internet
  • APN: orangeinternet
  • Proxy: <Not set>
  • Port: <Not set>
  • Username: <Not set>
  • Password: <Not set>
  • Server: <Not set>
  • MMSC: <Not set>
  • MMS proxy: <Not set>
  • MMS port: <Not set>
  • MMS protocol: WAP 2.0
  • MCC: 234
  • MNC: 33
  • Authentication Type: <Not set>
  • APN type: default
APN 2: Orange MMS
  • Name: Orange MMS
  • APN: orangemms
  • Proxy: <Not set>
  • Port: <Not set>
  • Username: <Not set>
  • Password: <Not set>
  • Server: <Not set>
  • MMSC: http://mms.orange.co.uk
  • MMS proxy: 192.168.224.010
  • MMS port: 8080
  • MMS protocol: WAP 2.0
  • MCC: 234
  • MNC: 33
  • Authentication Type: <Not set>
  • APN type: mms

Tuesday, 12 April 2011

Android Wifi password locations

The other day, I realised that I had forgotten my WiFi network password back at home.

The easy way to fix this would have been to use an already connected PC to access the router and change the password there, or to use an application like WirelessKeyView to recover the password of the said PC.

If I used method 1, I'd have to give the key to my housemates, which adds a bit of hassle.

When I tried method 2, the PC I was using was running XP I got a hex string which is not very user friendly (XP automatically converts WPA-PSK keys to a 64bit non-(easily?) reversible hash).

I then decided to try to get the key off my (rooted) Desire Z. Easy enough, the passwords are stored in plaintext in the following file:
/data/misc/wifi/wpa_supplicant.conf  

I then wrote a quick application to make the extraction a bit more painless (links at the bottom of the post), but when I was testing it on my Galaxy Tab, I realised that the file was not there. After a bit of digging around, I found the passwords here:
/data/wifi/bcm_supp.conf  

Also, here is the location of the file on the Streakroid ROM (this could be the default for all Dell Streaks, but as I don't have one I can't check):
/data/misc/wifi/wpa.conf  

The formatting of the files is identical and can be found here: http://linux.die.net/man/5/wpa_supplicant.conf, but the location is different (based on the whims of each developer/company I guess).

To access the files, you need root permissions.

Application Links:


If anyone has a device on which the app does not work, do two things:
  1. Make sure you are rooted.
  2. If you are certain you are rooted and it still does not work, email me (my email can be found here: http://aschillings.co.uk/html/contact.html).
I have no way to contact you if you post anonymously in the comments section!

Thursday, 10 February 2011

Desire Z Modaco Rom r2 and protected Market applications.

After installing r2 of Paul's Desire Z ROM, I noticed that I could no longer find or download applications from the Market if they have their Protected flag set

To fix this, you need to use adb to pull the build.prop (a type of ini file for Android, containing information about the ROM) to your PC and change one line.

So in a command line, type the following:
 adb -d remount  
 adb -d pull /system/build.prop   

This will download the build.prop file from your phone to the current directory. Use a text editor (like notepad++) to open the file and change the following line:
 ro.build.fingerprint=htc_wwe/htc_vision/vision/vision:2.2.1/FRG83D/296256:userdebug/test-keys 

to this:
 ro.build.fingerprint=tmobile/htc_vision/vision/vision:2.2/FRF91/277427:user/release-keys

be careful not to change anything else.

Then push it back to the phone and reboot it, by typing:
 adb -d push build.prop /system/build.prop   
 adb -d reboot   

After this, you should be able to see protected applications in the market.

If you make a mistake while editing build.prop and something gets messed up, simply reflash the ROM.

Tuesday, 18 January 2011

Market upgrade to v2.2.11 and loss of application links

A couple of days ago my Desire Z silently updated the Market application to v2.2.11.

No idea what the update actually brings (I guess stability fixes) but it had the side effect of removing all application links in the "My Apps" section. That means that no application was listed as Installed, and the only apps in the screen were the purchased ones.

After trying to use the Market Doctor feature of Titanium Backup Pro (yes, I've paid for it) with little to no effect, I decided to delete the Market app's data.

And that was it. All applications were listed properly.

To delete the data, from the home screen go to
"Menu->Settings->Applications->Manage Applications->All->Market" and press the "Force Stop" button to stop the Market App from running, and the press the "Clear Data" button.

When you start the Market again, everything should be listed normally.

I can't guarantee that this will work for everyone or that it won't break anything else!

Friday, 17 December 2010

Changing the build.prop on a X5A-G

The following procedure will make Skype work on a X5A-G, make sure that the unit is using the screen space properly (no more big icons) and possibly make it a bit smoother.

Make sure you are rooted, and that adb recognises the device.

Type the following to download the build.prop file to your harddrive:
 adb -d pull /system/build.prop  

Open the build.prop file with a text editor and set the following properties as follows:

 dalvik.vm.heapsize=32m  
 ro.sf.lcd_density=160  
 ro.kernel.android.checkjni=0  

And add the following line at the end
 dalvik.vm.execution-mode=int:fast  

Save it, and type the following to send the file back to the device.
 adb -d remount  
 adb -d push build.prop /system/build.prop  
 adb -d reboot  

If you get any error messages after the "adb -d remount" line, then you do not have root...

Edit: fixed missing '=' sign in the 'dalvik.vm.execution-mode=int:fast' line...

Thursday, 16 December 2010

HSG X5A and ADB Drivers

Similar to the previous posts about the Desire Z and Android devices in general, the lines you need to follow the following procedure to get a HSG X5A (which  is actually a decent device which can playback full HD with no problems):

1. Make sure you have updated the SDK and the USB driver to the latest version.
2. Open the android_winusb.inf file (located under the android-sdk-windows\usb_driver directory) using a text editor. I personally prefer Notepad++.
3. Add the following lines under the [Google.NTx86] and [Google.NTamd64] sections:
;
;HSG X5A
%SingleAdbInterface%        = USB_Install, USB\VID_18D1&PID_DEED
%CompositeAdbInterface%     = USB_Install, USB\VID_18D1&PID_DEED&MI_01
;
4. Save the file
5. Try to install the driver again.

This might work with other HSG tablets (or their rebrands).

Friday, 12 November 2010

HTC Desire Z (AKA HTC Vision AKA T-Mobile G2) and ADB drivers

I just got myself a Desire Z and once again the ADB drivers fail to install in windows.

It is the same problem described and solved in this post.

To fix:
1. Make sure you have updated the SDK and the USB driver to the latest version.
2. Open the android_winusb.inf file (located under the android-sdk-windows\usb_driver directory) using a text editor. I personally prefer Notepad++.
3. Add the following lines under the [Google.NTx86] and [Google.NTamd64] sections:
 ;Desire Z  
 %SingleAdbInterface%    = USB_Install, USB\VID_0BB4&PID_0C91  
 %CompositeAdbInterface% = USB_Install, USB\VID_0BB4&PID_0C91&MI_01  
 ;  
4. Save the file
5. Try to install the driver again.

If it still fails to load, follow the instructions from this post to find the correct VID and PID of your phone.

Sunday, 7 November 2010

"Unable to open connection to server" error when connecting to Exchange on Android 2.2

There seems to be a problem when using the default Android Email application to connect to an Exchange Server 2010 SP1.

When you go through the setup procedure and enter all necessary info to setup a new account on the phone, the setup will eventually fail throwing a "Setup could not finish" "Unable to open connection to server." message.

On the server side, the following log is generated:
 RequestBody :   
 <?xml version="1.0" encoding="utf-8" ?>  
 <FolderSync xmlns="FolderHierarchy:">  
      <SyncKey>0</SyncKey>  
 </FolderSync>  
   
 AccessState :   
 Blocked  
   
 AccessStateReason :   
 Policy  
   
 ResponseHeader :   
 HTTP/1.1 449 Retry after sending a PROVISION command  
 MS-Server-ActiveSync: 14.1  

Google is aware of this (http://code.google.com/p/android/issues/detail?id=11177) and they calim to have fixed it but it is unknown when an update will be pushed.

The interesting thing though is that the Email application which is included with the 2.2 emulator seems to be working fine. So what we will do is extract the Email.apk from the emulator and install it on the phone.

Requirements: Rooted device, installed ADB and Android 2.2 Emulator.
Note: This will remove any improvements a ROM developer has included in Email.apk.
Disclaimer: I am not responsible if by doing this you damage your phone/data/life in an way. Backup your data first, preferably by making a Nandroid backup.

Tuesday, 21 September 2010

ADB driver installation fails after ROM update or change

Sometimes, after changing the ROM in an Android device, ADB will not detect the phone when you plug it in.
This can happen because:
  1. The rom is a different OS version and there an updated driver version exists, so you will need to update your SDK.
  2. The update changed your device's PID (Product ID) and/or VID (Vendor ID). These two values are used in USB devices to identify them and to select the correct drivers for them.
In the second case, you can modify the inf file which comes with the Android drivers to associate it with the new VID/PID.

Sunday, 12 September 2010

Funny characters when using ADB,

Depending on the device's ROM and whether terminal colour is enabled, when you use ADB to connect to a shell, you might see weird characters in the terminal after you enter a command.

For example, this is my terminal after I entered 'ls':
The reason for this is that cmd cannot interpret the escape sequences for color and it is printing them as ASCII characters.

To fix this start adb shell normally and enter:
 alias ls='ls --color=never'  

It should then look like this:

Thursday, 9 September 2010

Android tabbed, horizontally and and vertically scrollable tablelayouts

The following layout XML will allow you to create an application layout similar to the following, where the data under the tabs can be scrolled both horizontally and vertically by a finger swipe.


To see the horizontal scrollbar, you'll have to scroll to the bottom of the screen.

The XML file is after the jump: