summaryrefslogtreecommitdiffstats
path: root/core/java/android/print/PrintManager.java
blob: df14a5c6c9860f5d7d7b5ea49468a437e07ca980 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * 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 android.print;

import android.content.Context;
import android.content.IntentSender;
import android.content.IntentSender.SendIntentException;
import android.os.CancellationSignal;
import android.os.Handler;
import android.os.ICancellationSignal;
import android.os.Looper;
import android.os.Message;
import android.os.ParcelFileDescriptor;
import android.os.RemoteException;
import android.print.PrintAdapter.PrintProgressCallback;
import android.util.Log;

import com.android.internal.os.SomeArgs;

import libcore.io.IoUtils;

import java.io.File;
import java.io.FileDescriptor;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * System level service for accessing the printing capabilities of the platform.
 * <p>
 * To obtain a handle to the print manager do the following:
 * </p>
 * <pre>
 * PrintManager printManager =
 *         (PrintManager) context.getSystemService(Context.PRINT_SERVICE);
 * </pre>
 */
public final class PrintManager {

    private static final String LOG_TAG = "PrintManager";

    /** @hide */
    public static final int APP_ID_ANY = -2;

    private final Context mContext;

    private final IPrintManager mService;

    private final int mUserId;

    private final int mAppId;

    private final PrintClient mPrintClient;

    private final Handler mHandler;

    /**
     * Listener for the state of a print job.
     */
    public static interface PrintJobStateListener {
        public void onStateChanged(int state);
    }

    /**
     * Creates a new instance.
     *
     * @param context The current context in which to operate.
     * @param service The backing system service.
     *
     * @hide
     */
    public PrintManager(Context context, IPrintManager service, int userId, int appId) {
        mContext = context;
        mService = service;
        mUserId = userId;
        mAppId = appId;
        mPrintClient = new PrintClient(this);
        mHandler = new Handler(context.getMainLooper(), null, false) {
            @Override
            public void handleMessage(Message message) {
                SomeArgs args = (SomeArgs) message.obj;
                Context context = (Context) args.arg1;
                IntentSender intent = (IntentSender) args.arg2;
                args.recycle();
                try {
                    context.startIntentSender(intent, null, 0, 0, 0);
                } catch (SendIntentException sie) {
                    Log.e(LOG_TAG, "Couldn't start print job config activity.", sie);
                }
            }
        };
    }

    /**
     * Creates an instance that can access all print jobs.
     *
     * @param userId The user id for which to get all print jobs.
     * @return An instance of the caller has the permission to access
     * all print jobs, null otherwise.
     *
     * @hide
     */
    public PrintManager getGlobalPrintManagerForUser(int userId) {
        return new PrintManager(mContext, mService, userId, APP_ID_ANY);
    }

    PrintJobInfo getPrintJob(int printJobId) {
        try {
            return mService.getPrintJob(printJobId, mAppId, mUserId);
        } catch (RemoteException re) {
            Log.e(LOG_TAG, "Error getting print job:" + printJobId, re);
        }
        return null;
    }

    /**
     * Gets the print jobs for this application.
     *
     * @return The print job list.
     *
     * @see PrintJob
     */
    public List<PrintJob> getPrintJobs() {
        try {
            List<PrintJobInfo> printJobInfos = mService.getPrintJobs(mAppId, mUserId);
            if (printJobInfos == null) {
                return Collections.emptyList();
            }
            final int printJobCount = printJobInfos.size();
            List<PrintJob> printJobs = new ArrayList<PrintJob>(printJobCount);
            for (int i = 0; i < printJobCount; i++) {
                printJobs.add(new PrintJob(printJobInfos.get(i), this));
            }
            return printJobs;
        } catch (RemoteException re) {
            Log.e(LOG_TAG, "Error getting print jobs!", re);
        }
        return Collections.emptyList();
    }

