Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.printspooler; |
| 18 | |
| 19 | import java.util.List; |
| 20 | |
| 21 | import android.app.PendingIntent; |
| 22 | import android.app.Service; |
| 23 | import android.content.ComponentName; |
| 24 | import android.content.Intent; |
| 25 | import android.content.IntentSender; |
| 26 | import android.os.Handler; |
| 27 | import android.os.IBinder; |
| 28 | import android.os.Looper; |
| 29 | import android.os.Message; |
| 30 | import android.os.ParcelFileDescriptor; |
| 31 | import android.os.RemoteException; |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 32 | import android.print.IPrintDocumentAdapter; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 33 | import android.print.IPrintClient; |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 34 | import android.print.IPrintSpoolerClient; |
| 35 | import android.print.IPrintSpooler; |
| 36 | import android.print.IPrintSpoolerCallbacks; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 37 | import android.print.PrintAttributes; |
| 38 | import android.print.PrintJobInfo; |
| 39 | import android.util.Slog; |
| 40 | |
| 41 | import com.android.internal.os.SomeArgs; |
| 42 | |
| 43 | /** |
| 44 | * Service for exposing some of the {@link PrintSpooler} functionality to |
| 45 | * another process. |
| 46 | */ |
| 47 | public final class PrintSpoolerService extends Service { |
| 48 | |
| 49 | private static final String LOG_TAG = "PrintSpoolerService"; |
| 50 | |
| 51 | private Intent mStartPrintJobConfigActivityIntent; |
| 52 | |
| 53 | private PrintSpooler mSpooler; |
| 54 | |
| 55 | private Handler mHanlder; |
| 56 | |
| 57 | @Override |
| 58 | public void onCreate() { |
| 59 | super.onCreate(); |
| 60 | mStartPrintJobConfigActivityIntent = new Intent(PrintSpoolerService.this, |
| 61 | PrintJobConfigActivity.class); |
| 62 | mSpooler = PrintSpooler.getInstance(this); |
| 63 | mHanlder = new MyHandler(getMainLooper()); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public IBinder onBind(Intent intent) { |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 68 | return new IPrintSpooler.Stub() { |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 69 | @Override |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 70 | public void getPrintJobInfos(IPrintSpoolerCallbacks callback, |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 71 | ComponentName componentName, int state, int appId, int sequence) |
| 72 | throws RemoteException { |
| 73 | List<PrintJobInfo> printJobs = null; |
| 74 | try { |
| 75 | printJobs = mSpooler.getPrintJobs(componentName, state, appId); |
| 76 | } finally { |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 77 | callback.onGetPrintJobInfosResult(printJobs, sequence); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
| 81 | @Override |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 82 | public void getPrintJobInfo(int printJobId, IPrintSpoolerCallbacks callback, |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 83 | int appId, int sequence) throws RemoteException { |
| 84 | PrintJobInfo printJob = null; |
| 85 | try { |
| 86 | printJob = mSpooler.getPrintJob(printJobId, appId); |
| 87 | } finally { |
| 88 | callback.onGetPrintJobInfoResult(printJob, sequence); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | @Override |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 93 | public void cancelPrintJob(int printJobId, IPrintSpoolerCallbacks callback, |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 94 | int appId, int sequence) throws RemoteException { |
| 95 | boolean success = false; |
| 96 | try { |
| 97 | success = mSpooler.cancelPrintJob(printJobId, appId); |
| 98 | } finally { |
| 99 | callback.onCancelPrintJobResult(success, sequence); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | @SuppressWarnings("deprecation") |
| 104 | @Override |
| 105 | public void createPrintJob(String printJobName, IPrintClient client, |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 106 | IPrintDocumentAdapter printAdapter, PrintAttributes attributes, |
| 107 | IPrintSpoolerCallbacks callback, int appId, int sequence) |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 108 | throws RemoteException { |
| 109 | PrintJobInfo printJob = null; |
| 110 | try { |
| 111 | printJob = mSpooler.createPrintJob(printJobName, client, |
| 112 | attributes, appId); |
| 113 | if (printJob != null) { |
| 114 | Intent intent = mStartPrintJobConfigActivityIntent; |
| 115 | intent.putExtra(PrintJobConfigActivity.EXTRA_PRINTABLE, |
| 116 | printAdapter.asBinder()); |
| 117 | intent.putExtra(PrintJobConfigActivity.EXTRA_APP_ID, appId); |
| 118 | intent.putExtra(PrintJobConfigActivity.EXTRA_PRINT_JOB_ID, |
| 119 | printJob.getId()); |
| 120 | intent.putExtra(PrintJobConfigActivity.EXTRA_ATTRIBUTES, attributes); |
| 121 | |
| 122 | IntentSender sender = PendingIntent.getActivity( |
| 123 | PrintSpoolerService.this, 0, intent, PendingIntent.FLAG_ONE_SHOT |
| 124 | | PendingIntent.FLAG_CANCEL_CURRENT).getIntentSender(); |
| 125 | |
| 126 | SomeArgs args = SomeArgs.obtain(); |
| 127 | args.arg1 = client; |
| 128 | args.arg2 = sender; |
| 129 | mHanlder.obtainMessage(0, args).sendToTarget(); |
| 130 | } |
| 131 | } finally { |
| 132 | callback.onCreatePrintJobResult(printJob, sequence); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | public void setPrintJobState(int printJobId, int state, |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 138 | IPrintSpoolerCallbacks callback, int sequece) |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 139 | throws RemoteException { |
| 140 | boolean success = false; |
| 141 | try { |
| 142 | // TODO: Make sure the clients (print services) can set the state |
| 143 | // only to acceptable ones, e.g. not settings STATE_CREATED. |
| 144 | success = mSpooler.setPrintJobState(printJobId, state); |
| 145 | } finally { |
| 146 | callback.onSetPrintJobStateResult(success, sequece); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public void setPrintJobTag(int printJobId, String tag, |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 152 | IPrintSpoolerCallbacks callback, int sequece) |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 153 | throws RemoteException { |
| 154 | boolean success = false; |
| 155 | try { |
| 156 | success = mSpooler.setPrintJobTag(printJobId, tag); |
| 157 | } finally { |
| 158 | callback.onSetPrintJobTagResult(success, sequece); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public void writePrintJobData(ParcelFileDescriptor fd, int printJobId) { |
| 164 | mSpooler.writePrintJobData(fd, printJobId); |
| 165 | } |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 166 | |
| 167 | @Override |
| 168 | public void setClient(IPrintSpoolerClient client) { |
| 169 | mSpooler.setCleint(client); |
| 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public void notifyClientForActivteJobs() { |
| 174 | mSpooler.notifyClientForActivteJobs(); |
| 175 | } |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 176 | }; |
| 177 | } |
| 178 | |
| 179 | private static final class MyHandler extends Handler { |
| 180 | |
| 181 | public MyHandler(Looper looper) { |
| 182 | super(looper, null, true); |
| 183 | } |
| 184 | |
| 185 | @Override |
| 186 | public void handleMessage(Message message) { |
| 187 | SomeArgs args = (SomeArgs) message.obj; |
| 188 | IPrintClient client = (IPrintClient) args.arg1; |
| 189 | IntentSender sender = (IntentSender) args.arg2; |
| 190 | args.recycle(); |
| 191 | try { |
| 192 | client.startPrintJobConfigActivity(sender); |
| 193 | } catch (RemoteException re) { |
| 194 | Slog.i(LOG_TAG, "Error starting print job config activity!", re); |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | } |