Search code examples
javaandroidreflectionandroid-audiomanager

Reflection of hide interface


How can i call registerAudioPortUpdateListener?
I succeeded to call a hidden function.
But, in this situation i need to call a function with hidden inner interface as parameter.

public class AudioManager {
    /**
     * Listener registered by client to be notified upon new audio port connections,
     * disconnections or attributes update.
     * @hide
     */
    public interface OnAudioPortUpdateListener {
        /**
         * Callback method called upon audio port list update.
         * @param portList the updated list of audio ports
         */
        public void onAudioPortListUpdate(AudioPort[] portList);

        /**
         * Callback method called upon audio patch list update.
         * @param patchList the updated list of audio patches
         */
        public void onAudioPatchListUpdate(AudioPatch[] patchList);

        /**
         * Callback method called when the mediaserver dies
         */
        public void onServiceDied();
    }


    /**
     * Register an audio port list update listener.
     * @hide
     */
    public void registerAudioPortUpdateListener(OnAudioPortUpdateListener l) {
        sAudioPortEventHandler.init();
        sAudioPortEventHandler.registerListener(l);
    }
}

Solution

  • Can you try this code , for me it is working in java,

    import java.lang.reflect.InvocationHandler;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.lang.reflect.Proxy;
    
    import de.apps.io.AudioManager;
    
    
    public class Application {
    
        public static void main(String[] args) {
            System.out.println("hello world");
    
    
    
             Class someInterface1 = null;
            try {
            //  someInterface = AudioManager.class.getDeclaredClasses();
    
                someInterface1 = Class.forName("de.apps.io.AudioManager$OnAudioPortUpdateListener");
            } catch (Exception e) {
                e.printStackTrace();
            }
    
             //System.out.println(someInterface);
    
    
             System.out.println(someInterface1);
    
    
    
            Object o = Proxy.newProxyInstance(someInterface1.getClassLoader(), new java.lang.Class[] { someInterface1 }, new Handler());
    
    
            AudioManager manager = new AudioManager();
            Method me = null;
            try {
                me = manager.getClass().getMethod("registerAudioPortUpdateListener", new java.lang.Class[] { someInterface1 });
            } catch (NoSuchMethodException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                me.invoke(manager, o);
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println(me);
    
        }
    
    
    
        static class Handler implements InvocationHandler
        {
    
            @Override
            public Object invoke(Object proxy, Method method, Object[] args)
                    throws Throwable {
    
                 String method_name = method.getName();
                 Class<?>[] classes = method.getParameterTypes();
    
                 System.out.println("called  " + method_name);
    
                return null;
            }
    
        }
    
    }