blob: c7cdfa8c69304e14b034bd1ff4ced02b8c36df5b [file] [log] [blame]
Sunny Goyal0fe505b2014-08-06 09:55:36 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.launcher3;
18
Nicolas Sleiman31e9fa42023-01-25 16:30:32 +000019import static com.android.launcher3.LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
Sunny Goyal7b9e28f2023-05-17 12:44:03 -070020import static com.android.launcher3.LauncherSettings.Favorites.TABLE_NAME;
21import static com.android.launcher3.provider.LauncherDbUtils.itemIdMatch;
Nicolas Sleiman31e9fa42023-01-25 16:30:32 +000022
Sunny Goyal0fe505b2014-08-06 09:55:36 -070023import android.content.ComponentName;
24import android.content.ContentValues;
25import android.content.Context;
26import android.content.Intent;
27import android.content.pm.ActivityInfo;
Nicolas Sleiman31e9fa42023-01-25 16:30:32 +000028import android.content.pm.LauncherActivityInfo;
Sunny Goyalafaa8f02023-03-30 12:34:43 -070029import android.content.pm.LauncherApps;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070030import android.content.pm.PackageManager;
31import android.content.res.Resources;
Sunny Goyalab2f8082023-04-11 11:35:43 -070032import android.content.res.Resources.NotFoundException;
33import android.content.res.XmlResourceParser;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070034import android.database.sqlite.SQLiteDatabase;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070035import android.os.Bundle;
Sunny Goyalafaa8f02023-03-30 12:34:43 -070036import android.os.Process;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070037import android.text.TextUtils;
Rajeev Kumar26453a22017-06-09 16:02:25 -070038import android.util.ArrayMap;
Sunny Goyal0d742312019-03-04 20:22:26 -080039import android.util.AttributeSet;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070040import android.util.Log;
Sunny Goyal0d742312019-03-04 20:22:26 -080041import android.util.Xml;
Sunny Goyal36b54222018-07-10 13:50:50 -070042
Samuel Fufaca37b8a2019-08-19 17:04:36 -070043import androidx.annotation.Nullable;
Sunny Goyalab2f8082023-04-11 11:35:43 -070044import androidx.annotation.StringRes;
Stefan Andonian7fcee912023-04-07 17:56:29 +000045import androidx.annotation.WorkerThread;
Sunny Goyalab2f8082023-04-11 11:35:43 -070046import androidx.annotation.XmlRes;
Samuel Fufaca37b8a2019-08-19 17:04:36 -070047
Sunny Goyal0fe505b2014-08-06 09:55:36 -070048import com.android.launcher3.LauncherSettings.Favorites;
Nicolas Sleiman31e9fa42023-01-25 16:30:32 +000049import com.android.launcher3.model.data.AppInfo;
Sunny Goyale396abf2020-04-06 15:11:17 -070050import com.android.launcher3.model.data.LauncherAppWidgetInfo;
51import com.android.launcher3.model.data.WorkspaceItemInfo;
Nicolas Sleiman31e9fa42023-01-25 16:30:32 +000052import com.android.launcher3.pm.UserCache;
Samuel Fufaca37b8a2019-08-19 17:04:36 -070053import com.android.launcher3.qsb.QsbContainerView;
Sunny Goyalafaa8f02023-03-30 12:34:43 -070054import com.android.launcher3.shortcuts.ShortcutKey;
Nicolas Sleiman31e9fa42023-01-25 16:30:32 +000055import com.android.launcher3.uioverrides.ApiWrapper;
Sunny Goyalefb7e842018-10-04 15:11:00 -070056import com.android.launcher3.util.IntArray;
Sunny Goyal3e58eea2022-11-14 14:30:07 -080057import com.android.launcher3.util.Partner;
Adam Cohen091440a2015-03-18 14:16:05 -070058import com.android.launcher3.util.Thunk;
Sihua Ma8bbfcb62022-11-08 16:46:07 -080059import com.android.launcher3.widget.LauncherWidgetHolder;
Sunny Goyal36b54222018-07-10 13:50:50 -070060
61import org.xmlpull.v1.XmlPullParser;
62import org.xmlpull.v1.XmlPullParserException;
63
Sunny Goyal0fe505b2014-08-06 09:55:36 -070064import java.io.IOException;
Sunny Goyalafaa8f02023-03-30 12:34:43 -070065import java.util.Collections;
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070066import java.util.Locale;
Nicolas Sleiman31e9fa42023-01-25 16:30:32 +000067import java.util.Map;
Sunny Goyalc0f03d92019-03-22 14:13:36 -070068import java.util.function.Supplier;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070069
70/**
Sunny Goyal3a5a9d12014-10-01 15:33:41 -070071 * Layout parsing code for auto installs layout
Sunny Goyal0fe505b2014-08-06 09:55:36 -070072 */
Sunny Goyal3a5a9d12014-10-01 15:33:41 -070073public class AutoInstallsLayout {
Sunny Goyal0fe505b2014-08-06 09:55:36 -070074 private static final String TAG = "AutoInstalls";
Sunny Goyal3a5a9d12014-10-01 15:33:41 -070075 private static final boolean LOGD = false;
Sunny Goyal0fe505b2014-08-06 09:55:36 -070076
77 /** Marker action used to discover a package which defines launcher customization */
78 static final String ACTION_LAUNCHER_CUSTOMIZATION =
Sunny Goyal2233c882014-09-18 14:36:48 -070079 "android.autoinstalls.config.action.PLAY_AUTO_INSTALL";
Sunny Goyal0fe505b2014-08-06 09:55:36 -070080
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070081 /**
82 * Layout resource which also includes grid size and hotseat count, e.g., default_layout_6x6_h5
83 */
84 private static final String FORMATTED_LAYOUT_RES_WITH_HOSTEAT = "default_layout_%dx%d_h%s";
85 private static final String FORMATTED_LAYOUT_RES = "default_layout_%dx%d";
Sunny Goyal0fe505b2014-08-06 09:55:36 -070086 private static final String LAYOUT_RES = "default_layout";
87
Sunny Goyal1ae46ca2023-04-10 15:28:59 -070088 public static AutoInstallsLayout get(Context context, LauncherWidgetHolder appWidgetHolder,
Sunny Goyal0fe505b2014-08-06 09:55:36 -070089 LayoutParserCallback callback) {
Sunny Goyal3e58eea2022-11-14 14:30:07 -080090 Partner partner = Partner.get(context.getPackageManager(), ACTION_LAUNCHER_CUSTOMIZATION);
91 if (partner == null) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -070092 return null;
93 }
Sunny Goyal87f784c2017-01-11 10:48:34 -080094 InvariantDeviceProfile grid = LauncherAppState.getIDP(context);
Sunny Goyalb2d46ce2015-03-26 11:32:11 -070095
96 // Try with grid size and hotseat count
97 String layoutName = String.format(Locale.ENGLISH, FORMATTED_LAYOUT_RES_WITH_HOSTEAT,
Tony Wickhamb87f3cd2021-04-07 15:02:37 -070098 grid.numColumns, grid.numRows, grid.numDatabaseHotseatIcons);
Sunny Goyal3e58eea2022-11-14 14:30:07 -080099 int layoutId = partner.getXmlResId(layoutName);
Sunny Goyalb2d46ce2015-03-26 11:32:11 -0700100
101 // Try with only grid size
102 if (layoutId == 0) {
103 Log.d(TAG, "Formatted layout: " + layoutName
104 + " not found. Trying layout without hosteat");
105 layoutName = String.format(Locale.ENGLISH, FORMATTED_LAYOUT_RES,
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700106 grid.numColumns, grid.numRows);
Sunny Goyal3e58eea2022-11-14 14:30:07 -0800107 layoutId = partner.getXmlResId(layoutName);
Sunny Goyalb2d46ce2015-03-26 11:32:11 -0700108 }
109
110 // Try the default layout
111 if (layoutId == 0) {
112 Log.d(TAG, "Formatted layout: " + layoutName + " not found. Trying the default layout");
Sunny Goyal3e58eea2022-11-14 14:30:07 -0800113 layoutId = partner.getXmlResId(LAYOUT_RES);
Sunny Goyalb2d46ce2015-03-26 11:32:11 -0700114 }
115
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700116 if (layoutId == 0) {
Sunny Goyal3e58eea2022-11-14 14:30:07 -0800117 Log.e(TAG, "Layout definition not found in package: " + partner.getPackageName());
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700118 return null;
119 }
Sunny Goyal3e58eea2022-11-14 14:30:07 -0800120 return new AutoInstallsLayout(context, appWidgetHolder, callback, partner.getResources(),
121 layoutId, TAG_WORKSPACE);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700122 }
123
124 // Object Tags
Sunny Goyalb564efb2015-01-23 13:45:20 -0800125 private static final String TAG_INCLUDE = "include";
Sunny Goyalc0f03d92019-03-22 14:13:36 -0700126 public static final String TAG_WORKSPACE = "workspace";
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700127 private static final String TAG_APP_ICON = "appicon";
128 private static final String TAG_AUTO_INSTALL = "autoinstall";
129 private static final String TAG_FOLDER = "folder";
130 private static final String TAG_APPWIDGET = "appwidget";
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700131 protected static final String TAG_SEARCH_WIDGET = "searchwidget";
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700132 private static final String TAG_SHORTCUT = "shortcut";
133 private static final String TAG_EXTRA = "extra";
134
135 private static final String ATTR_CONTAINER = "container";
136 private static final String ATTR_RANK = "rank";
137
138 private static final String ATTR_PACKAGE_NAME = "packageName";
139 private static final String ATTR_CLASS_NAME = "className";
140 private static final String ATTR_TITLE = "title";
Sunny Goyal55e2b162020-06-09 15:44:48 -0700141 private static final String ATTR_TITLE_TEXT = "titleText";
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700142 private static final String ATTR_SCREEN = "screen";
Sunny Goyalafaa8f02023-03-30 12:34:43 -0700143 private static final String ATTR_SHORTCUT_ID = "shortcutId";
Sunny Goyal96a09632015-12-16 11:32:54 -0800144
145 // x and y can be specified as negative integers, in which case -1 represents the
146 // last row / column, -2 represents the second last, and so on.
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700147 private static final String ATTR_X = "x";
148 private static final String ATTR_Y = "y";
Sunny Goyal96a09632015-12-16 11:32:54 -0800149
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700150 private static final String ATTR_SPAN_X = "spanX";
151 private static final String ATTR_SPAN_Y = "spanY";
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700152
Sunny Goyalb564efb2015-01-23 13:45:20 -0800153 // Attrs for "Include"
154 private static final String ATTR_WORKSPACE = "workspace";
155
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700156 // Style attrs -- "Extra"
157 private static final String ATTR_KEY = "key";
158 private static final String ATTR_VALUE = "value";
159
160 private static final String HOTSEAT_CONTAINER_NAME =
161 Favorites.containerToString(Favorites.CONTAINER_HOTSEAT);
162
Sunny Goyalafaa8f02023-03-30 12:34:43 -0700163 protected final Context mContext;
164 protected final LauncherWidgetHolder mAppWidgetHolder;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -0800165 protected final LayoutParserCallback mCallback;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700166
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700167 protected final PackageManager mPackageManager;
Sunny Goyalab2f8082023-04-11 11:35:43 -0700168 protected final SourceResources mSourceRes;
Sunny Goyalc0f03d92019-03-22 14:13:36 -0700169 protected final Supplier<XmlPullParser> mInitialLayoutSupplier;
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700170
Sunny Goyalbb011da2016-06-15 15:42:29 -0700171 private final InvariantDeviceProfile mIdp;
Sunny Goyal96a09632015-12-16 11:32:54 -0800172 private final int mRowCount;
173 private final int mColumnCount;
Nicolas Sleiman31e9fa42023-01-25 16:30:32 +0000174 private final Map<String, LauncherActivityInfo> mActivityOverride;
Sunny Goyalefb7e842018-10-04 15:11:00 -0700175 private final int[] mTemp = new int[2];
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700176 @Thunk
177 final ContentValues mValues;
Sunny Goyalbb3b02f2015-01-15 12:00:14 -0800178 protected final String mRootTag;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700179
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700180 protected SQLiteDatabase mDb;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700181
Sihua Maaa2b8722022-10-25 15:17:58 -0700182 public AutoInstallsLayout(Context context, LauncherWidgetHolder appWidgetHolder,
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700183 LayoutParserCallback callback, Resources res,
184 int layoutId, String rootTag) {
Sunny Goyalab2f8082023-04-11 11:35:43 -0700185 this(context, appWidgetHolder, callback, SourceResources.wrap(res),
186 () -> res.getXml(layoutId), rootTag);
Sunny Goyalc0f03d92019-03-22 14:13:36 -0700187 }
188
Sihua Maaa2b8722022-10-25 15:17:58 -0700189 public AutoInstallsLayout(Context context, LauncherWidgetHolder appWidgetHolder,
Sunny Goyalab2f8082023-04-11 11:35:43 -0700190 LayoutParserCallback callback, SourceResources res,
Sunny Goyalc0f03d92019-03-22 14:13:36 -0700191 Supplier<XmlPullParser> initialLayoutSupplier, String rootTag) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700192 mContext = context;
Sihua Maaa2b8722022-10-25 15:17:58 -0700193 mAppWidgetHolder = appWidgetHolder;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700194 mCallback = callback;
195
196 mPackageManager = context.getPackageManager();
197 mValues = new ContentValues();
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700198 mRootTag = rootTag;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700199
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700200 mSourceRes = res;
Sunny Goyalc0f03d92019-03-22 14:13:36 -0700201 mInitialLayoutSupplier = initialLayoutSupplier;
Sunny Goyal96a09632015-12-16 11:32:54 -0800202
Sunny Goyal87f784c2017-01-11 10:48:34 -0800203 mIdp = LauncherAppState.getIDP(context);
Sunny Goyalbb011da2016-06-15 15:42:29 -0700204 mRowCount = mIdp.numRows;
205 mColumnCount = mIdp.numColumns;
Nicolas Sleiman31e9fa42023-01-25 16:30:32 +0000206 mActivityOverride = ApiWrapper.getActivityOverrides(context);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700207 }
208
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700209 /**
210 * Loads the layout in the db and returns the number of entries added on the desktop.
211 */
Sunny Goyalefb7e842018-10-04 15:11:00 -0700212 public int loadLayout(SQLiteDatabase db, IntArray screenIds) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700213 mDb = db;
214 try {
Sunny Goyalc0f03d92019-03-22 14:13:36 -0700215 return parseLayout(mInitialLayoutSupplier.get(), screenIds);
Sameer Padala8fd74832014-09-08 16:00:29 -0700216 } catch (Exception e) {
Sunny Goyalc0f03d92019-03-22 14:13:36 -0700217 Log.e(TAG, "Error parsing layout: ", e);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700218 return -1;
219 }
220 }
221
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700222 /**
223 * Parses the layout and returns the number of elements added on the homescreen.
224 */
Sunny Goyalc0f03d92019-03-22 14:13:36 -0700225 protected int parseLayout(XmlPullParser parser, IntArray screenIds)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700226 throws XmlPullParserException, IOException {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700227 beginDocument(parser, mRootTag);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700228 final int depth = parser.getDepth();
229 int type;
Rajeev Kumar26453a22017-06-09 16:02:25 -0700230 ArrayMap<String, TagParser> tagParserMap = getLayoutElementsMap();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700231 int count = 0;
232
233 while (((type = parser.next()) != XmlPullParser.END_TAG ||
234 parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
235 if (type != XmlPullParser.START_TAG) {
236 continue;
237 }
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700238 count += parseAndAddNode(parser, tagParserMap, screenIds);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700239 }
240 return count;
241 }
242
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700243 /**
244 * Parses container and screenId attribute from the current tag, and puts it in the out.
245 * @param out array of size 2.
246 */
Sunny Goyal0d742312019-03-04 20:22:26 -0800247 protected void parseContainerAndScreen(XmlPullParser parser, int[] out) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700248 if (HOTSEAT_CONTAINER_NAME.equals(getAttributeValue(parser, ATTR_CONTAINER))) {
249 out[0] = Favorites.CONTAINER_HOTSEAT;
250 // Hack: hotseat items are stored using screen ids
Sunny Goyalefb7e842018-10-04 15:11:00 -0700251 out[1] = Integer.parseInt(getAttributeValue(parser, ATTR_RANK));
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700252 } else {
253 out[0] = Favorites.CONTAINER_DESKTOP;
Sunny Goyalefb7e842018-10-04 15:11:00 -0700254 out[1] = Integer.parseInt(getAttributeValue(parser, ATTR_SCREEN));
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700255 }
256 }
257
258 /**
259 * Parses the current node and returns the number of elements added.
260 */
261 protected int parseAndAddNode(
Sunny Goyal0d742312019-03-04 20:22:26 -0800262 XmlPullParser parser, ArrayMap<String, TagParser> tagParserMap, IntArray screenIds)
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700263 throws XmlPullParserException, IOException {
Sunny Goyalb564efb2015-01-23 13:45:20 -0800264
265 if (TAG_INCLUDE.equals(parser.getName())) {
266 final int resId = getAttributeResourceValue(parser, ATTR_WORKSPACE, 0);
267 if (resId != 0) {
268 // recursively load some more favorites, why not?
Sunny Goyalc0f03d92019-03-22 14:13:36 -0700269 return parseLayout(mSourceRes.getXml(resId), screenIds);
Sunny Goyalb564efb2015-01-23 13:45:20 -0800270 } else {
271 return 0;
272 }
273 }
274
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700275 mValues.clear();
276 parseContainerAndScreen(parser, mTemp);
Sunny Goyalefb7e842018-10-04 15:11:00 -0700277 final int container = mTemp[0];
278 final int screenId = mTemp[1];
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700279
280 mValues.put(Favorites.CONTAINER, container);
281 mValues.put(Favorites.SCREEN, screenId);
Sunny Goyal96a09632015-12-16 11:32:54 -0800282
283 mValues.put(Favorites.CELLX,
Sunny Goyalf82e5472016-01-06 15:09:22 -0800284 convertToDistanceFromEnd(getAttributeValue(parser, ATTR_X), mColumnCount));
Sunny Goyal96a09632015-12-16 11:32:54 -0800285 mValues.put(Favorites.CELLY,
Sunny Goyalf82e5472016-01-06 15:09:22 -0800286 convertToDistanceFromEnd(getAttributeValue(parser, ATTR_Y), mRowCount));
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700287
288 TagParser tagParser = tagParserMap.get(parser.getName());
289 if (tagParser == null) {
290 if (LOGD) Log.d(TAG, "Ignoring unknown element tag: " + parser.getName());
291 return 0;
292 }
Sunny Goyalefb7e842018-10-04 15:11:00 -0700293 int newElementId = tagParser.parseAndAdd(parser);
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700294 if (newElementId >= 0) {
295 // Keep track of the set of screens which need to be added to the db.
296 if (!screenIds.contains(screenId) &&
297 container == Favorites.CONTAINER_DESKTOP) {
298 screenIds.add(screenId);
299 }
300 return 1;
301 }
302 return 0;
303 }
304
Sunny Goyalefb7e842018-10-04 15:11:00 -0700305 protected int addShortcut(String title, Intent intent, int type) {
306 int id = mCallback.generateNewItemId();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700307 mValues.put(Favorites.INTENT, intent.toUri(0));
308 mValues.put(Favorites.TITLE, title);
309 mValues.put(Favorites.ITEM_TYPE, type);
310 mValues.put(Favorites.SPANX, 1);
311 mValues.put(Favorites.SPANY, 1);
312 mValues.put(Favorites._ID, id);
Nicolas Sleiman31e9fa42023-01-25 16:30:32 +0000313
Sunny Goyalafaa8f02023-03-30 12:34:43 -0700314 if (type == ITEM_TYPE_APPLICATION) {
315 ComponentName cn = intent.getComponent();
316 if (cn != null && mActivityOverride.containsKey(cn.getPackageName())) {
317 LauncherActivityInfo replacementInfo = mActivityOverride.get(cn.getPackageName());
318 mValues.put(Favorites.PROFILE_ID, UserCache.INSTANCE.get(mContext)
319 .getSerialNumberForUser(replacementInfo.getUser()));
320 mValues.put(Favorites.INTENT, AppInfo.makeLaunchIntent(replacementInfo).toUri(0));
321 }
322 }
Nicolas Sleiman31e9fa42023-01-25 16:30:32 +0000323
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700324 if (mCallback.insertAndCheck(mDb, mValues) < 0) {
325 return -1;
326 } else {
327 return id;
328 }
329 }
330
Rajeev Kumar26453a22017-06-09 16:02:25 -0700331 protected ArrayMap<String, TagParser> getFolderElementsMap() {
332 ArrayMap<String, TagParser> parsers = new ArrayMap<>();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700333 parsers.put(TAG_APP_ICON, new AppShortcutParser());
334 parsers.put(TAG_AUTO_INSTALL, new AutoInstallParser());
Sunny Goyalafaa8f02023-03-30 12:34:43 -0700335 parsers.put(TAG_SHORTCUT, new ShortcutParser());
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700336 return parsers;
337 }
338
Rajeev Kumar26453a22017-06-09 16:02:25 -0700339 protected ArrayMap<String, TagParser> getLayoutElementsMap() {
340 ArrayMap<String, TagParser> parsers = new ArrayMap<>();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700341 parsers.put(TAG_APP_ICON, new AppShortcutParser());
342 parsers.put(TAG_AUTO_INSTALL, new AutoInstallParser());
343 parsers.put(TAG_FOLDER, new FolderParser());
Sunny Goyal86df1382016-08-10 15:03:22 -0700344 parsers.put(TAG_APPWIDGET, new PendingWidgetParser());
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700345 parsers.put(TAG_SEARCH_WIDGET, new SearchWidgetParser());
Sunny Goyalafaa8f02023-03-30 12:34:43 -0700346 parsers.put(TAG_SHORTCUT, new ShortcutParser());
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700347 return parsers;
348 }
349
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700350 protected interface TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700351 /**
352 * Parses the tag and adds to the db
353 * @return the id of the row added or -1;
354 */
Sunny Goyal0d742312019-03-04 20:22:26 -0800355 int parseAndAdd(XmlPullParser parser)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700356 throws XmlPullParserException, IOException;
357 }
358
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700359 /**
360 * App shortcuts: required attributes packageName and className
361 */
362 protected class AppShortcutParser implements TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700363
364 @Override
Sunny Goyal0d742312019-03-04 20:22:26 -0800365 public int parseAndAdd(XmlPullParser parser) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700366 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
367 final String className = getAttributeValue(parser, ATTR_CLASS_NAME);
368
369 if (!TextUtils.isEmpty(packageName) && !TextUtils.isEmpty(className)) {
370 ActivityInfo info;
371 try {
372 ComponentName cn;
373 try {
374 cn = new ComponentName(packageName, className);
375 info = mPackageManager.getActivityInfo(cn, 0);
376 } catch (PackageManager.NameNotFoundException nnfe) {
377 String[] packages = mPackageManager.currentToCanonicalPackageNames(
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700378 new String[]{packageName});
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700379 cn = new ComponentName(packages[0], className);
380 info = mPackageManager.getActivityInfo(cn, 0);
381 }
382 final Intent intent = new Intent(Intent.ACTION_MAIN, null)
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700383 .addCategory(Intent.CATEGORY_LAUNCHER)
384 .setComponent(cn)
385 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
386 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700387
388 return addShortcut(info.loadLabel(mPackageManager).toString(),
Nicolas Sleiman31e9fa42023-01-25 16:30:32 +0000389 intent, ITEM_TYPE_APPLICATION);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700390 } catch (PackageManager.NameNotFoundException e) {
Sunny Goyaleb3ba0f2017-03-27 11:22:36 -0700391 Log.e(TAG, "Favorite not found: " + packageName + "/" + className);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700392 }
393 return -1;
394 } else {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700395 return invalidPackageOrClass(parser);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700396 }
397 }
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700398
399 /**
400 * Helper method to allow extending the parser capabilities
401 */
Sunny Goyal0d742312019-03-04 20:22:26 -0800402 protected int invalidPackageOrClass(XmlPullParser parser) {
Adam Cohencf0c7462015-08-06 14:02:23 -0700403 Log.w(TAG, "Skipping invalid <favorite> with no component");
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700404 return -1;
405 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700406 }
407
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700408 /**
409 * AutoInstall: required attributes packageName and className
410 */
411 protected class AutoInstallParser implements TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700412
413 @Override
Sunny Goyal0d742312019-03-04 20:22:26 -0800414 public int parseAndAdd(XmlPullParser parser) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700415 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
416 final String className = getAttributeValue(parser, ATTR_CLASS_NAME);
417 if (TextUtils.isEmpty(packageName) || TextUtils.isEmpty(className)) {
418 if (LOGD) Log.d(TAG, "Skipping invalid <favorite> with no component");
419 return -1;
420 }
421
Sunny Goyal95899162019-03-27 16:03:06 -0700422 mValues.put(Favorites.RESTORED, WorkspaceItemInfo.FLAG_AUTOINSTALL_ICON);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700423 final Intent intent = new Intent(Intent.ACTION_MAIN, null)
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700424 .addCategory(Intent.CATEGORY_LAUNCHER)
425 .setComponent(new ComponentName(packageName, className))
426 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
427 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700428 return addShortcut(mContext.getString(R.string.package_state_unknown), intent,
Nicolas Sleiman31e9fa42023-01-25 16:30:32 +0000429 ITEM_TYPE_APPLICATION);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700430 }
431 }
432
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700433 /**
Sunny Goyalafaa8f02023-03-30 12:34:43 -0700434 * Parses a deep shortcut. Required attributes packageName and shortcutId
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700435 */
436 protected class ShortcutParser implements TagParser {
437
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700438 @Override
Sunny Goyal0d742312019-03-04 20:22:26 -0800439 public int parseAndAdd(XmlPullParser parser) {
Sunny Goyalafaa8f02023-03-30 12:34:43 -0700440 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
441 final String shortcutId = getAttributeValue(parser, ATTR_SHORTCUT_ID);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700442
Sunny Goyalafaa8f02023-03-30 12:34:43 -0700443 try {
444 LauncherApps launcherApps = mContext.getSystemService(LauncherApps.class);
445 launcherApps.pinShortcuts(packageName, Collections.singletonList(shortcutId),
446 Process.myUserHandle());
447 Intent intent = ShortcutKey.makeIntent(shortcutId, packageName);
448 mValues.put(Favorites.RESTORED, WorkspaceItemInfo.FLAG_RESTORED_ICON);
449 return addShortcut(null, intent, Favorites.ITEM_TYPE_DEEP_SHORTCUT);
450 } catch (Exception e) {
451 Log.e(TAG, "Unable to pin the shortcut for shortcut id = " + shortcutId
452 + " and package name = " + packageName, e);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700453 }
Sunny Goyalafaa8f02023-03-30 12:34:43 -0700454 return -1;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700455 }
456 }
457
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700458 /**
459 * AppWidget parser: Required attributes packageName, className, spanX and spanY.
460 * Options child nodes: <extra key=... value=... />
Sunny Goyal86df1382016-08-10 15:03:22 -0700461 * It adds a pending widget which allows the widget to come later. If there are extras, those
462 * are passed to widget options during bind.
463 * The config activity for the widget (if present) is not shown, so any optional configurations
464 * should be passed as extras and the widget should support reading these widget options.
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700465 */
Sunny Goyal86df1382016-08-10 15:03:22 -0700466 protected class PendingWidgetParser implements TagParser {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700467
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700468 @Nullable
469 public ComponentName getComponentName(XmlPullParser parser) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700470 final String packageName = getAttributeValue(parser, ATTR_PACKAGE_NAME);
471 final String className = getAttributeValue(parser, ATTR_CLASS_NAME);
472 if (TextUtils.isEmpty(packageName) || TextUtils.isEmpty(className)) {
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700473 return null;
474 }
475 return new ComponentName(packageName, className);
476 }
477
478
479 @Override
480 public int parseAndAdd(XmlPullParser parser)
481 throws XmlPullParserException, IOException {
482 ComponentName cn = getComponentName(parser);
483 if (cn == null) {
Sunny Goyal86df1382016-08-10 15:03:22 -0700484 if (LOGD) Log.d(TAG, "Skipping invalid <appwidget> with no component");
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700485 return -1;
486 }
487
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700488 mValues.put(Favorites.SPANX, getAttributeValue(parser, ATTR_SPAN_X));
489 mValues.put(Favorites.SPANY, getAttributeValue(parser, ATTR_SPAN_Y));
Sunny Goyal86df1382016-08-10 15:03:22 -0700490 mValues.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_APPWIDGET);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700491
492 // Read the extras
493 Bundle extras = new Bundle();
494 int widgetDepth = parser.getDepth();
495 int type;
496 while ((type = parser.next()) != XmlPullParser.END_TAG ||
497 parser.getDepth() > widgetDepth) {
498 if (type != XmlPullParser.START_TAG) {
499 continue;
500 }
501
502 if (TAG_EXTRA.equals(parser.getName())) {
503 String key = getAttributeValue(parser, ATTR_KEY);
504 String value = getAttributeValue(parser, ATTR_VALUE);
505 if (key != null && value != null) {
506 extras.putString(key, value);
507 } else {
508 throw new RuntimeException("Widget extras must have a key and value");
509 }
510 } else {
511 throw new RuntimeException("Widgets can contain only extras");
512 }
513 }
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700514 return verifyAndInsert(cn, extras);
Sunny Goyal86df1382016-08-10 15:03:22 -0700515 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700516
Sunny Goyalefb7e842018-10-04 15:11:00 -0700517 protected int verifyAndInsert(ComponentName cn, Bundle extras) {
Sunny Goyal86df1382016-08-10 15:03:22 -0700518 mValues.put(Favorites.APPWIDGET_PROVIDER, cn.flattenToString());
519 mValues.put(Favorites.RESTORED,
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700520 LauncherAppWidgetInfo.FLAG_ID_NOT_VALID
521 | LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY
522 | LauncherAppWidgetInfo.FLAG_DIRECT_CONFIG);
Sunny Goyal86df1382016-08-10 15:03:22 -0700523 mValues.put(Favorites._ID, mCallback.generateNewItemId());
524 if (!extras.isEmpty()) {
525 mValues.put(Favorites.INTENT, new Intent().putExtras(extras).toUri(0));
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700526 }
Sunny Goyal86df1382016-08-10 15:03:22 -0700527
Sunny Goyalefb7e842018-10-04 15:11:00 -0700528 int insertedId = mCallback.insertAndCheck(mDb, mValues);
Sunny Goyal86df1382016-08-10 15:03:22 -0700529 if (insertedId < 0) {
530 return -1;
531 } else {
532 return insertedId;
533 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700534 }
535 }
536
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700537 protected class SearchWidgetParser extends PendingWidgetParser {
538 @Override
539 @Nullable
Stefan Andonian7fcee912023-04-07 17:56:29 +0000540 @WorkerThread
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700541 public ComponentName getComponentName(XmlPullParser parser) {
542 return QsbContainerView.getSearchComponentName(mContext);
543 }
544
545 @Override
546 protected int verifyAndInsert(ComponentName cn, Bundle extras) {
547 mValues.put(Favorites.OPTIONS, LauncherAppWidgetInfo.OPTION_SEARCH_WIDGET);
548 int flags = mValues.getAsInteger(Favorites.RESTORED)
549 | WorkspaceItemInfo.FLAG_RESTORE_STARTED;
550 mValues.put(Favorites.RESTORED, flags);
551 return super.verifyAndInsert(cn, extras);
552 }
553 }
554
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700555 protected class FolderParser implements TagParser {
Rajeev Kumar26453a22017-06-09 16:02:25 -0700556 private final ArrayMap<String, TagParser> mFolderElements;
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700557
558 public FolderParser() {
559 this(getFolderElementsMap());
560 }
561
Rajeev Kumar26453a22017-06-09 16:02:25 -0700562 public FolderParser(ArrayMap<String, TagParser> elements) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700563 mFolderElements = elements;
564 }
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700565
566 @Override
Nicolas Sleiman31e9fa42023-01-25 16:30:32 +0000567 public int parseAndAdd(XmlPullParser parser) throws XmlPullParserException, IOException {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700568 final String title;
569 final int titleResId = getAttributeResourceValue(parser, ATTR_TITLE, 0);
570 if (titleResId != 0) {
Sunny Goyal3a5a9d12014-10-01 15:33:41 -0700571 title = mSourceRes.getString(titleResId);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700572 } else {
Sunny Goyal55e2b162020-06-09 15:44:48 -0700573 String titleText = getAttributeValue(parser, ATTR_TITLE_TEXT);
574 title = TextUtils.isEmpty(titleText) ? "" : titleText;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700575 }
576
577 mValues.put(Favorites.TITLE, title);
578 mValues.put(Favorites.ITEM_TYPE, Favorites.ITEM_TYPE_FOLDER);
579 mValues.put(Favorites.SPANX, 1);
580 mValues.put(Favorites.SPANY, 1);
581 mValues.put(Favorites._ID, mCallback.generateNewItemId());
Sunny Goyalefb7e842018-10-04 15:11:00 -0700582 int folderId = mCallback.insertAndCheck(mDb, mValues);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700583 if (folderId < 0) {
584 if (LOGD) Log.e(TAG, "Unable to add folder");
585 return -1;
586 }
587
588 final ContentValues myValues = new ContentValues(mValues);
Sunny Goyalefb7e842018-10-04 15:11:00 -0700589 IntArray folderItems = new IntArray();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700590
591 int type;
592 int folderDepth = parser.getDepth();
Sunny Goyal56a57bb2015-07-06 11:15:45 -0700593 int rank = 0;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700594 while ((type = parser.next()) != XmlPullParser.END_TAG ||
595 parser.getDepth() > folderDepth) {
596 if (type != XmlPullParser.START_TAG) {
597 continue;
598 }
599 mValues.clear();
600 mValues.put(Favorites.CONTAINER, folderId);
Sunny Goyal56a57bb2015-07-06 11:15:45 -0700601 mValues.put(Favorites.RANK, rank);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700602
603 TagParser tagParser = mFolderElements.get(parser.getName());
604 if (tagParser != null) {
Sunny Goyalefb7e842018-10-04 15:11:00 -0700605 final int id = tagParser.parseAndAdd(parser);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700606 if (id >= 0) {
607 folderItems.add(id);
Sunny Goyal56a57bb2015-07-06 11:15:45 -0700608 rank++;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700609 }
610 } else {
611 throw new RuntimeException("Invalid folder item " + parser.getName());
612 }
613 }
614
Sunny Goyalefb7e842018-10-04 15:11:00 -0700615 int addedId = folderId;
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700616
617 // We can only have folders with >= 2 items, so we need to remove the
618 // folder and clean up if less than 2 items were included, or some
619 // failed to add, and less than 2 were actually added
620 if (folderItems.size() < 2) {
621 // Delete the folder
Sunny Goyal7b9e28f2023-05-17 12:44:03 -0700622 mDb.delete(TABLE_NAME, itemIdMatch(folderId), null);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700623 addedId = -1;
624
625 // If we have a single item, promote it to where the folder
626 // would have been.
627 if (folderItems.size() == 1) {
628 final ContentValues childValues = new ContentValues();
629 copyInteger(myValues, childValues, Favorites.CONTAINER);
630 copyInteger(myValues, childValues, Favorites.SCREEN);
631 copyInteger(myValues, childValues, Favorites.CELLX);
632 copyInteger(myValues, childValues, Favorites.CELLY);
633
634 addedId = folderItems.get(0);
Sunny Goyal7b9e28f2023-05-17 12:44:03 -0700635 mDb.update(TABLE_NAME, childValues,
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700636 Favorites._ID + "=" + addedId, null);
637 }
638 }
639 return addedId;
640 }
641 }
642
Sunny Goyal762d0612020-07-29 15:03:46 -0700643 public static void beginDocument(XmlPullParser parser, String firstElementName)
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700644 throws XmlPullParserException, IOException {
645 int type;
646 while ((type = parser.next()) != XmlPullParser.START_TAG
647 && type != XmlPullParser.END_DOCUMENT);
648
649 if (type != XmlPullParser.START_TAG) {
650 throw new XmlPullParserException("No start tag found");
651 }
652
653 if (!parser.getName().equals(firstElementName)) {
654 throw new XmlPullParserException("Unexpected start tag: found " + parser.getName() +
655 ", expected " + firstElementName);
656 }
657 }
658
Sunny Goyal96a09632015-12-16 11:32:54 -0800659 private static String convertToDistanceFromEnd(String value, int endValue) {
660 if (!TextUtils.isEmpty(value)) {
661 int x = Integer.parseInt(value);
662 if (x < 0) {
663 return Integer.toString(endValue + x);
664 }
665 }
666 return value;
667 }
668
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700669 /**
670 * Return attribute value, attempting launcher-specific namespace first
671 * before falling back to anonymous attribute.
672 */
Sunny Goyal0d742312019-03-04 20:22:26 -0800673 protected static String getAttributeValue(XmlPullParser parser, String attribute) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700674 String value = parser.getAttributeValue(
675 "http://schemas.android.com/apk/res-auto/com.android.launcher3", attribute);
676 if (value == null) {
677 value = parser.getAttributeValue(null, attribute);
678 }
679 return value;
680 }
681
682 /**
683 * Return attribute resource value, attempting launcher-specific namespace
684 * first before falling back to anonymous attribute.
685 */
Sunny Goyal0d742312019-03-04 20:22:26 -0800686 protected static int getAttributeResourceValue(XmlPullParser parser, String attribute,
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700687 int defaultValue) {
Sunny Goyal0d742312019-03-04 20:22:26 -0800688 AttributeSet attrs = Xml.asAttributeSet(parser);
689 int value = attrs.getAttributeResourceValue(
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700690 "http://schemas.android.com/apk/res-auto/com.android.launcher3", attribute,
691 defaultValue);
692 if (value == defaultValue) {
Sunny Goyal0d742312019-03-04 20:22:26 -0800693 value = attrs.getAttributeResourceValue(null, attribute, defaultValue);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700694 }
695 return value;
696 }
697
Rajeev Kumar26453a22017-06-09 16:02:25 -0700698 public interface LayoutParserCallback {
Sunny Goyalefb7e842018-10-04 15:11:00 -0700699 int generateNewItemId();
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700700
Sunny Goyalefb7e842018-10-04 15:11:00 -0700701 int insertAndCheck(SQLiteDatabase db, ContentValues values);
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700702 }
703
Samuel Fufaca37b8a2019-08-19 17:04:36 -0700704 @Thunk
705 static void copyInteger(ContentValues from, ContentValues to, String key) {
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700706 to.put(key, from.getAsInteger(key));
707 }
Nicolas Sleiman31e9fa42023-01-25 16:30:32 +0000708
Sunny Goyalab2f8082023-04-11 11:35:43 -0700709 /**
710 * Wrapper over resources for easier abstraction
711 */
712 public interface SourceResources {
713
714 /**
715 * Refer {@link Resources#getXml(int)}
716 */
717 default XmlResourceParser getXml(@XmlRes int id) throws NotFoundException {
718 throw new NotFoundException();
719 }
720
721 /**
722 * Refer {@link Resources#getString(int)}
723 */
724 default String getString(@StringRes int id) throws NotFoundException {
725 throw new NotFoundException();
726 }
727
728 /**
729 * Returns a {@link SourceResources} corresponding to the provided resources
730 */
731 static SourceResources wrap(Resources res) {
732 return new SourceResources() {
733 @Override
734 public XmlResourceParser getXml(int id) {
735 return res.getXml(id);
736 }
737
738 @Override
739 public String getString(int id) {
740 return res.getString(id);
741 }
742 };
743 }
744 }
745
746
Sunny Goyal0fe505b2014-08-06 09:55:36 -0700747}