blob: fc75050ba40e48487ae721cc94df8c7c1614d2bb [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;
Pankaj Garg21dad562015-07-02 17:17:24 -070027import android.widget.ImageView;
Leon Scroggins892df312009-07-14 14:48:02 -040028import android.widget.TextView;
The Android Open Source Project0c908882009-03-03 19:32:16 -080029
Dave Tharp9a082b12015-06-19 08:46:57 -070030import com.android.browser.mdm.EditBookmarksRestriction;
Dave Tharp490ca7f2015-06-16 11:17:34 -070031import com.android.browser.mdm.ManagedBookmarksRestriction;
Bijan Amirzada41242f22014-03-21 12:12:18 -070032import com.android.browser.platformsupport.BrowserContract.Bookmarks;
33import com.android.browser.util.ThreadedCursorAdapter;
34import com.android.browser.view.BookmarkContainer;
Kulanthaivel Palanichamya94b4d32015-01-29 19:32:14 -080035import com.android.browser.view.BookmarkThumbImageView;
John Reckf94abcf2011-10-10 15:33:48 -070036
37public class BrowserBookmarksAdapter extends
38 ThreadedCursorAdapter<BrowserBookmarksAdapterItem> {
39
Dave Tharp490ca7f2015-06-16 11:17:34 -070040 private static final String TAG = "BrowserBookmarksAdapter";
John Reck8af90642010-11-23 10:27:29 -080041 LayoutInflater mInflater;
John Reckf94abcf2011-10-10 15:33:48 -070042 Context mContext;
John Reck8af90642010-11-23 10:27:29 -080043
The Android Open Source Project0c908882009-03-03 19:32:16 -080044 /**
45 * Create a new BrowserBookmarksAdapter.
The Android Open Source Project0c908882009-03-03 19:32:16 -080046 */
John Reckaf991f92012-04-24 15:31:16 -070047 public BrowserBookmarksAdapter(Context context) {
Jeff Hamilton84029622010-08-05 14:29:28 -050048 // Make sure to tell the CursorAdapter to avoid the observer and auto-requery
49 // since the Loader will do that for us.
John Reckf94abcf2011-10-10 15:33:48 -070050 super(context, null);
John Reck8af90642010-11-23 10:27:29 -080051 mInflater = LayoutInflater.from(context);
John Reckf94abcf2011-10-10 15:33:48 -070052 mContext = context;
The Android Open Source Project0c908882009-03-03 19:32:16 -080053 }
Jeff Hamilton84029622010-08-05 14:29:28 -050054
55 @Override
John Reck9ee87d92012-05-04 13:53:25 -070056 protected long getItemId(Cursor c) {
57 return c.getLong(BookmarksLoader.COLUMN_INDEX_ID);
58 }
59
60 @Override
John Reckf94abcf2011-10-10 15:33:48 -070061 public View newView(Context context, ViewGroup parent) {
John Reckaf991f92012-04-24 15:31:16 -070062 return mInflater.inflate(R.layout.bookmark_thumbnail, parent, false);
John Reck8af90642010-11-23 10:27:29 -080063 }
64
John Reckf94abcf2011-10-10 15:33:48 -070065 @Override
66 public void bindView(View view, BrowserBookmarksAdapterItem object) {
67 BookmarkContainer container = (BookmarkContainer) view;
68 container.setIgnoreRequestLayout(true);
John Reckaf991f92012-04-24 15:31:16 -070069 bindGridView(view, mContext, object);
John Reckf94abcf2011-10-10 15:33:48 -070070 container.setIgnoreRequestLayout(false);
71 }
72
John Reckf94abcf2011-10-10 15:33:48 -070073 CharSequence getTitle(Cursor cursor) {
John Reckd18ac4b2012-04-12 17:27:34 -070074 int type = cursor.getInt(BookmarksLoader.COLUMN_INDEX_TYPE);
75 switch (type) {
76 case Bookmarks.BOOKMARK_TYPE_OTHER_FOLDER:
John Reckf94abcf2011-10-10 15:33:48 -070077 return mContext.getText(R.string.other_bookmarks);
John Reckd18ac4b2012-04-12 17:27:34 -070078 }
79 return cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE);
80 }
81
John Reckf94abcf2011-10-10 15:33:48 -070082 void bindGridView(View view, Context context, BrowserBookmarksAdapterItem item) {
John Reckf3828cd2011-06-14 17:21:55 -070083 // We need to set this to handle rotation and other configuration change
84 // events. If the padding didn't change, this is a no op.
85 int padding = context.getResources()
86 .getDimensionPixelSize(R.dimen.combo_horizontalSpacing);
87 view.setPadding(padding, view.getPaddingTop(),
88 padding, view.getPaddingBottom());
Pankaj Garg21dad562015-07-02 17:17:24 -070089 SiteTileView thumb = (SiteTileView) view.findViewById(R.id.thumb_image);
Jeff Hamilton84029622010-08-05 14:29:28 -050090 TextView tv = (TextView) view.findViewById(R.id.label);
John Reckf94abcf2011-10-10 15:33:48 -070091 tv.setText(item.title);
Dave Tharp9a082b12015-06-19 08:46:57 -070092
93 Bitmap b;
94
Pankaj Garg11015212015-07-06 14:38:43 -070095 thumb.setFloating(false);
96
John Reckf94abcf2011-10-10 15:33:48 -070097 if (item.is_folder) {
Dave Tharp9a082b12015-06-19 08:46:57 -070098 b = BitmapFactory.decodeResource(mContext.getResources(),
Pankaj Garg21dad562015-07-02 17:17:24 -070099 R.drawable.ic_deco_folder_normal);
100 thumb.setFloating(true);
Dave Tharp9a082b12015-06-19 08:46:57 -0700101 }
102 else if (item.thumbnail == null || !item.has_thumbnail) {
103 b = BitmapFactory.decodeResource(mContext.getResources(),
104 R.drawable.browser_thumbnail);
105 }
106 else {
107 b = item.thumbnail.getBitmap();
108 }
109
110 // If the item is managed by mdm or edit bookmark restriction enabled
Dave Tharp0eaec992015-07-08 10:58:04 -0700111 if (item.title != null &&
112 (item.is_mdm_managed || EditBookmarksRestriction.getInstance().isEnabled())) {
113 int containerWidth = view.getResources().getDimensionPixelSize(R.dimen.bookmarkThumbnailWidth);
114 int containerHeight = view.getResources().getDimensionPixelSize(R.dimen.bookmarkThumbnailHeight);
115 Bitmap bm;
Dave Tharp9a082b12015-06-19 08:46:57 -0700116
117 if (item.is_mdm_managed) {
Dave Tharp0eaec992015-07-08 10:58:04 -0700118 bm = BrowserBookmarksPage.overlayBookmarkBitmap(mContext, b,
119 R.drawable.img_deco_mdm_badge_bright,
120 containerWidth, containerHeight, 0.6f, 185, 20);
Dave Tharp490ca7f2015-06-16 11:17:34 -0700121 }
122 else {
Dave Tharp0eaec992015-07-08 10:58:04 -0700123 bm = BrowserBookmarksPage.overlayBookmarkBitmap(mContext, b,
124 R.drawable.ic_deco_secure,
125 containerWidth, containerHeight, 1.7f, 110, 0);
Dave Tharp490ca7f2015-06-16 11:17:34 -0700126 }
Dave Tharp0eaec992015-07-08 10:58:04 -0700127
Pankaj Garg21dad562015-07-02 17:17:24 -0700128 thumb.replaceFavicon(bm);
Dave Tharp9a082b12015-06-19 08:46:57 -0700129 }
130 else {
Pankaj Garg21dad562015-07-02 17:17:24 -0700131 thumb.replaceFavicon(b);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800132 }
Pankaj Garg21dad562015-07-02 17:17:24 -0700133 thumb.setLongClickable(true);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800134 }
John Reck8af90642010-11-23 10:27:29 -0800135
John Reck608baa72010-11-24 10:32:28 -0800136 @Override
John Reckf94abcf2011-10-10 15:33:48 -0700137 public BrowserBookmarksAdapterItem getRowObject(Cursor c,
138 BrowserBookmarksAdapterItem item) {
139 if (item == null) {
140 item = new BrowserBookmarksAdapterItem();
141 }
John Reckf94abcf2011-10-10 15:33:48 -0700142 Bitmap thumbnail = item.thumbnail != null ? item.thumbnail.getBitmap() : null;
Pankaj Garg68faf1c2015-06-26 17:07:37 -0700143
John Reckf94abcf2011-10-10 15:33:48 -0700144 thumbnail = BrowserBookmarksPage.getBitmap(c,
Pankaj Garg68faf1c2015-06-26 17:07:37 -0700145 BookmarksLoader.COLUMN_INDEX_TOUCH_ICON, thumbnail);
146 if (thumbnail == null) {
147 thumbnail = BrowserBookmarksPage.getBitmap(c,
148 BookmarksLoader.COLUMN_INDEX_THUMBNAIL, thumbnail);
149 }
John Reck1df6df72012-06-05 11:05:34 -0700150 item.has_thumbnail = thumbnail != null;
John Reckf94abcf2011-10-10 15:33:48 -0700151 if (thumbnail != null
152 && (item.thumbnail == null || item.thumbnail.getBitmap() != thumbnail)) {
153 item.thumbnail = new BitmapDrawable(mContext.getResources(), thumbnail);
154 }
155 item.is_folder = c.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0;
156 item.title = getTitle(c);
157 item.url = c.getString(BookmarksLoader.COLUMN_INDEX_URL);
Dave Tharp490ca7f2015-06-16 11:17:34 -0700158 item.is_mdm_managed = ManagedBookmarksRestriction.getInstance().mDb.isMdmElement(getItemId(c));
John Reckf94abcf2011-10-10 15:33:48 -0700159 return item;
160 }
161
162 @Override
163 public BrowserBookmarksAdapterItem getLoadingObject() {
164 BrowserBookmarksAdapterItem item = new BrowserBookmarksAdapterItem();
165 return item;
John Reck608baa72010-11-24 10:32:28 -0800166 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800167}