blob: a65753c66f388f907904e242fac59bb447556ec3 [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;
Pankaj Garg07b2fd92015-07-15 12:07:37 -070020import android.content.res.Resources;
The Android Open Source Project0c908882009-03-03 19:32:16 -080021import android.database.Cursor;
The Android Open Source Project0c908882009-03-03 19:32:16 -080022import android.graphics.Bitmap;
Dave Tharp490ca7f2015-06-16 11:17:34 -070023import android.graphics.BitmapFactory;
John Reckf94abcf2011-10-10 15:33:48 -070024import android.graphics.drawable.BitmapDrawable;
John Reck8af90642010-11-23 10:27:29 -080025import android.view.LayoutInflater;
The Android Open Source Project0c908882009-03-03 19:32:16 -080026import android.view.View;
John Reck8af90642010-11-23 10:27:29 -080027import android.view.ViewGroup;
The Android Open Source Project0c908882009-03-03 19:32:16 -080028
Dave Tharp9a082b12015-06-19 08:46:57 -070029import com.android.browser.mdm.EditBookmarksRestriction;
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 CharSequence getTitle(Cursor cursor) {
John Reckd18ac4b2012-04-12 17:27:34 -070065 int type = cursor.getInt(BookmarksLoader.COLUMN_INDEX_TYPE);
66 switch (type) {
Pankaj Garg07b2fd92015-07-15 12:07:37 -070067 case Bookmarks.BOOKMARK_TYPE_OTHER_FOLDER:
68 return mContext.getText(R.string.other_bookmarks);
John Reckd18ac4b2012-04-12 17:27:34 -070069 }
70 return cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE);
71 }
72
Pankaj Garg07b2fd92015-07-15 12:07:37 -070073 @Override
74 public void bindView(View view, BrowserBookmarksAdapterItem item) {
75 BookmarkContainer c = (BookmarkContainer) view;
76
77 // we need to set this to handle rotation and other configuration change events.
78 // (if the padding didn't change, this is a no op)
79 int padding = mContext.getResources()
John Reckf3828cd2011-06-14 17:21:55 -070080 .getDimensionPixelSize(R.dimen.combo_horizontalSpacing);
Pankaj Garg07b2fd92015-07-15 12:07:37 -070081 c.setPadding(padding, c.getPaddingTop(), padding, c.getPaddingBottom());
Dave Tharp9a082b12015-06-19 08:46:57 -070082
Pankaj Garg07b2fd92015-07-15 12:07:37 -070083 // configure the main content of the bookmark icon
John Reckf94abcf2011-10-10 15:33:48 -070084 if (item.is_folder) {
Pankaj Garg07b2fd92015-07-15 12:07:37 -070085 c.reConfigureAsFolder(item.title.toString(), "");
86 } else {
87 final Bitmap favicon = (item.thumbnail == null || !item.has_thumbnail) ?
88 null : item.thumbnail.getBitmap();
89 c.reConfigureAsSite(favicon);
Dave Tharp9a082b12015-06-19 08:46:57 -070090 }
91
Pankaj Garg07b2fd92015-07-15 12:07:37 -070092 // configure the label under the bookmark
93 if (item.title != null) {
94 c.setBottomLabelText(item.title.toString());
95 }
96
97 // if the item is managed by mdm or edit bookmark restriction, show a badge
Dave Tharp0eaec992015-07-08 10:58:04 -070098 if (item.title != null &&
99 (item.is_mdm_managed || EditBookmarksRestriction.getInstance().isEnabled())) {
Pankaj Garg07b2fd92015-07-15 12:07:37 -0700100 c.setOverlayBadge(item.is_mdm_managed ? R.drawable.img_deco_mdm_badge_bright :
101 R.drawable.ic_deco_secure);
102 } else
103 c.setOverlayBadge(0);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800104 }
John Reck8af90642010-11-23 10:27:29 -0800105
John Reck608baa72010-11-24 10:32:28 -0800106 @Override
John Reckf94abcf2011-10-10 15:33:48 -0700107 public BrowserBookmarksAdapterItem getRowObject(Cursor c,
108 BrowserBookmarksAdapterItem item) {
109 if (item == null) {
110 item = new BrowserBookmarksAdapterItem();
111 }
John Reckf94abcf2011-10-10 15:33:48 -0700112 Bitmap thumbnail = item.thumbnail != null ? item.thumbnail.getBitmap() : null;
Pankaj Garg68faf1c2015-06-26 17:07:37 -0700113
John Reckf94abcf2011-10-10 15:33:48 -0700114 thumbnail = BrowserBookmarksPage.getBitmap(c,
Pankaj Garg68faf1c2015-06-26 17:07:37 -0700115 BookmarksLoader.COLUMN_INDEX_TOUCH_ICON, thumbnail);
116 if (thumbnail == null) {
117 thumbnail = BrowserBookmarksPage.getBitmap(c,
118 BookmarksLoader.COLUMN_INDEX_THUMBNAIL, thumbnail);
119 }
John Reck1df6df72012-06-05 11:05:34 -0700120 item.has_thumbnail = thumbnail != null;
John Reckf94abcf2011-10-10 15:33:48 -0700121 if (thumbnail != null
122 && (item.thumbnail == null || item.thumbnail.getBitmap() != thumbnail)) {
123 item.thumbnail = new BitmapDrawable(mContext.getResources(), thumbnail);
124 }
125 item.is_folder = c.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0;
126 item.title = getTitle(c);
127 item.url = c.getString(BookmarksLoader.COLUMN_INDEX_URL);
Dave Tharp490ca7f2015-06-16 11:17:34 -0700128 item.is_mdm_managed = ManagedBookmarksRestriction.getInstance().mDb.isMdmElement(getItemId(c));
John Reckf94abcf2011-10-10 15:33:48 -0700129 return item;
130 }
131
132 @Override
133 public BrowserBookmarksAdapterItem getLoadingObject() {
134 BrowserBookmarksAdapterItem item = new BrowserBookmarksAdapterItem();
135 return item;
John Reck608baa72010-11-24 10:32:28 -0800136 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800137}