1 /*
2  * Copyright (C) 2017 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 
17 package com.android.launcher3.model;
18 
19 import android.content.ComponentName;
20 import android.content.ContentValues;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.Intent.ShortcutIconResource;
24 import android.content.pm.LauncherActivityInfo;
25 import android.database.Cursor;
26 import android.database.CursorWrapper;
27 import android.graphics.Bitmap;
28 import android.graphics.BitmapFactory;
29 import android.os.UserHandle;
30 import android.provider.BaseColumns;
31 import android.text.TextUtils;
32 import android.util.Log;
33 import android.util.LongSparseArray;
34 
35 import com.android.launcher3.IconCache;
36 import com.android.launcher3.InvariantDeviceProfile;
37 import com.android.launcher3.ItemInfo;
38 import com.android.launcher3.LauncherAppState;
39 import com.android.launcher3.LauncherSettings;
40 import com.android.launcher3.ShortcutInfo;
41 import com.android.launcher3.Utilities;
42 import com.android.launcher3.Workspace;
43 import com.android.launcher3.compat.LauncherAppsCompat;
44 import com.android.launcher3.compat.UserManagerCompat;
45 import com.android.launcher3.config.FeatureFlags;
46 import com.android.launcher3.graphics.LauncherIcons;
47 import com.android.launcher3.logging.FileLog;
48 import com.android.launcher3.util.ContentWriter;
49 import com.android.launcher3.util.GridOccupancy;
50 import com.android.launcher3.util.LongArrayMap;
51 import com.android.launcher3.util.PackageManagerHelper;
52 
53 import java.net.URISyntaxException;
54 import java.security.InvalidParameterException;
55 import java.util.ArrayList;
56 
57 /**
58  * Extension of {@link Cursor} with utility methods for workspace loading.
59  */
60 public class LoaderCursor extends CursorWrapper {
61 
62     private static final String TAG = "LoaderCursor";
63 
64     public final LongSparseArray<UserHandle> allUsers = new LongSparseArray<>();
65 
66     private final Context mContext;
67     private final UserManagerCompat mUserManager;
68     private final IconCache mIconCache;
69     private final InvariantDeviceProfile mIDP;
70 
71     private final ArrayList<Long> itemsToRemove = new ArrayList<>();
72     private final ArrayList<Long> restoredRows = new ArrayList<>();
73     private final LongArrayMap<GridOccupancy> occupied = new LongArrayMap<>();
74 
75     private final int iconPackageIndex;
76     private final int iconResourceIndex;
77     private final int iconIndex;
78     public final int titleIndex;
79 
80     private final int idIndex;
81     private final int containerIndex;
82     private final int itemTypeIndex;
83     private final int screenIndex;
84     private final int cellXIndex;
85     private final int cellYIndex;
86     private final int profileIdIndex;
87     private final int restoredIndex;
88     private final int intentIndex;
89 
90     // Properties loaded per iteration
91     public long serialNumber;
92     public UserHandle user;
93     public long id;
94     public long container;
95     public int itemType;
96     public int restoreFlag;
97 
LoaderCursor(Cursor c, LauncherAppState app)98     public LoaderCursor(Cursor c, LauncherAppState app) {
99         super(c);
100         mContext = app.getContext();
101         mIconCache = app.getIconCache();
102         mIDP = app.getInvariantDeviceProfile();
103         mUserManager = UserManagerCompat.getInstance(mContext);
104 
105         // Init column indices
106         iconIndex = getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
107         iconPackageIndex = getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE);
108         iconResourceIndex = getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE);
109         titleIndex = getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
110 
111         idIndex = getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
112         containerIndex = getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
113         itemTypeIndex = getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
114         screenIndex = getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
115         cellXIndex = getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
116         cellYIndex = getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
117         profileIdIndex = getColumnIndexOrThrow(LauncherSettings.Favorites.PROFILE_ID);
118         restoredIndex = getColumnIndexOrThrow(LauncherSettings.Favorites.RESTORED);
119         intentIndex = getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
120     }
121 
122     @Override
moveToNext()123     public boolean moveToNext() {
124         boolean result = super.moveToNext();
125         if (result) {
126             // Load common properties.
127             itemType = getInt(itemTypeIndex);
128             container = getInt(containerIndex);
129             id = getLong(idIndex);
130             serialNumber = getInt(profileIdIndex);
131             user = allUsers.get(serialNumber);
132             restoreFlag = getInt(restoredIndex);
133         }
134         return result;
135     }
136 
parseIntent()137     public Intent parseIntent() {
138         String intentDescription = getString(intentIndex);
139         try {
140             return TextUtils.isEmpty(intentDescription) ?
141                     null : Intent.parseUri(intentDescription, 0);
142         } catch (URISyntaxException e) {
143             Log.e(TAG, "Error parsing Intent");
144             return null;
145         }
146     }
147 
loadSimpleShortcut()148     public ShortcutInfo loadSimpleShortcut() {
149         final ShortcutInfo info = new ShortcutInfo();
150         // Non-app shortcuts are only supported for current user.
151         info.user = user;
152         info.itemType = itemType;
153         info.title = getTitle();
154         info.iconBitmap = loadIcon(info);
155         // the fallback icon
156         if (info.iconBitmap == null) {
157             info.iconBitmap = mIconCache.getDefaultIcon(info.user);
158         }
159 
160         // TODO: If there's an explicit component and we can't install that, delete it.
161 
162         return info;
163     }
164 
165     /**
166      * Loads the icon from the cursor and updates the {@param info} if the icon is an app resource.
167      */
loadIcon(ShortcutInfo info)168     protected Bitmap loadIcon(ShortcutInfo info) {
169         Bitmap icon = null;
170         if (itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) {
171             String packageName = getString(iconPackageIndex);
172             String resourceName = getString(iconResourceIndex);
173             if (!TextUtils.isEmpty(packageName) || !TextUtils.isEmpty(resourceName)) {
174                 info.iconResource = new ShortcutIconResource();
175                 info.iconResource.packageName = packageName;
176                 info.iconResource.resourceName = resourceName;
177                 icon = LauncherIcons.createIconBitmap(info.iconResource, mContext);
178             }
179         }
180         if (icon == null) {
181             // Failed to load from resource, try loading from DB.
182             byte[] data = getBlob(iconIndex);
183             try {
184                 icon = LauncherIcons.createIconBitmap(
185                         BitmapFactory.decodeByteArray(data, 0, data.length), mContext);
186             } catch (Exception e) {
187                 return null;
188             }
189         }
190         return icon;
191     }
192 
193     /**
194      * Returns the title or empty string
195      */
getTitle()196     private String getTitle() {
197         String title = getString(titleIndex);
198         return TextUtils.isEmpty(title) ? "" : Utilities.trim(title);
199     }
200 
201 
202     /**
203      * Make an ShortcutInfo object for a restored application or shortcut item that points
204      * to a package that is not yet installed on the system.
205      */
getRestoredItemInfo(Intent intent)206     public ShortcutInfo getRestoredItemInfo(Intent intent) {
207         final ShortcutInfo info = new ShortcutInfo();
208         info.user = user;
209         info.intent = intent;
210 
211         info.iconBitmap = loadIcon(info);
212         // the fallback icon
213         if (info.iconBitmap == null) {
214             mIconCache.getTitleAndIcon(info, false /* useLowResIcon */);
215         }
216 
217         if (hasRestoreFlag(ShortcutInfo.FLAG_RESTORED_ICON)) {
218             String title = getTitle();
219             if (!TextUtils.isEmpty(title)) {
220                 info.title = Utilities.trim(title);
221             }
222         } else if  (hasRestoreFlag(ShortcutInfo.FLAG_AUTOINTALL_ICON)) {
223             if (TextUtils.isEmpty(info.title)) {
224                 info.title = getTitle();
225             }
226         } else {
227             throw new InvalidParameterException("Invalid restoreType " + restoreFlag);
228         }
229 
230         info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
231         info.itemType = itemType;
232         info.status = restoreFlag;
233         return info;
234     }
235 
236     /**
237      * Make an ShortcutInfo object for a shortcut that is an application.
238      */
getAppShortcutInfo( Intent intent, boolean allowMissingTarget, boolean useLowResIcon)239     public ShortcutInfo getAppShortcutInfo(
240             Intent intent, boolean allowMissingTarget, boolean useLowResIcon) {
241         if (user == null) {
242             Log.d(TAG, "Null user found in getShortcutInfo");
243             return null;
244         }
245 
246         ComponentName componentName = intent.getComponent();
247         if (componentName == null) {
248             Log.d(TAG, "Missing component found in getShortcutInfo");
249             return null;
250         }
251 
252         Intent newIntent = new Intent(Intent.ACTION_MAIN, null);
253         newIntent.addCategory(Intent.CATEGORY_LAUNCHER);
254         newIntent.setComponent(componentName);
255         LauncherActivityInfo lai = LauncherAppsCompat.getInstance(mContext)
256                 .resolveActivity(newIntent, user);
257         if ((lai == null) && !allowMissingTarget) {
258             Log.d(TAG, "Missing activity found in getShortcutInfo: " + componentName);
259             return null;
260         }
261 
262         final ShortcutInfo info = new ShortcutInfo();
263         info.itemType = LauncherSettings.Favorites.ITEM_TYPE_APPLICATION;
264         info.user = user;
265         info.intent = newIntent;
266 
267         mIconCache.getTitleAndIcon(info, lai, useLowResIcon);
268         if (mIconCache.isDefaultIcon(info.iconBitmap, user)) {
269             Bitmap icon = loadIcon(info);
270             info.iconBitmap = icon != null ? icon : info.iconBitmap;
271         }
272 
273         if (lai != null && PackageManagerHelper.isAppSuspended(lai.getApplicationInfo())) {
274             info.isDisabled = ShortcutInfo.FLAG_DISABLED_SUSPENDED;
275         }
276 
277         // from the db
278         if (TextUtils.isEmpty(info.title)) {
279             info.title = getTitle();
280         }
281 
282         // fall back to the class name of the activity
283         if (info.title == null) {
284             info.title = componentName.getClassName();
285         }
286 
287         info.contentDescription = mUserManager.getBadgedLabelForUser(info.title, info.user);
288         return info;
289     }
290 
291     /**
292      * Returns a {@link ContentWriter} which can be used to update the current item.
293      */
updater()294     public ContentWriter updater() {
295        return new ContentWriter(mContext, new ContentWriter.CommitParams(
296                BaseColumns._ID + "= ?", new String[]{Long.toString(id)}));
297     }
298 
299     /**
300      * Marks the current item for removal
301      */
markDeleted(String reason)302     public void markDeleted(String reason) {
303         FileLog.e(TAG, reason);
304         itemsToRemove.add(id);
305     }
306 
307     /**
308      * Removes any items marked for removal.
309      * @return true is any item was removed.
310      */
commitDeleted()311     public boolean commitDeleted() {
312         if (itemsToRemove.size() > 0) {
313             // Remove dead items
314             mContext.getContentResolver().delete(LauncherSettings.Favorites.CONTENT_URI,
315                     Utilities.createDbSelectionQuery(
316                             LauncherSettings.Favorites._ID, itemsToRemove), null);
317             return true;
318         }
319         return false;
320     }
321 
322     /**
323      * Marks the current item as restored
324      */
markRestored()325     public void markRestored() {
326         if (restoreFlag != 0) {
327             restoredRows.add(id);
328             restoreFlag = 0;
329         }
330     }
331 
hasRestoreFlag(int flagMask)332     public boolean hasRestoreFlag(int flagMask) {
333         return (restoreFlag & flagMask) != 0;
334     }
335 
commitRestoredItems()336     public void commitRestoredItems() {
337         if (restoredRows.size() > 0) {
338             // Update restored items that no longer require special handling
339             ContentValues values = new ContentValues();
340             values.put(LauncherSettings.Favorites.RESTORED, 0);
341             mContext.getContentResolver().update(LauncherSettings.Favorites.CONTENT_URI, values,
342                     Utilities.createDbSelectionQuery(
343                             LauncherSettings.Favorites._ID, restoredRows), null);
344         }
345     }
346 
347     /**
348      * Returns true is the item is on workspace or hotseat
349      */
isOnWorkspaceOrHotseat()350     public boolean isOnWorkspaceOrHotseat() {
351         return container == LauncherSettings.Favorites.CONTAINER_DESKTOP ||
352                 container == LauncherSettings.Favorites.CONTAINER_HOTSEAT;
353     }
354 
355     /**
356      * Applies the following properties:
357      * {@link ItemInfo#id}
358      * {@link ItemInfo#container}
359      * {@link ItemInfo#screenId}
360      * {@link ItemInfo#cellX}
361      * {@link ItemInfo#cellY}
362      */
applyCommonProperties(ItemInfo info)363     public void applyCommonProperties(ItemInfo info) {
364         info.id = id;
365         info.container = container;
366         info.screenId = getInt(screenIndex);
367         info.cellX = getInt(cellXIndex);
368         info.cellY = getInt(cellYIndex);
369     }
370 
371     /**
372      * Adds the {@param info} to {@param dataModel} if it does not overlap with any other item,
373      * otherwise marks it for deletion.
374      */
checkAndAddItem(ItemInfo info, BgDataModel dataModel)375     public void checkAndAddItem(ItemInfo info, BgDataModel dataModel) {
376         if (checkItemPlacement(info, dataModel.workspaceScreens)) {
377             dataModel.addItem(mContext, info, false);
378         } else {
379             markDeleted("Item position overlap");
380         }
381     }
382 
383     /**
384      * check & update map of what's occupied; used to discard overlapping/invalid items
385      */
checkItemPlacement(ItemInfo item, ArrayList<Long> workspaceScreens)386     protected boolean checkItemPlacement(ItemInfo item, ArrayList<Long> workspaceScreens) {
387         long containerIndex = item.screenId;
388         if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
389             // Return early if we detect that an item is under the hotseat button
390             if (!FeatureFlags.NO_ALL_APPS_ICON &&
391                     mIDP.isAllAppsButtonRank((int) item.screenId)) {
392                 Log.e(TAG, "Error loading shortcut into hotseat " + item
393                         + " into position (" + item.screenId + ":" + item.cellX + ","
394                         + item.cellY + ") occupied by all apps");
395                 return false;
396             }
397 
398             final GridOccupancy hotseatOccupancy =
399                     occupied.get((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT);
400 
401             if (item.screenId >= mIDP.numHotseatIcons) {
402                 Log.e(TAG, "Error loading shortcut " + item
403                         + " into hotseat position " + item.screenId
404                         + ", position out of bounds: (0 to " + (mIDP.numHotseatIcons - 1)
405                         + ")");
406                 return false;
407             }
408 
409             if (hotseatOccupancy != null) {
410                 if (hotseatOccupancy.cells[(int) item.screenId][0]) {
411                     Log.e(TAG, "Error loading shortcut into hotseat " + item
412                             + " into position (" + item.screenId + ":" + item.cellX + ","
413                             + item.cellY + ") already occupied");
414                     return false;
415                 } else {
416                     hotseatOccupancy.cells[(int) item.screenId][0] = true;
417                     return true;
418                 }
419             } else {
420                 final GridOccupancy occupancy = new GridOccupancy(mIDP.numHotseatIcons, 1);
421                 occupancy.cells[(int) item.screenId][0] = true;
422                 occupied.put((long) LauncherSettings.Favorites.CONTAINER_HOTSEAT, occupancy);
423                 return true;
424             }
425         } else if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
426             if (!workspaceScreens.contains((Long) item.screenId)) {
427                 // The item has an invalid screen id.
428                 return false;
429             }
430         } else {
431             // Skip further checking if it is not the hotseat or workspace container
432             return true;
433         }
434 
435         final int countX = mIDP.numColumns;
436         final int countY = mIDP.numRows;
437         if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP &&
438                 item.cellX < 0 || item.cellY < 0 ||
439                 item.cellX + item.spanX > countX || item.cellY + item.spanY > countY) {
440             Log.e(TAG, "Error loading shortcut " + item
441                     + " into cell (" + containerIndex + "-" + item.screenId + ":"
442                     + item.cellX + "," + item.cellY
443                     + ") out of screen bounds ( " + countX + "x" + countY + ")");
444             return false;
445         }
446 
447         if (!occupied.containsKey(item.screenId)) {
448             GridOccupancy screen = new GridOccupancy(countX + 1, countY + 1);
449             if (item.screenId == Workspace.FIRST_SCREEN_ID) {
450                 // Mark the first row as occupied (if the feature is enabled)
451                 // in order to account for the QSB.
452                 screen.markCells(0, 0, countX + 1, 1, FeatureFlags.QSB_ON_FIRST_SCREEN);
453             }
454             occupied.put(item.screenId, screen);
455         }
456         final GridOccupancy occupancy = occupied.get(item.screenId);
457 
458         // Check if any workspace icons overlap with each other
459         if (occupancy.isRegionVacant(item.cellX, item.cellY, item.spanX, item.spanY)) {
460             occupancy.markCells(item, true);
461             return true;
462         } else {
463             Log.e(TAG, "Error loading shortcut " + item
464                     + " into cell (" + containerIndex + "-" + item.screenId + ":"
465                     + item.cellX + "," + item.cellX + "," + item.spanX + "," + item.spanY
466                     + ") already occupied");
467             return false;
468         }
469     }
470 }
471