diff --git a/src/android/webkit/WebViewClassic.java b/src/android/webkit/WebViewClassic.java new file mode 100644 index 000000000..e315beeab --- /dev/null +++ b/src/android/webkit/WebViewClassic.java @@ -0,0 +1,14 @@ +package android.webkit; + +import android.view.View; + +/** + * Trojan class for getting access to a hidden API level 16 interface + */ +public class WebViewClassic { + public interface TitleBarDelegate { + int getTitleHeight(); + + public void onSetEmbeddedTitleBar(final View title); + } +} diff --git a/src/com/fsck/k9/view/MessageWebView.java b/src/com/fsck/k9/view/MessageWebView.java index 3dc4545ce..bb5d3d737 100644 --- a/src/com/fsck/k9/view/MessageWebView.java +++ b/src/com/fsck/k9/view/MessageWebView.java @@ -13,8 +13,8 @@ import android.widget.Toast; import com.fsck.k9.K9; import com.fsck.k9.R; import java.lang.reflect.Method; - -public class MessageWebView extends WebView { +import com.nobu_games.android.view.web.TitleBarWebView; +public class MessageWebView extends TitleBarWebView { /** @@ -164,15 +164,4 @@ public class MessageWebView extends WebView { } } - public void wrapSetTitleBar(final View title) { - try { - Class webViewClass = Class.forName("android.webkit.WebView"); - Method setEmbeddedTitleBar = webViewClass.getMethod("setEmbeddedTitleBar", View.class); - setEmbeddedTitleBar.invoke(this, title); - } - - catch (Exception e) { - Log.v(K9.LOG_TAG, "failed to find the setEmbeddedTitleBar method",e); - } - } } diff --git a/src/com/fsck/k9/view/SingleMessageView.java b/src/com/fsck/k9/view/SingleMessageView.java index e5779e053..71a191c8e 100644 --- a/src/com/fsck/k9/view/SingleMessageView.java +++ b/src/com/fsck/k9/view/SingleMessageView.java @@ -159,8 +159,8 @@ public class SingleMessageView extends LinearLayout implements OnClickListener, mHeaderContainer.setBackgroundColor(((K9Activity)activity).getThemeBackgroundColor()); mTitleBarHeaderContainer = new LinearLayout(activity); + mMessageContentView.setEmbeddedTitleBarCompat(mTitleBarHeaderContainer); mTitleBarHeaderContainer.addView(mHeaderContainer); - mMessageContentView.wrapSetTitleBar(mTitleBarHeaderContainer); } mShowHiddenAttachments.setOnClickListener(this); diff --git a/src/com/nobu_games/android/view/web/JBTitleBarWebView.java b/src/com/nobu_games/android/view/web/JBTitleBarWebView.java new file mode 100644 index 000000000..b0908cdf8 --- /dev/null +++ b/src/com/nobu_games/android/view/web/JBTitleBarWebView.java @@ -0,0 +1,77 @@ +package com.nobu_games.android.view.web; + +import android.content.Context; +import android.util.AttributeSet; +import android.view.MotionEvent; +import android.view.View; +import android.webkit.WebViewClassic.TitleBarDelegate; + +public class JBTitleBarWebView extends TitleBarWebView implements + TitleBarDelegate { + + public JBTitleBarWebView(Context context) { + super(context); + } + + public JBTitleBarWebView(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public JBTitleBarWebView(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + } + + /** + * Makes sure that the title bar view gets touch events + *

+ * {@inheritDoc} + */ + @Override + public boolean dispatchTouchEvent(MotionEvent event) { + if(mTitleBar != null) { + final int sy = getScrollY(); + final int visTitleHeight = getVisibleTitleHeightCompat(); + final float x = event.getX(); + float y = event.getY(); + + switch(event.getActionMasked()) { + case MotionEvent.ACTION_DOWN: + if(y <= visTitleHeight) { + mTouchInTitleBar = true; + } + break; + + case MotionEvent.ACTION_MOVE: + mTouchMove = true; + break; + + case MotionEvent.ACTION_UP: + case MotionEvent.ACTION_CANCEL: + mTouchMove = false; + break; + + default: + } + + if(mTouchInTitleBar) { + y += sy; + event.setLocation(x, y); + + return mTitleBar.dispatchTouchEvent(event); + } + } + + return super.dispatchTouchEvent(event); + } + + @Override + public int getTitleHeight() { + if(mTitleBar != null) return mTitleBar.getHeight(); + return 0; + } + + @Override + public void onSetEmbeddedTitleBar(View title) { + } + +} diff --git a/src/com/nobu_games/android/view/web/TitleBarWebView.java b/src/com/nobu_games/android/view/web/TitleBarWebView.java new file mode 100644 index 000000000..f8c46bacc --- /dev/null +++ b/src/com/nobu_games/android/view/web/TitleBarWebView.java @@ -0,0 +1,340 @@ +package com.nobu_games.android.view.web; + +/* + * Copyright (C) 2012 Thomas Werner + * Portions Copyright (C) 2006 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import java.lang.reflect.Method; + +import android.content.Context; +import android.graphics.Canvas; +import android.graphics.Matrix; +import android.graphics.Rect; +import android.graphics.drawable.Drawable; +import android.os.Build; +import android.util.AttributeSet; +import android.util.Log; +import android.view.MotionEvent; +import android.view.View; +import android.view.ViewGroup; +import android.webkit.WebView; +import android.widget.FrameLayout; + +/** + * WebView derivative with custom setEmbeddedTitleBar implementation for Android + * versions that do not support that feature anymore. + *

+ * Usage + *

+ * Call {@link #setEmbeddedTitleBarCompat(View)} for setting a view as embedded + * title bar on top of the displayed WebView page. + */ +public class TitleBarWebView extends WebView { + /** + * Internally used view wrapper for suppressing unwanted touch events on the + * title bar view when WebView contents is being touched. + */ + private class TouchBlockView extends FrameLayout { + public TouchBlockView(Context context) { + super(context); + } + + @Override + public boolean dispatchTouchEvent(MotionEvent ev) { + if(!mTouchInTitleBar) { + return false; + } else { + switch(ev.getActionMasked()) { + case MotionEvent.ACTION_UP: + case MotionEvent.ACTION_CANCEL: + mTouchInTitleBar = false; + break; + + default: + + } + + return super.dispatchTouchEvent(ev); + } + } + } + + private static final String TAG = "TitleBarWebView"; + View mTitleBar; + int mTitleBarOffs; + boolean mTouchInTitleBar; + boolean mTouchMove; + private Rect mClipBounds = new Rect(); + private Matrix mMatrix = new Matrix(); + private Method mNativeGetVisibleTitleHeightMethod; + + public TitleBarWebView(Context context) { + super(context); + init(); + } + + public TitleBarWebView(Context context, AttributeSet attrs) { + super(context, attrs); + init(); + } + + public TitleBarWebView(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + init(); + } + + /** + * Gets the correct TitleBarWebView instance for the current API level. + * + * @param context + * @return + */ + public static TitleBarWebView getInstance(Context context) { + if(Build.VERSION.SDK_INT >= 16) { + return new JBTitleBarWebView(context); + } else { + return new TitleBarWebView(context); + } + } + + /** + * Corrects the visual displacement caused by the title bar view. + *

+ * {@inheritDoc} + */ + @Override + public boolean dispatchTouchEvent(MotionEvent event) { + if(mTitleBar != null) { + final int sy = getScrollY(); + final int visTitleHeight = getVisibleTitleHeightCompat(); + final float x = event.getX(); + float y = event.getY(); + + switch(event.getActionMasked()) { + case MotionEvent.ACTION_DOWN: + if(y <= visTitleHeight) { + mTouchInTitleBar = true; + } + break; + + case MotionEvent.ACTION_MOVE: + mTouchMove = true; + break; + + case MotionEvent.ACTION_UP: + case MotionEvent.ACTION_CANCEL: + mTouchMove = false; + break; + + default: + } + + if(mTouchInTitleBar) { + y += sy; + event.setLocation(x, y); + + return mTitleBar.dispatchTouchEvent(event); + } else { + if(Build.VERSION.SDK_INT < 16) { + if(!mTouchMove) { + mTitleBarOffs = getVisibleTitleHeightCompat(); + } + + y -= mTitleBarOffs; + if(y < 0) y = 0; + event.setLocation(x, y); + } + + return super.dispatchTouchEvent(event); + } + } else { + return super.dispatchTouchEvent(event); + } + } + + /** + * Sets a {@link View} as an embedded title bar to appear on top of the + * WebView page. + *

+ * This method tries to call the hidden API method setEmbeddedTitleBar if + * present. On failure the custom implementation provided by this class will + * be used instead. + * + * @param v The view to set or null for removing the title bar view. + */ + public void setEmbeddedTitleBarCompat(View v) { + try { + Method nativeMethod = getClass().getMethod("setEmbeddedTitleBar", + View.class); + nativeMethod.invoke(this, v); + } catch(Exception e) { + Log.d(TAG, + "Native setEmbeddedTitleBar not available. Starting workaround"); + setEmbeddedTitleBarJellyBean(v); + } + } + + @Override + protected int computeVerticalScrollExtent() { + if(mTitleBar == null) return super.computeVerticalScrollExtent(); + return getViewHeightWithTitle() - getVisibleTitleHeightCompat(); + } + + @Override + protected int computeVerticalScrollOffset() { + if(mTitleBar == null) return super.computeVerticalScrollOffset(); + return Math.max(getScrollY() - getTitleHeight(), 0); + } + + @Override + protected boolean drawChild(Canvas canvas, View child, long drawingTime) { + canvas.save(); + + if(child == mTitleBar) { + mTitleBar.offsetLeftAndRight(getScrollX() - mTitleBar.getLeft()); + + if(Build.VERSION.SDK_INT < 16) { + mMatrix.set(canvas.getMatrix()); + mMatrix.postTranslate(0, -getScrollY()); + canvas.setMatrix(mMatrix); + } + } + + boolean result = super.drawChild(canvas, child, drawingTime); + canvas.restore(); + + return result; + } + + /** + * Gets the currently visible height of the title bar view if set. + * + * @return Visible height of title bar view or 0 if not set. + */ + protected int getVisibleTitleHeightCompat() { + if(mTitleBar == null && mNativeGetVisibleTitleHeightMethod != null) { + try { + return (Integer) mNativeGetVisibleTitleHeightMethod + .invoke(this); + } catch(Exception e) { + } + } + + return Math.max(getTitleHeight() - Math.max(0, getScrollY()), 0); + } + + @Override + protected void onDraw(Canvas canvas) { + if(Build.VERSION.SDK_INT >= 16) { + super.onDraw(canvas); + return; + } + + canvas.save(); + + if(mTitleBar != null) { + final int sy = getScrollY(); + final int sx = getScrollX(); + mClipBounds.top = sy; + mClipBounds.left = sx; + mClipBounds.right = mClipBounds.left + getWidth(); + mClipBounds.bottom = mClipBounds.top + getHeight(); + canvas.clipRect(mClipBounds); + mMatrix.set(canvas.getMatrix()); + int titleBarOffs = getVisibleTitleHeightCompat(); + if(titleBarOffs < 0) titleBarOffs = 0; + mMatrix.postTranslate(0, titleBarOffs); + canvas.setMatrix(mMatrix); + } + + super.onDraw(canvas); + canvas.restore(); + } + + /** + * Overrides a hidden method by replicating the behavior of the original + * WebView class from Android 2.3.4. + *

+ * The worst that could happen is that this method never gets called, which + * isn't too bad because this does not harm the functionality of this class. + */ + protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar, + int l, int t, int r, int b) { + int sy = getScrollY(); + + if(sy < 0) { + t -= sy; + } + scrollBar.setBounds(l, t + getVisibleTitleHeightCompat(), r, b); + scrollBar.draw(canvas); + } + + private int getTitleHeight() { + if(mTitleBar != null) return mTitleBar.getHeight(); + return 0; + } + + private int getViewHeightWithTitle() { + int height = getHeight(); + if(isHorizontalScrollBarEnabled() && !overlayHorizontalScrollbar()) { + height -= getHorizontalScrollbarHeight(); + } + return height; + } + + private void init() { + try { + mNativeGetVisibleTitleHeightMethod = WebView.class + .getDeclaredMethod("getVisibleTitleHeight"); + } catch(NoSuchMethodException e) { + Log.w(TAG, + "Could not retrieve native hidden getVisibleTitleHeight method"); + } + } + + /** + * The hidden method setEmbeddedTitleBar has been removed from Jelly Bean. + * This method replicates the functionality. + * + * @param v + */ + private void setEmbeddedTitleBarJellyBean(View v) { + if(mTitleBar == v) return; + + if(mTitleBar != null) { + removeView(mTitleBar); + } + + if(null != v) { + LayoutParams vParams = new LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, + ViewGroup.LayoutParams.WRAP_CONTENT, 0, 0); + + if(Build.VERSION.SDK_INT >= 16) { + TouchBlockView tbv = new TouchBlockView(getContext()); + FrameLayout.LayoutParams tbvParams = new FrameLayout.LayoutParams( + LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); + tbv.addView(v, tbvParams); + addView(tbv, vParams); + v = tbv; + } else { + addView(v, vParams); + } + } + + mTitleBar = v; + } +}