Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / remoting / android / java / src / org / chromium / chromoting / HostInfo.java
blob128d74b2f563c79b43191146d54c102448227c43
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.chromoting;
7 import android.content.Context;
8 import android.content.res.Resources;
9 import android.text.TextUtils;
11 import org.chromium.base.Log;
13 import org.json.JSONArray;
14 import org.json.JSONException;
15 import org.json.JSONObject;
17 import java.text.ParsePosition;
18 import java.text.SimpleDateFormat;
19 import java.util.ArrayList;
20 import java.util.Date;
21 import java.util.Locale;
23 /** Class to represent a Host returned by {@link HostListLoader}. */
24 public class HostInfo {
25 private static final String TAG = "cr.Chromoting";
27 public final String name;
28 public final String id;
29 public final String jabberId;
30 public final String publicKey;
31 public final boolean isOnline;
32 public final String hostOfflineReason;
33 public final Date updatedTime;
35 private final ArrayList<String> mTokenUrlPatterns;
37 // Format of time values coming from the Chromoting REST API.
38 // This is a specific form of RFC3339 (with no timezone info).
39 // Example value to be parsed: 2014-11-21T01:02:33.814Z
40 private static final String RFC_3339_FORMAT = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'";
42 // Value to use if time string received from the network is malformed.
43 // Such malformed string should in theory never happen, but we want
44 // to have a safe fallback in case it does happen.
45 private static final Date FALLBACK_DATE_IN_THE_PAST = new Date(0);
47 public HostInfo(String name, String id, String jabberId, String publicKey,
48 ArrayList<String> tokenUrlPatterns, boolean isOnline, String hostOfflineReason,
49 String updatedTime) {
50 this.name = name;
51 this.id = id;
52 this.jabberId = jabberId;
53 this.publicKey = publicKey;
54 this.mTokenUrlPatterns = tokenUrlPatterns;
55 this.isOnline = isOnline;
56 this.hostOfflineReason = hostOfflineReason;
58 ParsePosition parsePosition = new ParsePosition(0);
59 SimpleDateFormat format = new SimpleDateFormat(RFC_3339_FORMAT, Locale.US);
60 Date updatedTimeCandidate = format.parse(updatedTime, parsePosition);
61 if (updatedTimeCandidate == null) {
62 Log.e(TAG, "Unparseable host.updatedTime JSON: errorIndex = %d, input = %s",
63 parsePosition.getErrorIndex(), updatedTime);
64 updatedTimeCandidate = FALLBACK_DATE_IN_THE_PAST;
66 this.updatedTime = updatedTimeCandidate;
69 public String getHostOfflineReasonText(Context context) {
70 if (TextUtils.isEmpty(hostOfflineReason)) {
71 return context.getString(R.string.host_offline_tooltip);
73 try {
74 String resourceName = "offline_reason_" + hostOfflineReason.toLowerCase(Locale.ENGLISH);
75 int resourceId = context.getResources().getIdentifier(
76 resourceName, "string", context.getPackageName());
77 return context.getString(resourceId);
78 } catch (Resources.NotFoundException ignored) {
79 return context.getString(R.string.offline_reason_unknown, hostOfflineReason);
83 public ArrayList<String> getTokenUrlPatterns() {
84 return new ArrayList<String>(mTokenUrlPatterns);
87 public static HostInfo create(JSONObject json) throws JSONException {
88 assert json != null;
90 ArrayList<String> tokenUrlPatterns = new ArrayList<String>();
91 JSONArray jsonPatterns = json.optJSONArray("tokenUrlPatterns");
93 if (jsonPatterns != null) {
94 for (int i = 0; i < jsonPatterns.length(); i++) {
95 String pattern = jsonPatterns.getString(i);
96 if (pattern != null && !pattern.isEmpty()) {
97 tokenUrlPatterns.add(pattern);
101 return new HostInfo(json.getString("hostName"), json.getString("hostId"),
102 json.optString("jabberId"), json.optString("publicKey"), tokenUrlPatterns,
103 json.optString("status").equals("ONLINE"), json.optString("hostOfflineReason"),
104 json.optString("updatedTime"));