Akshay Deo

Do(nt) Code Seriously

Why BATTERY_STATS Permission Is Not Gonna Work in KITKAT


I am working on an internal project which requires battery stats. A quick search would guide you to use android.permission.BATTERY_STATS, and you would be able to access battery stats directly. But you would notice that this code wont work in KITKAT.

Why ?

If you are coding in Android you must be familiar with the concept of permission group. Every of the android.permission.* is associated with a permissions group. And each of these permission groups have a protection level associated with them. There are six types of the protection levels.

Constant Value Description
normal A lower-risk permission that gives an application access to isolated
application-level features, with minimal risk to other applications,
the system, or the user. The system automatically grants this type
of permission to a requesting application at installation, without
asking for the user’s explicit approval (though the user always
has the option to review these permissions before installing).
dangerous 1 A higher-risk permission that would give a requesting application
access to private user data or control over the device that can
negatively impact the user. Because this type of permission
introduces potential risk, the system may not automatically
grant it to the requesting application. For example, any dangerous
permissions requested by an application may be displayed to the
user and require confirmation before proceeding, or some other
approach may be taken to avoid the user automatically allowing
the use of such facilities.
signature 2 A permission that the system is to grant only if the requesting
application is signed with the same certificate as the application
that declared the permission. If the certificates match, the system
automatically grants the permission without notifying the user or
asking for the user’s explicit approval.
signatureOrSystem 3 A permission that the system is to grant only to packages in the
Android system image or that are signed with the same
certificates. Please avoid using this option, as the
signature protection level should be sufficient for most needs and
works regardless of exactly where applications are installed. This
permission is used for certain special situations where multiple
vendors have applications built in to a system image which need
to share specific features explicitly because they are being built
together.
system 0x10 Additional flag from base permission type: this permission can also
be granted to any applications installed on the system image.
Please avoid using this option, as the
signature protection level should be sufficient for most needs and
works regardless of exactly where applications are installed. This
permission flag is used for certain special situations where multiple
vendors have applications built in to a system image which need
to share specific features explicitly because they are being built
together.
development 0x20 Additional flag from base permission type: this permission can also
(optionally) be granted to development applications.

Earlier BATTERY_STATS was having protection level as dangerous but from kitkat they have changed it to signature/system so unless your app is running as system app or your app is signed with same certificate as system apps, your app wont be able to access battery stats !!

Have a good day coders :)