summaryrefslogtreecommitdiffstats
path: root/remoting/android/java/src/org/chromium/chromoting/HostInfo.java
blob: e45ff74575662347a0422c1a020ff2c1e0a25fe2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.chromoting;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/** Class to represent a Host returned by {@link HostListLoader}. */
public class HostInfo {
    public final String name;
    public final String id;
    public final String jabberId;
    public final String publicKey;
    public final boolean isOnline;
    public final String hostOfflineReason;
    private final ArrayList<String> mTokenUrlPatterns;

    public HostInfo(String name,
                    String id,
                    String jabberId,
                    String publicKey,
                    ArrayList<String> tokenUrlPatterns,
                    boolean isOnline,
                    String hostOfflineReason) {
        this.name = name;
        this.id = id;
        this.jabberId = jabberId;
        this.publicKey = publicKey;
        this.mTokenUrlPatterns = tokenUrlPatterns;
        this.isOnline = isOnline;
        this.hostOfflineReason = hostOfflineReason;
    }

    public ArrayList<String> getTokenUrlPatterns() {
        return new ArrayList<String>(mTokenUrlPatterns);
    }

    public static HostInfo create(JSONObject json) throws JSONException {
        assert json != null;

        ArrayList<String> tokenUrlPatterns = new ArrayList<String>();
        JSONArray jsonPatterns = json.optJSONArray("tokenUrlPatterns");

        if (jsonPatterns != null) {
            for (int i = 0; i < jsonPatterns.length(); i++) {
                String pattern = jsonPatterns.getString(i);
                if (pattern != null && !pattern.isEmpty()) {
                    tokenUrlPatterns.add(pattern);
                }
            }
        }
        return new HostInfo(
                json.getString("hostName"),
                json.getString("hostId"),
                json.optString("jabberId"),
                json.optString("publicKey"),
                tokenUrlPatterns,
                json.optString("status").equals("ONLINE"),
                json.optString("hostOfflineReason"));
    }
}