Skip to content

Commit

Permalink
Partially revert previous commit + further caching and optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
farmerbb committed Aug 5, 2018
1 parent c5cf106 commit b2349f6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StartMenuAdapter extends ArrayAdapter<AppEntry> implements SectionIndexer {

Expand All @@ -62,8 +64,7 @@ public class StartMenuAdapter extends ArrayAdapter<AppEntry> implements SectionI
private final List<Character> sections = new ArrayList<>();
private final SparseIntArray gpfsCache = new SparseIntArray();
private final SparseIntArray gsfpCache = new SparseIntArray();

private Thread cacheThread;
private final Map<AppEntry, Boolean> topAppsCache = new HashMap<>();

private final List<Character> lowercase = Arrays.asList(
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
Expand Down Expand Up @@ -171,15 +172,23 @@ public StartMenuAdapter(Context context, int layout, List<AppEntry> list) {
}

private boolean isTopApp(AppEntry entry) {
if(topAppsCache.containsKey(entry))
return topAppsCache.get(entry);

Intent intent = new Intent();
intent.setComponent(ComponentName.unflattenFromString(entry.getComponentName()));
ActivityInfo activityInfo = intent.resolveActivityInfo(getContext().getPackageManager(), 0);

if(activityInfo != null) {
TopApps topApps = TopApps.getInstance(getContext());
return topApps.isTopApp(activityInfo.packageName + "/" + activityInfo.name) || topApps.isTopApp(activityInfo.name);
boolean isTopApp = topApps.isTopApp(activityInfo.packageName + "/" + activityInfo.name)
|| topApps.isTopApp(activityInfo.name);

topAppsCache.put(entry, isTopApp);
return isTopApp;
}

topAppsCache.put(entry, false);
return false;
}

Expand Down Expand Up @@ -235,38 +244,24 @@ public void updateList(List<AppEntry> list) {
}

private void updateList(List<AppEntry> list, boolean firstUpdate) {
if(cacheThread != null && cacheThread.isAlive())
cacheThread.interrupt();

if(!firstUpdate) {
clear();

sections.clear();
gsfpCache.clear();
gpfsCache.clear();
topAppsCache.clear();

addAll(list);
}

SharedPreferences pref = U.getSharedPreferences(getContext());
if(pref.getBoolean("scrollbar", false)) {
cacheThread = new Thread(() -> {
for(AppEntry entry : list) {
char firstLetter = getSectionForAppEntry(entry);
if(!sections.contains(firstLetter))
sections.add(firstLetter);
}

for(int i = 0; i < sections.size(); i++) {
getPositionForSection(i);
}

for(int i = 0; i < getCount(); i++) {
getSectionForPosition(i);
}
});

cacheThread.run();
for(AppEntry entry : list) {
char firstLetter = getSectionForAppEntry(entry);
if(!sections.contains(firstLetter))
sections.add(firstLetter);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ private void refreshApps(final String query, final boolean firstDraw) {

startMenu.setSelection(position);

if(adapter.getCount() > 0)
if(adapter != null && adapter.getCount() > 0)
textView.setText(null);
else if(query != null)
textView.setText(getString(Patterns.WEB_URL.matcher(query).matches() ? R.string.press_enter_alt : R.string.press_enter));
Expand Down

0 comments on commit b2349f6

Please sign in to comment.