Home | History | Annotate | Download | only in view
      1 /*
      2  * Copyright (C) 2011 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 
     17 
     18 package com.android.email.view;
     19 
     20 import android.content.Context;
     21 import android.util.AttributeSet;
     22 import android.util.Log;
     23 import android.webkit.WebView;
     24 
     25 import com.android.email.Clock;
     26 import com.android.email.Email;
     27 import com.android.email.Throttle;
     28 import com.android.emailcommon.Logging;
     29 import com.android.emailcommon.utility.Utility;
     30 
     31 /**
     32  * A custom WebView that is robust to rapid resize events in sequence.
     33  *
     34  * This is useful for a WebView which needs to have a layout of {@code WRAP_CONTENT}, since any
     35  * contents with percent-based height will force the WebView to infinitely expand (or shrink).
     36  */
     37 public class RigidWebView extends WebView {
     38 
     39     public RigidWebView(Context context) {
     40         super(context);
     41     }
     42     public RigidWebView(Context context, AttributeSet attrs) {
     43         super(context, attrs);
     44     }
     45     public RigidWebView(Context context, AttributeSet attrs, int defStyle) {
     46         super(context, attrs, defStyle);
     47     }
     48 
     49     private static final int MIN_RESIZE_INTERVAL = 200;
     50     private static final int MAX_RESIZE_INTERVAL = 300;
     51     private final Clock mClock = Clock.INSTANCE;
     52 
     53     private final Throttle mThrottle = new Throttle(getClass().getName(),
     54             new Runnable() {
     55                 @Override public void run() {
     56                     performSizeChangeDelayed();
     57                 }
     58             }, Utility.getMainThreadHandler(),
     59             MIN_RESIZE_INTERVAL, MAX_RESIZE_INTERVAL);
     60 
     61     private int mRealWidth;
     62     private int mRealHeight;
     63     private boolean mIgnoreNext;
     64     private long mLastSizeChangeTime = -1;
     65 
     66     @Override
     67     protected void onSizeChanged(int w, int h, int ow, int oh) {
     68         mRealWidth = w;
     69         mRealHeight = h;
     70         long now = mClock.getTime();
     71         boolean recentlySized = (now - mLastSizeChangeTime < MIN_RESIZE_INTERVAL);
     72 
     73         // It's known that the previous resize event may cause a resize event immediately. If
     74         // this happens sufficiently close to the last resize event, drop it on the floor.
     75         if (mIgnoreNext) {
     76             mIgnoreNext = false;
     77             if (recentlySized) {
     78                 if (Email.DEBUG) {
     79                     Log.w(Logging.LOG_TAG, "Supressing size change in RigidWebView");
     80                 }
     81                 return;
     82             }
     83         }
     84 
     85         if (recentlySized) {
     86             mThrottle.onEvent();
     87         } else {
     88             // It's been a sufficiently long time - just perform the resize as normal. This should
     89             // be the normal code path.
     90             performSizeChange(ow, oh);
     91         }
     92     }
     93 
     94     private void performSizeChange(int ow, int oh) {
     95         super.onSizeChanged(mRealWidth, mRealHeight, ow, oh);
     96         mLastSizeChangeTime = mClock.getTime();
     97     }
     98 
     99     private void performSizeChangeDelayed() {
    100         mIgnoreNext = true;
    101         performSizeChange(getWidth(), getHeight());
    102     }
    103 }
    104