1 /* 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 17 package com.android.launcher3.pm; 18 19 import static com.android.launcher3.util.Executors.MODEL_EXECUTOR; 20 21 import android.annotation.TargetApi; 22 import android.content.Context; 23 import android.content.Intent; 24 import android.content.pm.LauncherApps; 25 import android.content.pm.LauncherApps.PinItemRequest; 26 import android.content.pm.ShortcutInfo; 27 import android.content.pm.ShortcutManager; 28 import android.os.Build; 29 import android.os.Parcelable; 30 import android.os.SystemClock; 31 32 import androidx.annotation.Nullable; 33 34 import com.android.launcher3.LauncherAppState; 35 import com.android.launcher3.icons.CacheableShortcutCachingLogic; 36 import com.android.launcher3.icons.CacheableShortcutInfo; 37 import com.android.launcher3.model.data.WorkspaceItemInfo; 38 39 public class PinRequestHelper { 40 41 /** 42 * request.accept() will initiate the following flow: 43 * -> go-to-system-process for actual processing (a) 44 * -> callback-to-launcher on UI thread (b) 45 * -> post callback on the worker thread (c) 46 * -> Update model and unpin (in system) any shortcut not in out model. (d) 47 * 48 * Note that (b) will take at-least one frame as it involves posting callback from binder 49 * thread to UI thread. 50 * If (d) happens before we add this shortcut to our model, we will end up unpinning 51 * the shortcut in the system. 52 * Here its the caller's responsibility to add the newly created WorkspaceItemInfo immediately 53 * to the model (which may involves a single post-to-worker-thread). That will guarantee 54 * that (d) happens after model is updated. 55 */ 56 @Nullable 57 @TargetApi(Build.VERSION_CODES.O) createWorkspaceItemFromPinItemRequest( Context context, final PinItemRequest request, final long acceptDelay)58 public static WorkspaceItemInfo createWorkspaceItemFromPinItemRequest( 59 Context context, final PinItemRequest request, final long acceptDelay) { 60 if (request != null && request.getRequestType() == PinItemRequest.REQUEST_TYPE_SHORTCUT 61 && request.isValid()) { 62 63 if (acceptDelay <= 0) { 64 if (!request.accept()) { 65 return null; 66 } 67 } else { 68 // Block the worker thread until the accept() is called. 69 MODEL_EXECUTOR.execute(() -> { 70 SystemClock.sleep(acceptDelay); 71 if (request.isValid()) { 72 request.accept(); 73 } 74 }); 75 } 76 77 ShortcutInfo si = request.getShortcutInfo(); 78 WorkspaceItemInfo info = new WorkspaceItemInfo(si, context); 79 // Apply the unbadged icon synchronously using the caching logic directly and 80 // fetch the actual icon asynchronously. 81 LauncherAppState app = LauncherAppState.getInstance(context); 82 info.bitmap = CacheableShortcutCachingLogic.INSTANCE.loadIcon( 83 context, app.getIconCache(), new CacheableShortcutInfo(si, context)); 84 app.getModel().updateAndBindWorkspaceItem(info, si); 85 return info; 86 } else { 87 return null; 88 } 89 } 90 91 @TargetApi(Build.VERSION_CODES.O) getPinItemRequest(Intent intent)92 public static PinItemRequest getPinItemRequest(Intent intent) { 93 Parcelable extra = intent.getParcelableExtra(LauncherApps.EXTRA_PIN_ITEM_REQUEST); 94 return extra instanceof PinItemRequest ? (PinItemRequest) extra : null; 95 } 96 97 /** 98 * Returns a PinItemRequest corresponding to the provided ShortcutInfo 99 */ createRequestForShortcut(Context context, ShortcutInfo info)100 public static PinItemRequest createRequestForShortcut(Context context, ShortcutInfo info) { 101 return context.getSystemService(LauncherApps.class) 102 .getPinItemRequest(context.getSystemService(ShortcutManager.class) 103 .createShortcutResultIntent(info)); 104 } 105 } 106