1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.preload.actions;
18
19import com.android.ddmlib.Client;
20import com.android.ddmlib.IDevice;
21import com.android.preload.ClientUtils;
22
23import java.util.Arrays;
24import java.util.Comparator;
25
26import javax.swing.DefaultListModel;
27
28public class ReloadListAction extends AbstractThreadedDeviceSpecificAction {
29
30    private ClientUtils clientUtils;
31    private final DefaultListModel<Client> clientListModel;
32
33    public ReloadListAction(ClientUtils utils, IDevice device,
34            DefaultListModel<Client> clientListModel) {
35        super("Reload", device);
36        this.clientUtils = utils;
37        this.clientListModel = clientListModel;
38    }
39
40    @Override
41    public void run() {
42        Client[] clients = clientUtils.findAllClients(device);
43        if (clients != null) {
44            Arrays.sort(clients, new ClientComparator());
45        }
46        clientListModel.removeAllElements();
47        for (Client c : clients) {
48            clientListModel.addElement(c);
49        }
50    }
51
52    private static class ClientComparator implements Comparator<Client> {
53
54        @Override
55        public int compare(Client o1, Client o2) {
56            String s1 = o1.getClientData().getClientDescription();
57            String s2 = o2.getClientData().getClientDescription();
58
59            if (s1 == null || s2 == null) {
60                // Not good, didn't get all data?
61                return (s1 == null) ? -1 : 1;
62            }
63
64            return s1.compareTo(s2);
65        }
66
67    }
68}