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
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.compat.LauncherAppsCompat;
30import com.android.launcher3.util.Themes;
31
32public class InfoDropTarget extends UninstallDropTarget {
33
34    private static final String TAG = "InfoDropTarget";
35
36    public InfoDropTarget(Context context, AttributeSet attrs) {
37        this(context, attrs, 0);
38    }
39
40    public InfoDropTarget(Context context, AttributeSet attrs, int defStyle) {
41        super(context, attrs, defStyle);
42    }
43
44    @Override
45    protected void setupUi() {
46        // Get the hover color
47        mHoverColor = Themes.getColorAccent(getContext());
48        setDrawable(R.drawable.ic_info_shadow);
49    }
50
51    @Override
52    public void completeDrop(DragObject d) {
53        DropTargetResultCallback callback = d.dragSource instanceof DropTargetResultCallback
54                ? (DropTargetResultCallback) d.dragSource : null;
55        startDetailsActivityForInfo(d.dragInfo, mLauncher, callback);
56    }
57
58    /**
59     * @return Whether the activity was started.
60     */
61    public static boolean startDetailsActivityForInfo(
62            ItemInfo info, Launcher launcher, DropTargetResultCallback callback) {
63        return startDetailsActivityForInfo(info, launcher, callback, null, null);
64    }
65
66    public static boolean startDetailsActivityForInfo(ItemInfo info, Launcher launcher,
67            DropTargetResultCallback callback, Rect sourceBounds, Bundle opts) {
68        if (info instanceof PromiseAppInfo) {
69            PromiseAppInfo promiseAppInfo = (PromiseAppInfo) info;
70            launcher.startActivity(promiseAppInfo.getMarketIntent());
71            return true;
72        }
73        boolean result = false;
74        ComponentName componentName = null;
75        if (info instanceof AppInfo) {
76            componentName = ((AppInfo) info).componentName;
77        } else if (info instanceof ShortcutInfo) {
78            componentName = info.getTargetComponent();
79        } else if (info instanceof PendingAddItemInfo) {
80            componentName = ((PendingAddItemInfo) info).componentName;
81        } else if (info instanceof LauncherAppWidgetInfo) {
82            componentName = ((LauncherAppWidgetInfo) info).providerName;
83        }
84        if (componentName != null) {
85            try {
86                LauncherAppsCompat.getInstance(launcher)
87                        .showAppDetailsForProfile(componentName, info.user, sourceBounds, opts);
88                result = true;
89            } catch (SecurityException | ActivityNotFoundException e) {
90                Toast.makeText(launcher, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
91                Log.e(TAG, "Unable to launch settings", e);
92            }
93        }
94
95        if (callback != null) {
96            sendUninstallResult(launcher, result, componentName, info.user, callback);
97        }
98        return result;
99    }
100
101    @Override
102    protected boolean supportsDrop(DragSource source, ItemInfo info) {
103        return source.supportsAppInfoDropTarget() && supportsDrop(getContext(), info);
104    }
105
106    public static boolean supportsDrop(Context context, ItemInfo info) {
107        // Only show the App Info drop target if developer settings are enabled.
108        boolean developmentSettingsEnabled = Settings.Global.getInt(context.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}
121