import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
-import android.os.Handler;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ProgressBar;
import com.farmerbb.taskbar.R;
-import com.farmerbb.taskbar.activity.dark.DesktopIconSelectAppActivityDark;
import com.farmerbb.taskbar.adapter.DesktopIconAppListAdapter;
import com.farmerbb.taskbar.util.AppEntry;
import com.farmerbb.taskbar.util.DesktopIconInfo;
import org.json.JSONArray;
import org.json.JSONException;
+import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
desktopIcon = (DesktopIconInfo) getIntent().getSerializableExtra("desktop_icon");
boolean noShadow = getIntent().hasExtra("no_shadow");
- if(savedInstanceState == null) {
- setContentView(R.layout.desktop_icon_select_app);
- setFinishOnTouchOutside(false);
- setTitle(getString(R.string.select_an_app));
+ setContentView(R.layout.desktop_icon_select_app);
+ setFinishOnTouchOutside(false);
+ setTitle(getString(R.string.select_an_app));
- if(noShadow) {
- WindowManager.LayoutParams params = getWindow().getAttributes();
- params.dimAmount = 0;
- getWindow().setAttributes(params);
+ if(noShadow) {
+ WindowManager.LayoutParams params = getWindow().getAttributes();
+ params.dimAmount = 0;
+ getWindow().setAttributes(params);
- if(U.isChromeOs(this) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1)
- getWindow().setElevation(0);
- }
-
- progressBar = findViewById(R.id.progress_bar);
- appList = findViewById(R.id.list);
-
- appListGenerator = new DesktopIconAppListGenerator();
- appListGenerator.execute();
- } else {
- finish();
-
- if(!noShadow)
- new Handler().post(() -> {
- Intent intent = null;
- SharedPreferences pref = U.getSharedPreferences(this);
-
- switch(pref.getString("theme", "light")) {
- case "light":
- intent = new Intent(this, DesktopIconSelectAppActivity.class);
- break;
- case "dark":
- intent = new Intent(this, DesktopIconSelectAppActivityDark.class);
- break;
- }
-
- if(intent != null)
- intent.putExtra("desktop_icon", desktopIcon);
-
- startActivity(intent);
- });
+ if(U.isChromeOs(this) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1)
+ getWindow().setElevation(0);
}
+
+ progressBar = findViewById(R.id.progress_bar);
+ appList = findViewById(R.id.list);
+
+ appListGenerator = new DesktopIconAppListGenerator();
+ appListGenerator.execute();
}
@Override
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> info = pm.queryIntentActivities(intent, 0);
- Collections.sort(info, (ai1, ai2) -> ai1.activityInfo.loadLabel(pm).toString().compareTo(ai2.activityInfo.loadLabel(pm).toString()));
+ Collections.sort(info, (ai1, ai2) -> {
+ String label1;
+ String label2;
+
+ try {
+ label1 = ai1.activityInfo.loadLabel(pm).toString();
+ label2 = ai2.activityInfo.loadLabel(pm).toString();
+ } catch (OutOfMemoryError e) {
+ System.gc();
+
+ label1 = ai1.activityInfo.packageName;
+ label2 = ai2.activityInfo.packageName;
+ }
+
+ return Collator.getInstance().compare(label1, label2);
+ });
final List<AppEntry> entries = new ArrayList<>();
for(ResolveInfo appInfo : info) {
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
+import android.content.pm.LauncherActivityInfo;
+import android.graphics.PorterDuff;
+import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import org.json.JSONArray;
import org.json.JSONException;
+import java.text.Collator;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+
public class HomeActivityDelegate extends Activity implements UIHost {
private TaskbarController taskbarController;
private StartMenuController startMenuController;
}
};
+ private BroadcastReceiver arrangeDesktopIconsReceiver = new BroadcastReceiver() {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ arrangeDesktopIcons();
+ }
+ };
+
@SuppressLint("RestrictedApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
if(FeatureFlags.homeActivityUIHost())
lbm.registerReceiver(restartReceiver, new IntentFilter("com.farmerbb.taskbar.RESTART"));
- if(FeatureFlags.desktopIcons(this))
+ if(FeatureFlags.desktopIcons(this)) {
lbm.registerReceiver(refreshDesktopIconsReceiver, new IntentFilter("com.farmerbb.taskbar.REFRESH_DESKTOP_ICONS"));
+ lbm.registerReceiver(arrangeDesktopIconsReceiver, new IntentFilter("com.farmerbb.taskbar.ARRANGE_DESKTOP_ICONS"));
+ }
U.initPrefs(this);
}
if(FeatureFlags.homeActivityUIHost())
lbm.unregisterReceiver(restartReceiver);
- if(FeatureFlags.desktopIcons(this))
+ if(FeatureFlags.desktopIcons(this)) {
lbm.unregisterReceiver(refreshDesktopIconsReceiver);
+ lbm.unregisterReceiver(arrangeDesktopIconsReceiver);
+ }
}
@Override
}
private void refreshDesktopIcons() {
- int desktopIconSize = getResources().getDimensionPixelSize(R.dimen.start_menu_grid_width)
- + getResources().getDimensionPixelSize(R.dimen.start_menu_grid_padding);
+ int desktopIconSize = getResources().getDimensionPixelSize(R.dimen.start_menu_grid_width);
int columns = layout.getWidth() / desktopIconSize;
int rows = layout.getHeight() / desktopIconSize;
desktopIcons.removeAllViews();
+ desktopIcons.setOrientation(LinearLayout.VERTICAL);
desktopIcons.setColumnCount(columns);
desktopIcons.setRowCount(rows);
}
}
+ private void arrangeDesktopIcons() {
+ try {
+ SharedPreferences pref = U.getSharedPreferences(this);
+ JSONArray jsonIcons = new JSONArray(pref.getString("desktop_icons", "[]"));
+ 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("desktop_icons", jsonIcons.toString()).apply();
+ refreshDesktopIcons();
+ } catch (JSONException e) { /* Gracefully fail */ }
+ }
+
private int getIndex(DesktopIconInfo info) {
- return (info.row * desktopIcons.getColumnCount()) + info.column;
+ return (info.column * desktopIcons.getRowCount()) + info.row;
}
private DesktopIconInfo getDesktopIconInfo(int index) {
- int column = index % desktopIcons.getColumnCount();
+ int row = index % desktopIcons.getRowCount();
int pos = index;
- int row = -1;
+ int column = -1;
while(pos >= 0) {
- pos -= desktopIcons.getColumnCount();
- row++;
+ pos -= desktopIcons.getRowCount();
+ column++;
}
return new DesktopIconInfo(column, row, null);
// do nothing
break;
case DragEvent.ACTION_DRAG_ENTERED:
- v.setBackgroundResource(R.drawable.drop_target);
+ Drawable dropTarget = ContextCompat.getDrawable(HomeActivityDelegate.this, R.drawable.drop_target);
+ if(dropTarget != null) {
+ dropTarget.setColorFilter(U.getAccentColor(HomeActivityDelegate.this), PorterDuff.Mode.DST_IN);
+ v.setBackground(dropTarget);
+ }
break;
case DragEvent.ACTION_DRAG_EXITED:
case DragEvent.ACTION_DRAG_ENDED: