summaryrefslogtreecommitdiffstats
path: root/mobile/android/base/java/org/mozilla/gecko/widget/DoorhangerConfig.java
blob: 98f1e57e1400f140e44d13fffbdfe1d3fa677de1 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko.widget;

import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;

import org.mozilla.gecko.widget.DoorHanger.Type;

public class DoorhangerConfig {

    public static class Link {
        public final String label;
        public final String url;

        private Link(String label, String url) {
            this.label = label;
            this.url = url;
        }
    }

    public static class ButtonConfig {
        public final String label;
        public final int callback;

        public ButtonConfig(String label, int callback) {
            this.label = label;
            this.callback = callback;
        }
    }
    private static final String LOGTAG = "DoorhangerConfig";

    private final int tabId;
    private final String id;
    private final DoorHanger.OnButtonClickListener buttonClickListener;
    private final DoorHanger.Type type;
    private String message;
    private JSONObject options;
    private Link link;
    private ButtonConfig positiveButtonConfig;
    private ButtonConfig negativeButtonConfig;

    public DoorhangerConfig(Type type, DoorHanger.OnButtonClickListener listener) {
        // XXX: This should only be used by SiteIdentityPopup doorhangers which
        // don't need tab or id references, until bug 1141904 unifies doorhangers.

        this(-1, null, type, listener);
    }

    public DoorhangerConfig(int tabId, String id, DoorHanger.Type type, DoorHanger.OnButtonClickListener buttonClickListener) {
        this.tabId = tabId;
        this.id = id;
        this.type = type;
        this.buttonClickListener = buttonClickListener;
    }

    public int getTabId() {
        return tabId;
    }

    public String getId() {
        return id;
    }

    public Type getType() {
        return type;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

    public void setOptions(JSONObject options) {
        this.options = options;

        // Set link if there is a link provided in options.
        final JSONObject linkObj = options.optJSONObject("link");
        if (linkObj != null) {
            try {
                setLink(linkObj.getString("label"), linkObj.getString("url"));
            } catch (JSONException e) {
                Log.e(LOGTAG, "Malformed link object in options");
            }
        }
    }

    public JSONObject getOptions() {
        return options;
    }

    public void setButton(String label, int callbackId, boolean isPositive) {
        final ButtonConfig buttonConfig = new ButtonConfig(label, callbackId);
        if (isPositive) {
            positiveButtonConfig = buttonConfig;
        } else {
            negativeButtonConfig = buttonConfig;
        }
    }

    public ButtonConfig getPositiveButtonConfig() {
        return positiveButtonConfig;
    }

    public  ButtonConfig getNegativeButtonConfig() {
        return negativeButtonConfig;
    }

    public DoorHanger.OnButtonClickListener getButtonClickListener() {
        return this.buttonClickListener;
    }

    public void setLink(String label, String url) {
        this.link = new Link(label, url);
    }

    public Link getLink() {
        return link;
    }
}