blob: 289242f61e14555cb8cf7005d8368594cdabae77 [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;
Tonye3c59252017-05-02 21:32:27 -070022import android.graphics.Rect;
23import android.os.Bundle;
Tony Wickhamdf238372016-05-17 12:19:07 -070024import android.provider.Settings;
Winson Chung4c98d922011-05-31 16:50:48 -070025import android.util.AttributeSet;
Sunny Goyald5bd67d2016-03-11 01:10:19 -080026import android.util.Log;
27import android.widget.Toast;
28
Sunny Goyal0236d0b2017-10-24 14:54:30 -070029import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Sunny Goyald5bd67d2016-03-11 01:10:19 -080030import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal1f3f07d2017-02-10 16:52:16 -080031import com.android.launcher3.util.Themes;
Winson Chung4c98d922011-05-31 16:50:48 -070032
Tony Wickham734dfbe2015-09-16 16:22:36 -070033public class InfoDropTarget extends UninstallDropTarget {
Winson Chung4c98d922011-05-31 16:50:48 -070034
Sunny Goyald5bd67d2016-03-11 01:10:19 -080035 private static final String TAG = "InfoDropTarget";
36
Winson Chung4c98d922011-05-31 16:50:48 -070037 public InfoDropTarget(Context context, AttributeSet attrs) {
38 this(context, attrs, 0);
39 }
40
41 public InfoDropTarget(Context context, AttributeSet attrs, int defStyle) {
42 super(context, attrs, defStyle);
43 }
44
45 @Override
Sunny Goyalda1dfa32017-04-26 22:34:49 -070046 protected void setupUi() {
Winson Chung4c98d922011-05-31 16:50:48 -070047 // Get the hover color
Sunny Goyal1f3f07d2017-02-10 16:52:16 -080048 mHoverColor = Themes.getColorAccent(getContext());
Sunny Goyalda1dfa32017-04-26 22:34:49 -070049 setDrawable(R.drawable.ic_info_shadow);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080050 }
51
Sunny Goyald5bd67d2016-03-11 01:10:19 -080052 @Override
Sunny Goyal0236d0b2017-10-24 14:54:30 -070053 protected ComponentName performDropAction(ItemInfo item) {
54 return performDropAction(mLauncher, item, null, null);
Sunny Goyald5bd67d2016-03-11 01:10:19 -080055 }
56
Tony Wickham734dfbe2015-09-16 16:22:36 -070057 /**
58 * @return Whether the activity was started.
59 */
Sunny Goyald5bd67d2016-03-11 01:10:19 -080060 public static boolean startDetailsActivityForInfo(
Sunny Goyal3dce5f32017-10-05 11:40:05 -070061 ItemInfo info, Launcher launcher, Rect sourceBounds, Bundle opts) {
62 return performDropAction(launcher, info, sourceBounds, opts) != null;
Tonye3c59252017-05-02 21:32:27 -070063 }
64
Sunny Goyal3dce5f32017-10-05 11:40:05 -070065 /**
66 * Performs the drop action and returns the target component for the dragObject or null if
67 * the action was not performed.
68 */
69 private static ComponentName performDropAction(Context context, ItemInfo info,
70 Rect sourceBounds, Bundle opts) {
Mario Bertschler08ffaae2017-03-20 11:30:27 -070071 if (info instanceof PromiseAppInfo) {
72 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) info;
Sunny Goyal3dce5f32017-10-05 11:40:05 -070073 context.startActivity(promiseAppInfo.getMarketIntent());
74 return null;
Mario Bertschler08ffaae2017-03-20 11:30:27 -070075 }
Winson Chung4c98d922011-05-31 16:50:48 -070076 ComponentName componentName = null;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080077 if (info instanceof AppInfo) {
78 componentName = ((AppInfo) info).componentName;
79 } else if (info instanceof ShortcutInfo) {
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -070080 componentName = info.getTargetComponent();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080081 } else if (info instanceof PendingAddItemInfo) {
82 componentName = ((PendingAddItemInfo) info).componentName;
Tony Wickhamce86e192015-09-18 16:06:55 -070083 } else if (info instanceof LauncherAppWidgetInfo) {
84 componentName = ((LauncherAppWidgetInfo) info).providerName;
Winson Chung4c98d922011-05-31 16:50:48 -070085 }
86 if (componentName != null) {
Sunny Goyald5bd67d2016-03-11 01:10:19 -080087 try {
Sunny Goyal3dce5f32017-10-05 11:40:05 -070088 LauncherAppsCompat.getInstance(context)
Tonye3c59252017-05-02 21:32:27 -070089 .showAppDetailsForProfile(componentName, info.user, sourceBounds, opts);
Sunny Goyal3dce5f32017-10-05 11:40:05 -070090 return componentName;
Sunny Goyald5bd67d2016-03-11 01:10:19 -080091 } catch (SecurityException | ActivityNotFoundException e) {
Sunny Goyal3dce5f32017-10-05 11:40:05 -070092 Toast.makeText(context, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Sunny Goyald5bd67d2016-03-11 01:10:19 -080093 Log.e(TAG, "Unable to launch settings", e);
94 }
Winson Chung4c98d922011-05-31 16:50:48 -070095 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -070096 return null;
Winson Chung4c98d922011-05-31 16:50:48 -070097 }
98
99 @Override
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700100 public int getAccessibilityAction() {
101 return LauncherAccessibilityDelegate.INFO;
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700102 }
103
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700104 @Override
105 protected boolean supportsDrop(ItemInfo info) {
Tony Wickhamdf238372016-05-17 12:19:07 -0700106 // Only show the App Info drop target if developer settings are enabled.
Sunny Goyal0236d0b2017-10-24 14:54:30 -0700107 boolean developmentSettingsEnabled = Settings.Global.getInt(
108 getContext().getContentResolver(),
Tony Wickhamdf238372016-05-17 12:19:07 -0700109 Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) == 1;
Sunny Goyale6e72002017-01-12 16:55:36 -0800110 if (!developmentSettingsEnabled) {
111 return false;
112 }
113 return info.itemType != LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT &&
114 (info instanceof AppInfo ||
115 (info instanceof ShortcutInfo && !((ShortcutInfo) info).isPromise()) ||
116 (info instanceof LauncherAppWidgetInfo &&
117 ((LauncherAppWidgetInfo) info).restoreStatus == 0) ||
118 info instanceof PendingAddItemInfo);
Winson Chung4c98d922011-05-31 16:50:48 -0700119 }
Winson Chung4c98d922011-05-31 16:50:48 -0700120}