blob: ae9c4915cd8a44cc10fa9d94ba2cf3593191a6ed [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
2 * Copyright (C) 2006 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
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
The Android Open Source Project0c908882009-03-03 19:32:16 -080018
Jeff Hamilton84029622010-08-05 14:29:28 -050019import android.content.Context;
The Android Open Source Project0c908882009-03-03 19:32:16 -080020import android.database.Cursor;
The Android Open Source Project0c908882009-03-03 19:32:16 -080021import android.graphics.Bitmap;
Dave Tharp490ca7f2015-06-16 11:17:34 -070022import android.graphics.BitmapFactory;
John Reckf94abcf2011-10-10 15:33:48 -070023import android.graphics.drawable.BitmapDrawable;
John Reck8af90642010-11-23 10:27:29 -080024import android.view.LayoutInflater;
The Android Open Source Project0c908882009-03-03 19:32:16 -080025import android.view.View;
John Reck8af90642010-11-23 10:27:29 -080026import android.view.ViewGroup;
John Reck23c61dd2011-09-28 15:02:24 -070027import android.widget.ImageView.ScaleType;
Leon Scroggins892df312009-07-14 14:48:02 -040028import android.widget.TextView;
The Android Open Source Project0c908882009-03-03 19:32:16 -080029
Dave Tharp490ca7f2015-06-16 11:17:34 -070030import com.android.browser.mdm.ManagedBookmarksRestriction;
Bijan Amirzada41242f22014-03-21 12:12:18 -070031import com.android.browser.platformsupport.BrowserContract.Bookmarks;
32import com.android.browser.util.ThreadedCursorAdapter;
33import com.android.browser.view.BookmarkContainer;
Kulanthaivel Palanichamya94b4d32015-01-29 19:32:14 -080034import com.android.browser.view.BookmarkThumbImageView;
John Reckf94abcf2011-10-10 15:33:48 -070035
36public class BrowserBookmarksAdapter extends
37 ThreadedCursorAdapter<BrowserBookmarksAdapterItem> {
38
Dave Tharp490ca7f2015-06-16 11:17:34 -070039 private static final String TAG = "BrowserBookmarksAdapter";
John Reck8af90642010-11-23 10:27:29 -080040 LayoutInflater mInflater;
John Reckf94abcf2011-10-10 15:33:48 -070041 Context mContext;
John Reck8af90642010-11-23 10:27:29 -080042
The Android Open Source Project0c908882009-03-03 19:32:16 -080043 /**
44 * Create a new BrowserBookmarksAdapter.
The Android Open Source Project0c908882009-03-03 19:32:16 -080045 */
John Reckaf991f92012-04-24 15:31:16 -070046 public BrowserBookmarksAdapter(Context context) {
Jeff Hamilton84029622010-08-05 14:29:28 -050047 // Make sure to tell the CursorAdapter to avoid the observer and auto-requery
48 // since the Loader will do that for us.
John Reckf94abcf2011-10-10 15:33:48 -070049 super(context, null);
John Reck8af90642010-11-23 10:27:29 -080050 mInflater = LayoutInflater.from(context);
John Reckf94abcf2011-10-10 15:33:48 -070051 mContext = context;
The Android Open Source Project0c908882009-03-03 19:32:16 -080052 }
Jeff Hamilton84029622010-08-05 14:29:28 -050053
54 @Override
John Reck9ee87d92012-05-04 13:53:25 -070055 protected long getItemId(Cursor c) {
56 return c.getLong(BookmarksLoader.COLUMN_INDEX_ID);
57 }
58
59 @Override
John Reckf94abcf2011-10-10 15:33:48 -070060 public View newView(Context context, ViewGroup parent) {
John Reckaf991f92012-04-24 15:31:16 -070061 return mInflater.inflate(R.layout.bookmark_thumbnail, parent, false);
John Reck8af90642010-11-23 10:27:29 -080062 }
63
John Reckf94abcf2011-10-10 15:33:48 -070064 @Override
65 public void bindView(View view, BrowserBookmarksAdapterItem object) {
66 BookmarkContainer container = (BookmarkContainer) view;
67 container.setIgnoreRequestLayout(true);
John Reckaf991f92012-04-24 15:31:16 -070068 bindGridView(view, mContext, object);
John Reckf94abcf2011-10-10 15:33:48 -070069 container.setIgnoreRequestLayout(false);
70 }
71
John Reckf94abcf2011-10-10 15:33:48 -070072 CharSequence getTitle(Cursor cursor) {
John Reckd18ac4b2012-04-12 17:27:34 -070073 int type = cursor.getInt(BookmarksLoader.COLUMN_INDEX_TYPE);
74 switch (type) {
75 case Bookmarks.BOOKMARK_TYPE_OTHER_FOLDER:
John Reckf94abcf2011-10-10 15:33:48 -070076 return mContext.getText(R.string.other_bookmarks);
John Reckd18ac4b2012-04-12 17:27:34 -070077 }
78 return cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE);
79 }
80
John Reckf94abcf2011-10-10 15:33:48 -070081 void bindGridView(View view, Context context, BrowserBookmarksAdapterItem item) {
John Reckf3828cd2011-06-14 17:21:55 -070082 // We need to set this to handle rotation and other configuration change
83 // events. If the padding didn't change, this is a no op.
84 int padding = context.getResources()
85 .getDimensionPixelSize(R.dimen.combo_horizontalSpacing);
86 view.setPadding(padding, view.getPaddingTop(),
87 padding, view.getPaddingBottom());
Kulanthaivel Palanichamya94b4d32015-01-29 19:32:14 -080088 BookmarkThumbImageView thumb = (BookmarkThumbImageView) view.findViewById(R.id.thumb_image);
Jeff Hamilton84029622010-08-05 14:29:28 -050089 TextView tv = (TextView) view.findViewById(R.id.label);
Dave Tharp490ca7f2015-06-16 11:17:34 -070090 float badgeScale = 0.8f;
Jeff Hamilton84029622010-08-05 14:29:28 -050091
John Reckf94abcf2011-10-10 15:33:48 -070092 tv.setText(item.title);
93 if (item.is_folder) {
Michael Kolbfa314072010-09-17 11:33:58 -070094 // folder
Dave Tharp490ca7f2015-06-16 11:17:34 -070095 if(item.is_mdm_managed) {
96 // build a bitmap that has the mdm badge overlaid
97 Bitmap b = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.thumb_bookmark_widget_folder_holo);
98 Bitmap bm = BrowserBookmarksPage.overlayBookmarkBitmap(b, R.drawable.img_deco_mdm_badge_bright, mContext, badgeScale);
99 thumb.setmAdjustDown(false);
100 thumb.setScaleType(ScaleType.CENTER_INSIDE);
101 thumb.setImageBitmap(bm);
102 }
103 else {
104 thumb.setmAdjustDown(true);
105 thumb.setImageResource(R.drawable.thumb_bookmark_widget_folder_holo);
106 thumb.setScaleType(ScaleType.FIT_END);
107 }
John Reckf94abcf2011-10-10 15:33:48 -0700108 thumb.setBackground(null);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800109 } else {
John Reck1df6df72012-06-05 11:05:34 -0700110 if (item.thumbnail == null || !item.has_thumbnail) {
Dave Tharp490ca7f2015-06-16 11:17:34 -0700111 if(item.is_mdm_managed) {
112 Bitmap b = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.browser_thumbnail);
113 Bitmap bm = BrowserBookmarksPage.overlayBookmarkBitmap(b, R.drawable.img_deco_mdm_badge_bright, mContext, badgeScale);
114 thumb.setmAdjustDown(false);
115 thumb.setScaleType(ScaleType.CENTER_INSIDE);
116 thumb.setImageBitmap(bm);
117 }
118 else {
119 thumb.setmAdjustDown(true);
120 thumb.setScaleType(ScaleType.CENTER_CROP);
121 thumb.setImageResource(R.drawable.browser_thumbnail);
122 }
Michael Kolbfa314072010-09-17 11:33:58 -0700123 } else {
Dave Tharp490ca7f2015-06-16 11:17:34 -0700124 if (item.is_mdm_managed) {
125 Bitmap b = item.thumbnail.getBitmap();
126 Bitmap bm = BrowserBookmarksPage.overlayBookmarkBitmap(b, R.drawable.img_deco_mdm_badge_bright, mContext, badgeScale);
127 thumb.setmAdjustDown(false);
128 thumb.setScaleType(ScaleType.CENTER_INSIDE);
129 thumb.setImageBitmap(bm);
130 } else {
131 thumb.setmAdjustDown(true);
132 thumb.setImageDrawable(item.thumbnail);
133 }
Michael Kolbfa314072010-09-17 11:33:58 -0700134 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800135 }
136 }
John Reck8af90642010-11-23 10:27:29 -0800137
John Reck608baa72010-11-24 10:32:28 -0800138 @Override
John Reckf94abcf2011-10-10 15:33:48 -0700139 public BrowserBookmarksAdapterItem getRowObject(Cursor c,
140 BrowserBookmarksAdapterItem item) {
141 if (item == null) {
142 item = new BrowserBookmarksAdapterItem();
143 }
John Reckf94abcf2011-10-10 15:33:48 -0700144 Bitmap thumbnail = item.thumbnail != null ? item.thumbnail.getBitmap() : null;
John Reckf94abcf2011-10-10 15:33:48 -0700145 thumbnail = BrowserBookmarksPage.getBitmap(c,
146 BookmarksLoader.COLUMN_INDEX_THUMBNAIL, thumbnail);
John Reck1df6df72012-06-05 11:05:34 -0700147 item.has_thumbnail = thumbnail != null;
John Reckf94abcf2011-10-10 15:33:48 -0700148 if (thumbnail != null
149 && (item.thumbnail == null || item.thumbnail.getBitmap() != thumbnail)) {
150 item.thumbnail = new BitmapDrawable(mContext.getResources(), thumbnail);
151 }
152 item.is_folder = c.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0;
153 item.title = getTitle(c);
154 item.url = c.getString(BookmarksLoader.COLUMN_INDEX_URL);
Dave Tharp490ca7f2015-06-16 11:17:34 -0700155 item.is_mdm_managed = ManagedBookmarksRestriction.getInstance().mDb.isMdmElement(getItemId(c));
John Reckf94abcf2011-10-10 15:33:48 -0700156 return item;
157 }
158
159 @Override
160 public BrowserBookmarksAdapterItem getLoadingObject() {
161 BrowserBookmarksAdapterItem item = new BrowserBookmarksAdapterItem();
162 return item;
John Reck608baa72010-11-24 10:32:28 -0800163 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800164}