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(
OnPrintJobStateChangeListener listener) {
if (this.listener == listener) {
if (this.listener == listener)
return;
}
if (listener == null) {
unsetOnPrintJobStateChangeListener();
return;
}
Class<?> interfaceCls = null;
this.listener =
new WeakReference<OnPrintJobStateChangeListener>(listener);
try {
interfaceCls = Class.forName(
Class<?> interfaceCls = Meta.getClass(
"android.print.PrintManager$PrintJobStateChangeListener");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
if (interfaceCls == null)
return;
this.listener =
new WeakReference<OnPrintJobStateChangeListener>(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(
Class<?> interfaceCls = Meta.getClass(
"android.print.PrintManager$PrintJobStateChangeListener");
} catch (ClassNotFoundException e) {
// Nothing to do
}
if (interfaceCls == null || proxy == null)
return;
@ -238,6 +225,7 @@ 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(),
method, printJobId);

View File

@ -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(
Class<?> interfaceCls = Meta.getClass(
"android.print.PrinterDiscoverySession$OnPrintersChangeListener");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
if (interfaceCls == null)
return;
this.listener = listener;
Method method = Meta.getMethod(session.getClass(),
"setOnPrintersChangeListener", interfaceCls);

View File

@ -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,10 +66,9 @@ public abstract class Meta {
return cls.getDeclaredMethod(name, params);
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
return null;
}
}
/**
* Invokes the method on the given object with the specified arguments.