Pankaj Garg | 1c13cab | 2015-05-12 11:52:17 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015, The Linux Foundation. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * * Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * * Redistributions in binary form must reproduce the above |
| 10 | * copyright notice, this list of conditions and the following |
| 11 | * disclaimer in the documentation and/or other materials provided |
| 12 | * with the distribution. |
| 13 | * * Neither the name of The Linux Foundation nor the names of its |
| 14 | * contributors may be used to endorse or promote products derived |
| 15 | * from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 21 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 24 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 25 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 26 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 27 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | */ |
| 29 | |
| 30 | package com.android.browser; |
| 31 | |
| 32 | import android.graphics.Bitmap; |
Pankaj Garg | 036365b | 2015-09-14 11:21:19 -0700 | [diff] [blame] | 33 | import android.graphics.Color; |
Pankaj Garg | 1c13cab | 2015-05-12 11:52:17 -0700 | [diff] [blame] | 34 | import android.util.SparseArray; |
| 35 | import android.webkit.ValueCallback; |
| 36 | |
Pankaj Garg | 036365b | 2015-09-14 11:21:19 -0700 | [diff] [blame] | 37 | import org.codeaurora.swe.WebHistoryItem; |
| 38 | |
Pankaj Garg | 1c13cab | 2015-05-12 11:52:17 -0700 | [diff] [blame] | 39 | public class EdgeSwipeModel { |
Pankaj Garg | 4fded5d | 2015-12-01 15:24:21 -0800 | [diff] [blame] | 40 | private static final int MS_TIME_BETWEEN_CAPTURES = 1000; |
Pankaj Garg | 1c13cab | 2015-05-12 11:52:17 -0700 | [diff] [blame] | 41 | private SparseArray<Bitmap> mBitmaps; |
Pankaj Garg | 036365b | 2015-09-14 11:21:19 -0700 | [diff] [blame] | 42 | private SparseArray<Integer> mColors; |
Pankaj Garg | 4fded5d | 2015-12-01 15:24:21 -0800 | [diff] [blame] | 43 | |
| 44 | private long mLastCaptureTime; |
| 45 | private int mLastCaptureIndex; |
| 46 | private Bitmap mLastBitmap; |
| 47 | |
Pankaj Garg | 1c13cab | 2015-05-12 11:52:17 -0700 | [diff] [blame] | 48 | private Tab mTab; |
| 49 | private TitleBar mBar; |
| 50 | |
| 51 | private static final int mMinProgress = 85; |
| 52 | |
| 53 | private static final int mMaxBitmaps = 5; |
| 54 | |
| 55 | public EdgeSwipeModel(Tab tab, TitleBar bar) { |
| 56 | mTab = tab; |
| 57 | mBar = bar; |
Pankaj Garg | 4fded5d | 2015-12-01 15:24:21 -0800 | [diff] [blame] | 58 | mLastCaptureIndex = -1; |
| 59 | mLastCaptureTime = 0; |
Pankaj Garg | 1c13cab | 2015-05-12 11:52:17 -0700 | [diff] [blame] | 60 | mBitmaps = new SparseArray<>(); |
Pankaj Garg | 036365b | 2015-09-14 11:21:19 -0700 | [diff] [blame] | 61 | mColors = new SparseArray<>(); |
Pankaj Garg | 1c13cab | 2015-05-12 11:52:17 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | public void updateSnapshot(final int index) { |
| 65 | if (mBitmaps.get(index) != null) { |
| 66 | return; |
| 67 | } |
| 68 | |
Pankaj Garg | 4fded5d | 2015-12-01 15:24:21 -0800 | [diff] [blame] | 69 | final int captureIndex = mTab.getCaptureIndex(index); |
Pankaj Garg | 1c13cab | 2015-05-12 11:52:17 -0700 | [diff] [blame] | 70 | |
| 71 | boolean bitmapExists = mTab.getWebView().hasSnapshot(captureIndex); |
| 72 | |
| 73 | if (!mTab.isFirstVisualPixelPainted()) { |
| 74 | fetchSnapshot(index); |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | int progress = mBar.getProgressView().getProgressPercent(); |
Pankaj Garg | 4fded5d | 2015-12-01 15:24:21 -0800 | [diff] [blame] | 79 | long currentTime = System.currentTimeMillis(); |
Pankaj Garg | 1c13cab | 2015-05-12 11:52:17 -0700 | [diff] [blame] | 80 | |
Pankaj Garg | 4fded5d | 2015-12-01 15:24:21 -0800 | [diff] [blame] | 81 | if (bitmapExists) { |
| 82 | if (progress < mMinProgress || (captureIndex == mLastCaptureIndex && |
| 83 | (currentTime < (mLastCaptureTime + MS_TIME_BETWEEN_CAPTURES)))) { |
| 84 | fetchSnapshot(index); |
| 85 | return; |
| 86 | } |
Pankaj Garg | 1c13cab | 2015-05-12 11:52:17 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | mTab.getWebView().captureSnapshot(captureIndex, |
| 90 | new ValueCallback<Bitmap>() { |
| 91 | @Override |
| 92 | public void onReceiveValue(Bitmap value) { |
| 93 | mBitmaps.put(index, value); |
Pankaj Garg | 4fded5d | 2015-12-01 15:24:21 -0800 | [diff] [blame] | 94 | mLastCaptureTime = System.currentTimeMillis(); |
| 95 | mLastCaptureIndex = captureIndex; |
| 96 | mLastBitmap = value; |
Pankaj Garg | 1c13cab | 2015-05-12 11:52:17 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | ); |
| 100 | } |
| 101 | |
| 102 | public void fetchSnapshot(final int index) { |
Pankaj Garg | 036365b | 2015-09-14 11:21:19 -0700 | [diff] [blame] | 103 | if (mColors.get(index) == null && mTab.getWebView() != null) { |
| 104 | WebHistoryItem item = mTab.getWebView().copyBackForwardList().getItemAtIndex(index); |
| 105 | if (item != null) { |
| 106 | String url = item.getUrl(); |
| 107 | int color = NavigationBarBase.getSiteIconColor(url); |
| 108 | if (color != 0) { |
| 109 | mColors.put(index, color); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
Pankaj Garg | 1c13cab | 2015-05-12 11:52:17 -0700 | [diff] [blame] | 114 | if (mBitmaps.get(index) != null) { |
| 115 | return; |
| 116 | } |
| 117 | |
| 118 | int captureIndex = mTab.getCaptureIndex(index); |
Pankaj Garg | 4fded5d | 2015-12-01 15:24:21 -0800 | [diff] [blame] | 119 | long currentTime = System.currentTimeMillis(); |
| 120 | if (captureIndex == mLastCaptureIndex && |
| 121 | (currentTime < (mLastCaptureTime + MS_TIME_BETWEEN_CAPTURES))) { |
| 122 | mBitmaps.put(index, mLastBitmap); |
| 123 | return; |
| 124 | } |
Pankaj Garg | 1c13cab | 2015-05-12 11:52:17 -0700 | [diff] [blame] | 125 | |
| 126 | mTab.getWebView().getSnapshot(captureIndex, |
| 127 | new ValueCallback<Bitmap>() { |
| 128 | @Override |
| 129 | public void onReceiveValue(Bitmap bitmap) { |
| 130 | mBitmaps.put(index, bitmap); |
| 131 | } |
| 132 | } |
| 133 | ); |
| 134 | } |
| 135 | |
| 136 | public Bitmap readSnapshot(int index) { |
| 137 | if (index < 0) { |
| 138 | return null; |
| 139 | } |
| 140 | |
| 141 | if (index > (mTab.getWebView().copyBackForwardList().getSize() - 1)) { |
| 142 | return null; |
| 143 | } |
| 144 | |
| 145 | return mBitmaps.get(index); |
| 146 | } |
| 147 | |
Pankaj Garg | 036365b | 2015-09-14 11:21:19 -0700 | [diff] [blame] | 148 | public int getColor(int index) { |
| 149 | if (index < 0) { |
| 150 | return Color.DKGRAY; |
| 151 | } |
| 152 | |
| 153 | if (index > (mTab.getWebView().copyBackForwardList().getSize() - 1)) { |
| 154 | return Color.DKGRAY; |
| 155 | } |
| 156 | |
| 157 | Integer color = mColors.get(index); |
| 158 | if (color != null) { |
| 159 | return color.intValue(); |
| 160 | } |
| 161 | |
| 162 | return Color.DKGRAY; |
| 163 | } |
| 164 | |
Pankaj Garg | 1c13cab | 2015-05-12 11:52:17 -0700 | [diff] [blame] | 165 | public void deleteSnapshot(int index) { |
| 166 | mBitmaps.delete(index); |
| 167 | } |
| 168 | |
| 169 | public void cleanup() { |
| 170 | mBitmaps.clear(); |
Pankaj Garg | 036365b | 2015-09-14 11:21:19 -0700 | [diff] [blame] | 171 | mColors.clear(); |
Pankaj Garg | 1c13cab | 2015-05-12 11:52:17 -0700 | [diff] [blame] | 172 | } |
| 173 | } |