blob: 34adf47a18e18c0dc71072de86e9cbef45992b05 [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;
Tony Wickhamdf238372016-05-17 12:19:07 -070022import android.provider.Settings;
Winson Chung4c98d922011-05-31 16:50:48 -070023import android.util.AttributeSet;
Sunny Goyald5bd67d2016-03-11 01:10:19 -080024import android.util.Log;
25import android.widget.Toast;
26
27import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal1f3f07d2017-02-10 16:52:16 -080028import com.android.launcher3.util.Themes;
Winson Chung4c98d922011-05-31 16:50:48 -070029
Tony Wickham734dfbe2015-09-16 16:22:36 -070030public class InfoDropTarget extends UninstallDropTarget {
Winson Chung4c98d922011-05-31 16:50:48 -070031
Sunny Goyald5bd67d2016-03-11 01:10:19 -080032 private static final String TAG = "InfoDropTarget";
33
Winson Chung4c98d922011-05-31 16:50:48 -070034 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 Chung4c98d922011-05-31 16:50:48 -070045 // Get the hover color
Sunny Goyal1f3f07d2017-02-10 16:52:16 -080046 mHoverColor = Themes.getColorAccent(getContext());
Adam Cohen18bbc6a2014-06-03 21:43:24 -070047
Sunny Goyal40b626b2015-06-04 22:40:14 -070048 setDrawable(R.drawable.ic_info_launcher);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080049 }
50
Sunny Goyald5bd67d2016-03-11 01:10:19 -080051 @Override
Sunny Goyal0f76b562016-12-13 19:37:10 -080052 public void completeDrop(DragObject d) {
Sunny Goyald5bd67d2016-03-11 01:10:19 -080053 DropTargetResultCallback callback = d.dragSource instanceof DropTargetResultCallback
54 ? (DropTargetResultCallback) d.dragSource : null;
55 startDetailsActivityForInfo(d.dragInfo, mLauncher, callback);
56 }
57
Tony Wickham734dfbe2015-09-16 16:22:36 -070058 /**
59 * @return Whether the activity was started.
60 */
Sunny Goyald5bd67d2016-03-11 01:10:19 -080061 public static boolean startDetailsActivityForInfo(
62 ItemInfo info, Launcher launcher, DropTargetResultCallback callback) {
63 boolean result = false;
Winson Chung4c98d922011-05-31 16:50:48 -070064 ComponentName componentName = null;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080065 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 Wickhamce86e192015-09-18 16:06:55 -070071 } else if (info instanceof LauncherAppWidgetInfo) {
72 componentName = ((LauncherAppWidgetInfo) info).providerName;
Winson Chung4c98d922011-05-31 16:50:48 -070073 }
74 if (componentName != null) {
Sunny Goyald5bd67d2016-03-11 01:10:19 -080075 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 Chung4c98d922011-05-31 16:50:48 -070083 }
Tony Wickham734dfbe2015-09-16 16:22:36 -070084
Sunny Goyald5bd67d2016-03-11 01:10:19 -080085 if (callback != null) {
86 sendUninstallResult(launcher, result, componentName, info.user, callback);
87 }
88 return result;
Winson Chung4c98d922011-05-31 16:50:48 -070089 }
90
91 @Override
Sunny Goyalaa8ef112015-06-12 20:04:41 -070092 protected boolean supportsDrop(DragSource source, ItemInfo info) {
Sunny Goyal8ad02b82016-12-29 13:31:43 -080093 return source.supportsAppInfoDropTarget() && supportsDrop(getContext(), info);
Sunny Goyal1a70cef2015-04-22 11:29:51 -070094 }
95
Sunny Goyal8ad02b82016-12-29 13:31:43 -080096 public static boolean supportsDrop(Context context, ItemInfo info) {
Tony Wickhamdf238372016-05-17 12:19:07 -070097 // Only show the App Info drop target if developer settings are enabled.
Sunny Goyal8ad02b82016-12-29 13:31:43 -080098 boolean developmentSettingsEnabled = Settings.Global.getInt(context.getContentResolver(),
Tony Wickhamdf238372016-05-17 12:19:07 -070099 Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) == 1;
Sunny Goyale6e72002017-01-12 16:55:36 -0800100 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 Chung4c98d922011-05-31 16:50:48 -0700109 }
Winson Chung4c98d922011-05-31 16:50:48 -0700110}