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