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