1 /* 2 * Copyright (C) 2011 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.browser.addbookmark; 18 19 import com.android.browser.R; 20 21 import android.content.Context; 22 import android.graphics.drawable.Drawable; 23 import android.view.Gravity; 24 import android.view.LayoutInflater; 25 import android.view.View; 26 import android.view.ViewGroup; 27 import android.widget.BaseAdapter; 28 import android.widget.TextView; 29 30 /** 31 * SpinnerAdapter used in the AddBookmarkPage to select where to save a 32 * bookmark/folder. 33 */ 34 public class FolderSpinnerAdapter extends BaseAdapter { 35 36 public static final int HOME_SCREEN = 0; 37 public static final int ROOT_FOLDER = 1; 38 public static final int OTHER_FOLDER = 2; 39 public static final int RECENT_FOLDER = 3; 40 41 private boolean mIncludeHomeScreen; 42 private boolean mIncludesRecentFolder; 43 private long mRecentFolderId; 44 private String mRecentFolderName; 45 private LayoutInflater mInflater; 46 private Context mContext; 47 private String mOtherFolderDisplayText; 48 FolderSpinnerAdapter(Context context, boolean includeHomeScreen)49 public FolderSpinnerAdapter(Context context, boolean includeHomeScreen) { 50 mIncludeHomeScreen = includeHomeScreen; 51 mContext = context; 52 mInflater = LayoutInflater.from(mContext); 53 } 54 addRecentFolder(long folderId, String folderName)55 public void addRecentFolder(long folderId, String folderName) { 56 mIncludesRecentFolder = true; 57 mRecentFolderId = folderId; 58 mRecentFolderName = folderName; 59 } 60 recentFolderId()61 public long recentFolderId() { return mRecentFolderId; } 62 bindView(int position, View view, boolean isDropDown)63 private void bindView(int position, View view, boolean isDropDown) { 64 int labelResource; 65 int drawableResource; 66 if (!mIncludeHomeScreen) { 67 position++; 68 } 69 switch (position) { 70 case HOME_SCREEN: 71 labelResource = R.string.add_to_homescreen_menu_option; 72 drawableResource = R.drawable.ic_home_holo_dark; 73 break; 74 case ROOT_FOLDER: 75 labelResource = R.string.add_to_bookmarks_menu_option; 76 drawableResource = R.drawable.ic_bookmarks_holo_dark; 77 break; 78 case RECENT_FOLDER: 79 // Fall through and use the same icon resource 80 case OTHER_FOLDER: 81 labelResource = R.string.add_to_other_folder_menu_option; 82 drawableResource = R.drawable.ic_folder_holo_dark; 83 break; 84 default: 85 labelResource = 0; 86 drawableResource = 0; 87 // assert 88 break; 89 } 90 TextView textView = (TextView) view; 91 if (position == RECENT_FOLDER) { 92 textView.setText(mRecentFolderName); 93 } else if (position == OTHER_FOLDER && !isDropDown 94 && mOtherFolderDisplayText != null) { 95 textView.setText(mOtherFolderDisplayText); 96 } else { 97 textView.setText(labelResource); 98 } 99 textView.setGravity(Gravity.CENTER_VERTICAL); 100 Drawable drawable = mContext.getResources().getDrawable(drawableResource); 101 textView.setCompoundDrawablesWithIntrinsicBounds(drawable, null, 102 null, null); 103 } 104 105 @Override getDropDownView(int position, View convertView, ViewGroup parent)106 public View getDropDownView(int position, View convertView, ViewGroup parent) { 107 if (convertView == null) { 108 convertView = mInflater.inflate( 109 android.R.layout.simple_spinner_dropdown_item, parent, false); 110 } 111 bindView(position, convertView, true); 112 return convertView; 113 } 114 115 @Override getView(int position, View convertView, ViewGroup parent)116 public View getView(int position, View convertView, ViewGroup parent) { 117 if (convertView == null) { 118 convertView = mInflater.inflate(android.R.layout.simple_spinner_item, 119 parent, false); 120 } 121 bindView(position, convertView, false); 122 return convertView; 123 } 124 125 @Override getCount()126 public int getCount() { 127 int count = 2; 128 if (mIncludeHomeScreen) count++; 129 if (mIncludesRecentFolder) count++; 130 return count; 131 } 132 133 @Override getItem(int position)134 public Object getItem(int position) { 135 return null; 136 } 137 138 @Override getItemId(int position)139 public long getItemId(int position) { 140 long id = position; 141 if (!mIncludeHomeScreen) { 142 id++; 143 } 144 return id; 145 } 146 147 @Override hasStableIds()148 public boolean hasStableIds() { 149 return true; 150 } 151 setOtherFolderDisplayText(String parentTitle)152 public void setOtherFolderDisplayText(String parentTitle) { 153 mOtherFolderDisplayText = parentTitle; 154 notifyDataSetChanged(); 155 } 156 clearRecentFolder()157 public void clearRecentFolder() { 158 if (mIncludesRecentFolder) { 159 mIncludesRecentFolder = false; 160 notifyDataSetChanged(); 161 } 162 } 163 } 164