    ICancellationSignal cancelPrintJob(int printJobId) {
        try {
            mService.cancelPrintJob(printJobId, mAppId, mUserId);
        } catch (RemoteException re) {
            Log.e(LOG_TAG, "Error cancleing a print job:" + printJobId, re);
        }
        return null;
    }

    /**
     * Creates a print job for printing a file with default print attributes.
     *
     * @param printJobName A name for the new print job.
     * @param pdfFile The PDF file to print.
     * @param attributes The default print job attributes.
     * @return The created print job.
     */
    public PrintJob print(String printJobName, File pdfFile, PrintAttributes attributes) {
        PrintFileAdapter printable = new PrintFileAdapter(pdfFile);
        return print(printJobName, printable, attributes);
    }

    /**
     * Creates a print job for printing a {@link PrintAdapter} with default print
     * attributes.
     *
     * @param printJobName A name for the new print job.
     * @param printAdapter The printable adapter to print.
     * @param attributes The default print job attributes.
     * @return The created print job.
     */
    public PrintJob print(String printJobName, PrintAdapter printAdapter,
            PrintAttributes attributes) {
        PrintAdapterDelegate delegate = new PrintAdapterDelegate(printAdapter,
                mContext.getMainLooper());
        try {
            PrintJobInfo printJob = mService.print(printJobName, mPrintClient, delegate,
                    attributes, mAppId, mUserId);
            if (printJob != null) {
                return new PrintJob(printJob, this);
            }
        } catch (RemoteException re) {
            Log.e(LOG_TAG, "Error creating a print job", re);
        }
        return null;
    }

    private static final class PrintClient extends IPrintClient.Stub {

        private final WeakReference<PrintManager> mWeakPrintManager;

        public PrintClient(PrintManager manager) {
            mWeakPrintManager = new WeakReference<PrintManager>(manager);
        }

        @Override
        public void startPrintJobConfigActivity(IntentSender intent)  {
            PrintManager manager = mWeakPrintManager.get();
            if (manager != null) {
                SomeArgs args = SomeArgs.obtain();
                args.arg1 =  manager.mContext;
                args.arg2 = intent;
                manager.mHandler.obtainMessage(0, args).sendToTarget();
            }
        }
    }

    private static final class PrintAdapterDelegate extends IPrintAdapter.Stub {
        private final Object mLock = new Object();

        private PrintAdapter mPrintAdapter;

        private Handler mHandler;

        public PrintAdapterDelegate(PrintAdapter printAdapter, Looper looper) {
            mPrintAdapter = printAdapter;
            mHandler = new MyHandler(looper);
        }

        @Override
        public void start() {
            synchronized (mLock) {
                if (isFinishedLocked()) {
                    return;
                }
                mHandler.obtainMessage(MyHandler.MESSAGE_START,
                        mPrintAdapter).sendToTarget();
            }
        }

        @Override
        public void printAttributesChanged(PrintAttributes attributes) {
            synchronized (mLock) {
                if (isFinishedLocked()) {
                    return;
                }
                SomeArgs args = SomeArgs.obtain();
                args.arg1 = mPrintAdapter;
                args.arg2 = attributes;
                mHandler.obtainMessage(MyHandler.MESSAGE_PRINT_ATTRIBUTES_CHANGED,
                        args).sendToTarget();
            }
        }

        @Override
        public void print(List<PageRange> pages, ParcelFileDescriptor fd,
                IPrintProgressListener progressListener) {
            synchronized (mLock) {
                if (isFinishedLocked()) {
                    return;
                }
                SomeArgs args = SomeArgs.obtain();
                args.arg1 = mPrintAdapter;
                args.arg2 = pages;
                args.arg3 = fd.getFileDescriptor();
                args.arg4 = progressListener;
                mHandler.obtainMessage(MyHandler.MESSAGE_PRINT, args).sendToTarget();
            }
        }

