Tuesday 20 December 2011

Display random number around a circular image

Bitmap bonuswheel = loadImage(R.drawable.bonuswheel);
Bitmap bmOverlay = bonuswheel.copy(Bitmap.Config.ARGB_8888, true);
// create a canvas on which to draw
Paint bonusspinnerFont = setFontProperties("fonts/MyriadWebPro-Bold.ttf", 255, 0, 0, 0, 20, false, 2);
Canvas canvas = new Canvas(bmOverlay);
Random randomGenerator = new Random();
for(int a=0;a<18;a++)
{
bonus_array[a] = a;//*randomGenerator.nextInt(100);
Log.v("COMPAIR",""+bonus_array[a]);
}
for(int a=0;a<18;a++)
{
canvas.drawText(""+bonus_array[a], 60, bonuswheel.getHeight()/2 , bonusspinnerFont);
canvas.rotate(20, bonuswheel.getWidth()/2, bonuswheel.getHeight()/2);
}

Friday 14 October 2011

How to increase Flash Performance

Taken from
http://www.anandvardhan.com/2007/06/27/flex-performance-tuning-tips/

If you have a type in AS3, which you are not sure of always use the As operator to cast the type before you use it. This avoids VM errors with try/catch, which slow the execution and is ten times slower than the As operator.
Array is access is slow if the array is sparse. It may be faster to put nulls in empty values as this speeds things up. Array misses are very slow, up to 20 times slower than finding a valid entry.

Avoid implicit type conversion. In the player it will convert integers to numbers and back when asked to add integers. You might as well use numbers for everything and convert back to integer at the end.

Local variable access is faster, so assign variables to local if they are accessed a lot. They will be stored on the stack and access is much quicker.
Data Binding expressions take up memory and can slow down the application startup. It may be more efficient to do an assignment in code rather than using binding.

Find a slow computer and run your application. If it runs OK ship it! Other wise you can use flash.utils.getTimer():int to get a time value in miliseconds before and after some process to time it.

In Flex Builder 3 there is a profiler that allows you to take performance snapshots to record how long was spent in each function. This is useful to identify the areas of code that might benefit from optimization. While profiler is running everything is much slower. Often Mouse or similar Events will seem to take lots of time, you can ignore these. Investigate your own functions and see if they have been called too often or they take too long.

Friday 5 August 2011

Create password protected file in android

package com.cz.util;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
import java.util.Random;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;

import android.util.Log;


public class FileEncryptorDecryptor
{

private static String filename;
private static FileInputStream inFile;
private static FileOutputStream outFile;


public void encrypt(FileInputStream filename, FileOutputStream outFile,String password) throws Exception
{
inFile = filename;

// Use PBEKeySpec to create a key based on a password.
// The password is passed as a character array

PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());
SecretKeyFactory keyFactory =
SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey passwordKey = keyFactory.generateSecret(keySpec);

// PBE = hashing + symmetric encryption. A 64 bit random
// number (the salt) is added to the password and hashed
// using a Message Digest Algorithm (MD5 in this example.).
// The number of times the password is hashed is determined
// by the interation count. Adding a random number and
// hashing multiple times enlarges the key space.

byte[] salt = new byte[8];
Random rnd = new Random();
rnd.nextBytes(salt);
int iterations = 100;

//Create the parameter spec for this salt and interation count

PBEParameterSpec parameterSpec = new PBEParameterSpec(salt, iterations);

// Create the cipher and initialize it for encryption.

Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
cipher.init(Cipher.ENCRYPT_MODE, passwordKey, parameterSpec);

// Need to write the salt to the (encrypted) file. The
// salt is needed when reconstructing the key for decryption.

outFile.write(salt);

// Read the file and encrypt its bytes.

byte[] input = new byte[64];
int bytesRead;
while ((bytesRead = inFile.read(input)) != -1)
{
byte[] output = cipher.update(input, 0, bytesRead);
if (output != null) outFile.write(output);
}

byte[] output = cipher.doFinal();
if (output != null) outFile.write(output);

inFile.close();
outFile.flush();
outFile.close();

}

public void decrypt(FileInputStream filename, FileOutputStream outFile,String password) throws Exception
{
inFile = filename;

PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());
SecretKeyFactory keyFactory =
SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey passwordKey = keyFactory.generateSecret(keySpec);

// Read in the previouly stored salt and set the iteration count.

byte[] salt = new byte[8];
inFile.read(salt);
int iterations = 100;

PBEParameterSpec parameterSpec = new PBEParameterSpec(salt, iterations);

// Create the cipher and initialize it for decryption.

Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
cipher.init(Cipher.DECRYPT_MODE, passwordKey, parameterSpec);


byte[] input = new byte[64];
int bytesRead;
while ((bytesRead = inFile.read(input)) != -1)
{
byte[] output = cipher.update(input, 0, bytesRead);
if (output != null)
outFile.write(output);
}

byte[] output = cipher.doFinal();
if (output != null)
outFile.write(output);

inFile.close();
outFile.flush();
outFile.close();
}

}

Tuesday 19 July 2011

android load on startup service

Most of the time user needs to start a application on boot up, which needs to be auto start. I have searched a lot but didn;t find a good tutorial, but somehow I have managed it and it is working in my My Touch phone. Step to follow

1. Manifest FIle
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.android.events">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.VIBRATE" />
<application>
<receiver android:name=".MyBroadcastreceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name=".MyService" />
</application>
</manifest>


2. Receiver

package com.android.events;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyBroadcastreceiver extends BroadcastReceiver {
public static final String TAG = "Rajesh";

@Override
public void onReceive(Context context, Intent intent) {

if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
Intent pushIntent = new Intent(context, MyService.class);
context.startService(pushIntent);
}
}

}

3. MyService.class could be anything which extends Service

Hope this will help you :-)

Wednesday 13 July 2011

Check Google PR / Page Ranking For Multiple Domain Name

If you want to use a drop domain catcher script which could register only those domain which have Google Page Rank more than 3, then you might need a script like this example Google Page Rank Checker For Multiple Domain . Please upload only a text file, having domian names, followed by new line.

Sunday 10 July 2011

Android Chart Library

After googling I have found a good chart engine for android. Really good library to go ahead. I am thinking to donate something for him. First let me create a single project with this library and see if there is any flaw. But at a glance they did well. Please follow this link Android Chart Library

Tuesday 5 July 2011

Create a xml based family tree using Actionscript3

I haven't got a good flow chart creator tools in actionscript3, so I have started working on this myself and thought to share the code with you guys. So far I have been able to got some time to create a prototype of this and obviously a dirty code. Also here is a screen capture of the same. If you need the source code please drop me a mail at rajeshghosh1978@gmail.com.

How to draw an elastic line having circle at both end

If you are going to create a flow diagram or a white board application, you might need a line which acts like an elastic string, where as user can drag both end. So I have created for one of my project and thought, it might help someone to save some time. Here are the classes

elasticLine.as

