Added Meta#getClass

This commit is contained in:
Sebastián Katzer 2016-08-02 19:08:51 +02:00
parent c75bd32982
commit 144efee2a9
3 changed files with 33 additions and 34 deletions

View File

@ -166,30 +166,23 @@ public final class PrintManager {
public void setOnPrintJobStateChangeListener( public void setOnPrintJobStateChangeListener(
OnPrintJobStateChangeListener listener) { OnPrintJobStateChangeListener listener) {
if (this.listener == listener) { if (this.listener == listener)
return; return;
}
if (listener == null) { if (listener == null) {
unsetOnPrintJobStateChangeListener(); unsetOnPrintJobStateChangeListener();
return; return;
} }
Class<?> interfaceCls = null; Class<?> interfaceCls = Meta.getClass(
this.listener =
new WeakReference<OnPrintJobStateChangeListener>(listener);
try {
interfaceCls = Class.forName(
"android.print.PrintManager$PrintJobStateChangeListener"); "android.print.PrintManager$PrintJobStateChangeListener");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
if (interfaceCls == null) if (interfaceCls == null)
return; return;
this.listener =
new WeakReference<OnPrintJobStateChangeListener>(listener);
Method method = Meta.getMethod(getInstance().getClass(), Method method = Meta.getMethod(getInstance().getClass(),
"addPrintJobStateChangeListener", interfaceCls); "addPrintJobStateChangeListener", interfaceCls);
@ -208,14 +201,8 @@ public final class PrintManager {
* Removes the listener from the observing the state of print jobs. * Removes the listener from the observing the state of print jobs.
*/ */
public void unsetOnPrintJobStateChangeListener() { public void unsetOnPrintJobStateChangeListener() {
Class<?> interfaceCls = null; Class<?> interfaceCls = Meta.getClass(
try {
interfaceCls = Class.forName(
"android.print.PrintManager$PrintJobStateChangeListener"); "android.print.PrintManager$PrintJobStateChangeListener");
} catch (ClassNotFoundException e) {
// Nothing to do
}
if (interfaceCls == null || proxy == null) if (interfaceCls == null || proxy == null)
return; return;
@ -238,6 +225,7 @@ public final class PrintManager {
if (listener != null && listener.get() != null) { if (listener != null && listener.get() != null) {
Method method = Meta.getMethod(getInstance().getClass(), Method method = Meta.getMethod(getInstance().getClass(),
"getPrintJob", PrintJobId.class); "getPrintJob", PrintJobId.class);
PrintJob job = (PrintJob) Meta.invokeMethod(getInstance(), PrintJob job = (PrintJob) Meta.invokeMethod(getInstance(),
method, printJobId); method, printJobId);

View File

@ -110,21 +110,15 @@ public final class PrinterDiscoverySession {
public final void setOnPrintersChangeListener( public final void setOnPrintersChangeListener(
OnPrintersChangeListener listener) { OnPrintersChangeListener listener) {
Class<?> interfaceCls = null;
Object proxy = null; Object proxy = null;
Class<?> interfaceCls = Meta.getClass(
this.listener = listener;
try {
interfaceCls = Class.forName(
"android.print.PrinterDiscoverySession$OnPrintersChangeListener"); "android.print.PrinterDiscoverySession$OnPrintersChangeListener");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
if (interfaceCls == null) if (interfaceCls == null)
return; return;
this.listener = listener;
Method method = Meta.getMethod(session.getClass(), Method method = Meta.getMethod(session.getClass(),
"setOnPrintersChangeListener", interfaceCls); "setOnPrintersChangeListener", interfaceCls);

View File

@ -31,6 +31,24 @@ import java.lang.reflect.Method;
* This is an activity for selecting a printer. * This is an activity for selecting a printer.
*/ */
public abstract class Meta { 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. * Finds the method with given name and set of arguments.
* *
@ -48,10 +66,9 @@ public abstract class Meta {
return cls.getDeclaredMethod(name, params); return cls.getDeclaredMethod(name, params);
} catch (NoSuchMethodException e) { } catch (NoSuchMethodException e) {
e.printStackTrace(); e.printStackTrace();
}
return null; return null;
} }
}
/** /**
* Invokes the method on the given object with the specified arguments. * Invokes the method on the given object with the specified arguments.