        @Override
        public void finish() {
            synchronized (mLock) {
                if (isFinishedLocked()) {
                    return;
                }
                mHandler.obtainMessage(MyHandler.MESSAGE_FINIS,
                        mPrintAdapter).sendToTarget();
            }
        }

        private boolean isFinishedLocked() {
            return mPrintAdapter == null;
        }

        private void finishLocked() {
            mPrintAdapter = null;
            mHandler = null;
        }

        private final class MyHandler extends Handler {
            public static final int MESSAGE_START = 1;
            public static final int MESSAGE_PRINT_ATTRIBUTES_CHANGED = 2;
            public static final int MESSAGE_PRINT = 3;
            public static final int MESSAGE_FINIS = 4;

            public MyHandler(Looper looper) {
                super(looper, null, true);
            }

            @Override
            public void handleMessage(Message message) {
                switch (message.what) {
                    case MESSAGE_START: {
                        PrintAdapter adapter = (PrintAdapter) message.obj;
                        adapter.onStart();
                    } break;

                    case MESSAGE_PRINT_ATTRIBUTES_CHANGED: {
                        SomeArgs args = (SomeArgs) message.obj;
                        PrintAdapter adapter = (PrintAdapter) args.arg1;
                        PrintAttributes attributes = (PrintAttributes) args.arg2;
                        args.recycle();
                        adapter.onPrintAttributesChanged(attributes);
                    } break;

                    case MESSAGE_PRINT: {
                        SomeArgs args = (SomeArgs) message.obj;
                        PrintAdapter adapter = (PrintAdapter) args.arg1;
                        @SuppressWarnings("unchecked")
                        List<PageRange> pages = (List<PageRange>) args.arg2;
                        final FileDescriptor fd = (FileDescriptor) args.arg3;
                        IPrintProgressListener listener = (IPrintProgressListener) args.arg4;
                        args.recycle();
                        try {
                            ICancellationSignal remoteSignal = CancellationSignal.createTransport();
                            listener.onWriteStarted(adapter.getInfo(), remoteSignal);

                            CancellationSignal localSignal = CancellationSignal.fromTransport(
                                    remoteSignal);
                            adapter.onPrint(pages, fd, localSignal,
                                    new PrintProgressListenerWrapper(listener) {
                                        @Override
                                        public void onPrintFinished(List<PageRange> pages) {
                                            IoUtils.closeQuietly(fd);
                                            super.onPrintFinished(pages);
                                        }

                                        @Override
                                        public void onPrintFailed(CharSequence error) {
                                            IoUtils.closeQuietly(fd);
                                            super.onPrintFailed(error);
                                        }
                                    });
                        } catch (RemoteException re) {
                            Log.e(LOG_TAG, "Error printing", re);
                            IoUtils.closeQuietly(fd);
                        }
                    } break;

                    case MESSAGE_FINIS: {
                        PrintAdapter adapter = (PrintAdapter) message.obj;
                        adapter.onFinish();
                        synchronized (mLock) {
                            finishLocked();
                        }
                    } break;

                    default: {
                        throw new IllegalArgumentException("Unknown message: "
                                + message.what);
                    }
                }
            }
        }
    }

    private static abstract class PrintProgressListenerWrapper extends PrintProgressCallback {

        private final IPrintProgressListener mWrappedListener;

        public PrintProgressListenerWrapper(IPrintProgressListener listener) {
            mWrappedListener = listener;
        }

        @Override
        public void onPrintFinished(List<PageRange> pages) {
            try {
                mWrappedListener.onWriteFinished(pages);
            } catch (RemoteException re) {
                Log.e(LOG_TAG, "Error calling onWriteFinished", re);
            }
        }

        @Override
        public void onPrintFailed(CharSequence error) {
            try {
                mWrappedListener.onWriteFailed(error);
            } catch (RemoteException re) {
                Log.e(LOG_TAG, "Error calling onWriteFailed", re);
            }
        }
    }
}