package com.collectivezen{
import flash.display.DisplayObject;
import flash.display.Graphics;
import flash.display.Shape;
import flash.display.Sprite;
import flash.display.MovieClip;
import com.collectivezen.RoundRectangle;

import flash.filters.*;



public class elasticLine extends MovieClip {
private var rrX:Number;
private var rrY:Number;
private var rrWidth:Number;
private var rrHeight:Number;
private var bgColor:uint;
private var borderColor:uint;
private var borderSize:Number;
private var child:MovieClip = new MovieClip();
private var src:RoundRectangle;
private var dest:RoundRectangle;
private var _this:elasticLine;
private var lineDrawing:MovieClip = new MovieClip();

public function elasticLine(rrX:Number=0,rrY:Number=0,rrWidth:Number=10,rrHeight:Number=10,bgColor:uint=0x000000) {
this.rrX=rrX;
this.rrY=rrY;
this.rrWidth=rrWidth;
this.rrHeight=rrHeight;
this.bgColor=bgColor;
lineDrawing = new MovieClip();
lineDrawing.graphics.lineStyle(1, 0x000000, 1 );
lineDrawing.graphics.beginFill( bgColor, 1 );
lineDrawing.graphics.moveTo(rrX,rrY);
lineDrawing.graphics.lineTo(rrWidth,rrHeight);

///Glow Filter
var glow:GlowFilter = new GlowFilter();
glow.color = bgColor;
glow.alpha = 1;
glow.blurX = 8;
glow.blurY = 8;

var dropShadow:DropShadowFilter = new DropShadowFilter();
dropShadow.color = 0x000000;
dropShadow.blurX = 8;
dropShadow.blurY = 8;
dropShadow.angle = 0;
dropShadow.alpha = 0.5;
dropShadow.distance = 10;

var filtersArray:Array = new Array(glow, dropShadow);
lineDrawing.filters = filtersArray;
addChild(lineDrawing);
}
public function moveLine(src:RoundRectangle,dest:RoundRectangle) {
lineDrawing.graphics.clear();
lineDrawing.graphics.lineStyle(1, 0x000000, 1 );
lineDrawing.graphics.beginFill( this.bgColor, 1 );
lineDrawing.graphics.moveTo(src._CurCenX,src._CurCenY);
lineDrawing.graphics.lineTo(dest._CurCenX,dest._CurCenY);

///Glow Filter
var glow:GlowFilter = new GlowFilter();
glow.color = this.bgColor;
glow.alpha = 1;
glow.blurX = 8;
glow.blurY = 8;

var dropShadow:DropShadowFilter = new DropShadowFilter();
dropShadow.color = 0x000000;
dropShadow.blurX = 8;
dropShadow.blurY = 8;
dropShadow.angle = 0;
dropShadow.alpha = 0.5;
dropShadow.distance = 10;

var filtersArray:Array = new Array(glow, dropShadow);
lineDrawing.filters = filtersArray;
}
public function drawLine(_X:uint,_Y:uint,_eX:uint,_eY:uint,_col:uint) {
lineDrawing.graphics.clear();
lineDrawing.graphics.lineStyle(1, 0x000000, 1 );
lineDrawing.graphics.beginFill( _col, 1 );
lineDrawing.graphics.moveTo(_X,_Y);
lineDrawing.graphics.lineTo(_eX,_eY);

///Glow Filter
var glow:GlowFilter = new GlowFilter();
glow.color = _col;
glow.alpha = 1;
glow.blurX = 8;
glow.blurY = 8;

var dropShadow:DropShadowFilter = new DropShadowFilter();
dropShadow.color = 0x000000;
dropShadow.blurX = 8;
dropShadow.blurY = 8;
dropShadow.angle = 0;
dropShadow.alpha = 0.5;
dropShadow.distance = 10;

var filtersArray:Array = new Array(glow, dropShadow);
lineDrawing.filters = filtersArray;
}

}
}

See my different post to get com.collectivezen.RoundRectangle

Monday 4 July 2011

Steps to install new android application onto new htc device

1. Unplug the device

2. $ sudo gedit /etc/udev/rules.d/99-android.rules
(Taking help from http://dimitar.me/how-to-connect-your-android-phone-to-ubuntu-to-do-developmenttestinginstallations-or-tethering/ the file name should be 51--android.rules
but 99 is the latest.

3. add these lines and save
SUBSYSTEM=="usb", ATTR{idVendor}=="04e8", MODE="0666"
SUBSYSTEM=="usb", ATTR{idVendor}=="0bb4", MODE="0666"
SUBSYSTEM=="usb", ATTR{idVendor}=="18d1", MODE="0666"
SUBSYSTEM=="usb", ATTR{idVendor}=="1004", ATTR{idProduct}=="618e", MODE="0666"

4. Plug the device again.

5. If still doesn't work
$ adb kill-server
$ adb start-server

and do unplug and plug the device again.

Sunday 3 July 2011

Meteor Client JS doesn't work properly with Longpoll

As per the documentation given in the meteor.org , longpolling should poll the server once there is an event. But it is not working that way. It polls the server for a scheduled interval, based on meteor.js configuration. Basically Javascript is doing the socket connection using XHR like an AJAX, but I don't think it acts like a real socket connection and which is the reason meteor JS client needs to poll server at a regular interval, which is fine, but it breaks the Meteor JS documentation. Also there are some issues in the current meteor release. From a low bandwidth internet connection , it trapped into poll timeout and it polls again and again, which is creating multiple response at a certain point of time, so as a result , same message is comming more than one time for a given registerCallBack function. I am going to fix that issue in meteor.js, but it would be better if Meteor will release the next version.

Saturday 2 July 2011

Robotlegs

After going through some of the MVC framework over the net, Robotlegs seems good. I am going to pick this up in a couple of days and will post my comments. Between I would like to share some of my ready made script over here.