AddAdapter.java revision df2cc41acbfacd576f99483a4af1cda32ebd3d09
1/*
2 * Copyright (C) 2008 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.launcher2;
18
19import android.content.Context;
20import android.content.res.Resources;
21import android.graphics.drawable.Drawable;
22import android.view.LayoutInflater;
23import android.view.View;
24import android.view.ViewGroup;
25import android.widget.BaseAdapter;
26import android.widget.TextView;
27
28import java.util.ArrayList;
29
30import com.android.launcher.R;
31
32/**
33 * Adapter showing the types of items that can be added to a {@link Workspace}.
34 */
35public class AddAdapter extends BaseAdapter {
36
37    private final LayoutInflater mInflater;
38
39    private final ArrayList<ListItem> mItems = new ArrayList<ListItem>();
40
41    public static final int ITEM_SHORTCUT = 0;
42    public static final int ITEM_APPWIDGET = 1;
43    public static final int ITEM_WALLPAPER = 3;
44
45    /**
46     * Specific item in our list.
47     */
48    public class ListItem {
49        public final CharSequence text;
50        public final Drawable image;
51        public final int actionTag;
52
53        public ListItem(Resources res, int textResourceId, int imageResourceId, int actionTag) {
54            text = res.getString(textResourceId);
55            if (imageResourceId != -1) {
56                image = res.getDrawable(imageResourceId);
57            } else {
58                image = null;
59            }
60            this.actionTag = actionTag;
61        }
62    }
63
64    public AddAdapter(Launcher launcher) {
65        super();
66
67        mInflater = (LayoutInflater) launcher.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
68
69        // Create default actions
70        Resources res = launcher.getResources();
71
72        mItems.add(new ListItem(res, R.string.group_shortcuts,
73                R.drawable.ic_launcher_shortcut, ITEM_SHORTCUT));
74
75        mItems.add(new ListItem(res, R.string.group_widgets,
76                R.drawable.ic_launcher_appwidget, ITEM_APPWIDGET));
77
78        mItems.add(new ListItem(res, R.string.group_wallpapers,
79                R.drawable.ic_launcher_wallpaper, ITEM_WALLPAPER));
80
81    }
82
83    public View getView(int position, View convertView, ViewGroup parent) {
84        ListItem item = (ListItem) getItem(position);
85
86        if (convertView == null) {
87            convertView = mInflater.inflate(R.layout.add_list_item, parent, false);
88        }
89
90        TextView textView = (TextView) convertView;
91        textView.setTag(item);
92        textView.setText(item.text);
93        textView.setCompoundDrawablesWithIntrinsicBounds(item.image, null, null, null);
94
95        return convertView;
96    }
97
98    public int getCount() {
99        return mItems.size();
100    }
101
102    public Object getItem(int position) {
103        return mItems.get(position);
104    }
105
106    public long getItemId(int position) {
107        return position;
108    }
109
110}
111