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; |
| 33 | import android.graphics.Color; |
| 34 | import android.support.v4.widget.ViewDragHelper; |
| 35 | import android.view.View; |
| 36 | import android.widget.FrameLayout; |
| 37 | import android.widget.ImageView; |
| 38 | |
| 39 | public class EdgeSwipeView { |
| 40 | private ImageView mStationaryView; |
| 41 | private ImageView mSlidingView; |
| 42 | private ImageView mSlidingViewShadow; |
| 43 | private View mOpacityView; |
| 44 | private FrameLayout mLiveView; |
| 45 | private DraggableFrameLayout mViewGroup; |
| 46 | |
| 47 | private int mSlidingViewIndex; |
| 48 | |
| 49 | private boolean mbShowingLive = true; |
| 50 | |
| 51 | private boolean mbStationaryViewBMSet = false; |
| 52 | private boolean mbSlidingViewBMSet = false; |
| 53 | |
| 54 | private TitleBar mTitleBar; |
| 55 | |
| 56 | public EdgeSwipeView( |
| 57 | View container, |
| 58 | int stationaryViewId, |
| 59 | int slidingViewId, |
| 60 | int slidingViewShadowId, |
| 61 | int opacityViewId, |
| 62 | int liveViewId, |
| 63 | int viewGroupId, |
| 64 | TitleBar titleBar) { |
| 65 | mStationaryView = (ImageView) container.findViewById(stationaryViewId); |
| 66 | mSlidingView = (ImageView) container.findViewById(slidingViewId); |
| 67 | mSlidingViewShadow = (ImageView) container.findViewById(slidingViewShadowId); |
| 68 | mOpacityView = container.findViewById(opacityViewId); |
| 69 | mLiveView = (FrameLayout) container.findViewById(liveViewId); |
| 70 | mViewGroup = (DraggableFrameLayout) container.findViewById(viewGroupId); |
| 71 | mSlidingViewShadow.setBackgroundResource(R.drawable.left_shade); |
| 72 | |
| 73 | mSlidingView.setVisibility(View.GONE); |
| 74 | mSlidingViewShadow.setVisibility(View.GONE); |
| 75 | mOpacityView.setVisibility(View.GONE); |
| 76 | |
| 77 | final int childCount = mViewGroup.getChildCount(); |
| 78 | for (int i = childCount - 1; i >= 0; i--) { |
| 79 | final View child = mViewGroup.getChildAt(i); |
| 80 | if (mSlidingView == child) { |
| 81 | mSlidingViewIndex = i; |
| 82 | break; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | mTitleBar = titleBar; |
| 87 | } |
| 88 | |
| 89 | public void goLive() { |
| 90 | if (mbShowingLive) |
| 91 | return; |
| 92 | |
| 93 | mLiveView.setVisibility(View.VISIBLE); |
| 94 | mStationaryView.setVisibility(View.GONE); |
| 95 | mSlidingView.setVisibility(View.GONE); |
| 96 | mSlidingViewShadow.setVisibility(View.GONE); |
| 97 | mOpacityView.setVisibility(View.GONE); |
| 98 | mbShowingLive = true; |
| 99 | } |
| 100 | |
| 101 | public void goDormant() { |
| 102 | if (!mbShowingLive) |
| 103 | return; |
| 104 | |
| 105 | mSlidingView.setVisibility(View.VISIBLE); |
| 106 | mStationaryView.setVisibility(View.VISIBLE); |
| 107 | mOpacityView.setVisibility(View.VISIBLE); |
| 108 | mLiveView.setVisibility(View.GONE); |
| 109 | mbShowingLive = false; |
| 110 | } |
| 111 | |
| 112 | public boolean isLive() { |
| 113 | return mbShowingLive; |
| 114 | } |
| 115 | |
| 116 | private Bitmap getColorBitmap(int color) |
| 117 | { |
| 118 | int height = mViewGroup.getMeasuredHeight(); |
| 119 | int width = mViewGroup.getMeasuredWidth(); |
| 120 | height -= (mTitleBar.getY()>= 0) ? mTitleBar.getNavigationBar().getMeasuredHeight() : 0; |
| 121 | Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444); |
| 122 | bitmap.eraseColor(color); |
| 123 | return bitmap; |
| 124 | } |
| 125 | |
| 126 | private void clampViewIfNeeded(View view) { |
| 127 | int offset = 0; |
| 128 | if (mTitleBar.getY() >= 0) { |
| 129 | offset = mTitleBar.getNavigationBar().getMeasuredHeight(); |
| 130 | } |
| 131 | view.setPadding(0, offset - view.getTop(), 0, 0); |
| 132 | } |
| 133 | |
| 134 | public boolean isPortrait() { |
| 135 | return (mViewGroup.getHeight() < mViewGroup.getWidth()); |
| 136 | } |
| 137 | |
Pankaj Garg | 036365b | 2015-09-14 11:21:19 -0700 | [diff] [blame] | 138 | private void setBitmap(ImageView view, Bitmap bitmap, int color) { |
Pankaj Garg | 1c13cab | 2015-05-12 11:52:17 -0700 | [diff] [blame] | 139 | clampViewIfNeeded(view); |
| 140 | if (bitmap == null) { |
Pankaj Garg | 036365b | 2015-09-14 11:21:19 -0700 | [diff] [blame] | 141 | bitmap = getColorBitmap(color); |
Pankaj Garg | 1c13cab | 2015-05-12 11:52:17 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | int offset = 0; |
| 145 | if (mTitleBar.getY() >= 0) { |
| 146 | offset = mTitleBar.getNavigationBar().getMeasuredHeight(); |
| 147 | } |
| 148 | |
Pankaj Garg | 036365b | 2015-09-14 11:21:19 -0700 | [diff] [blame] | 149 | if ((view.getMeasuredHeight() > view.getMeasuredWidth()) != |
| 150 | (bitmap.getHeight() > bitmap.getWidth())) { |
| 151 | view.setImageBitmap(bitmap); |
| 152 | return; |
| 153 | } |
| 154 | |
Pankaj Garg | 1c13cab | 2015-05-12 11:52:17 -0700 | [diff] [blame] | 155 | int bitmap_height = bitmap.getHeight(); |
| 156 | |
| 157 | if (view.getMeasuredHeight() != 0) { |
| 158 | bitmap_height = (view.getMeasuredHeight() - offset) * bitmap.getWidth() / |
| 159 | view.getMeasuredWidth(); |
| 160 | } |
| 161 | |
| 162 | if ((bitmap.getHeight() - bitmap_height) > 5) { |
| 163 | Bitmap cropped = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap_height); |
| 164 | view.setImageBitmap(cropped); |
| 165 | } else { |
| 166 | view.setImageBitmap(bitmap); |
| 167 | } |
| 168 | } |
| 169 | |
Pankaj Garg | 036365b | 2015-09-14 11:21:19 -0700 | [diff] [blame] | 170 | public void setStationaryViewBitmap(Bitmap bitmap, int color) { |
Pankaj Garg | 1c13cab | 2015-05-12 11:52:17 -0700 | [diff] [blame] | 171 | mbStationaryViewBMSet = null != bitmap; |
Pankaj Garg | 036365b | 2015-09-14 11:21:19 -0700 | [diff] [blame] | 172 | setBitmap(mStationaryView, bitmap, color); |
Pankaj Garg | 1c13cab | 2015-05-12 11:52:17 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | public void setStationaryViewAlpha(float alpha) { |
| 176 | mStationaryView.setAlpha(alpha); |
| 177 | } |
| 178 | |
Pankaj Garg | 036365b | 2015-09-14 11:21:19 -0700 | [diff] [blame] | 179 | public void setSlidingViewBitmap(Bitmap bitmap, int color) { |
Pankaj Garg | 1c13cab | 2015-05-12 11:52:17 -0700 | [diff] [blame] | 180 | mbSlidingViewBMSet = null != bitmap; |
Pankaj Garg | 036365b | 2015-09-14 11:21:19 -0700 | [diff] [blame] | 181 | setBitmap(mSlidingView, bitmap, color); |
Pankaj Garg | 1c13cab | 2015-05-12 11:52:17 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | public boolean slidingViewHasImage() { |
| 185 | return mbSlidingViewBMSet; |
| 186 | } |
| 187 | |
| 188 | public boolean stationaryViewHasImage() { |
| 189 | return mbStationaryViewBMSet; |
| 190 | } |
| 191 | |
| 192 | public void slidingViewTouched(int edge) { |
| 193 | if (edge == ViewDragHelper.EDGE_RIGHT) { |
| 194 | mSlidingView.setTranslationX(mViewGroup.getMeasuredWidth()); |
| 195 | } else { |
| 196 | mSlidingView.setTranslationX(0); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | public void hideSlidingViews() { |
| 201 | mSlidingViewShadow.setVisibility(View.GONE); |
| 202 | mSlidingView.setVisibility(View.GONE); |
| 203 | } |
| 204 | |
| 205 | public void showSlidingViews() { |
| 206 | mSlidingViewShadow.setVisibility(View.VISIBLE); |
| 207 | mSlidingView.setVisibility(View.VISIBLE); |
| 208 | } |
| 209 | |
| 210 | public int slidingViewIndex() { |
| 211 | return mSlidingViewIndex; |
| 212 | } |
| 213 | |
| 214 | public void moveShadowView(float x) { |
| 215 | x -= mSlidingViewShadow.getMeasuredWidth(); |
| 216 | mSlidingViewShadow.setX(x); |
| 217 | mSlidingViewShadow.setVisibility(View.VISIBLE); |
| 218 | mOpacityView.setVisibility(View.VISIBLE); |
| 219 | } |
| 220 | |
| 221 | public boolean allowCapture(View view) { |
| 222 | return (view == mSlidingView); |
| 223 | } |
| 224 | |
| 225 | public int getMeasuredWidth() { |
| 226 | return mViewGroup.getMeasuredWidth(); |
| 227 | } |
| 228 | |
| 229 | public int getWidth() { |
| 230 | return mViewGroup.getWidth(); |
| 231 | } |
| 232 | |
| 233 | public void init() { |
| 234 | clampViewIfNeeded(mSlidingView); |
| 235 | clampViewIfNeeded(mStationaryView); |
| 236 | } |
| 237 | |
| 238 | public void invalidate() { |
| 239 | mViewGroup.invalidate(); |
| 240 | } |
| 241 | } |