Skip to content

Instantly share code, notes, and snippets.

@zaki50
Last active January 21, 2018 21:32
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save zaki50/7279212 to your computer and use it in GitHub Desktop.
Save zaki50/7279212 to your computer and use it in GitHub Desktop.
ためしに PrintService を作ってみた
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.zakky.myprintservice"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".MyPrintService"
android:permission="android.permission.BIND_PRINT_SERVICE">
<intent-filter>
<action android:name="android.printservice.PrintService" />
</intent-filter>
</service>
</application>
</manifest>
/*
* Copyright 2013 YAMAZAKI Makoto<makoto1975@gmail.com>
*
* Licensed 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 org.zakky.myprintservice;
import android.print.PrintAttributes;
import android.print.PrinterCapabilitiesInfo;
import android.print.PrinterId;
import android.print.PrinterInfo;
import android.printservice.PrintDocument;
import android.printservice.PrintJob;
import android.printservice.PrintService;
import android.printservice.PrinterDiscoverySession;
import android.util.Log;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class MyPrintService extends PrintService {
@Override
protected PrinterDiscoverySession onCreatePrinterDiscoverySession() {
Log.d("myprinter", "MyPrintService#onCreatePrinterDiscoverySession() called");
return new PrinterDiscoverySession() {
@Override
public void onStartPrinterDiscovery(List<PrinterId> priorityList) {
Log.d("myprinter", "PrinterDiscoverySession#onStartPrinterDiscovery(priorityList: " + priorityList + ") called");
if (!priorityList.isEmpty()) {
return;
}
final List<PrinterInfo> printers = new ArrayList<>();
final PrinterId printerId = generatePrinterId("aaa");
final PrinterInfo.Builder builder = new PrinterInfo.Builder(printerId, "dummy printer", PrinterInfo.STATUS_IDLE);
PrinterCapabilitiesInfo.Builder capBuilder = new PrinterCapabilitiesInfo.Builder(printerId);
capBuilder.addMediaSize(PrintAttributes.MediaSize.ISO_A4, true);
capBuilder.addMediaSize(PrintAttributes.MediaSize.ISO_A3, false);
capBuilder.addResolution(new PrintAttributes.Resolution("resolutionId", "default resolution", 600, 600), true);
capBuilder.setColorModes(PrintAttributes.COLOR_MODE_COLOR | PrintAttributes.COLOR_MODE_MONOCHROME, PrintAttributes.COLOR_MODE_COLOR);
builder.setCapabilities(capBuilder.build());
printers.add(builder.build());
addPrinters(printers);
}
@Override
public void onStopPrinterDiscovery() {
Log.d("myprinter", "MyPrintService#onStopPrinterDiscovery() called");
}
@Override
public void onValidatePrinters(List<PrinterId> printerIds) {
Log.d("myprinter", "MyPrintService#onValidatePrinters(printerIds: " + printerIds + ") called");
}
@Override
public void onStartPrinterStateTracking(PrinterId printerId) {
Log.d("myprinter", "MyPrintService#onStartPrinterStateTracking(printerId: " + printerId + ") called");
}
@Override
public void onStopPrinterStateTracking(PrinterId printerId) {
Log.d("myprinter", "MyPrintService#onStopPrinterStateTracking(printerId: " + printerId + ") called");
}
@Override
public void onDestroy() {
Log.d("myprinter", "MyPrintService#onDestroy() called");
}
};
}
@Override
protected void onPrintJobQueued(PrintJob printJob) {
Log.d("myprinter", "queued: " + printJob.getId().toString());
printJob.start();
final PrintDocument document = printJob.getDocument();
final FileInputStream in = new FileInputStream(document.getData().getFileDescriptor());
try {
final byte[] buffer = new byte[4];
@SuppressWarnings("unused")
final int read = in.read(buffer);
Log.d("myprinter", "first " + buffer.length + "bytes of content: " + toString(buffer));
} catch (IOException e) {
Log.d("myprinter", "", e);
} finally {
try {
in.close();
} catch (IOException e) {
assert true;
}
}
printJob.complete();
}
private static String toString(byte[] bytes) {
final StringBuilder sb = new StringBuilder();
for (byte b : bytes) {
sb.append(Byte.toString(b)).append(',');
}
if (sb.length() != 0) {
sb.setLength(sb.length() - 1);
}
return sb.toString();
}
@Override
protected void onRequestCancelPrintJob(PrintJob printJob) {
Log.d("myprinter", "canceled: " + printJob.getId().toString());
printJob.cancel();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment