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.discovery; 19 20 import android.content.Context; 21 import android.net.Uri; 22 import android.print.PrinterId; 23 import android.printservice.PrintService; 24 import android.text.TextUtils; 25 import android.util.JsonReader; 26 import android.util.JsonWriter; 27 28 import com.android.bips.R; 29 30 import java.io.IOException; 31 import java.io.StringWriter; 32 import java.util.Objects; 33 34 /** Represents a network-visible printer */ 35 public class DiscoveredPrinter { 36 /** UUID (RFC4122) uniquely identifying the print service, or null if not reported */ 37 public final Uri uuid; 38 39 /** User-visible name for the print service */ 40 public final String name; 41 42 /** Location of the device or null of not reported */ 43 public final String location; 44 45 /** Resource path at which the print service can be reached */ 46 public final Uri path; 47 48 /** Lazily-created printer id. */ 49 private PrinterId mPrinterId; 50 51 /** 52 * Construct minimal information about a network printer 53 * 54 * @param uuid Unique identification of the network printer, if known 55 * @param name Self-identified printer or service name 56 * @param path Network path at which the printer is currently available 57 * @param location Self-advertised location of the printer, if known 58 */ DiscoveredPrinter(Uri uuid, String name, Uri path, String location)59 public DiscoveredPrinter(Uri uuid, String name, Uri path, String location) { 60 this.uuid = uuid; 61 this.name = name; 62 this.path = path; 63 this.location = location; 64 } 65 66 /** Construct an object based on field values of an JSON object found next in the JsonReader */ DiscoveredPrinter(JsonReader reader)67 public DiscoveredPrinter(JsonReader reader) throws IOException { 68 String printerName = null, location = null; 69 Uri uuid = null, path = null; 70 71 reader.beginObject(); 72 while (reader.hasNext()) { 73 String itemName = reader.nextName(); 74 switch (itemName) { 75 case "uuid": 76 uuid = Uri.parse(reader.nextString()); 77 break; 78 case "name": 79 printerName = reader.nextString(); 80 break; 81 case "path": 82 path = Uri.parse(reader.nextString()); 83 break; 84 case "location": 85 location = reader.nextString(); 86 break; 87 } 88 } 89 reader.endObject(); 90 91 if (printerName == null || path == null) throw new IOException("Missing name or path"); 92 this.uuid = uuid; 93 this.name = printerName; 94 this.path = path; 95 this.location = location; 96 } 97 98 /** 99 * Return the best URI describing this printer: from the UUID (if present) or 100 * from the path (if UUID is missing) 101 */ getUri()102 public Uri getUri() { 103 return uuid != null ? uuid : path; 104 } 105 106 /** Return a generated printer ID based on uuid or (if uuid is missing) its path */ getId(PrintService printService)107 public PrinterId getId(PrintService printService) { 108 if (mPrinterId == null) { 109 mPrinterId = printService.generatePrinterId(getUri().toString()); 110 } 111 return mPrinterId; 112 } 113 114 /** Return a friendly description string including host and (if present) location */ getDescription(Context context)115 public String getDescription(Context context) { 116 String host = path.getHost().replaceAll(":[0-9]+", ""); 117 String description; 118 if (!TextUtils.isEmpty(location)) { 119 description = context.getString(R.string.printer_description, host, location); 120 } else { 121 description = host; 122 } 123 return description; 124 } 125 126 /** Writes all serializable fields into JSON form */ write(JsonWriter writer)127 public void write(JsonWriter writer) throws IOException { 128 writer.beginObject(); 129 writer.name("name").value(name); 130 writer.name("path").value(path.toString()); 131 if (uuid != null) { 132 writer.name("uuid").value(uuid.toString()); 133 } 134 if (!TextUtils.isEmpty(location)) { 135 writer.name("location").value(location); 136 } 137 writer.endObject(); 138 } 139 140 @Override equals(Object obj)141 public boolean equals(Object obj) { 142 if (!(obj instanceof DiscoveredPrinter)) return false; 143 DiscoveredPrinter other = (DiscoveredPrinter) obj; 144 return Objects.equals(uuid, other.uuid) && 145 Objects.equals(name, other.name) && 146 Objects.equals(path, other.path) && 147 Objects.equals(location, other.location); 148 } 149 150 @Override hashCode()151 public int hashCode() { 152 int result = 17; 153 result = 31 * result + name.hashCode(); 154 result = 31 * result + (uuid != null ? uuid.hashCode() : 0); 155 result = 31 * result + path.hashCode(); 156 result = 31 * result + (location != null ? location.hashCode() : 0); 157 return result; 158 } 159 160 @Override toString()161 public String toString() { 162 StringWriter sw = new StringWriter(); 163 try { 164 write(new JsonWriter(sw)); 165 } catch (IOException ignored) { 166 } 167 return "DiscoveredPrinter" + sw.toString(); 168 } 169 }