The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 1 | /* |
| 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 Amirzada | 41242f2 | 2014-03-21 12:12:18 -0700 | [diff] [blame] | 17 | package com.android.browser; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 18 | |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 19 | import android.content.Context; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 20 | import android.database.Cursor; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 21 | import android.graphics.Bitmap; |
Dave Tharp | 490ca7f | 2015-06-16 11:17:34 -0700 | [diff] [blame] | 22 | import android.graphics.BitmapFactory; |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 23 | import android.graphics.drawable.BitmapDrawable; |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 24 | import android.view.LayoutInflater; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 25 | import android.view.View; |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 26 | import android.view.ViewGroup; |
Pankaj Garg | 21dad56 | 2015-07-02 17:17:24 -0700 | [diff] [blame] | 27 | import android.widget.ImageView; |
Leon Scroggins | 892df31 | 2009-07-14 14:48:02 -0400 | [diff] [blame] | 28 | import android.widget.TextView; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 29 | |
Dave Tharp | 9a082b1 | 2015-06-19 08:46:57 -0700 | [diff] [blame] | 30 | import com.android.browser.mdm.EditBookmarksRestriction; |
Dave Tharp | 490ca7f | 2015-06-16 11:17:34 -0700 | [diff] [blame] | 31 | import com.android.browser.mdm.ManagedBookmarksRestriction; |
Bijan Amirzada | 41242f2 | 2014-03-21 12:12:18 -0700 | [diff] [blame] | 32 | import com.android.browser.platformsupport.BrowserContract.Bookmarks; |
| 33 | import com.android.browser.util.ThreadedCursorAdapter; |
| 34 | import com.android.browser.view.BookmarkContainer; |
Kulanthaivel Palanichamy | a94b4d3 | 2015-01-29 19:32:14 -0800 | [diff] [blame] | 35 | import com.android.browser.view.BookmarkThumbImageView; |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 36 | |
| 37 | public class BrowserBookmarksAdapter extends |
| 38 | ThreadedCursorAdapter<BrowserBookmarksAdapterItem> { |
| 39 | |
Dave Tharp | 490ca7f | 2015-06-16 11:17:34 -0700 | [diff] [blame] | 40 | private static final String TAG = "BrowserBookmarksAdapter"; |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 41 | LayoutInflater mInflater; |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 42 | Context mContext; |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 43 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 44 | /** |
| 45 | * Create a new BrowserBookmarksAdapter. |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 46 | */ |
John Reck | af991f9 | 2012-04-24 15:31:16 -0700 | [diff] [blame] | 47 | public BrowserBookmarksAdapter(Context context) { |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 48 | // Make sure to tell the CursorAdapter to avoid the observer and auto-requery |
| 49 | // since the Loader will do that for us. |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 50 | super(context, null); |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 51 | mInflater = LayoutInflater.from(context); |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 52 | mContext = context; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 53 | } |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 54 | |
| 55 | @Override |
John Reck | 9ee87d9 | 2012-05-04 13:53:25 -0700 | [diff] [blame] | 56 | protected long getItemId(Cursor c) { |
| 57 | return c.getLong(BookmarksLoader.COLUMN_INDEX_ID); |
| 58 | } |
| 59 | |
| 60 | @Override |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 61 | public View newView(Context context, ViewGroup parent) { |
John Reck | af991f9 | 2012-04-24 15:31:16 -0700 | [diff] [blame] | 62 | return mInflater.inflate(R.layout.bookmark_thumbnail, parent, false); |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 63 | } |
| 64 | |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 65 | @Override |
| 66 | public void bindView(View view, BrowserBookmarksAdapterItem object) { |
| 67 | BookmarkContainer container = (BookmarkContainer) view; |
| 68 | container.setIgnoreRequestLayout(true); |
John Reck | af991f9 | 2012-04-24 15:31:16 -0700 | [diff] [blame] | 69 | bindGridView(view, mContext, object); |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 70 | container.setIgnoreRequestLayout(false); |
| 71 | } |
| 72 | |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 73 | CharSequence getTitle(Cursor cursor) { |
John Reck | d18ac4b | 2012-04-12 17:27:34 -0700 | [diff] [blame] | 74 | int type = cursor.getInt(BookmarksLoader.COLUMN_INDEX_TYPE); |
| 75 | switch (type) { |
| 76 | case Bookmarks.BOOKMARK_TYPE_OTHER_FOLDER: |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 77 | return mContext.getText(R.string.other_bookmarks); |
John Reck | d18ac4b | 2012-04-12 17:27:34 -0700 | [diff] [blame] | 78 | } |
| 79 | return cursor.getString(BookmarksLoader.COLUMN_INDEX_TITLE); |
| 80 | } |
| 81 | |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 82 | void bindGridView(View view, Context context, BrowserBookmarksAdapterItem item) { |
John Reck | f3828cd | 2011-06-14 17:21:55 -0700 | [diff] [blame] | 83 | // 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 Garg | 21dad56 | 2015-07-02 17:17:24 -0700 | [diff] [blame] | 89 | SiteTileView thumb = (SiteTileView) view.findViewById(R.id.thumb_image); |
Jeff Hamilton | 8402962 | 2010-08-05 14:29:28 -0500 | [diff] [blame] | 90 | TextView tv = (TextView) view.findViewById(R.id.label); |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 91 | tv.setText(item.title); |
Dave Tharp | 9a082b1 | 2015-06-19 08:46:57 -0700 | [diff] [blame] | 92 | int containerWidth = thumb.getWidth() - thumb.getPaddingLeft() - thumb.getPaddingRight(); |
| 93 | |
| 94 | Bitmap b; |
| 95 | |
Pankaj Garg | 1101521 | 2015-07-06 14:38:43 -0700 | [diff] [blame^] | 96 | thumb.setFloating(false); |
| 97 | |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 98 | if (item.is_folder) { |
Dave Tharp | 9a082b1 | 2015-06-19 08:46:57 -0700 | [diff] [blame] | 99 | b = BitmapFactory.decodeResource(mContext.getResources(), |
Pankaj Garg | 21dad56 | 2015-07-02 17:17:24 -0700 | [diff] [blame] | 100 | R.drawable.ic_deco_folder_normal); |
| 101 | thumb.setFloating(true); |
Dave Tharp | 9a082b1 | 2015-06-19 08:46:57 -0700 | [diff] [blame] | 102 | } |
| 103 | else if (item.thumbnail == null || !item.has_thumbnail) { |
| 104 | b = BitmapFactory.decodeResource(mContext.getResources(), |
| 105 | R.drawable.browser_thumbnail); |
| 106 | } |
| 107 | else { |
| 108 | b = item.thumbnail.getBitmap(); |
| 109 | } |
| 110 | |
| 111 | // If the item is managed by mdm or edit bookmark restriction enabled |
Pankaj Garg | 21dad56 | 2015-07-02 17:17:24 -0700 | [diff] [blame] | 112 | if (containerWidth != 0 && (item.is_mdm_managed || |
| 113 | EditBookmarksRestriction.getInstance().isEnabled())) { |
Dave Tharp | 9a082b1 | 2015-06-19 08:46:57 -0700 | [diff] [blame] | 114 | int iconResId; |
| 115 | float overlayScale, overlayVertPos; |
| 116 | |
| 117 | if (item.is_mdm_managed) { |
| 118 | iconResId = R.drawable.img_deco_mdm_badge_bright; |
| 119 | overlayScale = 0.6f; |
| 120 | overlayVertPos = 100f; |
Dave Tharp | 490ca7f | 2015-06-16 11:17:34 -0700 | [diff] [blame] | 121 | } |
| 122 | else { |
Dave Tharp | 9a082b1 | 2015-06-19 08:46:57 -0700 | [diff] [blame] | 123 | iconResId = R.drawable.ic_deco_secure; |
| 124 | overlayScale = 1.2f; |
| 125 | overlayVertPos = 75f; |
Dave Tharp | 490ca7f | 2015-06-16 11:17:34 -0700 | [diff] [blame] | 126 | } |
Dave Tharp | 9a082b1 | 2015-06-19 08:46:57 -0700 | [diff] [blame] | 127 | float willScale = (float) containerWidth / (float) b.getWidth(); |
| 128 | Bitmap bm = BrowserBookmarksPage.overlayBookmarkBitmap(b, iconResId, mContext, |
| 129 | overlayScale / willScale, (int) (overlayVertPos / willScale)); |
Pankaj Garg | 21dad56 | 2015-07-02 17:17:24 -0700 | [diff] [blame] | 130 | thumb.replaceFavicon(bm); |
Dave Tharp | 9a082b1 | 2015-06-19 08:46:57 -0700 | [diff] [blame] | 131 | } |
| 132 | else { |
Pankaj Garg | 21dad56 | 2015-07-02 17:17:24 -0700 | [diff] [blame] | 133 | thumb.replaceFavicon(b); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 134 | } |
Pankaj Garg | 21dad56 | 2015-07-02 17:17:24 -0700 | [diff] [blame] | 135 | thumb.setLongClickable(true); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 136 | } |
John Reck | 8af9064 | 2010-11-23 10:27:29 -0800 | [diff] [blame] | 137 | |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 138 | @Override |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 139 | public BrowserBookmarksAdapterItem getRowObject(Cursor c, |
| 140 | BrowserBookmarksAdapterItem item) { |
| 141 | if (item == null) { |
| 142 | item = new BrowserBookmarksAdapterItem(); |
| 143 | } |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 144 | Bitmap thumbnail = item.thumbnail != null ? item.thumbnail.getBitmap() : null; |
Pankaj Garg | 68faf1c | 2015-06-26 17:07:37 -0700 | [diff] [blame] | 145 | |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 146 | thumbnail = BrowserBookmarksPage.getBitmap(c, |
Pankaj Garg | 68faf1c | 2015-06-26 17:07:37 -0700 | [diff] [blame] | 147 | BookmarksLoader.COLUMN_INDEX_TOUCH_ICON, thumbnail); |
| 148 | if (thumbnail == null) { |
| 149 | thumbnail = BrowserBookmarksPage.getBitmap(c, |
| 150 | BookmarksLoader.COLUMN_INDEX_THUMBNAIL, thumbnail); |
| 151 | } |
John Reck | 1df6df7 | 2012-06-05 11:05:34 -0700 | [diff] [blame] | 152 | item.has_thumbnail = thumbnail != null; |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 153 | if (thumbnail != null |
| 154 | && (item.thumbnail == null || item.thumbnail.getBitmap() != thumbnail)) { |
| 155 | item.thumbnail = new BitmapDrawable(mContext.getResources(), thumbnail); |
| 156 | } |
| 157 | item.is_folder = c.getInt(BookmarksLoader.COLUMN_INDEX_IS_FOLDER) != 0; |
| 158 | item.title = getTitle(c); |
| 159 | item.url = c.getString(BookmarksLoader.COLUMN_INDEX_URL); |
Dave Tharp | 490ca7f | 2015-06-16 11:17:34 -0700 | [diff] [blame] | 160 | item.is_mdm_managed = ManagedBookmarksRestriction.getInstance().mDb.isMdmElement(getItemId(c)); |
John Reck | f94abcf | 2011-10-10 15:33:48 -0700 | [diff] [blame] | 161 | return item; |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public BrowserBookmarksAdapterItem getLoadingObject() { |
| 166 | BrowserBookmarksAdapterItem item = new BrowserBookmarksAdapterItem(); |
| 167 | return item; |
John Reck | 608baa7 | 2010-11-24 10:32:28 -0800 | [diff] [blame] | 168 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 169 | } |