From 144efee2a987666997c619e419f3f7266b3a5df7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Katzer?= Date: Tue, 2 Aug 2016 19:08:51 +0200 Subject: [PATCH] Added Meta#getClass --- src/android/ext/PrintManager.java | 32 ++++++-------------- src/android/ext/PrinterDiscoverySession.java | 14 +++------ src/android/reflect/Meta.java | 21 +++++++++++-- 3 files changed, 33 insertions(+), 34 deletions(-) diff --git a/src/android/ext/PrintManager.java b/src/android/ext/PrintManager.java index d184473..d1afe89 100644 --- a/src/android/ext/PrintManager.java +++ b/src/android/ext/PrintManager.java @@ -166,30 +166,23 @@ public final class PrintManager { public void setOnPrintJobStateChangeListener( OnPrintJobStateChangeListener listener) { - if (this.listener == listener) { + if (this.listener == listener) return; - } if (listener == null) { unsetOnPrintJobStateChangeListener(); return; } - Class interfaceCls = null; - - this.listener = - new WeakReference(listener); - - try { - interfaceCls = Class.forName( - "android.print.PrintManager$PrintJobStateChangeListener"); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } + Class interfaceCls = Meta.getClass( + "android.print.PrintManager$PrintJobStateChangeListener"); if (interfaceCls == null) return; + this.listener = + new WeakReference(listener); + Method method = Meta.getMethod(getInstance().getClass(), "addPrintJobStateChangeListener", interfaceCls); @@ -208,14 +201,8 @@ public final class PrintManager { * Removes the listener from the observing the state of print jobs. */ public void unsetOnPrintJobStateChangeListener() { - Class interfaceCls = null; - - try { - interfaceCls = Class.forName( - "android.print.PrintManager$PrintJobStateChangeListener"); - } catch (ClassNotFoundException e) { - // Nothing to do - } + Class interfaceCls = Meta.getClass( + "android.print.PrintManager$PrintJobStateChangeListener"); if (interfaceCls == null || proxy == null) return; @@ -238,7 +225,8 @@ public final class PrintManager { if (listener != null && listener.get() != null) { Method method = Meta.getMethod(getInstance().getClass(), "getPrintJob", PrintJobId.class); - PrintJob job = (PrintJob) Meta.invokeMethod(getInstance(), + + PrintJob job = (PrintJob) Meta.invokeMethod(getInstance(), method, printJobId); listener.get().onPrintJobStateChanged(job); diff --git a/src/android/ext/PrinterDiscoverySession.java b/src/android/ext/PrinterDiscoverySession.java index e4802be..d30f700 100644 --- a/src/android/ext/PrinterDiscoverySession.java +++ b/src/android/ext/PrinterDiscoverySession.java @@ -110,21 +110,15 @@ public final class PrinterDiscoverySession { public final void setOnPrintersChangeListener( OnPrintersChangeListener listener) { - Class interfaceCls = null; Object proxy = null; - - this.listener = listener; - - try { - interfaceCls = Class.forName( - "android.print.PrinterDiscoverySession$OnPrintersChangeListener"); - } catch (ClassNotFoundException e) { - e.printStackTrace(); - } + Class interfaceCls = Meta.getClass( + "android.print.PrinterDiscoverySession$OnPrintersChangeListener"); if (interfaceCls == null) return; + this.listener = listener; + Method method = Meta.getMethod(session.getClass(), "setOnPrintersChangeListener", interfaceCls); diff --git a/src/android/reflect/Meta.java b/src/android/reflect/Meta.java index a030e03..e5aea82 100644 --- a/src/android/reflect/Meta.java +++ b/src/android/reflect/Meta.java @@ -31,6 +31,24 @@ import java.lang.reflect.Method; * This is an activity for selecting a printer. */ public abstract class Meta { + + /** + * Tries to find the class for the given name. + * + * @param fullName + * The full class name including the package scope. + * @return + * The found class or null. + */ + public static Class getClass (String fullName) { + try { + return Class.forName(fullName); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + return null; + } + } + /** * Finds the method with given name and set of arguments. * @@ -48,9 +66,8 @@ public abstract class Meta { return cls.getDeclaredMethod(name, params); } catch (NoSuchMethodException e) { e.printStackTrace(); + return null; } - - return null; } /**