1/*
2 * Copyright (C) 2010 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.appwidget.AppWidgetProviderInfo;
20import android.content.ClipData;
21import android.content.Context;
22import android.content.DialogInterface;
23import android.content.pm.PackageManager;
24import android.content.pm.ResolveInfo;
25import android.database.DataSetObserver;
26import android.graphics.drawable.Drawable;
27import android.view.LayoutInflater;
28import android.view.View;
29import android.view.ViewGroup;
30import android.widget.ImageView;
31import android.widget.ListAdapter;
32import android.widget.TextView;
33
34import java.util.List;
35
36
37/**
38 * We will likely flesh this out later, to handle allow external apps to place widgets, but for now,
39 * we just want to expose the action around for checking elsewhere.
40 */
41public class InstallWidgetReceiver {
42    public static final String ACTION_INSTALL_WIDGET =
43            "com.android.launcher3.action.INSTALL_WIDGET";
44    public static final String ACTION_SUPPORTS_CLIPDATA_MIMETYPE =
45            "com.android.launcher3.action.SUPPORTS_CLIPDATA_MIMETYPE";
46
47    // Currently not exposed.  Put into Intent when we want to make it public.
48    // TEMP: Should we call this "EXTRA_APPWIDGET_PROVIDER"?
49    public static final String EXTRA_APPWIDGET_COMPONENT =
50        "com.android.launcher3.extra.widget.COMPONENT";
51    public static final String EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE =
52        "com.android.launcher3.extra.widget.CONFIGURATION_DATA_MIME_TYPE";
53    public static final String EXTRA_APPWIDGET_CONFIGURATION_DATA =
54        "com.android.launcher3.extra.widget.CONFIGURATION_DATA";
55
56    /**
57     * A simple data class that contains per-item information that the adapter below can reference.
58     */
59    public static class WidgetMimeTypeHandlerData {
60        public ResolveInfo resolveInfo;
61        public AppWidgetProviderInfo widgetInfo;
62
63        public WidgetMimeTypeHandlerData(ResolveInfo rInfo, AppWidgetProviderInfo wInfo) {
64            resolveInfo = rInfo;
65            widgetInfo = wInfo;
66        }
67    }
68
69    /**
70     * The ListAdapter which presents all the valid widgets that can be created for a given drop.
71     */
72    public static class WidgetListAdapter implements ListAdapter, DialogInterface.OnClickListener {
73        private LayoutInflater mInflater;
74        private Launcher mLauncher;
75        private String mMimeType;
76        private ClipData mClipData;
77        private List<WidgetMimeTypeHandlerData> mActivities;
78        private int mTargetLayoutScreen;
79        private int[] mTargetLayoutPos;
80
81        public WidgetListAdapter(Launcher l, String mimeType, ClipData data,
82                List<WidgetMimeTypeHandlerData> list, int targetScreen, int[] targetPos) {
83            mLauncher = l;
84            mMimeType = mimeType;
85            mClipData = data;
86            mActivities = list;
87            mTargetLayoutScreen = targetScreen;
88            mTargetLayoutPos = targetPos;
89        }
90
91        @Override
92        public void registerDataSetObserver(DataSetObserver observer) {
93        }
94
95        @Override
96        public void unregisterDataSetObserver(DataSetObserver observer) {
97        }
98
99        @Override
100        public int getCount() {
101            return mActivities.size();
102        }
103
104        @Override
105        public Object getItem(int position) {
106            return null;
107        }
108
109        @Override
110        public long getItemId(int position) {
111            return position;
112        }
113
114        @Override
115        public boolean hasStableIds() {
116            return true;
117        }
118
119        @Override
120        public View getView(int position, View convertView, ViewGroup parent) {
121            final Context context = parent.getContext();
122            final PackageManager packageManager = context.getPackageManager();
123
124            // Lazy-create inflater
125            if (mInflater == null) {
126                mInflater = LayoutInflater.from(context);
127            }
128
129            // Use the convert-view where possible
130            if (convertView == null) {
131                convertView = mInflater.inflate(R.layout.external_widget_drop_list_item, parent,
132                        false);
133            }
134
135            final WidgetMimeTypeHandlerData data = mActivities.get(position);
136            final ResolveInfo resolveInfo = data.resolveInfo;
137            final AppWidgetProviderInfo widgetInfo = data.widgetInfo;
138
139            // Set the icon
140            Drawable d = resolveInfo.loadIcon(packageManager);
141            ImageView i = (ImageView) convertView.findViewById(R.id.provider_icon);
142            i.setImageDrawable(d);
143
144            // Set the text
145            final CharSequence component = resolveInfo.loadLabel(packageManager);
146            final int[] widgetSpan = new int[2];
147            CellLayout.rectToCell(widgetInfo.minWidth, widgetInfo.minHeight, widgetSpan);
148            TextView t = (TextView) convertView.findViewById(R.id.provider);
149            t.setText(context.getString(R.string.external_drop_widget_pick_format,
150                    component, widgetSpan[0], widgetSpan[1]));
151
152            return convertView;
153        }
154
155        @Override
156        public int getItemViewType(int position) {
157            return 0;
158        }
159
160        @Override
161        public int getViewTypeCount() {
162            return 1;
163        }
164
165        @Override
166        public boolean isEmpty() {
167            return mActivities.isEmpty();
168        }
169
170        @Override
171        public boolean areAllItemsEnabled() {
172            return false;
173        }
174
175        @Override
176        public boolean isEnabled(int position) {
177            return true;
178        }
179
180        @Override
181        public void onClick(DialogInterface dialog, int which) {
182            final AppWidgetProviderInfo widgetInfo = mActivities.get(which).widgetInfo;
183
184            final PendingAddWidgetInfo createInfo = new PendingAddWidgetInfo(widgetInfo, mMimeType,
185                    mClipData);
186            mLauncher.addAppWidgetFromDrop(createInfo, LauncherSettings.Favorites.CONTAINER_DESKTOP,
187                    mTargetLayoutScreen, null, null, mTargetLayoutPos);
188        }
189    }
190}
191