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