blob: 419e2652d15f1c817bfacfae99c6b053a1557848 [file] [log] [blame]
Pankaj Garg1c13cab2015-05-12 11:52:17 -07001/*
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
30package com.android.browser;
31
32import android.graphics.Bitmap;
Pankaj Garg036365b2015-09-14 11:21:19 -070033import android.graphics.Color;
Pankaj Garg1c13cab2015-05-12 11:52:17 -070034import android.util.SparseArray;
35import android.webkit.ValueCallback;
36
Pankaj Garg036365b2015-09-14 11:21:19 -070037import org.codeaurora.swe.WebHistoryItem;
38
Pankaj Garg1c13cab2015-05-12 11:52:17 -070039public class EdgeSwipeModel {
Pankaj Garg4fded5d2015-12-01 15:24:21 -080040 private static final int MS_TIME_BETWEEN_CAPTURES = 1000;
Pankaj Garg1c13cab2015-05-12 11:52:17 -070041 private SparseArray<Bitmap> mBitmaps;
Pankaj Garg036365b2015-09-14 11:21:19 -070042 private SparseArray<Integer> mColors;
Pankaj Garg4fded5d2015-12-01 15:24:21 -080043
44 private long mLastCaptureTime;
45 private int mLastCaptureIndex;
46 private Bitmap mLastBitmap;
47
Pankaj Garg1c13cab2015-05-12 11:52:17 -070048 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 Garg4fded5d2015-12-01 15:24:21 -080058 mLastCaptureIndex = -1;
59 mLastCaptureTime = 0;
Pankaj Garg1c13cab2015-05-12 11:52:17 -070060 mBitmaps = new SparseArray<>();
Pankaj Garg036365b2015-09-14 11:21:19 -070061 mColors = new SparseArray<>();
Pankaj Garg1c13cab2015-05-12 11:52:17 -070062 }
63
64 public void updateSnapshot(final int index) {
65 if (mBitmaps.get(index) != null) {
66 return;
67 }
68
Pankaj Garg4fded5d2015-12-01 15:24:21 -080069 final int captureIndex = mTab.getCaptureIndex(index);
Pankaj Garg1c13cab2015-05-12 11:52:17 -070070
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 Garg4fded5d2015-12-01 15:24:21 -080079 long currentTime = System.currentTimeMillis();
Pankaj Garg1c13cab2015-05-12 11:52:17 -070080
Pankaj Garg4fded5d2015-12-01 15:24:21 -080081 if (bitmapExists) {
82 if (progress < mMinProgress || (captureIndex == mLastCaptureIndex &&
83 (currentTime < (mLastCaptureTime + MS_TIME_BETWEEN_CAPTURES)))) {
84 fetchSnapshot(index);
85 return;
86 }
Pankaj Garg1c13cab2015-05-12 11:52:17 -070087 }
88
89 mTab.getWebView().captureSnapshot(captureIndex,
90 new ValueCallback<Bitmap>() {
91 @Override
92 public void onReceiveValue(Bitmap value) {
93 mBitmaps.put(index, value);
Pankaj Garg4fded5d2015-12-01 15:24:21 -080094 mLastCaptureTime = System.currentTimeMillis();
95 mLastCaptureIndex = captureIndex;
96 mLastBitmap = value;
Pankaj Garg1c13cab2015-05-12 11:52:17 -070097 }
98 }
99 );
100 }
101
102 public void fetchSnapshot(final int index) {
Pankaj Garg036365b2015-09-14 11:21:19 -0700103 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 Garg1c13cab2015-05-12 11:52:17 -0700114 if (mBitmaps.get(index) != null) {
115 return;
116 }
117
118 int captureIndex = mTab.getCaptureIndex(index);
Pankaj Garg4fded5d2015-12-01 15:24:21 -0800119 long currentTime = System.currentTimeMillis();
120 if (captureIndex == mLastCaptureIndex &&
121 (currentTime < (mLastCaptureTime + MS_TIME_BETWEEN_CAPTURES))) {
122 mBitmaps.put(index, mLastBitmap);
123 return;
124 }
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700125
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 Garg036365b2015-09-14 11:21:19 -0700148 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 Garg1c13cab2015-05-12 11:52:17 -0700165 public void deleteSnapshot(int index) {
166 mBitmaps.delete(index);
167 }
168
169 public void cleanup() {
170 mBitmaps.clear();
Pankaj Garg036365b2015-09-14 11:21:19 -0700171 mColors.clear();
Pankaj Garg1c13cab2015-05-12 11:52:17 -0700172 }
173}