Tuesday, September 13, 2011

JoEy & Daisy

Finally, Joey & Daisy has an official FB Fan Page!!!
The main purpose is for those who miss them!
But you are more welcome to get to know them by clicking the image below:


And there is a Homepage on the FB Fan Page too!
Not fancy but good for now.
(click the image below to visit)

For those of you visited above and wants to be FB friends with them:


Finally, last but not least - their blog:


Oh yes, since you've made this far of the post, here is a little announcement to make (this is the first place to make this announcement): it is not only that Joey & Daisy turned to 1 year old but Daisy is pregnant as well!! Stay tuned!!! Kittens!!! Awww!!!

[Flash][Debug][Javascript] Debug Flash App on Web Page

Sometimes I found Debugging a Flash App is a big headache. I don't want to always check the Flash Player's log (it logs everything) and I don't want to setup the Flash Player Debug, there's always some problems if you use a different OS or reinstalled your OS. I found the following method efficient if you want to find out what is happening with your Flash App on a web page.

The basic idea is a call from your Flash App to Javascript and Javascript set the received value to a textarea on the html page. So as you go (play your Flash App), you can see your debug message instantly on the textarea within the same page).

On your page, add a javascript function to set the received string to the textarea. And a form with a textarea in html to display the received string:

function getTextFromFlash(str) {

document.htmlForm.receivedField.value += str + "\n";

}


<textarea rows="10" cols="20" name="receivedField"></textarea>


In flash, import the ExternalInterface and then use that to make a call to the Javascript function:

import flash.external.ExternalInterface;

ExternalInterface.call("getTextFromFlash", "Your message");

[Flash] Security Update for FileReference.browse()

The other day I was about to deploy a change that I've done a few months ago, then I found the image upload function is totally broken. i.e. Click on the upload button does not show the file system browser.
Thanks to the Flash Player security update, this is the reason:

In Flash Player 10 and Flash Player 9 Update 5, you can only call this method successfully in response to a user event (for example, in an event handler for a mouse click or keypress event). Otherwise, calling this method results in Flash Player throwing an Error exception.
My code is to retrieve the next id to save the new image, so it is a click on a button and waiting for the callback function is triggered enable to call the fileReference.browse(), so here is a workaround:

function askUserFunction():Void{

Alert.show("Allow App to access my file system","Permissions Required",Alert.OK|Alert.CANCEL,null,handleAlert);

}


var handleAlert:Function = function (evt_obj:Object) {

if (evt_obj.detail == Alert.OK) {

fileRef = new FileReference();

fileRef.addListener(fileListener_News);

fileRef.browse(fileTypes);

}

};


Call the askUserFunction() at the place where you want to call fileReference.browse(). This triggers an alert and user can only browser through their file system if he clicked on OK. This is a workaround that forces a user event.


On another note, if you have a block of important code in your onProgress function, then you may want to pay attention to this:


In some cases, onProgress listeners are not invoked; for example, if the file being transmitted is very small, or if the upload or download happens very quickly.


Make sure you do a check in your onComplete function :)

Wednesday, August 17, 2011

[iReport][Java] Dynamic Image with Transparent Background

  • Make sure the opaque box is ticked
  • Make sure your field class type is: java.io.InputStream
  • Your field is retrieved from DB that is a byte array
  • Use the following for the image expression:
net.sf.jasperreports.engine.util.JRImageLoader.loadImage((byte [ ] )(net.sf.jasperreports.engine.util.JRImageLoader.loadImageDataFromInputStream($F{YOUR_FIELD_NAME}})))
  • Image Expression Class: java.awt.Image
Note: the following creates a dynamic image too BUT the image background is not transparent
new javax.swing.ImageIcon(java.awt.Toolkit.getDefaultToolkit().createImage((byte[]) $F{YOUR_FIELD_NAME})).getImage()



[Java][Postgres][Hibernate] Image Retrieved from DB Doubled in Size

A project that I am working on require iReport to retrieve image data from Postgres and display it in the report. We use hibernate annotation @Blob and it only creates a reference (oid) to the byte array, that works fine so far until it gives trouble in iReport converting it to an InputStream.
So we removed the @Blob annotation and recreates the schema, this time the image column has a type bytea (no longer oid).

The app works fine on others machines but not mine. On my local app, no images are shown on the screen and firebug told me that they were corrupted. But by retrieving bytea from database and convert them to the png shows that images are all fine. Access log shows the images went through fine as well. Printing hibernate debug messages doesn't help at all.
Since the problem started right after the column type is changed, so I did some Google search around bytea and postgres:

  • It is true that the bytea_output variable (sets the output format for values of type bytea) in postgres was default to 'hex' in an earlier version of postgersql jdbc jar but it is default to 'escape' in an later version.
  • Some Google search tells me that bytea data should load fine if you have hibernate 3.3.2GA and postgresql-9.0-801.jdbc3.jar and that is exactly what I've got.
  • So I tried to query the following:
select * from pg_settings where name='bytea_output';
select current_setting('bytea_output') from "showhome";
And the result is 'escape' as expected.
  • I also tried to add this particular variable for database showhome via the pgadmin UI, but without any luck.
  • The last thing I tried is to look into the postgresql.conf, that has #bytea_output = 'hex' # hex, escape which is a comment. I tried to insert the following line:
bytea_output = 'escape'
And oohlala!!! Images I've been craving to see now finally appear in the app.

(Note: make sure you follow this link HowTo: Edit PostgreSQL Configuration Files to fiddle with the postgresql.conf file permission)



