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 Wickham | df23837 | 2016-05-17 12:19:07 -0700 | [diff] [blame] | 22 | import android.provider.Settings; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 23 | import android.util.AttributeSet; |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 24 | import android.util.Log; |
| 25 | import android.widget.Toast; |
| 26 | |
| 27 | import com.android.launcher3.compat.LauncherAppsCompat; |
Sunny Goyal | 1f3f07d | 2017-02-10 16:52:16 -0800 | [diff] [blame^] | 28 | import com.android.launcher3.util.Themes; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 29 | |
Tony Wickham | 734dfbe | 2015-09-16 16:22:36 -0700 | [diff] [blame] | 30 | public class InfoDropTarget extends UninstallDropTarget { |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 31 | |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 32 | private static final String TAG = "InfoDropTarget"; |
| 33 | |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 34 | public InfoDropTarget(Context context, AttributeSet attrs) { |
| 35 | this(context, attrs, 0); |
| 36 | } |
| 37 | |
| 38 | public InfoDropTarget(Context context, AttributeSet attrs, int defStyle) { |
| 39 | super(context, attrs, defStyle); |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | protected void onFinishInflate() { |
| 44 | super.onFinishInflate(); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 45 | // Get the hover color |
Sunny Goyal | 1f3f07d | 2017-02-10 16:52:16 -0800 | [diff] [blame^] | 46 | mHoverColor = Themes.getColorAccent(getContext()); |
Adam Cohen | 18bbc6a | 2014-06-03 21:43:24 -0700 | [diff] [blame] | 47 | |
Sunny Goyal | 40b626b | 2015-06-04 22:40:14 -0700 | [diff] [blame] | 48 | setDrawable(R.drawable.ic_info_launcher); |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 51 | @Override |
Sunny Goyal | 0f76b56 | 2016-12-13 19:37:10 -0800 | [diff] [blame] | 52 | public void completeDrop(DragObject d) { |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 53 | DropTargetResultCallback callback = d.dragSource instanceof DropTargetResultCallback |
| 54 | ? (DropTargetResultCallback) d.dragSource : null; |
| 55 | startDetailsActivityForInfo(d.dragInfo, mLauncher, callback); |
| 56 | } |
| 57 | |
Tony Wickham | 734dfbe | 2015-09-16 16:22:36 -0700 | [diff] [blame] | 58 | /** |
| 59 | * @return Whether the activity was started. |
| 60 | */ |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 61 | public static boolean startDetailsActivityForInfo( |
| 62 | ItemInfo info, Launcher launcher, DropTargetResultCallback callback) { |
| 63 | boolean result = false; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 64 | ComponentName componentName = null; |
Sunny Goyal | 71b5c0b | 2015-01-08 16:59:04 -0800 | [diff] [blame] | 65 | if (info instanceof AppInfo) { |
| 66 | componentName = ((AppInfo) info).componentName; |
| 67 | } else if (info instanceof ShortcutInfo) { |
| 68 | componentName = ((ShortcutInfo) info).intent.getComponent(); |
| 69 | } else if (info instanceof PendingAddItemInfo) { |
| 70 | componentName = ((PendingAddItemInfo) info).componentName; |
Tony Wickham | ce86e19 | 2015-09-18 16:06:55 -0700 | [diff] [blame] | 71 | } else if (info instanceof LauncherAppWidgetInfo) { |
| 72 | componentName = ((LauncherAppWidgetInfo) info).providerName; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 73 | } |
| 74 | if (componentName != null) { |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 75 | try { |
| 76 | LauncherAppsCompat.getInstance(launcher) |
| 77 | .showAppDetailsForProfile(componentName, info.user); |
| 78 | result = true; |
| 79 | } catch (SecurityException | ActivityNotFoundException e) { |
| 80 | Toast.makeText(launcher, R.string.activity_not_found, Toast.LENGTH_SHORT).show(); |
| 81 | Log.e(TAG, "Unable to launch settings", e); |
| 82 | } |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 83 | } |
Tony Wickham | 734dfbe | 2015-09-16 16:22:36 -0700 | [diff] [blame] | 84 | |
Sunny Goyal | d5bd67d | 2016-03-11 01:10:19 -0800 | [diff] [blame] | 85 | if (callback != null) { |
| 86 | sendUninstallResult(launcher, result, componentName, info.user, callback); |
| 87 | } |
| 88 | return result; |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | @Override |
Sunny Goyal | aa8ef11 | 2015-06-12 20:04:41 -0700 | [diff] [blame] | 92 | protected boolean supportsDrop(DragSource source, ItemInfo info) { |
Sunny Goyal | 8ad02b8 | 2016-12-29 13:31:43 -0800 | [diff] [blame] | 93 | return source.supportsAppInfoDropTarget() && supportsDrop(getContext(), info); |
Sunny Goyal | 1a70cef | 2015-04-22 11:29:51 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Sunny Goyal | 8ad02b8 | 2016-12-29 13:31:43 -0800 | [diff] [blame] | 96 | public static boolean supportsDrop(Context context, ItemInfo info) { |
Tony Wickham | df23837 | 2016-05-17 12:19:07 -0700 | [diff] [blame] | 97 | // Only show the App Info drop target if developer settings are enabled. |
Sunny Goyal | 8ad02b8 | 2016-12-29 13:31:43 -0800 | [diff] [blame] | 98 | boolean developmentSettingsEnabled = Settings.Global.getInt(context.getContentResolver(), |
Tony Wickham | df23837 | 2016-05-17 12:19:07 -0700 | [diff] [blame] | 99 | Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) == 1; |
Sunny Goyal | e6e7200 | 2017-01-12 16:55:36 -0800 | [diff] [blame] | 100 | if (!developmentSettingsEnabled) { |
| 101 | return false; |
| 102 | } |
| 103 | return info.itemType != LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT && |
| 104 | (info instanceof AppInfo || |
| 105 | (info instanceof ShortcutInfo && !((ShortcutInfo) info).isPromise()) || |
| 106 | (info instanceof LauncherAppWidgetInfo && |
| 107 | ((LauncherAppWidgetInfo) info).restoreStatus == 0) || |
| 108 | info instanceof PendingAddItemInfo); |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 109 | } |
Winson Chung | 4c98d92 | 2011-05-31 16:50:48 -0700 | [diff] [blame] | 110 | } |