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