blob: 74b9e3d99fd6c095aebf3804e936ecf9c0990ed1 [file] [log] [blame]
Winson Chung55cef262010-10-28 14:14:18 -07001/*
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
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung55cef262010-10-28 14:14:18 -070018
Winson Chung68846fd2010-10-29 11:00:27 -070019import 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
Michael Jurka34c2e6c2013-12-13 16:07:45 +010034import java.util.List;
Winson Chung68846fd2010-10-29 11:00:27 -070035
Winson Chung55cef262010-10-28 14:14:18 -070036
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 =
Daniel Sandler325dc232013-06-05 22:57:57 -040043 "com.android.launcher3.action.INSTALL_WIDGET";
Winson Chung68846fd2010-10-29 11:00:27 -070044 public static final String ACTION_SUPPORTS_CLIPDATA_MIMETYPE =
Daniel Sandler325dc232013-06-05 22:57:57 -040045 "com.android.launcher3.action.SUPPORTS_CLIPDATA_MIMETYPE";
Winson Chung55cef262010-10-28 14:14:18 -070046
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 =
Daniel Sandler325dc232013-06-05 22:57:57 -040050 "com.android.launcher3.extra.widget.COMPONENT";
Winson Chung68846fd2010-10-29 11:00:27 -070051 public static final String EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE =
Daniel Sandler325dc232013-06-05 22:57:57 -040052 "com.android.launcher3.extra.widget.CONFIGURATION_DATA_MIME_TYPE";
Winson Chung55cef262010-10-28 14:14:18 -070053 public static final String EXTRA_APPWIDGET_CONFIGURATION_DATA =
Daniel Sandler325dc232013-06-05 22:57:57 -040054 "com.android.launcher3.extra.widget.CONFIGURATION_DATA";
Winson Chung68846fd2010-10-29 11:00:27 -070055
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;
Winson Chung68846fd2010-10-29 11:00:27 -070078 private int mTargetLayoutScreen;
79 private int[] mTargetLayoutPos;
80
81 public WidgetListAdapter(Launcher l, String mimeType, ClipData data,
Michael Jurka414300a2013-08-27 15:42:35 +020082 List<WidgetMimeTypeHandlerData> list, int targetScreen, int[] targetPos) {
Winson Chung68846fd2010-10-29 11:00:27 -070083 mLauncher = l;
84 mMimeType = mimeType;
85 mClipData = data;
86 mActivities = list;
Winson Chung68846fd2010-10-29 11:00:27 -070087 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];
Michael Jurka414300a2013-08-27 15:42:35 +0200147 CellLayout.rectToCell(widgetInfo.minWidth, widgetInfo.minHeight, widgetSpan);
Winson Chung68846fd2010-10-29 11:00:27 -0700148 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) {
Winson Chung68846fd2010-10-29 11:00:27 -0700182 final AppWidgetProviderInfo widgetInfo = mActivities.get(which).widgetInfo;
183
184 final PendingAddWidgetInfo createInfo = new PendingAddWidgetInfo(widgetInfo, mMimeType,
Michael Jurkac9d95c52011-08-29 14:03:34 -0700185 mClipData);
Winson Chung3d503fb2011-07-13 17:25:49 -0700186 mLauncher.addAppWidgetFromDrop(createInfo, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Adam Cohend41fbf52012-02-16 23:53:59 -0800187 mTargetLayoutScreen, null, null, mTargetLayoutPos);
Winson Chung68846fd2010-10-29 11:00:27 -0700188 }
189 }
Winson Chung55cef262010-10-28 14:14:18 -0700190}