synchronized private void updateRootNode(final boolean force) {
if (!mStartFinished && !force) {
return;
}
final PluginTreeNode root = getRootNode();
root.removeAllActions();
root.removeAllChildren();
int progCount = 0;
final HashMap<String, PluginTreeNode> genreNodes = new HashMap<String, PluginTreeNode>();
currentGenres = new ArrayList<String>();
final Channel[] channels = devplugin.Plugin.getPluginManager().getSubscribedChannels();
Date date = Date.getCurrentDate();
final int maxDays = mSettings.getDays();
for (int days = 0; days < maxDays; days++) {
for (int i = 0; i < channels.length; ++i) {
for (Iterator<Program> iter = devplugin.Plugin.getPluginManager()
.getChannelDayProgram(date, channels[i]); iter.hasNext();) {
final Program prog = iter.next();
String genreField = prog.getTextField(ProgramFieldType.GENRE_TYPE);
if (genreField != null) {
// some programs have buggy fields with brackets
if (genreField.startsWith("(") && genreField.endsWith(")")) {
genreField = genreField.substring(1, genreField.length() - 1);
}
// remove sub genres in braces
if (mSettings.getUnifyBraceGenres()) {
int brace = genreField.indexOf('(');
if (brace > 0) {
genreField = genreField.substring(0, brace).trim();
}
}
// some programs have multiple genres in the field
final String[] genres = genreField.split(",|/");
for (int genreIndex = 0; genreIndex < genres.length; genreIndex++) {
String genre = genres[genreIndex].trim();
// fix genres ending in dash
if (genre.endsWith("-")) {
if (genreIndex < genres.length - 1) {
for (String suffix : GENRE_SUFFIX) {
if (genres[genreIndex + 1].endsWith(suffix)) {
genre = genre.substring(0, genre.length() - 1).trim() + suffix;
break;
}
}
}
}
if (genre.length() >= MIN_GENRE_LENGTH && !hiddenGenres.contains(genre)) {
PluginTreeNode node = genreNodes.get(genre);
if (node == null) {
node = new PluginTreeNode(genre);
formatGenreNode(maxDays, node);
genreNodes.put(genre, node);
currentGenres.add(genre);
}
node.addProgramWithoutCheck(prog);
progCount++;
}
}
}
}
}
date = date.addDays(1);
// stop if there are to many nodes for the tree
if (progCount > 10000) {
break;
}
}
Collections.sort(currentGenres, String.CASE_INSENSITIVE_ORDER);
mergeSimilarGenres(genreNodes);
for (String genre : currentGenres) {
final PluginTreeNode genreNode = genreNodes.get(genre);
// the node may be deleted because of merging
if (genreNode != null) {
root.add(genreNode);
}
}