Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | |
Daniel Sandler | 325dc23 | 2013-06-05 22:57:57 -0400 | [diff] [blame] | 17 | package com.android.launcher3; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 18 | |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 19 | import android.content.ActivityNotFoundException; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 20 | import android.content.ComponentName; |
| 21 | import android.content.Context; |
Tony | e3c5925 | 2017-05-02 21:32:27 -0700 | [diff] [blame] | 22 | import android.graphics.Rect; |
| 23 | import android.os.Bundle; |
Tony Wickham | df23837 | 2016-05-17 12:19:07 -0700 | [diff] [blame] | 24 | import android.provider.Settings; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 25 | import android.util.AttributeSet; |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 26 | import android.util.Log; |
| 27 | import android.widget.Toast; |
| 28 | |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 29 | import com.android.launcher3.accessibility.LauncherAccessibilityDelegate; |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 30 | import com.android.launcher3.compat.LauncherAppsCompat; |
Sunny Goyal | 1f3f07d | 2017-02-10 16:52:16 -0800 | [diff] [blame] | 31 | import com.android.launcher3.util.Themes; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 32 | |
Tony Wickham | 734dfbe | 2015-09-16 16:22:36 -0700 | [diff] [blame] | 33 | public class InfoDropTarget extends UninstallDropTarget { |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 34 | |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 35 | private static final String TAG = "InfoDropTarget"; |
| 36 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 37 | public InfoDropTarget(Context context, AttributeSet attrs) { |
| 38 | this(context, attrs, 0); |
| 39 | } |
| 40 | |
| 41 | public InfoDropTarget(Context context, AttributeSet attrs, int defStyle) { |
| 42 | super(context, attrs, defStyle); |
| 43 | } |
| 44 | |
| 45 | @Override |
Sunny Goyal | da1dfa3 | 2017-04-26 22:34:49 -0700 | [diff] [blame] | 46 | protected void setupUi() { |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 47 | // Get the hover color |
Sunny Goyal | 1f3f07d | 2017-02-10 16:52:16 -0800 | [diff] [blame] | 48 | mHoverColor = Themes.getColorAccent(getContext()); |
Sunny Goyal | da1dfa3 | 2017-04-26 22:34:49 -0700 | [diff] [blame] | 49 | setDrawable(R.drawable.ic_info_shadow); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 52 | @Override |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 53 | protected ComponentName performDropAction(ItemInfo item) { |
| 54 | return performDropAction(mLauncher, item, null, null); |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Tony Wickham | 734dfbe | 2015-09-16 16:22:36 -0700 | [diff] [blame] | 57 | /** |
| 58 | * @return Whether the activity was started. |
| 59 | */ |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 60 | public static boolean startDetailsActivityForInfo( |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 61 | ItemInfo info, Launcher launcher, Rect sourceBounds, Bundle opts) { |
| 62 | return performDropAction(launcher, info, sourceBounds, opts) != null; |
Tony | e3c5925 | 2017-05-02 21:32:27 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 65 | /** |
| 66 | * Performs the drop action and returns the target component for the dragObject or null if |
| 67 | * the action was not performed. |
| 68 | */ |
| 69 | private static ComponentName performDropAction(Context context, ItemInfo info, |
| 70 | Rect sourceBounds, Bundle opts) { |
Mario Bertschler | 08ffaae | 2017-03-20 11:30:27 -0700 | [diff] [blame] | 71 | if (info instanceof PromiseAppInfo) { |
| 72 | PromiseAppInfo promiseAppInfo = (PromiseAppInfo) info; |
Sunny Goyal | 67e75e2 | 2018-01-22 15:14:00 -0800 | [diff] [blame^] | 73 | context.startActivity(promiseAppInfo.getMarketIntent(context)); |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 74 | return null; |
Mario Bertschler | 08ffaae | 2017-03-20 11:30:27 -0700 | [diff] [blame] | 75 | } |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 76 | ComponentName componentName = null; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 77 | if (info instanceof AppInfo) { |
| 78 | componentName = ((AppInfo) info).componentName; |
| 79 | } else if (info instanceof ShortcutInfo) { |
Mario Bertschler | 8ff9e1d | 2017-08-08 16:26:18 -0700 | [diff] [blame] | 80 | componentName = info.getTargetComponent(); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 81 | } else if (info instanceof PendingAddItemInfo) { |
| 82 | componentName = ((PendingAddItemInfo) info).componentName; |
Tony Wickham | ce86e19 | 2015-09-18 16:06:55 -0700 | [diff] [blame] | 83 | } else if (info instanceof LauncherAppWidgetInfo) { |
| 84 | componentName = ((LauncherAppWidgetInfo) info).providerName; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 85 | } |
| 86 | if (componentName != null) { |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 87 | try { |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 88 | LauncherAppsCompat.getInstance(context) |
Tony | e3c5925 | 2017-05-02 21:32:27 -0700 | [diff] [blame] | 89 | .showAppDetailsForProfile(componentName, info.user, sourceBounds, opts); |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 90 | return componentName; |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 91 | } catch (SecurityException | ActivityNotFoundException e) { |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 92 | Toast.makeText(context, R.string.activity_not_found, Toast.LENGTH_SHORT).show(); |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 93 | Log.e(TAG, "Unable to launch settings", e); |
| 94 | } |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 95 | } |
Sunny Goyal | 3dce5f3 | 2017-10-05 11:40:05 -0700 | [diff] [blame] | 96 | return null; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Override |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 100 | public int getAccessibilityAction() { |
| 101 | return LauncherAccessibilityDelegate.INFO; |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 104 | @Override |
| 105 | protected boolean supportsDrop(ItemInfo info) { |
Tony Wickham | df23837 | 2016-05-17 12:19:07 -0700 | [diff] [blame] | 106 | // Only show the App Info drop target if developer settings are enabled. |
Sunny Goyal | 0236d0b | 2017-10-24 14:54:30 -0700 | [diff] [blame] | 107 | boolean developmentSettingsEnabled = Settings.Global.getInt( |
| 108 | getContext().getContentResolver(), |
Tony Wickham | df23837 | 2016-05-17 12:19:07 -0700 | [diff] [blame] | 109 | Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) == 1; |
Sunny Goyal | e6e7200 | 2017-01-12 16:55:36 -0800 | [diff] [blame] | 110 | if (!developmentSettingsEnabled) { |
| 111 | return false; |
| 112 | } |
| 113 | return info.itemType != LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT && |
| 114 | (info instanceof AppInfo || |
| 115 | (info instanceof ShortcutInfo && !((ShortcutInfo) info).isPromise()) || |
| 116 | (info instanceof LauncherAppWidgetInfo && |
| 117 | ((LauncherAppWidgetInfo) info).restoreStatus == 0) || |
| 118 | info instanceof PendingAddItemInfo); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 119 | } |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 120 | } |