blob: 191becf137f58b02451af9042ee55607c96b51f3 [file] [log] [blame]
Winson Chung4c98d922011-05-31 16:50:48 -07001/*
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 Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung4c98d922011-05-31 16:50:48 -070018
Sunny Goyald5bd67d2016-03-11 01:10:19 -080019import android.content.ActivityNotFoundException;
Winson Chung4c98d922011-05-31 16:50:48 -070020import android.content.ComponentName;
21import android.content.Context;
Winson Chung4c98d922011-05-31 16:50:48 -070022import android.util.AttributeSet;
Sunny Goyald5bd67d2016-03-11 01:10:19 -080023import android.util.Log;
24import android.widget.Toast;
25
26import com.android.launcher3.compat.LauncherAppsCompat;
Winson Chung4c98d922011-05-31 16:50:48 -070027
Tony Wickham734dfbe2015-09-16 16:22:36 -070028public class InfoDropTarget extends UninstallDropTarget {
Winson Chung4c98d922011-05-31 16:50:48 -070029
Sunny Goyald5bd67d2016-03-11 01:10:19 -080030 private static final String TAG = "InfoDropTarget";
31
Winson Chung4c98d922011-05-31 16:50:48 -070032 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 Chung4c98d922011-05-31 16:50:48 -070043 // Get the hover color
Sunny Goyalfa401a12015-04-10 13:45:42 -070044 mHoverColor = getResources().getColor(R.color.info_target_hover_tint);
Adam Cohen18bbc6a2014-06-03 21:43:24 -070045
Sunny Goyal40b626b2015-06-04 22:40:14 -070046 setDrawable(R.drawable.ic_info_launcher);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080047 }
48
Sunny Goyald5bd67d2016-03-11 01:10:19 -080049 @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 Wickham734dfbe2015-09-16 16:22:36 -070056 /**
57 * @return Whether the activity was started.
58 */
Sunny Goyald5bd67d2016-03-11 01:10:19 -080059 public static boolean startDetailsActivityForInfo(
60 ItemInfo info, Launcher launcher, DropTargetResultCallback callback) {
61 boolean result = false;
Winson Chung4c98d922011-05-31 16:50:48 -070062 ComponentName componentName = null;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080063 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 Wickhamce86e192015-09-18 16:06:55 -070069 } else if (info instanceof LauncherAppWidgetInfo) {
70 componentName = ((LauncherAppWidgetInfo) info).providerName;
Winson Chung4c98d922011-05-31 16:50:48 -070071 }
72 if (componentName != null) {
Sunny Goyald5bd67d2016-03-11 01:10:19 -080073 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 Chung4c98d922011-05-31 16:50:48 -070081 }
Tony Wickham734dfbe2015-09-16 16:22:36 -070082
Sunny Goyald5bd67d2016-03-11 01:10:19 -080083 if (callback != null) {
84 sendUninstallResult(launcher, result, componentName, info.user, callback);
85 }
86 return result;
Winson Chung4c98d922011-05-31 16:50:48 -070087 }
88
89 @Override
Sunny Goyalaa8ef112015-06-12 20:04:41 -070090 protected boolean supportsDrop(DragSource source, ItemInfo info) {
Sunny Goyald5bd67d2016-03-11 01:10:19 -080091 return source.supportsAppInfoDropTarget() && supportsDrop(info);
Sunny Goyal1a70cef2015-04-22 11:29:51 -070092 }
93
Sunny Goyald5bd67d2016-03-11 01:10:19 -080094 public static boolean supportsDrop(ItemInfo info) {
Tony Wickham34d2c912015-09-11 09:27:58 -070095 return info instanceof AppInfo || info instanceof ShortcutInfo
Tony Wickhamce86e192015-09-18 16:06:55 -070096 || info instanceof PendingAddItemInfo || info instanceof LauncherAppWidgetInfo;
Winson Chung4c98d922011-05-31 16:50:48 -070097 }
Winson Chung4c98d922011-05-31 16:50:48 -070098}