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 /** Return blocked reasons */ getBlockedReasons()117 public Set<String> getBlockedReasons() { 118 return mBlockedReasons; 119 } 120 121 @Override toString()122 public String toString() { 123 return "JobStatus{id=" + mId 124 + ", jobState=" + mJobState 125 + ", jobResult=" + mJobResult 126 + ", blockedReasons=" + mBlockedReasons 127 + ", certificate=" + (mCertificate != null) 128 + "}"; 129 } 130 131 static class Builder { 132 final JobStatus mPrototype; 133 Builder()134 Builder() { 135 mPrototype = new JobStatus(); 136 } 137 Builder(JobStatus from)138 Builder(JobStatus from) { 139 mPrototype = new JobStatus(from); 140 } 141 setId(int id)142 public Builder setId(int id) { 143 mPrototype.mId = id; 144 return this; 145 } 146 setJobState(String jobState)147 Builder setJobState(String jobState) { 148 mPrototype.mJobState = jobState; 149 return this; 150 } 151 setJobResult(String jobResult)152 Builder setJobResult(String jobResult) { 153 mPrototype.mJobResult = jobResult; 154 return this; 155 } 156 setCertificate(byte[] certificate)157 Builder setCertificate(byte[] certificate) { 158 mPrototype.mCertificate = certificate; 159 return this; 160 } 161 clearBlockedReasons()162 Builder clearBlockedReasons() { 163 mPrototype.mBlockedReasons.clear(); 164 return this; 165 } 166 addBlockedReason(String blockedReason)167 Builder addBlockedReason(String blockedReason) { 168 mPrototype.mBlockedReasons.add(blockedReason); 169 return this; 170 } 171 build()172 public JobStatus build() { 173 return new JobStatus(mPrototype); 174 } 175 } 176 } 177