Tuesday, May 17, 2011

how to answer an incoming call in android 2.3

Today, I was researching on a how to answer incoming call in android 2.3 automatically. My first thought was using "ITelephony.aidl" and call the answerRingingCall(). When i looked into more details answerRingingCall() function all requires MODIFY_PHONE_STATE permission which is marked as a as "signatureOrSystem" which is mentioned here
http://android.git.kernel.org/?p=platform/frameworks/base.git;a=commit;h=f4ece2086f3b7060edc4b93a12f04c9af648867a

and here
http://code.google.com/p/android/issues/detail?id=15031

bummer. Then thought of a another work-around and bluetooth headset popuped to my mind. In this all I had to do was to call start new intent with ACTION_UP. It worked!

here is the soulution

BroadcastReceiver PhoneState = new BroadcastReceiver() {

@Override
public void onReceive(Context context, Intent intent) {
if (!intent.getAction().equals("android.intent.action.PHONE_STATE")) return;
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);

Intent answer = new Intent(Intent.ACTION_MEDIA_BUTTON);
answer.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
sendOrderedBroadcast(answer, null);

}

return;
}};

// Update on 2011-09-27

In Android 2.3.3 HTC Sensation this piece of code does not work. Reason is in 2.3.3 I found a HeadsetObserver  listening for actual bluetooth plug-in event. So you need to send a Intent pretending there is a headset connected already. To fix this problem you need to send the ACTION_HEADSET_PLUG Intent before calling the above code.


 Intent buttonDown = new Intent(Intent.ACTION_MEDIA_BUTTON);          
 buttonDown.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK));
 context.sendOrderedBroadcast(buttonDown, "android.permission.CALL_PRIVILEGED");

 // froyo and beyond trigger on buttonUp instead of buttonDown
 Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON);            
 buttonUp.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
 context.sendOrderedBroadcast(buttonUp, "android.permission.CALL_PRIVILEGED");

 Intent headSetUnPluggedintent = new Intent(Intent.ACTION_HEADSET_PLUG);
 headSetUnPluggedintent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
 headSetUnPluggedintent.putExtra("state", 1); // 0 = unplugged  1 = Headset with microphone 2 = Headset without microphone
 headSetUnPluggedintent.putExtra("name", "Headset");
 // TODO: Should we require a permission?
 sendOrderedBroadcast(headSetUnPluggedintent, null);

// Update on 2012-04-27

Lot of people are asking me how to end call, here is the code


Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON);            
buttonUp.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK));
getContext().sendOrderedBroadcast(buttonUp, "android.permission.CALL_PRIVILEGED");

// Update on 2012 - 08 - 21
This method no longer works on Android Jelly Bean (4.1.1) and possibly later versions. Google has blocked  3rd party apps from broadcasting Intent.ACTION_MEDIA_BUTTON.

java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.HEADSET_PLUG from pid=30943, uid=10064


Android 1 : Me 0

How to turn on noise suppression in Android

While researching on another topic i found the answer to this question. it's easy all you have to do is set the parameter.

AudioManager localAudioManager = (AudioManager)paramContext.getSystemService("audio");
localAudioManager.setParameters("noise_suppression=auto");

or if you do not want
localAudioManager.setParameters("noise_suppression=off");

Monday, May 9, 2011

How to create a Thumbnail image in Android

Today, I wanted to create an image thumbnail in Android. There is no easyway out. So I wrote the code by my self.

byte[] imageData = null;

try
{

final int THUMBNAIL_SIZE = 64;

FileInputStream fis = new FileInputStream(fileName);
Bitmap imageBitmap = BitmapFactory.decodeStream(fis);

Float width = new Float(imageBitmap.getWidth());
Float height = new Float(imageBitmap.getHeight());
Float ratio = width/height;
imageBitmap = Bitmap.createScaledBitmap(imageBitmap, (int)(THUMBNAIL_SIZE * ratio), THUMBNAIL_SIZE, false);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
imageData = baos.toByteArray();

}
catch(Exception ex) {

}