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
17package com.android.browser.addbookmark;
18
19import com.android.browser.R;
20
21import android.content.Context;
22import android.graphics.drawable.Drawable;
23import android.view.Gravity;
24import android.view.LayoutInflater;
25import android.view.View;
26import android.view.ViewGroup;
27import android.widget.BaseAdapter;
28import android.widget.TextView;
29
30/**
31 * SpinnerAdapter used in the AddBookmarkPage to select where to save a
32 * bookmark/folder.
33 */
34public 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
49    public FolderSpinnerAdapter(Context context, boolean includeHomeScreen) {
50        mIncludeHomeScreen = includeHomeScreen;
51        mContext = context;
52        mInflater = LayoutInflater.from(mContext);
53    }
54
55    public void addRecentFolder(long folderId, String folderName) {
56        mIncludesRecentFolder = true;
57        mRecentFolderId = folderId;
58        mRecentFolderName = folderName;
59    }
60
61    public long recentFolderId() { return mRecentFolderId; }
62
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
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
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
126    public int getCount() {
127        int count = 2;
128        if (mIncludeHomeScreen) count++;
129        if (mIncludesRecentFolder) count++;
130        return count;
131    }
132
133    @Override
134    public Object getItem(int position) {
135        return null;
136    }
137
138    @Override
139    public long getItemId(int position) {
140        long id = position;
141        if (!mIncludeHomeScreen) {
142            id++;
143        }
144        return id;
145    }
146
147    @Override
148    public boolean hasStableIds() {
149        return true;
150    }
151
152    public void setOtherFolderDisplayText(String parentTitle) {
153        mOtherFolderDisplayText = parentTitle;
154        notifyDataSetChanged();
155    }
156
157    public void clearRecentFolder() {
158        if (mIncludesRecentFolder) {
159            mIncludesRecentFolder = false;
160            notifyDataSetChanged();
161        }
162    }
163}
164