2019-01-09 17:29:49 -08:00
|
|
|
package com.android.launcher3.graphics;
|
|
|
|
|
|
2021-05-17 13:23:08 -07:00
|
|
|
import static com.android.launcher3.Utilities.getPrefs;
|
|
|
|
|
import static com.android.launcher3.util.Themes.KEY_THEMED_ICONS;
|
|
|
|
|
import static com.android.launcher3.util.Themes.isThemedIconEnabled;
|
|
|
|
|
|
2021-05-19 19:42:14 -07:00
|
|
|
import android.annotation.TargetApi;
|
2019-01-09 17:29:49 -08:00
|
|
|
import android.content.ContentProvider;
|
|
|
|
|
import android.content.ContentValues;
|
2020-05-23 01:24:25 -07:00
|
|
|
import android.content.pm.PackageManager;
|
2019-01-09 17:29:49 -08:00
|
|
|
import android.content.res.XmlResourceParser;
|
|
|
|
|
import android.database.Cursor;
|
|
|
|
|
import android.database.MatrixCursor;
|
|
|
|
|
import android.net.Uri;
|
2020-05-23 01:24:25 -07:00
|
|
|
import android.os.Binder;
|
2021-05-19 19:42:14 -07:00
|
|
|
import android.os.Build;
|
2019-01-09 17:29:49 -08:00
|
|
|
import android.os.Bundle;
|
2021-05-19 19:42:14 -07:00
|
|
|
import android.os.Handler;
|
|
|
|
|
import android.os.IBinder;
|
|
|
|
|
import android.os.IBinder.DeathRecipient;
|
|
|
|
|
import android.os.Looper;
|
|
|
|
|
import android.os.Message;
|
|
|
|
|
import android.os.Messenger;
|
|
|
|
|
import android.util.ArrayMap;
|
2019-01-09 17:29:49 -08:00
|
|
|
import android.util.Log;
|
|
|
|
|
import android.util.Xml;
|
|
|
|
|
|
|
|
|
|
import com.android.launcher3.InvariantDeviceProfile;
|
|
|
|
|
import com.android.launcher3.InvariantDeviceProfile.GridOption;
|
|
|
|
|
import com.android.launcher3.R;
|
2021-05-19 19:42:14 -07:00
|
|
|
import com.android.launcher3.Utilities;
|
2021-05-17 13:23:08 -07:00
|
|
|
import com.android.launcher3.config.FeatureFlags;
|
2021-05-19 19:42:14 -07:00
|
|
|
import com.android.launcher3.util.Executors;
|
2019-01-09 17:29:49 -08:00
|
|
|
|
|
|
|
|
import org.xmlpull.v1.XmlPullParser;
|
|
|
|
|
import org.xmlpull.v1.XmlPullParserException;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
2019-01-11 15:08:44 -08:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Collections;
|
2019-01-09 17:29:49 -08:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Exposes various launcher grid options and allows the caller to change them.
|
|
|
|
|
* APIs:
|
|
|
|
|
* /list_options: List the various available grip options, has following columns
|
|
|
|
|
* name: name of the grid
|
|
|
|
|
* rows: number of rows in the grid
|
|
|
|
|
* cols: number of columns in the grid
|
|
|
|
|
* preview_count: number of previews available for this grid option. The preview uri
|
|
|
|
|
* looks like /preview/<grid-name>/<preview index starting with 0>
|
|
|
|
|
* is_default: true if this grid is currently active
|
|
|
|
|
*
|
|
|
|
|
* /preview: Opens a file stream for the grid preview
|
2019-01-11 15:08:44 -08:00
|
|
|
*
|
|
|
|
|
* /default_grid: Call update to set the current grid, with values
|
|
|
|
|
* name: name of the grid to apply
|
2019-01-09 17:29:49 -08:00
|
|
|
*/
|
2021-01-27 14:05:01 -08:00
|
|
|
public class GridCustomizationsProvider extends ContentProvider {
|
2019-01-09 17:29:49 -08:00
|
|
|
|
2021-01-27 14:05:01 -08:00
|
|
|
private static final String TAG = "GridCustomizationsProvider";
|
2019-01-09 17:29:49 -08:00
|
|
|
|
|
|
|
|
private static final String KEY_NAME = "name";
|
|
|
|
|
private static final String KEY_ROWS = "rows";
|
|
|
|
|
private static final String KEY_COLS = "cols";
|
|
|
|
|
private static final String KEY_PREVIEW_COUNT = "preview_count";
|
|
|
|
|
private static final String KEY_IS_DEFAULT = "is_default";
|
|
|
|
|
|
2019-01-11 15:08:44 -08:00
|
|
|
private static final String KEY_LIST_OPTIONS = "/list_options";
|
|
|
|
|
private static final String KEY_DEFAULT_GRID = "/default_grid";
|
|
|
|
|
|
2020-03-06 16:22:50 -08:00
|
|
|
private static final String METHOD_GET_PREVIEW = "get_preview";
|
2019-01-09 17:29:49 -08:00
|
|
|
|
2021-05-17 13:23:08 -07:00
|
|
|
private static final String GET_ICON_THEMED = "/get_icon_themed";
|
|
|
|
|
private static final String SET_ICON_THEMED = "/set_icon_themed";
|
|
|
|
|
private static final String ICON_THEMED = "/icon_themed";
|
|
|
|
|
private static final String BOOLEAN_VALUE = "boolean_value";
|
|
|
|
|
|
2021-05-19 19:42:14 -07:00
|
|
|
private static final String KEY_SURFACE_PACKAGE = "surface_package";
|
|
|
|
|
private static final String KEY_CALLBACK = "callback";
|
|
|
|
|
|
|
|
|
|
private final ArrayMap<IBinder, PreviewLifecycleObserver> mActivePreviews = new ArrayMap<>();
|
|
|
|
|
|
2019-01-09 17:29:49 -08:00
|
|
|
@Override
|
|
|
|
|
public boolean onCreate() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Cursor query(Uri uri, String[] projection, String selection,
|
|
|
|
|
String[] selectionArgs, String sortOrder) {
|
2021-05-17 13:23:08 -07:00
|
|
|
switch (uri.getPath()) {
|
|
|
|
|
case KEY_LIST_OPTIONS: {
|
|
|
|
|
MatrixCursor cursor = new MatrixCursor(new String[] {
|
|
|
|
|
KEY_NAME, KEY_ROWS, KEY_COLS, KEY_PREVIEW_COUNT, KEY_IS_DEFAULT});
|
|
|
|
|
InvariantDeviceProfile idp = InvariantDeviceProfile.INSTANCE.get(getContext());
|
|
|
|
|
for (GridOption gridOption : parseAllGridOptions()) {
|
|
|
|
|
cursor.newRow()
|
|
|
|
|
.add(KEY_NAME, gridOption.name)
|
|
|
|
|
.add(KEY_ROWS, gridOption.numRows)
|
|
|
|
|
.add(KEY_COLS, gridOption.numColumns)
|
|
|
|
|
.add(KEY_PREVIEW_COUNT, 1)
|
|
|
|
|
.add(KEY_IS_DEFAULT, idp.numColumns == gridOption.numColumns
|
|
|
|
|
&& idp.numRows == gridOption.numRows);
|
|
|
|
|
}
|
|
|
|
|
return cursor;
|
|
|
|
|
}
|
|
|
|
|
case GET_ICON_THEMED:
|
|
|
|
|
case ICON_THEMED: {
|
|
|
|
|
MatrixCursor cursor = new MatrixCursor(new String[] {BOOLEAN_VALUE});
|
|
|
|
|
cursor.newRow().add(BOOLEAN_VALUE, isThemedIconEnabled(getContext()) ? 1 : 0);
|
|
|
|
|
return cursor;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
return null;
|
2019-01-11 15:08:44 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private List<GridOption> parseAllGridOptions() {
|
|
|
|
|
List<GridOption> result = new ArrayList<>();
|
2019-01-09 17:29:49 -08:00
|
|
|
try (XmlResourceParser parser = getContext().getResources().getXml(R.xml.device_profiles)) {
|
|
|
|
|
final int depth = parser.getDepth();
|
|
|
|
|
int type;
|
|
|
|
|
while (((type = parser.next()) != XmlPullParser.END_TAG ||
|
|
|
|
|
parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
|
2019-01-11 15:08:44 -08:00
|
|
|
if ((type == XmlPullParser.START_TAG)
|
|
|
|
|
&& GridOption.TAG_NAME.equals(parser.getName())) {
|
2021-04-22 10:12:54 -07:00
|
|
|
result.add(new GridOption(getContext(), Xml.asAttributeSet(parser)));
|
2019-01-09 17:29:49 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException | XmlPullParserException e) {
|
|
|
|
|
Log.e(TAG, "Error parsing device profile", e);
|
2019-01-11 15:08:44 -08:00
|
|
|
return Collections.emptyList();
|
2019-01-09 17:29:49 -08:00
|
|
|
}
|
2019-01-11 15:08:44 -08:00
|
|
|
return result;
|
2019-01-09 17:29:49 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String getType(Uri uri) {
|
|
|
|
|
return "vnd.android.cursor.dir/launcher_grid";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Uri insert(Uri uri, ContentValues initialValues) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int delete(Uri uri, String selection, String[] selectionArgs) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
|
2021-05-17 13:23:08 -07:00
|
|
|
switch (uri.getPath()) {
|
|
|
|
|
case KEY_DEFAULT_GRID: {
|
|
|
|
|
String gridName = values.getAsString(KEY_NAME);
|
|
|
|
|
// Verify that this is a valid grid option
|
|
|
|
|
GridOption match = null;
|
|
|
|
|
for (GridOption option : parseAllGridOptions()) {
|
|
|
|
|
if (option.name.equals(gridName)) {
|
|
|
|
|
match = option;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (match == null) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2019-01-11 15:08:44 -08:00
|
|
|
|
2021-05-17 13:23:08 -07:00
|
|
|
InvariantDeviceProfile.INSTANCE.get(getContext())
|
|
|
|
|
.setCurrentGrid(getContext(), gridName);
|
|
|
|
|
return 1;
|
2019-01-11 15:08:44 -08:00
|
|
|
}
|
2021-05-17 13:23:08 -07:00
|
|
|
case ICON_THEMED:
|
|
|
|
|
case SET_ICON_THEMED: {
|
|
|
|
|
if (FeatureFlags.ENABLE_THEMED_ICONS.get()) {
|
|
|
|
|
getPrefs(getContext()).edit()
|
|
|
|
|
.putBoolean(KEY_THEMED_ICONS, values.getAsBoolean(BOOLEAN_VALUE))
|
|
|
|
|
.apply();
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
return 0;
|
2019-01-11 15:08:44 -08:00
|
|
|
}
|
2019-01-09 17:29:49 -08:00
|
|
|
}
|
|
|
|
|
|
2020-03-06 16:22:50 -08:00
|
|
|
@Override
|
2020-03-20 16:04:05 -07:00
|
|
|
public Bundle call(String method, String arg, Bundle extras) {
|
2020-05-23 01:24:25 -07:00
|
|
|
if (getContext().checkPermission("android.permission.BIND_WALLPAPER",
|
|
|
|
|
Binder.getCallingPid(), Binder.getCallingUid())
|
|
|
|
|
!= PackageManager.PERMISSION_GRANTED) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-19 19:42:14 -07:00
|
|
|
if (!Utilities.ATLEAST_R || !METHOD_GET_PREVIEW.equals(method)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return getPreview(extras);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@TargetApi(Build.VERSION_CODES.R)
|
|
|
|
|
private synchronized Bundle getPreview(Bundle request) {
|
|
|
|
|
PreviewLifecycleObserver observer = null;
|
|
|
|
|
try {
|
|
|
|
|
PreviewSurfaceRenderer renderer = new PreviewSurfaceRenderer(getContext(), request);
|
|
|
|
|
|
|
|
|
|
// Destroy previous
|
|
|
|
|
destroyObserver(mActivePreviews.get(renderer.getHostToken()));
|
|
|
|
|
|
|
|
|
|
observer = new PreviewLifecycleObserver(renderer);
|
|
|
|
|
mActivePreviews.put(renderer.getHostToken(), observer);
|
|
|
|
|
|
|
|
|
|
renderer.loadAsync();
|
|
|
|
|
renderer.getHostToken().linkToDeath(observer, 0);
|
|
|
|
|
|
|
|
|
|
Bundle result = new Bundle();
|
|
|
|
|
result.putParcelable(KEY_SURFACE_PACKAGE, renderer.getSurfacePackage());
|
|
|
|
|
|
|
|
|
|
Messenger messenger = new Messenger(new Handler(Looper.getMainLooper(), observer));
|
|
|
|
|
Message msg = Message.obtain();
|
|
|
|
|
msg.replyTo = messenger;
|
|
|
|
|
result.putParcelable(KEY_CALLBACK, msg);
|
|
|
|
|
return result;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.e(TAG, "Unable to generate preview", e);
|
|
|
|
|
if (observer != null) {
|
|
|
|
|
destroyObserver(observer);
|
|
|
|
|
}
|
2020-03-06 16:22:50 -08:00
|
|
|
return null;
|
|
|
|
|
}
|
2021-05-19 19:42:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private synchronized void destroyObserver(PreviewLifecycleObserver observer) {
|
|
|
|
|
if (observer == null || observer.destroyed) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
observer.destroyed = true;
|
|
|
|
|
Executors.MAIN_EXECUTOR.execute(observer.renderer::destroy);
|
|
|
|
|
PreviewLifecycleObserver cached = mActivePreviews.get(observer.renderer.getHostToken());
|
|
|
|
|
if (cached == observer) {
|
|
|
|
|
mActivePreviews.remove(observer.renderer.getHostToken());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class PreviewLifecycleObserver implements Handler.Callback, DeathRecipient {
|
2020-03-06 16:22:50 -08:00
|
|
|
|
2021-05-19 19:42:14 -07:00
|
|
|
public final PreviewSurfaceRenderer renderer;
|
|
|
|
|
public boolean destroyed = false;
|
|
|
|
|
|
|
|
|
|
PreviewLifecycleObserver(PreviewSurfaceRenderer renderer) {
|
|
|
|
|
this.renderer = renderer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean handleMessage(Message message) {
|
|
|
|
|
destroyObserver(this);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void binderDied() {
|
|
|
|
|
destroyObserver(this);
|
|
|
|
|
}
|
2020-03-06 16:22:50 -08:00
|
|
|
}
|
2019-01-09 17:29:49 -08:00
|
|
|
}
|