Friday, June 17, 2011

No CSS styling in Firefox with Resin4 as the server

Read on if your site:
  • support Firefox
  • runs on Resin 4.x server
  • server is running on a Mac OS X

I just spent countless time on a very tiny problem.
Recently one of our project's resin server got upgraded from 3.x to 4.x, that breaks the security filter that we use (because Resin 4 now wrap the request). That is O.K. since the security filter is so out of date and we can solve it by using our own authenticator and the resin FormLogin tag.
Then after everything is working, I noticed if the site is opened in Firefox 4.0.1, it's plain, that is without any styling.
The CSS file is loaded properly though, the path is correct, it opens fine, the content type in response header is also correctly set to text/css.
I then even install the virtual machine, then Windows XP on it, then different versions of firefox by using firefoxportable, then check the site in those versions, not surprised, plain styling for all.
An interesting fact: if you use firebug and modify the css file (just by inserting a space), the entire site is colourful again, weird.

Accidently by checking the response header the countless number of times, one particular time the following caught my eyes: Content-Type: text/css; charset=MacRoman.
With this, I now have a more specific search term in Google and I got my answer in seconds. It turns out that Resin 4 has this bug.
I then tried to insert this in resin.xml with no luck:
-Dfile.encoding=UTF-8
One more search I found this is the line that does the trick:
Hallelujah! Now I can go home early, enjoy a nice hot drink at home on a cold windy Friday afternoon. Happy weekend everyone :)

Monday, April 18, 2011

Retrieve Data from iPhone

The story starts from a jailbreaked iPhone 3G that was left in a jacket pocket and had a long swim in the washing machine.
I was asked to help recover the contact numbers right after it.
After some searches, I found it is quite to achieve easy by knowing where the backup file is and with sqlite installed.

In windows7, you can find the backup files under:
C:\Documents and Settings\USERNAME\Application Data\Apple Computer\MobileSync\Backup
Then all you need to do is to download sqlite, query on the file that has the extension of *.mdbackup and save the output to a file and you are done. You can find detailed
steps here.

A few months later, this iPhone finished his trip to the other side of the world and back alive again. Sounds like a good ending huh?
BUT, one day his owner decided to upgrade it to the latest OS without having a second thought that it is already jailbreaked...

Right, this time it is locked. Everytime you switched on the phone, it tells you there's no sim card and you can make emergency call only. So he is back to my hand again. But crap I am not an iPhone expert, I failed to unlock it.

There is a video on youtube shows you how to unlock an iPhone 3G, I followed that but soon I find out it is not possible to do so since the iTunes is now updated to version 10.2 and they've locked that trick shown in the video.
Obviously you can downgrade your iTunes by remove the new one and install an old version, but it keeps saying a *.dll file that is created by a new version. If you've already have files backuped, you don't want to delete that...
So then I tried to install an old version (9.2) on an old laptop that has nothing important on there to have a go. This time I get a bit further to the point to restore the OS to 4.0. But if failed on verification... did some googling, there is a method to change your host file but it no longer works.
OK, so later on I find that you can downgrade it to a previous OS, so again I tried that, it restores fine but failed again when it tries to restart and verify, this time it is because it figured out the iPhone has already been updated to a later OS. Oh Christ! At this point I would rather go to the store and pay some money to unlock and un-jailbreak it. So, back to the original topic, retrieve data...

This time I found the *.mdbackup is missing! How awesome is that!? But thanks to Google, I found the solution again.
Obviously, the contact list is stored in this file under backup folder:
31bb7ba8914766d4ba40d6dfb6113c8b614be442.mddata


Refer to this tricks page for more information as well as some iPhone backup file schema.

Great! Now all I need to do is open up the windows command line tool, cd to where the backup file is and do:
"select ABPerson.first, ABPerson.last, ABMultiValue.value from ABPerson, ABMultiValu
e where ABMultiValue.record_id = ABPerson.ROWID" > ../contact.csv

Open the contact.csv file in the folder up a level and you will find all your contacts are there. I haven't figured out how to deal with the UTF-8 codings yet, but it is good enough for now. Of course there are softwares available on the internet but requires an expensive licence fee (from my point of view, a small software like this does not deserve $25 USD).

This time I also need to retrieve the photos if possible, I found a useful post but sorry I forgot where I keep the link. Apparently, iPhone does not backup your photos to a particular folder, simply renaming the *.mddata to *.jpg you can retrieve some of your photos!!! Some of the photos are still corrupted, but I got most of the photos that I want so it is good to me. You can find tools to retrieve corrupted jpg online with a price though.

So firstly copy all your *.mddata files (those that have a reasonable size to be a photo) to a place and in the windows command line tool, do:
ren *.mddata *.jpg

Try open them up in an image review tool to make sure they are the images that you want.

Tuesday, March 15, 2011

Get document height (cross-browser)

I have a page built in 70% of html and javascript that does drag and drop images within a table. IE is not doing a good job at all on figuring out the content height, underneath is a great way to get the content height no matter what kind of browser it is. It always return the greatest value of the three, very useful.

Source: http://james.padolsey.com/javascript/get-document-height-cross-browser



This function will return any document’s height. It’s been tested in IE6/7, FF2/3, Safari (Windows), Google Chrome and Opera 9.5. If the actual document’s body height is less thanthe viewport height then it will return the viewport height instead:

The Code:

function getDocHeight() {
var D = document;
return Math.max(
Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
Math.max(D.body.clientHeight, D.documentElement.clientHeight)
);
}

Usage:

alert( getDocHeight() );