use of com.farmerbb.taskbar.util.AppEntry in project Taskbar by farmerbb.
the class TaskbarController method updateRecentApps.
@SuppressWarnings("Convert2streamapi")
@TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
private void updateRecentApps(final boolean firstRefresh) {
if (isScreenOff())
return;
updateSystemTray();
SharedPreferences pref = U.getSharedPreferences(context);
final PackageManager pm = context.getPackageManager();
final List<AppEntry> entries = new ArrayList<>();
List<LauncherActivityInfo> launcherAppCache = new ArrayList<>();
int maxNumOfEntries = firstRefresh ? 0 : U.getMaxNumOfEntries(context);
boolean fullLength = pref.getBoolean(PREF_FULL_LENGTH, true);
PinnedBlockedApps pba = PinnedBlockedApps.getInstance(context);
List<AppEntry> pinnedApps = pba.getPinnedApps();
List<AppEntry> blockedApps = pba.getBlockedApps();
List<String> applicationIdsToRemove = new ArrayList<>();
// Filter out anything on the pinned/blocked apps lists
int realNumOfPinnedApps = filterRealPinnedApps(context, pinnedApps, entries, applicationIdsToRemove);
if (blockedApps.size() > 0) {
// noinspection SynchronizationOnLocalVariableOrMethodParameter
synchronized (blockedApps) {
for (AppEntry entry : blockedApps) {
applicationIdsToRemove.add(entry.getPackageName());
}
}
}
// Get list of all recently used apps
List<AppEntry> usageStatsList = realNumOfPinnedApps < maxNumOfEntries ? getAppEntries() : new ArrayList<>();
if (usageStatsList.size() > 0 || realNumOfPinnedApps > 0 || fullLength) {
if (realNumOfPinnedApps < maxNumOfEntries) {
List<AppEntry> usageStatsList2 = new ArrayList<>();
List<AppEntry> usageStatsList3 = new ArrayList<>();
List<AppEntry> usageStatsList4 = new ArrayList<>();
List<AppEntry> usageStatsList5 = new ArrayList<>();
List<AppEntry> usageStatsList6;
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory(Intent.CATEGORY_HOME);
ResolveInfo defaultLauncher = pm.resolveActivity(homeIntent, PackageManager.MATCH_DEFAULT_ONLY);
// Also filter out the current launcher, and Taskbar itself
for (AppEntry packageInfo : usageStatsList) {
if (hasLauncherIntent(packageInfo.getPackageName()) && !packageInfo.getPackageName().contains(BuildConfig.BASE_APPLICATION_ID) && !packageInfo.getPackageName().equals(defaultLauncher.activityInfo.packageName) && (!(U.launcherIsDefault(context) && pref.getBoolean(PREF_DESKTOP_MODE, false)) || !packageInfo.getPackageName().equals(pref.getString(PREF_HSL_ID, "null"))))
usageStatsList2.add(packageInfo);
}
// Filter out apps that don't fall within our current search interval
for (AppEntry stats : usageStatsList2) {
if (stats.getLastTimeUsed() > searchInterval || runningAppsOnly)
usageStatsList3.add(stats);
}
// Sort apps by either most recently used, or most time used
if (!runningAppsOnly && sortOrder.contains("most_used")) {
Collections.sort(usageStatsList3, (us1, us2) -> Long.compare(us2.getTotalTimeInForeground(), us1.getTotalTimeInForeground()));
} else {
Collections.sort(usageStatsList3, (us1, us2) -> Long.compare(us2.getLastTimeUsed(), us1.getLastTimeUsed()));
}
// Filter out any duplicate entries
List<String> applicationIds = new ArrayList<>();
for (AppEntry stats : usageStatsList3) {
if (!applicationIds.contains(stats.getPackageName())) {
usageStatsList4.add(stats);
applicationIds.add(stats.getPackageName());
}
}
// Filter out the currently running foreground app, if requested by the user
filterForegroundApp(context, pref, searchInterval, applicationIdsToRemove);
for (AppEntry stats : usageStatsList4) {
if (!applicationIdsToRemove.contains(stats.getPackageName())) {
usageStatsList5.add(stats);
}
}
// Truncate list to a maximum length
if (usageStatsList5.size() > maxNumOfEntries)
usageStatsList6 = usageStatsList5.subList(0, maxNumOfEntries);
else
usageStatsList6 = usageStatsList5;
// Determine if we need to reverse the order
if (needToReverseOrder(context, sortOrder)) {
Collections.reverse(usageStatsList6);
}
// Generate the AppEntries for the recent apps list
int number = usageStatsList6.size() == maxNumOfEntries ? usageStatsList6.size() - realNumOfPinnedApps : usageStatsList6.size();
generateAppEntries(context, number, usageStatsList6, entries, launcherAppCache);
}
while (entries.size() > maxNumOfEntries) {
try {
entries.remove(entries.size() - 1);
launcherAppCache.remove(launcherAppCache.size() - 1);
} catch (ArrayIndexOutOfBoundsException ignored) {
}
}
// Determine if we need to reverse the order again
if (TaskbarPosition.isVertical(context)) {
Collections.reverse(entries);
Collections.reverse(launcherAppCache);
}
// Now that we've generated the list of apps,
// we need to determine if we need to redraw the Taskbar or not
boolean shouldRedrawTaskbar = firstRefresh;
List<String> finalApplicationIds = new ArrayList<>();
for (AppEntry entry : entries) {
finalApplicationIds.add(entry.getPackageName());
}
int realNumOfSysTrayIcons = 0;
for (Integer key : sysTrayIconStates.keySet()) {
if (sysTrayIconStates.get(key))
realNumOfSysTrayIcons++;
}
if (finalApplicationIds.size() != currentTaskbarIds.size() || numOfPinnedApps != realNumOfPinnedApps || numOfSysTrayIcons != realNumOfSysTrayIcons)
shouldRedrawTaskbar = true;
else {
for (int i = 0; i < finalApplicationIds.size(); i++) {
if (!finalApplicationIds.get(i).equals(currentTaskbarIds.get(i))) {
shouldRedrawTaskbar = true;
break;
}
}
}
if (shouldRedrawTaskbar) {
currentTaskbarIds = finalApplicationIds;
numOfPinnedApps = realNumOfPinnedApps;
numOfSysTrayIcons = realNumOfSysTrayIcons;
populateAppEntries(context, pm, entries, launcherAppCache);
final int numOfEntries = Math.min(entries.size(), maxNumOfEntries);
handler.post(() -> {
if (numOfEntries > 0 || fullLength) {
ViewGroup.LayoutParams params = scrollView.getLayoutParams();
calculateScrollViewParams(context, pref, params, fullLength, numOfEntries);
scrollView.setLayoutParams(params);
for (Integer key : sysTrayIconStates.keySet()) {
sysTrayLayout.findViewById(key).setVisibility(sysTrayIconStates.get(key) ? View.VISIBLE : View.GONE);
}
taskbar.removeAllViews();
for (int i = 0; i < entries.size(); i++) {
taskbar.addView(getView(entries, i));
}
if (runningAppsOnly)
updateRunningAppIndicators(pinnedApps, usageStatsList, entries);
isShowingRecents = true;
if (shouldRefreshRecents && scrollView.getVisibility() != View.VISIBLE) {
if (firstRefresh)
scrollView.setVisibility(View.INVISIBLE);
else
scrollView.setVisibility(View.VISIBLE);
}
if (firstRefresh && scrollView.getVisibility() != View.VISIBLE) {
U.newHandler().post(() -> scrollTaskbar(scrollView, taskbar, TaskbarPosition.getTaskbarPosition(context), sortOrder, shouldRefreshRecents));
}
} else {
isShowingRecents = false;
scrollView.setVisibility(View.GONE);
}
});
} else if (runningAppsOnly)
handler.post(() -> updateRunningAppIndicators(pinnedApps, usageStatsList, entries));
} else if (firstRefresh || currentTaskbarIds.size() > 0) {
currentTaskbarIds.clear();
handler.post(() -> {
isShowingRecents = false;
scrollView.setVisibility(View.GONE);
});
}
}
use of com.farmerbb.taskbar.util.AppEntry in project Taskbar by farmerbb.
the class PersistentShortcutSelectAppActivity method selectApp.
@Override
public void selectApp(AppEntry entry) {
selectedEntry = entry;
boolean windowSizeOptions = Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && U.hasFreeformSupport(this);
boolean iconOptions = getIntent().getIntExtra(PREF_QS_TILE, 0) > 0;
if (!windowSizeOptions && !iconOptions) {
createShortcut(null);
return;
}
LinearLayout layout = (LinearLayout) View.inflate(this, R.layout.tb_shortcut_options, null);
final Spinner spinner = layout.findViewById(R.id.spinner);
final CheckBox checkBox = layout.findViewById(R.id.checkBox);
String[] windowSizes = getResources().getStringArray(R.array.tb_pref_window_size_list_values);
if (windowSizeOptions) {
layout.findViewById(R.id.window_size_options).setVisibility(View.VISIBLE);
SharedPreferences pref = U.getSharedPreferences(this);
boolean isFreeformEnabled = U.isFreeformModeEnabled(this);
checkBox.setChecked(isFreeformEnabled);
spinner.setEnabled(isFreeformEnabled);
String defaultWindowSize = pref.getString(PREF_WINDOW_SIZE, "standard");
for (int i = 0; i < windowSizes.length; i++) {
if (windowSizes[i].equals(defaultWindowSize)) {
spinner.setSelection(i);
break;
}
}
checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> spinner.setEnabled(isChecked));
}
if (iconOptions) {
layout.findViewById(R.id.icon_options).setVisibility(View.VISIBLE);
SeekBar seekBar = layout.findViewById(R.id.seekbar);
ImageView imageView = layout.findViewById(R.id.icon_preview);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
Context context = PersistentShortcutSelectAppActivity.this;
Drawable icon = selectedEntry.getIcon(context);
threshold = (float) Math.log10(progress + 1) / 2;
if (processing)
return;
Handler handler = U.newHandler();
new Thread(() -> {
processing = true;
while (threshold != thresholdInProcess) {
thresholdInProcess = threshold;
Drawable monoIcon = U.convertToMonochrome(context, icon, thresholdInProcess);
Drawable resizedIcon = U.resizeDrawable(context, monoIcon, R.dimen.tb_qs_icon_preview_size);
handler.post(() -> imageView.setImageDrawable(resizedIcon));
}
processing = false;
}).start();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
thresholdInProcess = -1;
seekBar.setProgress(50);
}
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(selectedEntry.getLabel()).setView(layout).setPositiveButton(R.string.tb_action_ok, (dialog, which) -> {
if (windowSizeOptions)
createShortcut(checkBox.isChecked() ? windowSizes[spinner.getSelectedItemPosition()] : null);
else
createShortcut(null);
}).setNegativeButton(R.string.tb_action_cancel, null);
AlertDialog dialog = builder.create();
dialog.show();
}
use of com.farmerbb.taskbar.util.AppEntry in project Taskbar by farmerbb.
the class TaskbarController method updateRunningAppIndicators.
private void updateRunningAppIndicators(List<AppEntry> pinnedApps, List<AppEntry> usageStatsList, List<AppEntry> entries) {
if (taskbar.getChildCount() != entries.size())
return;
List<String> pinnedPackageList = new ArrayList<>();
List<String> runningPackageList = new ArrayList<>();
for (AppEntry entry : pinnedApps) pinnedPackageList.add(entry.getPackageName());
for (AppEntry entry : usageStatsList) runningPackageList.add(entry.getPackageName());
for (int i = 0; i < taskbar.getChildCount(); i++) {
View convertView = taskbar.getChildAt(i);
String packageName = entries.get(i).getPackageName();
ImageView runningAppIndicator = convertView.findViewById(R.id.running_app_indicator);
if (pinnedPackageList.contains(packageName) && !runningPackageList.contains(packageName))
runningAppIndicator.setVisibility(View.GONE);
else {
runningAppIndicator.setVisibility(View.VISIBLE);
runningAppIndicator.setColorFilter(U.getAccentColor(context));
}
}
}
use of com.farmerbb.taskbar.util.AppEntry in project Taskbar by farmerbb.
the class TaskbarController method getView.
private View getView(List<AppEntry> list, int position) {
View convertView = View.inflate(context, R.layout.tb_icon, null);
final AppEntry entry = list.get(position);
final SharedPreferences pref = U.getSharedPreferences(context);
ImageView imageView = convertView.findViewById(R.id.icon);
ImageView imageView2 = convertView.findViewById(R.id.shortcut_icon);
imageView.setImageDrawable(entry.getIcon(context));
imageView2.setBackgroundColor(U.getAccentColor(context));
String taskbarPosition = TaskbarPosition.getTaskbarPosition(context);
if (pref.getBoolean(PREF_SHORTCUT_ICON, true)) {
boolean shouldShowShortcutIcon;
if (taskbarPosition.contains("vertical"))
shouldShowShortcutIcon = position >= list.size() - numOfPinnedApps;
else
shouldShowShortcutIcon = position < numOfPinnedApps;
if (shouldShowShortcutIcon)
imageView2.setVisibility(View.VISIBLE);
}
if (POSITION_BOTTOM_RIGHT.equals(taskbarPosition) || POSITION_TOP_RIGHT.equals(taskbarPosition)) {
imageView.setRotationY(180);
imageView2.setRotationY(180);
}
FrameLayout layout = convertView.findViewById(R.id.entry);
layout.setOnClickListener(view -> U.launchApp(context, entry, null, true, false, view));
layout.setOnLongClickListener(view -> {
int[] location = new int[2];
view.getLocationOnScreen(location);
openContextMenu(entry, location);
return true;
});
layout.setOnGenericMotionListener((view, motionEvent) -> {
int action = motionEvent.getAction();
if (action == MotionEvent.ACTION_BUTTON_PRESS && motionEvent.getButtonState() == MotionEvent.BUTTON_SECONDARY) {
int[] location = new int[2];
view.getLocationOnScreen(location);
openContextMenu(entry, location);
}
if (action == MotionEvent.ACTION_SCROLL && pref.getBoolean(PREF_VISUAL_FEEDBACK, true))
view.setBackgroundColor(0);
return false;
});
if (pref.getBoolean(PREF_VISUAL_FEEDBACK, true)) {
layout.setOnHoverListener((v, event) -> {
if (event.getAction() == MotionEvent.ACTION_HOVER_ENTER) {
int accentColor = U.getAccentColor(context);
accentColor = ColorUtils.setAlphaComponent(accentColor, Color.alpha(accentColor) / 2);
v.setBackgroundColor(accentColor);
}
if (event.getAction() == MotionEvent.ACTION_HOVER_EXIT)
v.setBackgroundColor(0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
v.setPointerIcon(PointerIcon.getSystemIcon(context, PointerIcon.TYPE_DEFAULT));
return false;
});
layout.setOnTouchListener((v, event) -> {
v.setAlpha(event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE ? 0.5f : 1);
return false;
});
}
return convertView;
}
use of com.farmerbb.taskbar.util.AppEntry in project Taskbar by farmerbb.
the class TaskbarController method filterRealPinnedApps.
@VisibleForTesting
int filterRealPinnedApps(Context context, List<AppEntry> pinnedApps, List<AppEntry> entries, List<String> applicationIdsToRemove) {
int realNumOfPinnedApps = 0;
if (pinnedApps.size() > 0) {
// noinspection SynchronizationOnLocalVariableOrMethodParameter
synchronized (pinnedApps) {
UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
LauncherApps launcherApps = (LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE);
for (AppEntry entry : pinnedApps) {
boolean packageEnabled = launcherApps.isPackageEnabled(entry.getPackageName(), userManager.getUserForSerialNumber(entry.getUserId(context)));
if (packageEnabled)
entries.add(entry);
else
realNumOfPinnedApps--;
applicationIdsToRemove.add(entry.getPackageName());
}
realNumOfPinnedApps = realNumOfPinnedApps + pinnedApps.size();
}
}
return realNumOfPinnedApps;
}
Aggregations