• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  * Copyright (C) 2016 Mopria Alliance, Inc.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 package com.android.bips.ipp;
19 
20 import android.text.TextUtils;
21 
22 import com.android.bips.R;
23 import com.android.bips.jni.BackendConstants;
24 
25 import java.util.HashMap;
26 import java.util.LinkedHashSet;
27 import java.util.Map;
28 import java.util.Set;
29 
30 public class JobStatus {
31     public static final int ID_UNKNOWN = -1;
32 
33     /** Maps backend blocked reason codes to string resource IDs */
34     private static final Map<String, Integer> sBlockReasonsMap = new HashMap<>();
35 
36     static {
sBlockReasonsMap.put(BackendConstants.BLOCKED_REASON__DOOR_OPEN, R.string.printer_door_open)37         sBlockReasonsMap.put(BackendConstants.BLOCKED_REASON__DOOR_OPEN,
38                 R.string.printer_door_open);
sBlockReasonsMap.put(BackendConstants.BLOCKED_REASON__JAMMED, R.string.printer_jammed)39         sBlockReasonsMap.put(BackendConstants.BLOCKED_REASON__JAMMED, R.string.printer_jammed);
sBlockReasonsMap.put(BackendConstants.BLOCKED_REASON__OUT_OF_PAPER, R.string.printer_out_of_paper)40         sBlockReasonsMap.put(BackendConstants.BLOCKED_REASON__OUT_OF_PAPER,
41                 R.string.printer_out_of_paper);
sBlockReasonsMap.put(BackendConstants.BLOCKED_REASON__SERVICE_REQUEST, R.string.printer_check)42         sBlockReasonsMap.put(BackendConstants.BLOCKED_REASON__SERVICE_REQUEST,
43                 R.string.printer_check);
sBlockReasonsMap.put(BackendConstants.BLOCKED_REASON__OUT_OF_INK, R.string.printer_out_of_ink)44         sBlockReasonsMap.put(BackendConstants.BLOCKED_REASON__OUT_OF_INK,
45                 R.string.printer_out_of_ink);
sBlockReasonsMap.put(BackendConstants.BLOCKED_REASON__OUT_OF_TONER, R.string.printer_out_of_toner)46         sBlockReasonsMap.put(BackendConstants.BLOCKED_REASON__OUT_OF_TONER,
47                 R.string.printer_out_of_toner);
sBlockReasonsMap.put(BackendConstants.BLOCKED_REASON__LOW_ON_INK, R.string.printer_low_on_ink)48         sBlockReasonsMap.put(BackendConstants.BLOCKED_REASON__LOW_ON_INK,
49                 R.string.printer_low_on_ink);
sBlockReasonsMap.put(BackendConstants.BLOCKED_REASON__REALLY_LOW_ON_INK, R.string.printer_low_on_ink)50         sBlockReasonsMap.put(BackendConstants.BLOCKED_REASON__REALLY_LOW_ON_INK,
51                 R.string.printer_low_on_ink);
sBlockReasonsMap.put(BackendConstants.BLOCKED_REASON__LOW_ON_TONER, R.string.printer_low_on_toner)52         sBlockReasonsMap.put(BackendConstants.BLOCKED_REASON__LOW_ON_TONER,
53                 R.string.printer_low_on_toner);
sBlockReasonsMap.put(BackendConstants.BLOCKED_REASON__BUSY, R.string.printer_busy)54         sBlockReasonsMap.put(BackendConstants.BLOCKED_REASON__BUSY, R.string.printer_busy);
sBlockReasonsMap.put(BackendConstants.BLOCKED_REASON__OFFLINE, R.string.printer_offline)55         sBlockReasonsMap.put(BackendConstants.BLOCKED_REASON__OFFLINE, R.string.printer_offline);
sBlockReasonsMap.put(BackendConstants.BLOCKED_REASON__BAD_CERTIFICATE, R.string.printer_bad_certificate)56         sBlockReasonsMap.put(BackendConstants.BLOCKED_REASON__BAD_CERTIFICATE,
57                 R.string.printer_bad_certificate);
58     }
59 
60     private int mId;
61     private String mJobState;
62     private String mJobResult;
63     private final Set<String> mBlockedReasons;
64     private byte[] mCertificate;
65 
66     /** Create a new, blank job status */
JobStatus()67     public JobStatus() {
68         mId = ID_UNKNOWN;
69         mBlockedReasons = new LinkedHashSet<>();
70     }
71 
72     /** Create a copy of another object */
JobStatus(JobStatus other)73     private JobStatus(JobStatus other) {
74         mId = other.mId;
75         mJobState = other.mJobState;
76         mJobResult = other.mJobResult;
77         mBlockedReasons = other.mBlockedReasons;
78         mCertificate = other.mCertificate;
79     }
80 
81     /** Returns a string resource ID corresponding to a blocked reason, or 0 if none found */
getBlockedReasonId()82     public int getBlockedReasonId() {
83         for (String reason : mBlockedReasons) {
84             if (sBlockReasonsMap.containsKey(reason)) {
85                 return sBlockReasonsMap.get(reason);
86             }
87         }
88         return 0;
89     }
90 
91     /** Returns a job state (see {@link BackendConstants} JOB_DONE_*}) or null if not known */
getJobState()92     public String getJobState() {
93         return mJobState;
94     }
95 
96     /** Returns a job result (see {@link BackendConstants} JOB_RESULT_*}) or null if not known */
getJobResult()97     public String getJobResult() {
98         return mJobResult;
99     }
100 
101     /** Return the job's identifier or ID_UNKNOWN */
getId()102     public int getId() {
103         return mId;
104     }
105 
106     /** Return true if the job is in a completion state */
isJobDone()107     boolean isJobDone() {
108         return !TextUtils.isEmpty(mJobResult);
109     }
110 
111     /** Return certificate if supplied as part of this status. */
getCertificate()112     public byte[] getCertificate() {
113         return mCertificate;
114     }
115 
116     @Override
toString()117     public String toString() {
118         return "JobStatus{id=" + mId
119                 + ", jobState=" + mJobState
120                 + ", jobResult=" + mJobResult
121                 + ", blockedReasons=" + mBlockedReasons
122                 + ", certificate=" + (mCertificate != null)
123                 + "}";
124     }
125 
126     static class Builder {
127         final JobStatus mPrototype;
128 
Builder()129         Builder() {
130             mPrototype = new JobStatus();
131         }
132 
Builder(JobStatus from)133         Builder(JobStatus from) {
134             mPrototype = new JobStatus(from);
135         }
136 
setId(int id)137         public Builder setId(int id) {
138             mPrototype.mId = id;
139             return this;
140         }
141 
setJobState(String jobState)142         Builder setJobState(String jobState) {
143             mPrototype.mJobState = jobState;
144             return this;
145         }
146 
setJobResult(String jobResult)147         Builder setJobResult(String jobResult) {
148             mPrototype.mJobResult = jobResult;
149             return this;
150         }
151 
setCertificate(byte[] certificate)152         Builder setCertificate(byte[] certificate) {
153             mPrototype.mCertificate = certificate;
154             return this;
155         }
156 
clearBlockedReasons()157         Builder clearBlockedReasons() {
158             mPrototype.mBlockedReasons.clear();
159             return this;
160         }
161 
addBlockedReason(String blockedReason)162         Builder addBlockedReason(String blockedReason) {
163             mPrototype.mBlockedReasons.add(blockedReason);
164             return this;
165         }
166 
build()167         public JobStatus build() {
168             return new JobStatus(mPrototype);
169         }
170     }
171 }
172