Support for picking a printer for iOS and Android
This commit is contained in:
115
src/android/ext/PrintManager.java
Normal file
115
src/android/ext/PrintManager.java
Normal file
@@ -0,0 +1,115 @@
|
||||
/*
|
||||
Copyright 2013-2016 appPlant GmbH
|
||||
|
||||
Licensed to the Apache Software Foundation (ASF) under one
|
||||
or more contributor license agreements. See the NOTICE file
|
||||
distributed with this work for additional information
|
||||
regarding copyright ownership. The ASF licenses this file
|
||||
to you under the Apache License, Version 2.0 (the
|
||||
"License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing,
|
||||
software distributed under the License is distributed on an
|
||||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations
|
||||
under the License.
|
||||
*/
|
||||
|
||||
package de.appplant.cordova.plugin.printer.ext;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import de.appplant.cordova.plugin.printer.reflect.Meta;
|
||||
|
||||
public final class PrintManager {
|
||||
|
||||
/**
|
||||
* The application context.
|
||||
*/
|
||||
private Context ctx;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param context The context where to look for.
|
||||
*/
|
||||
public PrintManager (Context context) {
|
||||
this.ctx = context;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an instance from PrintManager service.
|
||||
*
|
||||
* @return A PrintManager instance.
|
||||
*/
|
||||
public final android.print.PrintManager getInstance () {
|
||||
return (android.print.PrintManager)
|
||||
ctx.getSystemService(Context.PRINT_SERVICE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the list of installed print services.
|
||||
*
|
||||
* @return The found service list or an empty list.
|
||||
*/
|
||||
public final List<PrintServiceInfo> getInstalledPrintServices () {
|
||||
List printers = (List) Meta.invokeMethod(getInstance(),
|
||||
"getInstalledPrintServices");
|
||||
|
||||
ArrayList<PrintServiceInfo> services =
|
||||
new ArrayList<PrintServiceInfo>();
|
||||
|
||||
if (printers == null)
|
||||
return Collections.emptyList();
|
||||
|
||||
for (Object printer : printers) {
|
||||
services.add(new PrintServiceInfo(printer));
|
||||
}
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the list of enabled print services.
|
||||
*
|
||||
* @return The found service list or an empty list.
|
||||
*/
|
||||
public final List<PrintServiceInfo> getEnabledPrintServices () {
|
||||
List printers = (List) Meta.invokeMethod(getInstance(),
|
||||
"getEnabledPrintServices");
|
||||
|
||||
ArrayList<PrintServiceInfo> services =
|
||||
new ArrayList<PrintServiceInfo>();
|
||||
|
||||
if (printers == null)
|
||||
return Collections.emptyList();
|
||||
|
||||
for (Object printer : printers) {
|
||||
services.add(new PrintServiceInfo(printer));
|
||||
}
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an session object to discover all printer services. To do so
|
||||
* you need to register a listener object and start the discovery process.
|
||||
*
|
||||
* @return An instance of class PrinterDiscoverySession.
|
||||
*/
|
||||
public final PrinterDiscoverySession createPrinterDiscoverySession () {
|
||||
Object session = Meta.invokeMethod(getInstance(),
|
||||
"createPrinterDiscoverySession");
|
||||
|
||||
return new PrinterDiscoverySession(session);
|
||||
}
|
||||
|
||||
}
|
||||
62
src/android/ext/PrintServiceInfo.java
Normal file
62
src/android/ext/PrintServiceInfo.java
Normal file
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
Copyright 2013-2016 appPlant GmbH
|
||||
|
||||
Licensed to the Apache Software Foundation (ASF) under one
|
||||
or more contributor license agreements. See the NOTICE file
|
||||
distributed with this work for additional information
|
||||
regarding copyright ownership. The ASF licenses this file
|
||||
to you under the Apache License, Version 2.0 (the
|
||||
"License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing,
|
||||
software distributed under the License is distributed on an
|
||||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations
|
||||
under the License.
|
||||
*/
|
||||
|
||||
package de.appplant.cordova.plugin.printer.ext;
|
||||
|
||||
import android.content.pm.ResolveInfo;
|
||||
|
||||
import de.appplant.cordova.plugin.printer.reflect.Meta;
|
||||
|
||||
public final class PrintServiceInfo {
|
||||
|
||||
/**
|
||||
* The wrapped object of type android.printservice.PrintServiceInfo
|
||||
*/
|
||||
private Object obj;
|
||||
|
||||
/**
|
||||
* Wraps the object of the hidden class
|
||||
* android.printservice.PrintServiceInfo.
|
||||
*
|
||||
* @param wrappedObj The object to wrap.
|
||||
*/
|
||||
PrintServiceInfo (Object wrappedObj) {
|
||||
obj = wrappedObj;
|
||||
}
|
||||
|
||||
/**
|
||||
* The add printers activity name.
|
||||
*
|
||||
* @return The add printers activity name.
|
||||
*/
|
||||
public final String getAddPrintersActivityName() {
|
||||
return (String) Meta.invokeMethod(obj, "getAddPrintersActivityName");
|
||||
}
|
||||
/**
|
||||
* The service {@link ResolveInfo}.
|
||||
*
|
||||
* @return The info.
|
||||
*/
|
||||
public final ResolveInfo getResolveInfo() {
|
||||
return (ResolveInfo) Meta.invokeMethod(obj, "getResolveInfo");
|
||||
}
|
||||
|
||||
}
|
||||
178
src/android/ext/PrinterDiscoverySession.java
Normal file
178
src/android/ext/PrinterDiscoverySession.java
Normal file
@@ -0,0 +1,178 @@
|
||||
/*
|
||||
Copyright 2013-2016 appPlant GmbH
|
||||
|
||||
Licensed to the Apache Software Foundation (ASF) under one
|
||||
or more contributor license agreements. See the NOTICE file
|
||||
distributed with this work for additional information
|
||||
regarding copyright ownership. The ASF licenses this file
|
||||
to you under the Apache License, Version 2.0 (the
|
||||
"License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing,
|
||||
software distributed under the License is distributed on an
|
||||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations
|
||||
under the License.
|
||||
*/
|
||||
|
||||
package de.appplant.cordova.plugin.printer.ext;
|
||||
|
||||
import android.print.PrinterInfo;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import de.appplant.cordova.plugin.printer.reflect.Meta;
|
||||
|
||||
public final class PrinterDiscoverySession {
|
||||
|
||||
/**
|
||||
* Required interface of the listener.
|
||||
*/
|
||||
public interface OnPrintersChangeListener {
|
||||
void onPrintersChanged(List<PrinterInfo> printerInfos);
|
||||
}
|
||||
|
||||
/**
|
||||
* Required to be able to register a listener of the expected type.
|
||||
*/
|
||||
private class OnPrintersChangeProxy implements InvocationHandler {
|
||||
@Override
|
||||
public Object invoke (Object o, Method method, Object[] objects)
|
||||
throws Throwable {
|
||||
|
||||
if (method.getName().equals("onPrintersChanged")) {
|
||||
onPrintersChanged();
|
||||
return null;
|
||||
} else throw new Exception();
|
||||
}
|
||||
|
||||
/**
|
||||
* Delegate the event to the listener.
|
||||
*/
|
||||
public void onPrintersChanged() {
|
||||
notifyOnPrintersChanged();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The wrapped session of type
|
||||
* android.print.PrinterDiscoverySession
|
||||
*/
|
||||
private Object session;
|
||||
|
||||
/**
|
||||
* Registered listener object.
|
||||
*/
|
||||
private OnPrintersChangeListener listener = null;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param session An instance of type
|
||||
* android.print.PrinterDiscoverySession
|
||||
*/
|
||||
public PrinterDiscoverySession (Object session) {
|
||||
this.session = session;
|
||||
}
|
||||
|
||||
/**
|
||||
* Start discovering available printers.
|
||||
*/
|
||||
public final void startPrinterDiscovery () {
|
||||
Method method = Meta.getMethod(session.getClass(),
|
||||
"startPrinterDiscovery", List.class);
|
||||
|
||||
Meta.invokeMethod(session, method, Collections.emptyList());
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop discovering printers.
|
||||
*/
|
||||
public final void stopPrinterDiscovery() {
|
||||
Meta.invokeMethod(session, "stopPrinterDiscovery");
|
||||
}
|
||||
|
||||
/**
|
||||
* If session is discovering printers.
|
||||
*/
|
||||
@SuppressWarnings("ConstantConditions")
|
||||
public final boolean isPrinterDiscoveryStarted() {
|
||||
return (Boolean) Meta.invokeMethod(session,
|
||||
"isPrinterDiscoveryStarted");
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the listener.
|
||||
*
|
||||
* @param listener Use null to unregister the listener.
|
||||
*/
|
||||
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();
|
||||
}
|
||||
|
||||
if (interfaceCls == null)
|
||||
return;
|
||||
|
||||
Method method = Meta.getMethod(session.getClass(),
|
||||
"setOnPrintersChangeListener", interfaceCls);
|
||||
|
||||
if (listener != null) {
|
||||
Class<?>[] interfaces = {interfaceCls};
|
||||
|
||||
proxy = Proxy.newProxyInstance(
|
||||
interfaceCls.getClassLoader(),
|
||||
interfaces,
|
||||
new OnPrintersChangeProxy()
|
||||
);
|
||||
}
|
||||
|
||||
Meta.invokeMethod(session, method, proxy);
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy the session if not already done.
|
||||
*/
|
||||
public final void destroy() {
|
||||
Meta.invokeMethod(session, "destroy");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of all yet discovered printers.
|
||||
*
|
||||
* @return List of their basic infos
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public final List<PrinterInfo> getPrinters() {
|
||||
Method method = Meta.getMethod(session.getClass(), "getPrinters");
|
||||
|
||||
return (List<PrinterInfo>) Meta.invokeMethod(session, method);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notifies the listener about the occurred event.
|
||||
*/
|
||||
private void notifyOnPrintersChanged() {
|
||||
if (listener != null) {
|
||||
listener.onPrintersChanged(getPrinters());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user