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.preload.DumpData;
20import com.android.preload.DumpTableModel;
21import com.android.preload.Main;
22
23import java.awt.event.ActionEvent;
24import java.io.File;
25import java.io.PrintWriter;
26import java.util.HashMap;
27import java.util.HashSet;
28import java.util.List;
29import java.util.Map;
30import java.util.Set;
31import java.util.TreeSet;
32import java.util.regex.Pattern;
33
34import javax.swing.AbstractAction;
35
36/**
37 * Compute an intersection of classes from the given data. A class is in the intersection if it
38 * appears in at least the number of threshold given packages. An optional blacklist can be
39 * used to filter classes from the intersection.
40 */
41public class ComputeThresholdAction extends AbstractThreadedAction {
42    protected int threshold;
43    private Pattern blacklist;
44    private DumpTableModel dataTableModel;
45
46    /**
47     * Create an action with the given parameters. The blacklist is a regular expression
48     * that filters classes.
49     */
50    public ComputeThresholdAction(String name, DumpTableModel dataTableModel, int threshold,
51            String blacklist) {
52        super(name);
53        this.dataTableModel = dataTableModel;
54        this.threshold = threshold;
55        if (blacklist != null) {
56            this.blacklist = Pattern.compile(blacklist);
57        }
58    }
59
60    @Override
61    public void actionPerformed(ActionEvent e) {
62        List<DumpData> data = dataTableModel.getData();
63        if (data.size() == 0) {
64            Main.getUI().showMessageDialog("No data available, please scan packages or run "
65                    + "monkeys.");
66            return;
67        }
68        if (data.size() == 1) {
69            Main.getUI().showMessageDialog("Cannot compute list from only one data set, please "
70                    + "scan packages or run monkeys.");
71            return;
72        }
73
74        super.actionPerformed(e);
75    }
76
77    @Override
78    public void run() {
79        Main.getUI().showWaitDialog();
80
81        Map<String, Set<String>> uses = new HashMap<String, Set<String>>();
82        for (DumpData d : dataTableModel.getData()) {
83            Main.getUI().updateWaitDialog("Merging " + d.getPackageName());
84            updateClassUse(d.getPackageName(), uses, getBootClassPathClasses(d.getDumpData()));
85        }
86
87        Main.getUI().updateWaitDialog("Computing thresholded set");
88        Set<String> result = fromThreshold(uses, blacklist, threshold);
89        Main.getUI().hideWaitDialog();
90
91        boolean ret = Main.getUI().showConfirmDialog("Computed a set with " + result.size()
92                + " classes, would you like to save to disk?", "Save?");
93        if (ret) {
94            File f = Main.getUI().showSaveDialog();
95            if (f != null) {
96                saveSet(result, f);
97            }
98        }
99    }
100
101    private Set<String> fromThreshold(Map<String, Set<String>> classUses, Pattern blacklist,
102            int threshold) {
103        TreeSet<String> ret = new TreeSet<>(); // TreeSet so it's nicely ordered by name.
104
105        for (Map.Entry<String, Set<String>> e : classUses.entrySet()) {
106            if (e.getValue().size() >= threshold) {
107                if (blacklist == null || !blacklist.matcher(e.getKey()).matches()) {
108                    ret.add(e.getKey());
109                }
110            }
111        }
112
113        return ret;
114    }
115
116    private static void updateClassUse(String pkg, Map<String, Set<String>> classUses,
117            Set<String> classes) {
118        for (String className : classes) {
119            Set<String> old = classUses.get(className);
120            if (old == null) {
121                classUses.put(className, new HashSet<String>());
122            }
123            classUses.get(className).add(pkg);
124        }
125    }
126
127    private static Set<String> getBootClassPathClasses(Map<String, String> source) {
128        Set<String> ret = new HashSet<>();
129        for (Map.Entry<String, String> e : source.entrySet()) {
130            if (e.getValue() == null) {
131                ret.add(e.getKey());
132            }
133        }
134        return ret;
135    }
136
137    private static void saveSet(Set<String> result, File f) {
138        try {
139            PrintWriter out = new PrintWriter(f);
140            for (String s : result) {
141                out.println(s);
142            }
143            out.close();
144        } catch (Exception e) {
145            e.printStackTrace();
146        }
147    }
148}