blob: e52fd765b970a0657c0bacf24ca480ca191917ea [file] [log] [blame]
Winson Chungf502e5f2018-03-05 18:06:52 +00001/*
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
17package com.android.launcher3;
18
19import android.content.ActivityNotFoundException;
20import android.content.ComponentName;
21import android.content.Context;
22import android.graphics.Rect;
23import android.os.Bundle;
24import android.provider.Settings;
25import android.util.AttributeSet;
26import android.util.Log;
27import android.widget.Toast;
28
29import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
30import com.android.launcher3.compat.LauncherAppsCompat;
31import com.android.launcher3.util.Themes;
32
33public class InfoDropTarget extends UninstallDropTarget {
34
35 private static final String TAG = "InfoDropTarget";
36
37 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
46 protected void setupUi() {
47 // Get the hover color
48 mHoverColor = Themes.getColorAccent(getContext());
49 setDrawable(R.drawable.ic_info_shadow);
50 }
51
52 @Override
53 protected ComponentName performDropAction(ItemInfo item) {
54 return performDropAction(mLauncher, item, null, null);
55 }
56
57 /**
58 * @return Whether the activity was started.
59 */
60 public static boolean startDetailsActivityForInfo(
61 ItemInfo info, Launcher launcher, Rect sourceBounds, Bundle opts) {
62 return performDropAction(launcher, info, sourceBounds, opts) != null;
63 }
64
65 /**
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) {
71 if (info instanceof PromiseAppInfo) {
72 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) info;
73 context.startActivity(promiseAppInfo.getMarketIntent(context));
74 return null;
75 }
76 ComponentName componentName = null;
77 if (info instanceof AppInfo) {
78 componentName = ((AppInfo) info).componentName;
79 } else if (info instanceof ShortcutInfo) {
80 componentName = info.getTargetComponent();
81 } else if (info instanceof PendingAddItemInfo) {
82 componentName = ((PendingAddItemInfo) info).componentName;
83 } else if (info instanceof LauncherAppWidgetInfo) {
84 componentName = ((LauncherAppWidgetInfo) info).providerName;
85 }
86 if (componentName != null) {
87 try {
88 LauncherAppsCompat.getInstance(context)
89 .showAppDetailsForProfile(componentName, info.user, sourceBounds, opts);
90 return componentName;
91 } catch (SecurityException | ActivityNotFoundException e) {
92 Toast.makeText(context, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
93 Log.e(TAG, "Unable to launch settings", e);
94 }
95 }
96 return null;
97 }
98
99 @Override
100 public int getAccessibilityAction() {
101 return LauncherAccessibilityDelegate.INFO;
102 }
103
104 @Override
105 protected boolean supportsDrop(ItemInfo info) {
106 // Only show the App Info drop target if developer settings are enabled.
107 boolean developmentSettingsEnabled = Settings.Global.getInt(
108 getContext().getContentResolver(),
109 Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) == 1;
110 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);
119 }
120}