1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
package com.android.launcher3.graphics;
import static com.android.launcher3.Utilities.getPrefs;
import static com.android.launcher3.util.Executors.UI_HELPER_EXECUTOR;
import static com.android.launcher3.util.Themes.KEY_THEMED_ICONS;
import static com.android.launcher3.util.Themes.isThemedIconEnabled;
import android.annotation.TargetApi;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.net.Uri;
import android.os.Binder;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.IBinder.DeathRecipient;
import android.os.Message;
import android.os.Messenger;
import android.util.ArrayMap;
import android.util.Log;
import com.android.launcher3.InvariantDeviceProfile;
import com.android.launcher3.InvariantDeviceProfile.GridOption;
import com.android.launcher3.Utilities;
import com.android.launcher3.config.FeatureFlags;
import com.android.launcher3.util.Executors;
/**
* 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
*
* /default_grid: Call update to set the current grid, with values
* name: name of the grid to apply
*/
public class GridCustomizationsProvider extends ContentProvider {
private static final String TAG = "GridCustomizationsProvider";
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";
private static final String KEY_LIST_OPTIONS = "/list_options";
private static final String KEY_DEFAULT_GRID = "/default_grid";
private static final String METHOD_GET_PREVIEW = "get_preview";
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";
private static final String KEY_SURFACE_PACKAGE = "surface_package";
private static final String KEY_CALLBACK = "callback";
private final ArrayMap<IBinder, PreviewLifecycleObserver> mActivePreviews = new ArrayMap<>();
@Override
public boolean onCreate() {
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
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 : idp.parseAllGridOptions(getContext())) {
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;
}
}
@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) {
switch (uri.getPath()) {
case KEY_DEFAULT_GRID: {
String gridName = values.getAsString(KEY_NAME);
InvariantDeviceProfile idp = InvariantDeviceProfile.INSTANCE.get(getContext());
// Verify that this is a valid grid option
GridOption match = null;
for (GridOption option : idp.parseAllGridOptions(getContext())) {
if (option.name.equals(gridName)) {
match = option;
break;
}
}
if (match == null) {
return 0;
}
idp.setCurrentGrid(getContext(), gridName);
return 1;
}
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;
}
}
@Override
public Bundle call(String method, String arg, Bundle extras) {
if (getContext().checkPermission("android.permission.BIND_WALLPAPER",
Binder.getCallingPid(), Binder.getCallingUid())
!= PackageManager.PERMISSION_GRANTED) {
return null;
}
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(UI_HELPER_EXECUTOR.getLooper(), 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);
}
return null;
}
}
private synchronized void destroyObserver(PreviewLifecycleObserver observer) {
if (observer == null || observer.destroyed) {
return;
}
observer.destroyed = true;
observer.renderer.getHostToken().unlinkToDeath(observer, 0);
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 {
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);
}
}
}
|