summaryrefslogtreecommitdiffstats
path: root/mobile/android/base/java/org/mozilla/gecko/overlays/ui/SendTabDeviceListArrayAdapter.java
blob: 08e9c59f55d6053192def11ee707ab72dcaebc01 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* 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.overlays.ui;

import java.util.Collection;

import org.mozilla.gecko.AppConstants;
import org.mozilla.gecko.R;
import org.mozilla.gecko.db.RemoteClient;
import org.mozilla.gecko.overlays.ui.SendTabList.State;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

public class SendTabDeviceListArrayAdapter extends ArrayAdapter<RemoteClient> {
    @SuppressWarnings("unused")
    private static final String LOGTAG = "GeckoSendTabAdapter";

    private State currentState;

    // String to display when in a "button-like" special state. Instead of using a
    // RemoteClient we override the rendering using this string.
    private String dummyRecordName;

    private final SendTabTargetSelectedListener listener;

    private Collection<RemoteClient> records;

    // The AlertDialog to show in the event the record is pressed while in the SHOW_DEVICES state.
    // This will show the user a prompt to select a device from a longer list of devices.
    private AlertDialog dialog;

    public SendTabDeviceListArrayAdapter(Context context, SendTabTargetSelectedListener aListener) {
        super(context, R.layout.overlay_share_send_tab_item, R.id.overlaybtn_label);

        listener = aListener;

        // We do this manually and avoid multiple notifications when doing compound operations.
        setNotifyOnChange(false);
    }

    /**
     * Get an array of the contents of this adapter were it in the LIST state.
     * Useful for determining the "real" contents of the adapter.
     */
    public RemoteClient[] toArray() {
        return records.toArray(new RemoteClient[records.size()]);
    }

    public void setRemoteClientsList(Collection<RemoteClient> remoteClientsList) {
        records = remoteClientsList;
        updateRecordList();
    }

    /**
     * Ensure the contents of the Adapter are synchronised with the `records` field. This may not
     * be the case if records has recently changed, or if we have experienced a state change.
     */
    public void updateRecordList() {
        if (currentState != State.LIST) {
            return;
        }

        clear();

        setNotifyOnChange(false);    // So we don't notify for each add.
        addAll(records);

        notifyDataSetChanged();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final Context context = getContext();

        // Reuse View objects if they exist.
        OverlayDialogButton row = (OverlayDialogButton) convertView;
        if (row == null) {
            row = (OverlayDialogButton) View.inflate(context, R.layout.overlay_share_send_tab_item, null);
        }

        // The first view in the list has a unique style.
        if (position == 0) {
            row.setBackgroundResource(R.drawable.overlay_share_button_background_first);
        } else {
            row.setBackgroundResource(R.drawable.overlay_share_button_background);
        }

        if (currentState != State.LIST) {
            // If we're in a special "Button-like" state, use the override string and a generic icon.
            final Drawable sendTabIcon = context.getResources().getDrawable(R.drawable.shareplane);
            row.setText(dummyRecordName);
            row.setDrawable(sendTabIcon);
        }

        // If we're just a button to launch the dialog, set the listener and abort.
        if (currentState == State.SHOW_DEVICES) {
            row.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    dialog.show();
                }
            });

            return row;
        }

        // The remaining states delegate to the SentTabTargetSelectedListener.
        final RemoteClient remoteClient = getItem(position);
        if (currentState == State.LIST) {
            final Drawable clientIcon = context.getResources().getDrawable(getImage(remoteClient));
            row.setText(remoteClient.name);
            row.setDrawable(clientIcon);

            final String listenerGUID = remoteClient.guid;

            row.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    listener.onSendTabTargetSelected(listenerGUID);
                }
            });
        } else {
            row.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    listener.onSendTabActionSelected();
                }
            });
        }

        return row;
    }

    private static int getImage(RemoteClient record) {
        if ("mobile".equals(record.deviceType)) {
            return R.drawable.device_mobile;
        }

        return R.drawable.device_desktop;
    }

    public void switchState(State newState) {
        if (currentState == newState) {
            return;
        }

        currentState = newState;

        switch (newState) {
            case LIST:
                updateRecordList();
                break;
            case NONE:
                showDummyRecord(getContext().getResources().getString(R.string.overlay_share_send_tab_btn_label));
                break;
            case SHOW_DEVICES:
                showDummyRecord(getContext().getResources().getString(R.string.overlay_share_send_other));
                break;
            default:
                throw new IllegalStateException("Unexpected state transition: " + newState);
        }
    }

    /**
     * Set the dummy override string to the given value and clear the list.
     */
    private void showDummyRecord(String name) {
        dummyRecordName = name;
        clear();
        add(null);
        notifyDataSetChanged();
    }

    public void setDialog(AlertDialog aDialog) {
        dialog = aDialog;
    }
}