feat: Add option to clear home screen in settings (#6125)

Signed-off-by: abhixv <abhi.sharma1@hotmail.com>
This commit is contained in:
Abhishek Sharma
2025-12-20 03:26:30 +05:30
committed by Pun Butrach
parent 9898749619
commit 5f3a03f4fb
1577 changed files with 112563 additions and 80248 deletions
+49 -82
View File
@@ -24,47 +24,35 @@ import android.content.ComponentName;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.os.Binder;
import android.os.Bundle;
import android.os.Process;
import android.text.TextUtils;
import android.util.Log;
import android.util.Pair;
import com.android.launcher3.LauncherSettings.Favorites;
import com.android.launcher3.model.ModelDbController;
import com.android.launcher3.util.LayoutImportExportHelper;
import com.android.launcher3.widget.LauncherWidgetHolder;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.ToIntFunction;
public class LauncherProvider extends ContentProvider {
private static final String TAG = "LauncherProvider";
// Method API For Provider#call method.
private static final String METHOD_EXPORT_LAYOUT_XML = "EXPORT_LAYOUT_XML";
private static final String METHOD_IMPORT_LAYOUT_XML = "IMPORT_LAYOUT_XML";
private static final String KEY_RESULT = "KEY_RESULT";
private static final String KEY_LAYOUT = "KEY_LAYOUT";
private static final String SUCCESS = "success";
private static final String FAILURE = "failure";
/**
* $ adb shell dumpsys activity provider com.android.launcher3
*/
@Override
public void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
LauncherModel model = LauncherAppState.INSTANCE.get(getContext()).getModel();
if (model.isModelLoaded()) {
model.dumpState("", fd, writer, args);
}
LauncherAppState.INSTANCE.executeIfCreated(appState -> {
if (appState.getModel().isModelLoaded()) {
appState.getModel().dumpState("", fd, writer, args);
}
});
}
@Override
@@ -79,20 +67,24 @@ public class LauncherProvider extends ContentProvider {
@Override
public String getType(Uri uri) {
if (TextUtils.isEmpty(parseUri(uri, null, null).first)) {
return "vnd.android.cursor.dir/" + Favorites.TABLE_NAME;
SqlArguments args = new SqlArguments(uri, null, null);
if (TextUtils.isEmpty(args.where)) {
return "vnd.android.cursor.dir/" + args.table;
} else {
return "vnd.android.cursor.item/" + Favorites.TABLE_NAME;
return "vnd.android.cursor.item/" + args.table;
}
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
Pair<String, String[]> args = parseUri(uri, selection, selectionArgs);
SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(args.table);
Cursor[] result = new Cursor[1];
executeControllerTask(controller -> {
result[0] = controller.query(projection, args.first, args.second, sortOrder);
result[0] = controller.query(args.table, projection, args.where, args.args, sortOrder);
return 0;
});
return result[0];
@@ -109,7 +101,7 @@ public class LauncherProvider extends ContentProvider {
// attempt allocate and bind the widget.
Integer itemType = values.getAsInteger(Favorites.ITEM_TYPE);
if (itemType != null
&& itemType == Favorites.ITEM_TYPE_APPWIDGET
&& itemType.intValue() == Favorites.ITEM_TYPE_APPWIDGET
&& !values.containsKey(Favorites.APPWIDGET_ID)) {
ComponentName cn = ComponentName.unflattenFromString(
@@ -136,7 +128,8 @@ public class LauncherProvider extends ContentProvider {
}
}
return controller.insert(values);
SqlArguments args = new SqlArguments(uri);
return controller.insert(args.table, values);
});
return rowId < 0 ? null : ContentUris.withAppendedId(uri, rowId);
@@ -144,51 +137,14 @@ public class LauncherProvider extends ContentProvider {
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
Pair<String, String[]> args = parseUri(uri, selection, selectionArgs);
return executeControllerTask(c -> c.delete(args.first, args.second));
SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
return executeControllerTask(c -> c.delete(args.table, args.where, args.args));
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
Pair<String, String[]> args = parseUri(uri, selection, selectionArgs);
return executeControllerTask(c -> c.update(values, args.first, args.second));
}
@Override
public Bundle call(String method, String arg, Bundle extras) {
Bundle b = new Bundle();
// The caller must have the read or write permission for this content provider to
// access the "call" method at all. We also enforce the appropriate per-method permissions.
switch(method) {
case METHOD_EXPORT_LAYOUT_XML:
if (getContext().checkCallingOrSelfPermission(getReadPermission())
!= PackageManager.PERMISSION_GRANTED) {
throw new SecurityException("Caller doesn't have read permission");
}
CompletableFuture<String> resultFuture = LayoutImportExportHelper.INSTANCE
.exportModelDbAsXmlFuture(getContext());
try {
b.putString(KEY_LAYOUT, resultFuture.get());
b.putString(KEY_RESULT, SUCCESS);
} catch (ExecutionException | InterruptedException e) {
b.putString(KEY_RESULT, FAILURE);
}
return b;
case METHOD_IMPORT_LAYOUT_XML:
if (getContext().checkCallingOrSelfPermission(getWritePermission())
!= PackageManager.PERMISSION_GRANTED) {
throw new SecurityException("Caller doesn't have write permission");
}
LayoutImportExportHelper.INSTANCE.importModelFromXml(getContext(), arg);
b.putString(KEY_RESULT, SUCCESS);
return b;
default:
return null;
}
SqlArguments args = new SqlArguments(uri, selection, selectionArgs);
return executeControllerTask(c -> c.update(args.table, values, args.where, args.args));
}
private int executeControllerTask(ToIntFunction<ModelDbController> task) {
@@ -209,24 +165,35 @@ public class LauncherProvider extends ContentProvider {
}
}
/**
* Parses the uri and returns the where and arg clause.
*
* Note: This should be called on the binder thread (before posting on any executor) so that
* any parsing error gets propagated to the caller.
*/
private static Pair<String, String[]> parseUri(Uri url, String where, String[] args) {
switch (url.getPathSegments().size()) {
case 1 -> {
return Pair.create(where, args);
static class SqlArguments {
public final String table;
public final String where;
public final String[] args;
SqlArguments(Uri url, String where, String[] args) {
if (url.getPathSegments().size() == 1) {
this.table = url.getPathSegments().get(0);
this.where = where;
this.args = args;
} else if (url.getPathSegments().size() != 2) {
throw new IllegalArgumentException("Invalid URI: " + url);
} else if (!TextUtils.isEmpty(where)) {
throw new UnsupportedOperationException("WHERE clause not supported: " + url);
} else {
this.table = url.getPathSegments().get(0);
this.where = "_id=" + ContentUris.parseId(url);
this.args = null;
}
case 2 -> {
if (!TextUtils.isEmpty(where)) {
throw new UnsupportedOperationException("WHERE clause not supported: " + url);
}
return Pair.create("_id=" + ContentUris.parseId(url), null);
}
SqlArguments(Uri url) {
if (url.getPathSegments().size() == 1) {
table = url.getPathSegments().get(0);
where = null;
args = null;
} else {
throw new IllegalArgumentException("Invalid URI: " + url);
}
default -> throw new IllegalArgumentException("Invalid URI: " + url);
}
}
}