1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5  * except in compliance with the License. You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the
10  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11  * KIND, either express or implied. See the License for the specific language governing
12  * permissions and limitations under the License.
13  */
14 
15 package com.android.car.developeroptions.print;
16 
17 import android.content.Context;
18 import android.content.res.TypedArray;
19 import android.graphics.drawable.Drawable;
20 import android.print.PrintJob;
21 import android.print.PrintJobInfo;
22 import android.text.format.DateUtils;
23 
24 import com.android.car.developeroptions.R;
25 
26 import java.text.DateFormat;
27 
28 public class PrintJobPreferenceController extends PrintJobPreferenceControllerBase {
29 
PrintJobPreferenceController(Context context, String key)30     public PrintJobPreferenceController(Context context, String key) {
31         super(context, key);
32     }
33 
34     @Override
updateUi()35     protected void updateUi() {
36         final PrintJob printJob = getPrintJob();
37 
38         if (printJob == null) {
39             mFragment.finish();
40             return;
41         }
42 
43         if (printJob.isCancelled() || printJob.isCompleted()) {
44             mFragment.finish();
45             return;
46         }
47 
48         PrintJobInfo info = printJob.getInfo();
49 
50         switch (info.getState()) {
51             case PrintJobInfo.STATE_CREATED: {
52                 mPreference.setTitle(mContext.getString(
53                         R.string.print_configuring_state_title_template, info.getLabel()));
54             }
55             break;
56             case PrintJobInfo.STATE_QUEUED:
57             case PrintJobInfo.STATE_STARTED: {
58                 if (!printJob.getInfo().isCancelling()) {
59                     mPreference.setTitle(mContext.getString(
60                             R.string.print_printing_state_title_template, info.getLabel()));
61                 } else {
62                     mPreference.setTitle(mContext.getString(
63                             R.string.print_cancelling_state_title_template, info.getLabel()));
64                 }
65             }
66             break;
67 
68             case PrintJobInfo.STATE_FAILED: {
69                 mPreference.setTitle(mContext.getString(
70                         R.string.print_failed_state_title_template, info.getLabel()));
71             }
72             break;
73 
74             case PrintJobInfo.STATE_BLOCKED: {
75                 if (!printJob.getInfo().isCancelling()) {
76                     mPreference.setTitle(mContext.getString(
77                             R.string.print_blocked_state_title_template, info.getLabel()));
78                 } else {
79                     mPreference.setTitle(mContext.getString(
80                             R.string.print_cancelling_state_title_template, info.getLabel()));
81                 }
82             }
83             break;
84         }
85 
86         mPreference.setSummary(mContext.getString(R.string.print_job_summary,
87                 info.getPrinterName(), DateUtils.formatSameDayTime(
88                         info.getCreationTime(), info.getCreationTime(), DateFormat.SHORT,
89                         DateFormat.SHORT)));
90 
91         TypedArray a = mContext.obtainStyledAttributes(new int[]{
92                 android.R.attr.colorControlNormal});
93         int tintColor = a.getColor(0, 0);
94         a.recycle();
95 
96         switch (info.getState()) {
97             case PrintJobInfo.STATE_QUEUED:
98             case PrintJobInfo.STATE_STARTED: {
99                 Drawable icon = mContext.getDrawable(com.android.internal.R.drawable.ic_print);
100                 icon.setTint(tintColor);
101                 mPreference.setIcon(icon);
102                 break;
103             }
104 
105             case PrintJobInfo.STATE_FAILED:
106             case PrintJobInfo.STATE_BLOCKED: {
107                 Drawable icon = mContext.getDrawable(
108                         com.android.internal.R.drawable.ic_print_error);
109                 icon.setTint(tintColor);
110                 mPreference.setIcon(icon);
111                 break;
112             }
113         }
114     }
115 }
116