1554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe/*
2554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * Copyright (C) 2015 The Android Open Source Project
3554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe *
4554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * Licensed under the Apache License, Version 2.0 (the "License");
5554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * you may not use this file except in compliance with the License.
6554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * You may obtain a copy of the License at
7554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe *
8554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe *      http://www.apache.org/licenses/LICENSE-2.0
9554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe *
10554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * Unless required by applicable law or agreed to in writing, software
11554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * distributed under the License is distributed on an "AS IS" BASIS,
12554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * See the License for the specific language governing permissions and
14554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe * limitations under the License.
15554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe */
16554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
17554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampepackage com.android.preload.actions;
18554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
19554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport com.android.ddmlib.Client;
20554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport com.android.ddmlib.IDevice;
21554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport com.android.preload.ClientUtils;
22554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
23554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport java.util.Arrays;
24554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport java.util.Comparator;
25554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
26554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampeimport javax.swing.DefaultListModel;
27554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
28554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampepublic class ReloadListAction extends AbstractThreadedDeviceSpecificAction {
29554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
30554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    private ClientUtils clientUtils;
31554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    private final DefaultListModel<Client> clientListModel;
32554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
33554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    public ReloadListAction(ClientUtils utils, IDevice device,
34554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            DefaultListModel<Client> clientListModel) {
35554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        super("Reload", device);
36554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        this.clientUtils = utils;
37554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        this.clientListModel = clientListModel;
38554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    }
39554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
40554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    @Override
41554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    public void run() {
42554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        Client[] clients = clientUtils.findAllClients(device);
43554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        if (clients != null) {
44554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            Arrays.sort(clients, new ClientComparator());
45554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        }
46554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        clientListModel.removeAllElements();
47554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        for (Client c : clients) {
48554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            clientListModel.addElement(c);
49554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        }
50554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    }
51554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
52554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    private static class ClientComparator implements Comparator<Client> {
53554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
54554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        @Override
55554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        public int compare(Client o1, Client o2) {
56554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            String s1 = o1.getClientData().getClientDescription();
57554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            String s2 = o2.getClientData().getClientDescription();
58554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
59554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            if (s1 == null || s2 == null) {
60554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe                // Not good, didn't get all data?
61554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe                return (s1 == null) ? -1 : 1;
62554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            }
63554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
64554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe            return s1.compareTo(s2);
65554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe        }
66554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe
67554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe    }
68554d7ee0f5d177b6c0bce805f5a5917b6b211978Andreas Gampe}