use of com.farmerbb.taskbar.util.DesktopIconInfo in project Taskbar by farmerbb.
the class HomeActivityDelegate method refreshDesktopIcons.
private void refreshDesktopIcons() {
if (desktopIcons == null)
return;
boolean taskbarIsVertical = TaskbarPosition.isVertical(this);
int iconSize = getResources().getDimensionPixelSize(R.dimen.tb_icon_size);
int desktopIconSize = getResources().getDimensionPixelSize(R.dimen.tb_start_menu_grid_width);
int columns = (layout.getWidth() - (taskbarIsVertical ? iconSize : 0)) / desktopIconSize;
int rows = (layout.getHeight() - (!taskbarIsVertical ? iconSize : 0)) / desktopIconSize;
desktopIcons.removeAllViews();
desktopIcons.setOrientation(GridLayout.VERTICAL);
desktopIcons.setColumnCount(columns);
desktopIcons.setRowCount(rows);
LauncherApps launcherApps = (LauncherApps) getSystemService(LAUNCHER_APPS_SERVICE);
UserManager userManager = (UserManager) getSystemService(USER_SERVICE);
SparseArray<DesktopIconInfo> icons = new SparseArray<>();
List<Integer> iconsToRemove = new ArrayList<>();
try {
SharedPreferences pref = U.getSharedPreferences(this);
JSONArray jsonIcons = new JSONArray(pref.getString(PREF_DESKTOP_ICONS, "[]"));
for (int i = 0; i < jsonIcons.length(); i++) {
DesktopIconInfo info = DesktopIconInfo.fromJson(jsonIcons.getJSONObject(i));
if (info != null) {
if (launcherApps.isActivityEnabled(ComponentName.unflattenFromString(info.entry.getComponentName()), userManager.getUserForSerialNumber(info.entry.getUserId(this))))
icons.put(getIndex(info), info);
else
iconsToRemove.add(i);
}
}
if (!iconsToRemove.isEmpty()) {
for (int i : iconsToRemove) {
jsonIcons.remove(i);
}
pref.edit().putString(PREF_DESKTOP_ICONS, jsonIcons.toString()).apply();
}
} catch (JSONException ignored) {
}
for (int i = 0; i < columns * rows; i++) {
GridLayout.LayoutParams params = new GridLayout.LayoutParams(GridLayout.spec(GridLayout.UNDEFINED, GridLayout.FILL, 1f), GridLayout.spec(GridLayout.UNDEFINED, GridLayout.FILL, 1f));
params.width = 0;
params.height = 0;
FrameLayout iconContainer = new FrameLayout(this);
iconContainer.setLayoutParams(params);
iconContainer.setOnDragListener(new DesktopIconDragListener());
int index = i;
iconContainer.setOnClickListener(view -> {
boolean isStartMenuOpen = MenuHelper.getInstance().isStartMenuOpen();
U.sendBroadcast(this, ACTION_HIDE_START_MENU);
DesktopIconInfo info = icons.get(index);
if (!isStartMenuOpen && info != null && info.entry != null) {
U.launchApp(this, info.entry, null, false, false, view);
}
});
iconContainer.setOnLongClickListener(view -> {
int[] location = new int[2];
view.getLocationOnScreen(location);
DesktopIconInfo info = icons.get(index);
if (info == null)
info = getDesktopIconInfo(index);
openContextMenu(info, location);
return true;
});
iconContainer.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);
DesktopIconInfo info = icons.get(index);
if (info == null)
info = getDesktopIconInfo(index);
openContextMenu(info, location);
}
return false;
});
iconContainer.setOnTouchListener((v, event) -> {
if (detector != null)
detector.onTouchEvent(event);
return false;
});
iconContainer.setFocusable(false);
DesktopIconInfo info = icons.get(index);
if (info != null && info.entry != null && info.column < columns && info.row < rows)
iconContainer.addView(inflateDesktopIcon(iconContainer, info.entry));
desktopIcons.addView(iconContainer);
}
}
use of com.farmerbb.taskbar.util.DesktopIconInfo in project Taskbar by farmerbb.
the class HomeActivityDelegate method sortDesktopIcons.
private void sortDesktopIcons() {
try {
SharedPreferences pref = U.getSharedPreferences(this);
JSONArray jsonIcons = new JSONArray(pref.getString(PREF_DESKTOP_ICONS, "[]"));
if (jsonIcons.length() == 0) {
U.showToast(this, R.string.tb_no_icons_to_sort);
return;
}
List<DesktopIconInfo> icons = new ArrayList<>();
for (int i = 0; i < jsonIcons.length(); i++) {
DesktopIconInfo info = DesktopIconInfo.fromJson(jsonIcons.getJSONObject(i));
if (info != null)
icons.add(info);
}
Collections.sort(icons, (o1, o2) -> Collator.getInstance().compare(o1.entry.getLabel(), o2.entry.getLabel()));
jsonIcons = new JSONArray();
for (int i = 0; i < icons.size(); i++) {
DesktopIconInfo oldInfo = icons.get(i);
DesktopIconInfo newInfo = getDesktopIconInfo(i);
oldInfo.column = newInfo.column;
oldInfo.row = newInfo.row;
jsonIcons.put(oldInfo.toJson(this));
}
pref.edit().putString(PREF_DESKTOP_ICONS, jsonIcons.toString()).apply();
refreshDesktopIcons();
} catch (JSONException ignored) {
}
}
use of com.farmerbb.taskbar.util.DesktopIconInfo in project Taskbar by farmerbb.
the class ContextMenuActivity method onPreferenceClick.
@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.N_MR1)
@Override
public boolean onPreferenceClick(Preference p) {
UserManager userManager = (UserManager) getSystemService(USER_SERVICE);
LauncherApps launcherApps = (LauncherApps) getSystemService(LAUNCHER_APPS_SERVICE);
boolean appIsValid = isStartButton || isOverflowMenu || desktopIcon != null || (entry != null && !launcherApps.getActivityList(entry.getPackageName(), userManager.getUserForSerialNumber(entry.getUserId(this))).isEmpty());
secondaryMenu = false;
if (appIsValid)
switch(p.getKey()) {
case PREF_APP_INFO:
U.launchApp(this, () -> launcherApps.startAppDetailsActivity(ComponentName.unflattenFromString(entry.getComponentName()), userManager.getUserForSerialNumber(entry.getUserId(this)), null, U.getActivityOptionsBundle(this, ApplicationType.APP_PORTRAIT, getListView().getChildAt(p.getOrder()))));
prepareToClose();
break;
case PREF_UNINSTALL:
if (U.hasFreeformSupport(this) && isInMultiWindowMode()) {
Intent intent2 = new Intent(this, DummyActivity.class);
intent2.putExtra("uninstall", entry.getPackageName());
intent2.putExtra("user_id", entry.getUserId(this));
try {
startActivity(intent2);
} catch (IllegalArgumentException ignored) {
}
} else {
Intent intent2 = new Intent(Intent.ACTION_DELETE, Uri.parse("package:" + entry.getPackageName()));
intent2.putExtra(Intent.EXTRA_USER, userManager.getUserForSerialNumber(entry.getUserId(this)));
try {
startActivity(intent2);
} catch (ActivityNotFoundException | IllegalArgumentException ignored) {
}
}
prepareToClose();
break;
case PREF_OPEN_TASKBAR_SETTINGS:
U.launchApp(this, () -> {
Intent intent2 = new Intent(this, MainActivity.class);
intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
LauncherHelper helper = LauncherHelper.getInstance();
if (helper.isOnHomeScreen(this) || helper.isOnSecondaryHomeScreen(this))
U.applyOpenInNewWindow(this, intent2);
try {
startActivity(intent2, U.getActivityOptionsBundle(this, ApplicationType.APP_PORTRAIT, getListView().getChildAt(p.getOrder())));
} catch (IllegalArgumentException ignored) {
}
});
prepareToClose();
break;
case PREF_QUIT_TASKBAR:
Intent quitIntent = new Intent(ACTION_QUIT);
quitIntent.setPackage(getPackageName());
sendBroadcast(quitIntent);
prepareToClose();
break;
case PREF_PIN_APP:
PinnedBlockedApps pba = PinnedBlockedApps.getInstance(this);
if (pba.isPinned(entry.getComponentName()))
pba.removePinnedApp(this, entry.getComponentName());
else {
Intent intent = new Intent();
intent.setComponent(ComponentName.unflattenFromString(entry.getComponentName()));
LauncherActivityInfo appInfo = launcherApps.resolveActivity(intent, userManager.getUserForSerialNumber(entry.getUserId(this)));
if (appInfo != null) {
AppEntry newEntry = new AppEntry(entry.getPackageName(), entry.getComponentName(), entry.getLabel(), IconCache.getInstance(this).getIcon(this, appInfo), true);
newEntry.setUserId(entry.getUserId(this));
pba.addPinnedApp(this, newEntry);
}
}
break;
case PREF_BLOCK_APP:
PinnedBlockedApps pba2 = PinnedBlockedApps.getInstance(this);
if (pba2.isBlocked(entry.getComponentName()))
pba2.removeBlockedApp(this, entry.getComponentName());
else
pba2.addBlockedApp(this, entry);
break;
case PREF_SHOW_WINDOW_SIZES:
generateWindowSizes();
if (U.hasBrokenSetLaunchBoundsApi())
U.showToastLong(this, R.string.tb_window_sizes_not_available);
getListView().setOnItemLongClickListener((parent, view, position, id) -> {
String[] windowSizes = getResources().getStringArray(R.array.tb_pref_window_size_list_values);
SavedWindowSizes.getInstance(this).setWindowSize(this, entry.getPackageName(), windowSizes[position]);
generateWindowSizes();
return true;
});
secondaryMenu = true;
break;
case PREF_WINDOW_SIZE_STANDARD:
case PREF_WINDOW_SIZE_LARGE:
case PREF_WINDOW_SIZE_FULLSCREEN:
case PREF_WINDOW_SIZE_HALF_LEFT:
case PREF_WINDOW_SIZE_HALF_RIGHT:
case PREF_WINDOW_SIZE_PHONE_SIZE:
String windowSize = p.getKey().replace("window_size_", "");
SharedPreferences pref2 = U.getSharedPreferences(this);
if (pref2.getBoolean(PREF_SAVE_WINDOW_SIZES, true)) {
SavedWindowSizes.getInstance(this).setWindowSize(this, entry.getPackageName(), windowSize);
}
U.launchApp(U.getDisplayContext(this), entry, windowSize, false, true, getListView().getChildAt(p.getOrder()));
if (U.hasBrokenSetLaunchBoundsApi())
U.cancelToast();
prepareToClose();
break;
case PREF_APP_SHORTCUTS:
getPreferenceScreen().removeAll();
generateShortcuts();
secondaryMenu = true;
break;
case PREF_SHORTCUT_1:
case PREF_SHORTCUT_2:
case PREF_SHORTCUT_3:
case PREF_SHORTCUT_4:
case PREF_SHORTCUT_5:
U.startShortcut(U.getDisplayContext(this), entry, shortcuts.get(Integer.parseInt(p.getKey().replace("shortcut_", "")) - 1), getListView().getChildAt(p.getOrder()));
prepareToClose();
break;
case PREF_START_MENU_APPS:
Intent intent = U.getThemedIntent(this, SelectAppActivity.class);
if (U.hasFreeformSupport(this) && U.isFreeformModeEnabled(this) && isInMultiWindowMode()) {
intent.putExtra("no_shadow", true);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT);
U.startActivityMaximized(U.getDisplayContext(this), intent);
} else {
try {
startActivity(intent);
} catch (IllegalArgumentException ignored) {
}
}
prepareToClose();
break;
case PREF_VOLUME:
AudioManager audio = (AudioManager) getSystemService(AUDIO_SERVICE);
audio.adjustSuggestedStreamVolume(AudioManager.ADJUST_SAME, AudioManager.USE_DEFAULT_STREAM_TYPE, AudioManager.FLAG_SHOW_UI);
if (LauncherHelper.getInstance().isOnSecondaryHomeScreen(this)) {
U.showToast(this, R.string.tb_opening_volume_control);
U.sendBroadcast(this, ACTION_UNDIM_SCREEN);
}
prepareToClose();
break;
case PREF_FILE_MANAGER:
U.launchApp(this, () -> {
Intent fileManagerIntent;
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1)
fileManagerIntent = new Intent(Intent.ACTION_VIEW);
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
fileManagerIntent = new Intent("android.provider.action.BROWSE");
else {
fileManagerIntent = new Intent("android.provider.action.BROWSE_DOCUMENT_ROOT");
fileManagerIntent.setComponent(ComponentName.unflattenFromString("com.android.documentsui/.DocumentsActivity"));
}
fileManagerIntent.addCategory(Intent.CATEGORY_DEFAULT);
fileManagerIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
fileManagerIntent.setData(Uri.parse("content://com.android.externalstorage.documents/root/primary"));
try {
startActivity(fileManagerIntent, U.getActivityOptionsBundle(this, ApplicationType.APP_PORTRAIT, getListView().getChildAt(p.getOrder())));
} catch (ActivityNotFoundException e) {
U.showToast(this, R.string.tb_lock_device_not_supported);
} catch (IllegalArgumentException ignored) {
}
});
prepareToClose();
break;
case PREF_SYSTEM_SETTINGS:
U.launchApp(this, () -> {
Intent settingsIntent = new Intent(Settings.ACTION_SETTINGS);
settingsIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
startActivity(settingsIntent, U.getActivityOptionsBundle(this, ApplicationType.APP_PORTRAIT, getListView().getChildAt(p.getOrder())));
} catch (ActivityNotFoundException e) {
U.showToast(this, R.string.tb_lock_device_not_supported);
} catch (IllegalArgumentException ignored) {
}
});
prepareToClose();
break;
case PREF_LOCK_DEVICE:
U.lockDevice(this);
prepareToClose();
break;
case PREF_POWER_MENU:
U.sendAccessibilityAction(this, AccessibilityService.GLOBAL_ACTION_POWER_DIALOG, () -> {
if (LauncherHelper.getInstance().isOnSecondaryHomeScreen(this)) {
U.showToast(this, R.string.tb_opening_power_menu);
U.sendBroadcast(this, ACTION_UNDIM_SCREEN);
}
});
prepareToClose();
break;
case PREF_ADD_ICON_TO_DESKTOP:
Intent intent2 = U.getThemedIntent(this, DesktopIconSelectAppActivity.class);
intent2.putExtra("desktop_icon", desktopIcon);
if (U.hasFreeformSupport(this) && U.isFreeformModeEnabled(this) && isInMultiWindowMode()) {
intent2.putExtra("no_shadow", true);
intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT);
U.startActivityMaximized(U.getDisplayContext(this), intent2);
} else {
try {
startActivity(intent2);
} catch (IllegalArgumentException ignored) {
}
}
prepareToClose();
break;
case PREF_ARRANGE_ICONS:
U.sendBroadcast(this, ACTION_ENTER_ICON_ARRANGE_MODE);
break;
case PREF_SORT_BY_NAME:
U.sendBroadcast(this, ACTION_SORT_DESKTOP_ICONS);
break;
case PREF_CHANGE_WALLPAPER:
if (LauncherHelper.getInstance().isOnSecondaryHomeScreen(this)) {
generateWallpaperOptions();
secondaryMenu = true;
} else if (U.isChromeOs(this)) {
U.sendBroadcast(this, ACTION_WALLPAPER_CHANGE_REQUESTED);
} else {
changeWallpaper();
prepareToClose();
}
break;
case PREF_REMOVE_DESKTOP_ICON:
try {
SharedPreferences pref5 = U.getSharedPreferences(this);
JSONArray jsonIcons = new JSONArray(pref5.getString(PREF_DESKTOP_ICONS, "[]"));
int iconToRemove = -1;
for (int i = 0; i < jsonIcons.length(); i++) {
DesktopIconInfo info = DesktopIconInfo.fromJson(jsonIcons.getJSONObject(i));
if (info != null && info.column == desktopIcon.column && info.row == desktopIcon.row) {
iconToRemove = i;
break;
}
}
if (iconToRemove > -1) {
jsonIcons.remove(iconToRemove);
pref5.edit().putString(PREF_DESKTOP_ICONS, jsonIcons.toString()).apply();
U.sendBroadcast(this, ACTION_REFRESH_DESKTOP_ICONS);
}
} catch (JSONException ignored) {
}
break;
case PREF_CHANGE_WALLPAPER_GLOBAL:
changeWallpaper();
prepareToClose();
break;
case PREF_CHANGE_WALLPAPER_DESKTOP:
U.sendBroadcast(this, ACTION_WALLPAPER_CHANGE_REQUESTED);
break;
case PREF_REMOVE_DESKTOP_WALLPAPER:
U.sendBroadcast(this, ACTION_REMOVE_DESKTOP_WALLPAPER);
break;
}
if (!secondaryMenu)
finish();
return true;
}
use of com.farmerbb.taskbar.util.DesktopIconInfo in project Taskbar by farmerbb.
the class HomeActivityDelegate method getDesktopIconInfo.
private DesktopIconInfo getDesktopIconInfo(int index) {
int row = index % desktopIcons.getRowCount();
int pos = index;
int column = -1;
while (pos >= 0) {
pos -= desktopIcons.getRowCount();
column++;
}
return new DesktopIconInfo(column, row, null);
}
use of com.farmerbb.taskbar.util.DesktopIconInfo in project Taskbar by farmerbb.
the class HomeActivityDelegate method reassignDroppedIcon.
private void reassignDroppedIcon() {
if (startDragIndex == endDragIndex)
return;
try {
SharedPreferences pref = U.getSharedPreferences(this);
JSONArray jsonIcons = new JSONArray(pref.getString(PREF_DESKTOP_ICONS, "[]"));
int iconToRemove = -1;
DesktopIconInfo oldInfo = getDesktopIconInfo(startDragIndex);
DesktopIconInfo newInfo = getDesktopIconInfo(endDragIndex);
for (int i = 0; i < jsonIcons.length(); i++) {
DesktopIconInfo info = DesktopIconInfo.fromJson(jsonIcons.getJSONObject(i));
if (info != null && info.column == oldInfo.column && info.row == oldInfo.row) {
newInfo.entry = info.entry;
iconToRemove = i;
break;
}
}
if (iconToRemove > -1) {
jsonIcons.remove(iconToRemove);
jsonIcons.put(newInfo.toJson(this));
pref.edit().putString(PREF_DESKTOP_ICONS, jsonIcons.toString()).apply();
}
} catch (JSONException ignored) {
}
}
Aggregations