1 /*
2  * Copyright (C) 2018 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.cts.devicepolicy.printingapp;
18 
19 import android.app.Activity;
20 import android.content.ComponentName;
21 import android.content.Intent;
22 import android.os.Bundle;
23 import android.os.CancellationSignal;
24 import android.os.ParcelFileDescriptor;
25 import android.print.PageRange;
26 import android.print.PrintAttributes;
27 import android.print.PrintDocumentAdapter;
28 import android.print.PrintJob;
29 import android.print.PrintManager;
30 
31 public class PrintActivity extends Activity {
32 
33     private static final String PRINT_JOB_NAME = "Test print job";
34     private static final String EXTRA_ERROR_MESSAGE = "error_message";
35     private static final int STATE_INIT = 0;
36     private static final int STATE_STARTED = 1;
37     private static final int STATE_FINISHED = 2;
38 
39     @Override
onStart()40     public void onStart() {
41         super.onStart();
42         PrintManager printManager = getSystemService(PrintManager.class);
43         final int[] state = new int[]{STATE_INIT};
44         PrintJob printJob = printManager.print(PRINT_JOB_NAME, new PrintDocumentAdapter() {
45             @Override
46             public void onStart() {
47                 if (state[0] != STATE_INIT) {
48                     fail("Unexpected call to onStart()");
49                 }
50                 state[0] = STATE_STARTED;
51             }
52 
53             @Override
54             public void onFinish() {
55                 if (state[0] != STATE_STARTED) {
56                     fail("Unexpected call to onFinish()");
57                 }
58                 state[0] = STATE_FINISHED;
59                 // Use RESULT_FIRST_USER for success to avoid false positives.
60                 setResult(RESULT_FIRST_USER);
61                 finish();
62             }
63 
64             @Override
65             public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes,
66                     CancellationSignal signal, PrintDocumentAdapter.LayoutResultCallback cb,
67                     Bundle extras) {
68                 fail("onLayout() should never be called");
69             }
70 
71             @Override
72             public void onWrite(PageRange[] pages, ParcelFileDescriptor dest, CancellationSignal signal,
73                     PrintDocumentAdapter.WriteResultCallback cb) {
74                 fail("onWrite() should never be called");
75             }
76         }, new PrintAttributes.Builder().build());
77         if (printJob != null) {
78             fail("print() should return null");
79         }
80     }
81 
fail(String message)82     private final void fail(String message) {
83         setResult(RESULT_FIRST_USER + 1, new Intent().putExtra(EXTRA_ERROR_MESSAGE, message));
84         finish();
85     }
86 }
87