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