Android Reflection - Android Jars - android

Case 1: Look at the code below. I am able to get Class and Method objects and it works well. Method I am trying to access is android.view.View::dispatchPointerEvent.
Case 2: When I replace class/Method with com.android.server.pm.PackageManagerService::grantPermissionsLPw, I get NoMethodFoundException. Class was accessible though.
Case 3: When I replace class/Method with android.hardware.input.InputManager::injectInputEvent, I get NoMethodFoundException. Class was accessible though.
Question is: Why some of the android class/methods are accessible via reflection and some other not?
Class _class = null;
try {
_class = Class.forName("android.view.View");
Log.i("Test", "Class found");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Method method = null;
try {
Log.i("Test", "Pre-Method found");
method = _class.getDeclaredMethod("dispatchPointerEvent",
MotionEvent.class);
Log.i("Test", "Method found");
} catch (Exception e) {
Log.i("Test","I failed."+e.getMessage()+e.toString());
//e.printStackTrace();
}

Try this
Class _class = null;
try {
_class = Class.forName("com.android.server.pm.PackageManagerService");
Log.i("Test", "Class found");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Method method = null;
try {
Log.i("Test", "Pre-Method found");
Class _class2 = Class.forName("android.content.pm.PackageParser$Package");
method = _class.getDeclaredMethod("grantPermissionsLPw",
_class2, boolean.class);
Log.i("Test", "Method found");
} catch (Exception e) {
Log.i("Test","I failed."+e.getMessage()+e.toString());
e.printStackTrace();
}

(Sorry but I cannot post comments, so have to post response)
Did you put the right method parameters? Where you have MotionEvent.class.

android.hardware.input.InputManager::injectInputEvent requires the android.permission.INJECT_EVENTS permission which is a system permission not available to apps. If you need to do this you'll have to root the device and sign your app as a system app.

Related

is it possible to catch Exception in Android?

Recently I modified my Android code for Android-12.
I was not having android-12 related bluetooth connect permissions for my API. So I handled as below :
public List<BluetoothDevice> getHfpDevices() {
List<BluetoothDevice> hfpConnectedDevs = new ArrayList<>();
try {
if (mHeadsetService != null) {
hfpConnectedDevs = mHeadsetService.getConnectedDevices();
}
} catch (SecurityException ex) {
Log.e(TAG, "Security Exception in Android-12", ex);
}
return hfpConnectedDevs;
}
Does this catch the securityException thrown when this API is accessed ? or do I need to handle the Exception itself ?
catch (Exception ex) {
PS : I don't want to add the requestPermission flow as of now. I just need to solve the exception raised when this API is called

send sms in pdu mode using android

I am developing an application which needs to send sms in pdu mode.
I am using this code but it gives NoSuchElementException on first line.
try {
Method m2 = sms.getClass().getDeclaredMethod("sendRawPdu", pdu.getClass(), pdu.getClass(), piSent.getClass(), piDelivered.getClass());
m2.setAccessible(true);
SmsMessage.SubmitPdu pdus = SmsMessage.getSubmitPdu(null, "", "Test", false);
Object[] arrayOfObject2 = new Object[5];
arrayOfObject2[0] = pdus.encodedScAddress;
arrayOfObject2[1] = pdus.encodedMessage;
arrayOfObject2[2] = piSent;
arrayOfObject2[3] = piDelivered;
arrayOfObject2[4] = null;
try {
m2.invoke(sms, arrayOfObject2);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
Any help will be appreciated.
I tried it on Lollipop, but there is no method related to sendRawPdu
Do a little more thing just print the list of methods available to check if there is any method related to sendRawPdu
Method[] methods = sms.getClass().getDeclaredMethods();
boolean methodAvailable = false;
for(Method m : methods) {
Log.d("SmsManager", m.toString());
if(m.toString().contains("sendRawPdu")) {
methodAvailable = true;
}
}
now you have methodAvailable, if it is true you can send Raw PDU, if not then you can't. sendRawPdu was available before JellyBeans. Try to run this on Pre JellyBeans devices.

setPin() shows error in eclipse that "setPin() is undefined for BluetoothDevice"

I don't find many BluetoothDevice methodes such as , setPasskey(), setPin(), setPairingConfirmation(), setRemoteOutOfBandData().
I searched on Android site as well but I don't find it. When I use these methods in my program in eclipse it shows me an error: its undefined for the type BluetoothDevice.
Are these obsolete now? If yes then what are the new methods of same type.
It is assumed that paring process is performed only by applications delivered with a platform!
This means that this application have access to hidden API. For example you can find hidden API for Bluetooth here.
It is strongly recommended to not use hidden API since it can change without warning in next Android release.
If you are still planning to use this API safest way is to use reflection:
try {
Class<? extends BluetoothDevice> c = device.getClass(); // BluetoothDevice.class
Method createBond = c.getMethod("createBond");
Object result = createBond.invoke(device);
Boolean castedResult = (Boolean)result;
Log.d(TAG, "Result: " + castedResult.toString());
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
There is also alternative way to easy use hidden API, but I didn't try it.

Why am I getting warnings Serialize ArrayList

I'm getting odd warnings in my reading of a ArrayList of Serializable objects. Here is the code:
public void loadBoard() {
FileInputStream fis = null;
ObjectInputStream is;
try {
fis = this.openFileInput(saveFile);
is = new ObjectInputStream(fis);
// Build up sample vision board
if (mVisionBoard == null) {
mVisionBoard = new ArrayList<VisionObject>();
} else {
mVisionBoard.clear();
}
ArrayList<VisionObject> readObject = (ArrayList<VisionObject>) is.readObject();
mVisionBoard = readObject;
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
Log.e(TAG, "loadBoard failed: "+e);
} catch (StreamCorruptedException e) {
e.printStackTrace();
Log.e(TAG, "loadBoard failed: "+e);
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "loadBoard failed: "+e);
} catch (ClassNotFoundException e) {
e.printStackTrace();
Log.e(TAG, "loadBoard failed: "+e);
}
}
and the warning I'm getting is (on readObject line):
"Type safety: unchecked cast from Object to ArrayList"
The few examples I've read indicate that this is the correct code for reading an ArrayList of serializable objects. The code I made to write the arraylist isn't giving me any warnings. Am I doing something wrong here?
kind of late but it will help someone...
the reason of the warning is because of the return of the method readObject...
see:
public final Object readObject()
it returns actually an object
and if you just by mistake read and deserialize a lets say String object ant try to cast that into an array list then you will get a runtime execption (the reason must be obvious)
in order to avoid that predictable failure you can check the type of the returned object before the cast...
that is why you get the warning:
"Type safety: unchecked cast from Object to ArrayList<VisionObject>"

Update missed calls notification on android

I need to cancel the missed calls notification for a certain number. I've seen the NotificationMgr class on com.android.phone but i'm unable to call it trough reflection. Is there any other way?
The code below will cancel the missed call notification.
To get the method work correctly, you must gain MODIFY_PHONE_STATE permission in AndroidManifest.xml like
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE"></uses-permission>
in your AndroidManifest.xml
String Log_Tag = "log";
try
{
Class serviceManagerClass = Class.forName("android.os.ServiceManager");
Method getServiceMethod = serviceManagerClass.getMethod("getService", String.class);
Object phoneService = getServiceMethod.invoke(null, "phone");
Class ITelephonyClass = Class.forName("com.android.internal.telephony.ITelephony");
Class ITelephonyStubClass = null;
for(Class clazz : ITelephonyClass.getDeclaredClasses())
{
if (clazz.getSimpleName().equals("Stub"))
{
ITelephonyStubClass = clazz;
break;
}
}
if (ITelephonyStubClass != null)
{
Class IBinderClass = Class.forName("android.os.IBinder");
Method asInterfaceMethod = ITelephonyStubClass.getDeclaredMethod("asInterface",
IBinderClass);
Object iTelephony = asInterfaceMethod.invoke(null, phoneService);
if (iTelephony != null)
{
Method cancelMissedCallsNotificationMethod = iTelephony.getClass().getMethod(
"cancelMissedCallsNotification");
cancelMissedCallsNotificationMethod.invoke(iTelephony);
}
else
{
Log.w(LOG_TAG, "Telephony service is null, can't call "
+ "cancelMissedCallsNotification");
}
}
else
{
Log.d(LOG_TAG, "Unable to locate ITelephony.Stub class!");
}
} catch (ClassNotFoundException ex)
{
Log.e(LOG_TAG,
"Failed to clear missed calls notification due to ClassNotFoundException!", ex);
} catch (InvocationTargetException ex)
{
Log.e(LOG_TAG,
"Failed to clear missed calls notification due to InvocationTargetException!",
ex);
} catch (NoSuchMethodException ex)
{
Log.e(LOG_TAG,
"Failed to clear missed calls notification due to NoSuchMethodException!", ex);
} catch (Throwable ex)
{
Log.e(LOG_TAG, "Failed to clear missed calls notification due to Throwable!", ex);
}
The original link is
http://sites.google.com/site/t2k269group/development-diary/reset-missed-calls-notification
If some know how to use the reflection to access class in com.android.phone, please tell me.
You cannot affect anyone other application's Notifications, let alone one for missed calls.

Categories

Resources