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 { |
| 40 | private SparseArray<Bitmap> mBitmaps; |
Pankaj Garg | 036365b | 2015-09-14 11:21:19 -0700 | [diff] [blame^] | 41 | private SparseArray<Integer> mColors; |
Pankaj Garg | 1c13cab | 2015-05-12 11:52:17 -0700 | [diff] [blame] | 42 | private Tab mTab; |
| 43 | private TitleBar mBar; |
| 44 | |
| 45 | private static final int mMinProgress = 85; |
| 46 | |
| 47 | private static final int mMaxBitmaps = 5; |
| 48 | |
| 49 | public EdgeSwipeModel(Tab tab, TitleBar bar) { |
| 50 | mTab = tab; |
| 51 | mBar = bar; |
| 52 | mBitmaps = new SparseArray<>(); |
Pankaj Garg | 036365b | 2015-09-14 11:21:19 -0700 | [diff] [blame^] | 53 | mColors = new SparseArray<>(); |
Pankaj Garg | 1c13cab | 2015-05-12 11:52:17 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | public void updateSnapshot(final int index) { |
| 57 | if (mBitmaps.get(index) != null) { |
| 58 | return; |
| 59 | } |
| 60 | |
| 61 | int captureIndex = mTab.getCaptureIndex(index); |
| 62 | |
| 63 | boolean bitmapExists = mTab.getWebView().hasSnapshot(captureIndex); |
| 64 | |
| 65 | if (!mTab.isFirstVisualPixelPainted()) { |
| 66 | fetchSnapshot(index); |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | int progress = mBar.getProgressView().getProgressPercent(); |
| 71 | |
| 72 | if (bitmapExists && progress < mMinProgress) { |
| 73 | fetchSnapshot(index); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | mTab.getWebView().captureSnapshot(captureIndex, |
| 78 | new ValueCallback<Bitmap>() { |
| 79 | @Override |
| 80 | public void onReceiveValue(Bitmap value) { |
| 81 | mBitmaps.put(index, value); |
| 82 | } |
| 83 | } |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | public void fetchSnapshot(final int index) { |
Pankaj Garg | 036365b | 2015-09-14 11:21:19 -0700 | [diff] [blame^] | 88 | if (mColors.get(index) == null && mTab.getWebView() != null) { |
| 89 | WebHistoryItem item = mTab.getWebView().copyBackForwardList().getItemAtIndex(index); |
| 90 | if (item != null) { |
| 91 | String url = item.getUrl(); |
| 92 | int color = NavigationBarBase.getSiteIconColor(url); |
| 93 | if (color != 0) { |
| 94 | mColors.put(index, color); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
Pankaj Garg | 1c13cab | 2015-05-12 11:52:17 -0700 | [diff] [blame] | 99 | if (mBitmaps.get(index) != null) { |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | int captureIndex = mTab.getCaptureIndex(index); |
| 104 | |
| 105 | mTab.getWebView().getSnapshot(captureIndex, |
| 106 | new ValueCallback<Bitmap>() { |
| 107 | @Override |
| 108 | public void onReceiveValue(Bitmap bitmap) { |
| 109 | mBitmaps.put(index, bitmap); |
| 110 | } |
| 111 | } |
| 112 | ); |
| 113 | } |
| 114 | |
| 115 | public Bitmap readSnapshot(int index) { |
| 116 | if (index < 0) { |
| 117 | return null; |
| 118 | } |
| 119 | |
| 120 | if (index > (mTab.getWebView().copyBackForwardList().getSize() - 1)) { |
| 121 | return null; |
| 122 | } |
| 123 | |
| 124 | return mBitmaps.get(index); |
| 125 | } |
| 126 | |
Pankaj Garg | 036365b | 2015-09-14 11:21:19 -0700 | [diff] [blame^] | 127 | public int getColor(int index) { |
| 128 | if (index < 0) { |
| 129 | return Color.DKGRAY; |
| 130 | } |
| 131 | |
| 132 | if (index > (mTab.getWebView().copyBackForwardList().getSize() - 1)) { |
| 133 | return Color.DKGRAY; |
| 134 | } |
| 135 | |
| 136 | Integer color = mColors.get(index); |
| 137 | if (color != null) { |
| 138 | return color.intValue(); |
| 139 | } |
| 140 | |
| 141 | return Color.DKGRAY; |
| 142 | } |
| 143 | |
Pankaj Garg | 1c13cab | 2015-05-12 11:52:17 -0700 | [diff] [blame] | 144 | public void deleteSnapshot(int index) { |
| 145 | mBitmaps.delete(index); |
| 146 | } |
| 147 | |
| 148 | public void cleanup() { |
| 149 | mBitmaps.clear(); |
Pankaj Garg | 036365b | 2015-09-14 11:21:19 -0700 | [diff] [blame^] | 150 | mColors.clear(); |
Pankaj Garg | 1c13cab | 2015-05-12 11:52:17 -0700 | [diff] [blame] | 151 | } |
| 152 | } |