0

Whenever Android App needs a handle to system service(such as LAYOUT_INFLATER_SERVICE or TELEPHONY_SERVICE), the usual way is to make a call to getSystemService() API with appropriate service name, like below:

Context.getSystemService(Context.TELEPHONY_SERVICE)

In AOSP, in the source code of TelephonyManager class, it is clear that 1 Constructor is "private" but this class also has 1 "Public" constructor as well.

public TelephonyManager(Context context)

Reference: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.0.0_r1/android/telephony/TelephonyManager.java?av=f

But when in code, when I try to create a class using this "public" constructor in below code:

TelephonyManager tm = new TelephonyManager(context);

During compilation only, Eclipse reports an error saying:

"The constructor TelephonyManager(context) is undefined"

So, somehow Android is ensuring that handle to system service can only be obtained using getSystemService() API, and not even through "public" constructor.

What is that code/technique used to achieve this?

There is @hide annotation which is associated with these constructors.

AFAIK, @hide attribute is just part of javadoc(droiddoc also), so the "hide" annotation just simply mean the method/class/field is excluded from the API docs.

I am not sure if this annotation is preventing "TelephonyManager " object creation.

AADProgramming
  • 6,077
  • 11
  • 38
  • 58
  • see https://code.google.com/p/doclava/wiki/JavadocTags#@hide – Populus Jan 22 '15 at 19:07
  • Ya, @hide removes that node and all of its children from the documentation. Means when Javadocs created, "description" of those classes and fields will be removed. Hence, we can not see description of public TelephonyManager(Context context) in http://developer.android.com/reference/android/telephony/TelephonyManager.html. But this does not imply that this annotation is preventing object creation....Right? – AADProgramming Jan 22 '15 at 19:11
  • This may be what you're looking for? http://stackoverflow.com/a/25723049/520857 – Populus Jan 22 '15 at 19:27
  • Useful info, but NO Code :( on how it has been achieved. And even answer over there ends with this note: f your SDK only consists of a jar file or something similar, there really isn't a way to hide symbols in it. :( – AADProgramming Jan 22 '15 at 19:33
  • "Useful info, but NO Code :( on how it has been achieved" -- the code would be in the firmware/SDK build process that creates the `android.jar` file for an SDK distribution. – CommonsWare Jan 22 '15 at 20:26

0 Answers0