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