Wednesday, February 11, 2015

Reflection

Let's talk about reflection and "private" APIs.

Power Manager uses this specific snippet of code to be able to toggle Mobile Data on (almost all) Android devices. Up until Android Kitkat(4.4), this was a perfectly working, albeit ugly solution, which used a feature of the Java programming language called "reflection":


/*
 * Get the state of mobile data
 * returns false if disabled, true if enabled
 */ 
public final boolean getMobileDataEnabled(final Context context) {
    final ConnectivityManager manager = 

        context.getSystemService(Context.CONNECTIVITY_SERVICE); 
    final String methodName = "getMobileDataEnabled";
    final Method getMobileData = ConnectivityManager.class 

        .getDeclaredMethod(methodName);
    method.setAccessible(true);
    return (Boolean) method.invoke(manager);
}


/*
 * Set the state of mobile data
 * true enables data, false disables data
 */ 
public final void setMobileDataEnabled(final Context context, final boolean state) {
    final ConnectivityManager manager = 

        context.getSystemService(Context.CONNECTIVITY_SERVICE); 
    final String methodName = "setMobileDataEnabled";
    final Method setMobileData = ConnectivityManager.class 

        .getDeclaredMethod(methodName, Boolean.TYPE);
    method.setAccessible(true);
    method.invoke(manager, state);
}
 

This ugly reflection solution was all that a developer could do to work with the mobile data on an Android device, as there was no public API available for standard use. Was reflection good? No. It was a hack to get around limitations, it was unsupported on some devices, and it could disappear with no warning. But reflection was all we had as developers. And it stuck around on Android for the first 19 versions, all the way up to Kitkat 4.4.

In the new Android, Lollipop (5.0), all of this has changed.

There is no way to access or modify the state of the mobile data connection in Android Lollipop.
No way at all*. Any developer looking to perhaps, save power by toggling the state of mobile data on a device, will have no way to do so on Android Lollipop, and there is nothing that anyone can do at the moment.

With the growing adoption of Android Lollipop, I would just like to make this knowledge known in the hopes of avoiding angry users who do not understand why they can no longer use Power Manager or other related applications to toggle the mobile data of their device any longer. If you boot up one day to find Lollipop on your phone, and Power Manager no longer works to toggle data, please understand that I am powerless to do anything about it.

*Technically, there are system-accessible methods which do exist to change the state of mobile data, however, they are unusable by normal applications. See Android Issue tracker (78084, 81091).

========================
Follow pyamsoft around the Web for updates and announcements about the newest applications!
Like what I do?

Send me an email at: pyam.soft@gmail.com
Or find me online at: http://pyamsoft-official.blogspot.com/

Check out my Android Applications
Check out my Github Repositories
Check out my AUR packages

Follow my FaceBook Page
Follow my Google+ Page
=========================