1 package foo.bar.printservice;
2 
3 import android.app.ListActivity;
4 import android.os.Bundle;
5 import android.print.PrintJobId;
6 import android.view.View;
7 import android.widget.AdapterView;
8 import android.widget.ArrayAdapter;
9 
10 public class MyDialogActivity extends ListActivity {
11 
12     private static final int ITEM_INDEX_PRINT_NOW = 0;
13     private static final int ITEM_INDEX_PRINT_DELAYED = 1;
14     private static final int ITEM_INDEX_FAIL_NOW = 2;
15     private static final int ITEM_INDEX_FAIL_DELAYED = 3;
16     private static final int ITEM_INDEX_BLOCK_NOW = 4;
17     private static final int ITEM_INDEX_BLOCK_DELAYED = 5;
18     private static final int ITEM_INDEX_BLOCK_AND_DELAYED_UNBLOCK = 6;
19     private static final int ITEM_INDEX_PRINT_SLOWLY = 7;
20 
21     private static final int ITEM_INDEX_CANCEL_YES = 0;
22 
23     @Override
onCreate(Bundle savedInstanceState)24     protected void onCreate(Bundle savedInstanceState) {
25         super.onCreate(savedInstanceState);
26 
27         PrintJobId printJobId = getIntent().getParcelableExtra(
28                 MyPrintService.INTENT_EXTRA_PRINT_JOB_ID);
29         final int actionType = getIntent().getIntExtra(MyPrintService.INTENT_EXTRA_ACTION_TYPE,
30                 MyPrintService.ACTION_TYPE_ON_PRINT_JOB_PENDING);
31 
32         if (actionType == MyPrintService.ACTION_TYPE_ON_PRINT_JOB_PENDING) {
33             createActionTypeOnPrintJobPendingUi(printJobId);
34         } else {
35             createActionTypeOnReqeustCancelPrintJobUi(printJobId);
36         }
37     }
38 
createActionTypeOnPrintJobPendingUi(final PrintJobId printJobId)39     private void createActionTypeOnPrintJobPendingUi(final PrintJobId printJobId) {
40         setTitle(getString(R.string.on_print_job_pending_activity_title));
41 
42         setListAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,
43                 getResources().getStringArray(R.array.on_print_job_queued_actions)));
44         getListView().setOnItemClickListener((parent, view, position, id) -> {
45             switch (position) {
46                 case ITEM_INDEX_PRINT_NOW: {
47                     MyPrintService.peekInstance().handleQueuedPrintJob(printJobId);
48                 } break;
49 
50                 case ITEM_INDEX_PRINT_DELAYED: {
51                     MyPrintService.peekInstance().handleQueuedPrintJobDelayed(printJobId);
52                 } break;
53 
54                 case ITEM_INDEX_FAIL_NOW: {
55                     MyPrintService.peekInstance().handleFailPrintJob(printJobId);
56                 } break;
57 
58                 case ITEM_INDEX_FAIL_DELAYED: {
59                     MyPrintService.peekInstance().handleFailPrintJobDelayed(printJobId);
60                 } break;
61 
62                 case ITEM_INDEX_BLOCK_NOW: {
63                     MyPrintService.peekInstance().handleBlockPrintJob(printJobId);
64                 } break;
65 
66                 case ITEM_INDEX_BLOCK_DELAYED: {
67                     MyPrintService.peekInstance().handleBlockPrintJobDelayed(printJobId);
68                 } break;
69 
70                 case ITEM_INDEX_BLOCK_AND_DELAYED_UNBLOCK: {
71                     MyPrintService.peekInstance().handleBlockAndDelayedUnblockPrintJob(
72                             printJobId);
73                 } break;
74 
75                 case ITEM_INDEX_PRINT_SLOWLY: {
76                     MyPrintService.peekInstance().handlePrintJobProgress(printJobId, 0);
77                 } break;
78             }
79             finishAndRemoveTask();
80         });
81     }
82 
createActionTypeOnReqeustCancelPrintJobUi(final PrintJobId printJobId)83     private void createActionTypeOnReqeustCancelPrintJobUi(final PrintJobId printJobId) {
84         setTitle(getString(R.string.on_cancle_print_job_requested_activity_title));
85 
86         setListAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1,
87                 getResources().getStringArray(R.array.on_request_cancel_print_job_actions)));
88         getListView().setOnItemClickListener((parent, view, position, id) -> {
89             switch (position) {
90                 case ITEM_INDEX_CANCEL_YES: {
91                     MyPrintService.peekInstance().handleRequestCancelPrintJob(printJobId);
92                 } break;
93             }
94             finishAndRemoveTask();
95         });
96     }
97 }
98