blob: f6ab21eb41f0ec8f5376a37a645a39b33a80ccf4 [file] [log] [blame]
Hyunyoung Song3f471442015-04-08 19:01:34 -07001/*
2 * Copyright (C) 2015 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 */
16package com.android.launcher3.widget;
17
18import android.content.Context;
19import android.content.pm.ResolveInfo;
20import android.support.v7.widget.RecyclerView.Adapter;
21import android.util.Log;
22import android.view.LayoutInflater;
23import android.view.View;
24import android.view.ViewGroup;
25import android.widget.ImageView;
26import android.widget.TextView;
27
28import com.android.launcher3.IconCache;
29import com.android.launcher3.Launcher;
30import com.android.launcher3.LauncherAppState;
31import com.android.launcher3.LauncherAppWidgetProviderInfo;
32import com.android.launcher3.R;
33import com.android.launcher3.WidgetPreviewLoader;
34import com.android.launcher3.compat.UserHandleCompat;
35
36import java.util.List;
37
38/**
39 * List view adapter for the widget tray.
40 *
41 * <p>Memory vs. Performance:
42 * The less number of types of views are inserted into a {@link RecyclerView}, the more recycling
43 * happens and less memory is consumed. {@link #getItemViewType} was not overridden as there is
44 * only a single type of view.
45 */
46public class WidgetsListAdapter extends Adapter<WidgetsRowViewHolder> {
47
48 private static final String TAG = "WidgetsListAdapter";
49 private static final boolean DEBUG = false;
50
51 private Context mContext;
52 private Launcher mLauncher;
53 private LayoutInflater mLayoutInflater;
54 private IconCache mIconCache;
55
56 private WidgetsModel mWidgetsModel;
57 private WidgetPreviewLoader mWidgetPreviewLoader;
58
Hyunyoung Song3f471442015-04-08 19:01:34 -070059 private View.OnClickListener mIconClickListener;
60 private View.OnLongClickListener mIconLongClickListener;
61
Hyunyoung Song3f471442015-04-08 19:01:34 -070062 public WidgetsListAdapter(Context context,
Hyunyoung Song3f471442015-04-08 19:01:34 -070063 View.OnClickListener iconClickListener,
64 View.OnLongClickListener iconLongClickListener,
65 Launcher launcher) {
66 mLayoutInflater = LayoutInflater.from(context);
67 mContext = context;
68
Hyunyoung Song3f471442015-04-08 19:01:34 -070069 mIconClickListener = iconClickListener;
70 mIconLongClickListener = iconLongClickListener;
71
72 mLauncher = launcher;
73 mIconCache = LauncherAppState.getInstance().getIconCache();
74 }
75
76 public void setWidgetsModel(WidgetsModel w) {
77 mWidgetsModel = w;
78 }
79
80 @Override
81 public int getItemCount() {
82 return mWidgetsModel.getPackageSize();
83 }
84
85 @Override
86 public void onBindViewHolder(WidgetsRowViewHolder holder, int pos) {
Hyunyoung Song4e8fb912015-04-11 15:44:32 -070087 List<Object> infoList = mWidgetsModel.getSortedWidgets(pos);
Hyunyoung Song3f471442015-04-08 19:01:34 -070088
89 ViewGroup row = ((ViewGroup) holder.getContent().findViewById(R.id.widgets_cell_list));
90 if (DEBUG) {
91 Log.d(TAG, String.format(
Hyunyoung Song4e8fb912015-04-11 15:44:32 -070092 "onBindViewHolder [pos=%d, widget#=%d, row.getChildCount=%d]",
93 pos, infoList.size(), row.getChildCount()));
Hyunyoung Song3f471442015-04-08 19:01:34 -070094 }
95
96 // Add more views.
97 // if there are too many, hide them.
98 int diff = infoList.size() - row.getChildCount();
99 if (diff > 0) {
100 for (int i = 0; i < diff; i++) {
101 WidgetCell widget = new WidgetCell(mContext);
102 widget = (WidgetCell) mLayoutInflater.inflate(
103 R.layout.widget_cell, row, false);
104
105 // set up touch.
106 widget.setOnClickListener(mIconClickListener);
107 widget.setOnLongClickListener(mIconLongClickListener);
Hyunyoung Song3f471442015-04-08 19:01:34 -0700108 row.addView(widget);
109 }
110 } else if (diff < 0) {
111 for (int i=infoList.size() ; i < row.getChildCount(); i++) {
112 row.getChildAt(i).setVisibility(View.GONE);
113 }
114 }
115
116 // Bind the views in the application info section.
Hyunyoung Song4e8fb912015-04-11 15:44:32 -0700117 PackageItemInfo infoOut = mWidgetsModel.getPackageItemInfo(pos);
Hyunyoung Song3f471442015-04-08 19:01:34 -0700118 if (infoOut.usingLowResIcon) {
Hyunyoung Song4e8fb912015-04-11 15:44:32 -0700119 // TODO(hyunyoungs): call this in none UI thread in the same way as BubbleTextView.
120 mIconCache.getTitleAndIconForApp(infoOut.packageName,
121 UserHandleCompat.myUserHandle(), false /* useLowResIcon */, infoOut);
Hyunyoung Song3f471442015-04-08 19:01:34 -0700122 }
123 ((TextView) holder.getContent().findViewById(R.id.section)).setText(infoOut.title);
124 ImageView iv = (ImageView) holder.getContent().findViewById(R.id.section_image);
125 iv.setImageBitmap(infoOut.iconBitmap);
126
127 // Bind the view in the widget horizontal tray region.
128 for (int i=0; i < infoList.size(); i++) {
129 WidgetCell widget = (WidgetCell) row.getChildAt(i);
Hyunyoung Song4e8fb912015-04-11 15:44:32 -0700130 widget.reset();
131 if (getWidgetPreviewLoader() == null) {
Hyunyoung Song3f471442015-04-08 19:01:34 -0700132 return;
133 }
134 if (infoList.get(i) instanceof LauncherAppWidgetProviderInfo) {
135 LauncherAppWidgetProviderInfo info = (LauncherAppWidgetProviderInfo) infoList.get(i);
136 PendingAddWidgetInfo pawi = new PendingAddWidgetInfo(info, null);
137 widget.setTag(pawi);
138 widget.applyFromAppWidgetProviderInfo(info, -1, mWidgetPreviewLoader);
139 } else if (infoList.get(i) instanceof ResolveInfo) {
140 ResolveInfo info = (ResolveInfo) infoList.get(i);
141 PendingAddShortcutInfo pasi = new PendingAddShortcutInfo(info.activityInfo);
142 widget.setTag(pasi);
143 widget.applyFromResolveInfo(mLauncher.getPackageManager(), info, mWidgetPreviewLoader);
144 }
145 widget.setVisibility(View.VISIBLE);
146 widget.ensurePreview();
147 }
Hyunyoung Song3f471442015-04-08 19:01:34 -0700148 }
149
150 @Override
151 public WidgetsRowViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
152 if (DEBUG) {
153 Log.v(TAG, String.format("\nonCreateViewHolder, [widget#=%d]", viewType));
154 }
155
156 ViewGroup container = (ViewGroup) mLayoutInflater.inflate(
157 R.layout.widgets_list_row_view, parent, false);
158 return new WidgetsRowViewHolder(container);
159 }
160
161 @Override
162 public long getItemId(int pos) {
163 return pos;
164 }
165
166 private WidgetPreviewLoader getWidgetPreviewLoader() {
167 if (mWidgetPreviewLoader == null) {
168 mWidgetPreviewLoader = LauncherAppState.getInstance().getWidgetCache();
169 }
170 return mWidgetPreviewLoader;
171 }
Hyunyoung Song3f471442015-04-08 19:01:34 -0700172}