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);
56     }
57 
58     private int mId;
59     private String mJobState;
60     private String mJobResult;
61     private final Set<String> mBlockedReasons;
62 
63     /** Create a new, blank job status */
JobStatus()64     public JobStatus() {
65         mId = ID_UNKNOWN;
66         mBlockedReasons = new LinkedHashSet<>();
67     }
68 
69     /** Create a copy of another object */
JobStatus(JobStatus other)70     private JobStatus(JobStatus other) {
71         mId = other.mId;
72         mJobState = other.mJobState;
73         mJobResult = other.mJobResult;
74         mBlockedReasons = other.mBlockedReasons;
75     }
76 
77     /** Returns a string resource ID corresponding to a blocked reason, or 0 if none found */
getBlockedReasonId()78     public int getBlockedReasonId() {
79         for (String reason : mBlockedReasons) {
80             if (sBlockReasonsMap.containsKey(reason)) {
81                 return sBlockReasonsMap.get(reason);
82             }
83         }
84         return 0;
85     }
86 
87     /** Returns a job state (see {@link BackendConstants} JOB_DONE_*}) or null if not known */
getJobState()88     public String getJobState() {
89         return mJobState;
90     }
91 
92     /** Returns a job result (see {@link BackendConstants} JOB_RESULT_*}) or null if not known */
getJobResult()93     public String getJobResult() {
94         return mJobResult;
95     }
96 
97     /** Return the job's identifier or ID_UNKNOWN */
getId()98     public int getId() {
99         return mId;
100     }
101 
102     /** Return true if the job is in a completion state */
isJobDone()103     boolean isJobDone() {
104         return !TextUtils.isEmpty(mJobResult);
105     }
106 
107     @Override
toString()108     public String toString() {
109         return "JobStatus{id=" + mId +
110                 ", jobState=" + mJobState +
111                 ", jobResult=" + mJobResult +
112                 ", blockedReasons=" + mBlockedReasons +
113                 "}";
114     }
115 
116     static class Builder {
117         final JobStatus mPrototype;
118 
Builder()119         Builder() {
120             mPrototype = new JobStatus();
121         }
122 
Builder(JobStatus from)123         Builder(JobStatus from) {
124             mPrototype = new JobStatus(from);
125         }
126 
setId(int id)127         public Builder setId(int id) {
128             mPrototype.mId = id;
129             return this;
130         }
131 
setJobState(String jobState)132         Builder setJobState(String jobState) {
133             mPrototype.mJobState = jobState;
134             return this;
135         }
136 
setJobResult(String jobResult)137         Builder setJobResult(String jobResult) {
138             mPrototype.mJobResult = jobResult;
139             return this;
140         }
141 
clearBlockedReasons()142         Builder clearBlockedReasons() {
143             mPrototype.mBlockedReasons.clear();
144             return this;
145         }
146 
addBlockedReason(String blockedReason)147         Builder addBlockedReason(String blockedReason) {
148             mPrototype.mBlockedReasons.add(blockedReason);
149             return this;
150         }
151 
build()152         public JobStatus build() {
153             return new JobStatus(mPrototype);
154         }
155     }
156 }