• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.dragndrop;
18 
19 import static android.content.pm.LauncherApps.EXTRA_PIN_ITEM_REQUEST;
20 
21 import static com.android.launcher3.LauncherAnimUtils.SPRING_LOADED_EXIT_DELAY;
22 import static com.android.launcher3.LauncherState.EDIT_MODE;
23 import static com.android.launcher3.LauncherState.SPRING_LOADED;
24 import static com.android.launcher3.config.FeatureFlags.MULTI_SELECT_EDIT_MODE;
25 
26 import android.annotation.TargetApi;
27 import android.app.Activity;
28 import android.content.ComponentName;
29 import android.content.Context;
30 import android.content.Intent;
31 import android.content.pm.LauncherApps;
32 import android.content.pm.LauncherApps.PinItemRequest;
33 import android.content.pm.ShortcutInfo;
34 import android.graphics.drawable.Drawable;
35 import android.os.Build;
36 import android.os.Process;
37 
38 import com.android.launcher3.Launcher;
39 import com.android.launcher3.LauncherAppState;
40 import com.android.launcher3.LauncherSettings;
41 import com.android.launcher3.R;
42 import com.android.launcher3.icons.cache.BaseIconCache;
43 import com.android.launcher3.model.data.WorkspaceItemInfo;
44 import com.android.launcher3.pm.PinRequestHelper;
45 import com.android.launcher3.pm.ShortcutConfigActivityInfo;
46 import com.android.launcher3.util.StartActivityParams;
47 
48 import java.util.function.Supplier;
49 
50 /**
51  * Extension of ShortcutConfigActivityInfo to be used in the confirmation prompt for pin item
52  * request.
53  */
54 @TargetApi(Build.VERSION_CODES.O)
55 public class PinShortcutRequestActivityInfo extends ShortcutConfigActivityInfo {
56 
57     // Class name used in the target component, such that it will never represent an
58     // actual existing class.
59     private static final String STUB_COMPONENT_CLASS = "pinned-shortcut";
60 
61     private final Supplier<PinItemRequest> mRequestSupplier;
62     private final ShortcutInfo mInfo;
63     private final Context mContext;
64 
PinShortcutRequestActivityInfo(PinItemRequest request, Context context)65     public PinShortcutRequestActivityInfo(PinItemRequest request, Context context) {
66         this(request.getShortcutInfo(), () -> request, context);
67     }
68 
PinShortcutRequestActivityInfo( ShortcutInfo si, Supplier<PinItemRequest> requestSupplier, Context context)69     public PinShortcutRequestActivityInfo(
70             ShortcutInfo si, Supplier<PinItemRequest> requestSupplier, Context context) {
71         super(new ComponentName(si.getPackage(), STUB_COMPONENT_CLASS),
72                 si.getUserHandle(), context);
73         mRequestSupplier = requestSupplier;
74         mInfo = si;
75         mContext = context;
76     }
77 
78     @Override
getItemType()79     public int getItemType() {
80         return LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT;
81     }
82 
83     @Override
getLabel()84     public CharSequence getLabel() {
85         return mInfo.getShortLabel();
86     }
87 
88     @Override
getFullResIcon(BaseIconCache cache)89     public Drawable getFullResIcon(BaseIconCache cache) {
90         Drawable d = mContext.getSystemService(LauncherApps.class)
91                 .getShortcutIconDrawable(mInfo, LauncherAppState.getIDP(mContext).fillResIconDpi);
92         if (d == null) {
93             d = cache.getDefaultIcon(Process.myUserHandle()).newIcon(mContext);
94         }
95         return d;
96     }
97 
98     @Override
createWorkspaceItemInfo()99     public WorkspaceItemInfo createWorkspaceItemInfo() {
100         long transitionDuration = (MULTI_SELECT_EDIT_MODE.get() ? EDIT_MODE : SPRING_LOADED)
101                 .getTransitionDuration(Launcher.getLauncher(mContext), true /* isToState */);
102         // Total duration for the drop animation to complete.
103         long duration = mContext.getResources().getInteger(R.integer.config_dropAnimMaxDuration) +
104                 SPRING_LOADED_EXIT_DELAY + transitionDuration;
105         // Delay the actual accept() call until the drop animation is complete.
106         return PinRequestHelper.createWorkspaceItemFromPinItemRequest(
107                 mContext, mRequestSupplier.get(), duration);
108     }
109 
110     @Override
startConfigActivity(Activity activity, int requestCode)111     public boolean startConfigActivity(Activity activity, int requestCode) {
112         new StartActivityParams(activity, requestCode).deliverResult(
113                 activity,
114                 Activity.RESULT_OK,
115                 new Intent().putExtra(EXTRA_PIN_ITEM_REQUEST, mRequestSupplier.get()));
116         return true;
117     }
118 
119     @Override
isPersistable()120     public boolean isPersistable() {
121         return false;
122     }
123 }
124