0

I'm trying to write an app that is able to access the Android Keyguard app (the system app that holds the "lock screen"). I tried using reflection to get a reference to the running KeyguardUpdateMonitor instance, but so far, I've only received an java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation. Here's the code that causes the issue:

try {
    DexFile df = new DexFile(new File("/system/priv-app/Keyguard.apk"));
    String packageName = "com.android.keyguard";
    Context packageContext = context.createPackageContext(packageName, 
                                 Context.CONTEXT_INCLUDE_CODE
                               | Context.CONTEXT_IGNORE_SECURITY);
    ClassLoader cl = packageContext.getClassLoader();
    Class keyguardUpdateMonitor = df.loadClass(packageName 
                                    + ".KeyguardUpdateMonitor", cl);
    Method getKeyguardUpdateMonitorInstance 
        = keyguardUpdateMonitor.getMethod("getInstance", Context.class);
    Object argumentsForGetInstance[] = new Object[1];
    argumentsForGetInstance[0] = packageContext;
    Object keyguardUpdateMonitorInstance 
        = getKeyguardUpdateMonitorInstance.invoke(null, packageContext);
    ...
}

This code is running within a Class that is instanciated in a Service.

The signature of the method I'm trying to call is public static KeyguardUpdateMonitor getInstance(Context context).

  • Can I use reflection the way I intend to or do I NEED to have an own instance of the KeyguardUpdateMonitor?
  • Do I need special permissions?
  • Is there a fundamental mistake in my idea? If yes, how can I access the Keyguard in Android (KitKat and upwards)?

1 Answers1

0

IMHO - there is a fundamental mistake in your idea. The KeyguardUpdateMonitor is a class that is instantiated inside Home process. For your code (running as a service) it is out of process and thus protected from you by normal Linux (Unix) process memory protection. You tried to create package context but it is created if ever inside your process and has nothing to do with Home app. And on top of it the code of KeyguardUpdateMonitor (link below) does not seem to have "getInstance(Context)" static function...

The good news however is that https://github.com/android/platform_frameworks_policies_base/blob/master/phone/com/android/internal/policy/impl/KeyguardUpdateMonitor.java is a broadcast receiver.

You may be able to "talk" to it via sending intents to http://developer.android.com/reference/android/app/KeyguardManager.html getSystemService(KEYGUARD_SERVICE)

Hope it helps a little.

Leo
  • 790
  • 8
  • 10
  • Thank you for the hint, but the intents the broadcast receiver handles don't help me with my problem. Thanks anyway. The first paragraph was very helpful. Btw: The KeyguardUpdateMonitor does have an getInstance(Context) function, the link you provided is outdated. – wonkydonkey Jan 09 '15 at 13:17