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