From 5b73f99213039f025fbe6476b440900b387d2cf4 Mon Sep 17 00:00:00 2001 From: vanitasvitae Date: Sun, 9 Oct 2016 00:30:04 +0200 Subject: [PATCH 1/2] Replaced NestedWebView with a ContextMenuWebView in a NestedScrollView in a SwipeRefreshLayout and deleted NestedWebView. No we do have working SwipeRefresh back. Why the hell has this been so simple?? --- .../fragment/BrowserFragment.java | 13 ++ .../ui/ContextMenuWebView.java | 3 +- .../diaspora_android/ui/NestedWebView.java | 142 ------------------ app/src/main/res/layout/browser__fragment.xml | 17 ++- 4 files changed, 28 insertions(+), 147 deletions(-) delete mode 100644 app/src/main/java/com/github/dfa/diaspora_android/ui/NestedWebView.java diff --git a/app/src/main/java/com/github/dfa/diaspora_android/fragment/BrowserFragment.java b/app/src/main/java/com/github/dfa/diaspora_android/fragment/BrowserFragment.java index f790fef8..1bc7195f 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/fragment/BrowserFragment.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/fragment/BrowserFragment.java @@ -13,6 +13,7 @@ import android.os.Bundle; import android.os.Environment; import android.os.StrictMode; import android.support.design.widget.Snackbar; +import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.AlertDialog; import android.view.LayoutInflater; import android.view.Menu; @@ -55,6 +56,7 @@ public class BrowserFragment extends CustomFragment { public static final String TAG = "com.github.dfa.diaspora_android.BrowserFragment"; protected View rootLayout; + protected SwipeRefreshLayout swipeRefreshLayout; protected ContextMenuWebView webView; protected ProgressBar progressBar; protected AppSettings appSettings; @@ -90,6 +92,17 @@ public class BrowserFragment extends CustomFragment { this.progressBar = (ProgressBar) view.findViewById(R.id.progressBar); } + if(this.swipeRefreshLayout == null) { + this.swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh); + this.swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { + @Override + public void onRefresh() { + webView.reload(); + BrowserFragment.this.swipeRefreshLayout.setRefreshing(false); + } + }); + } + if (appSettings.isProxyEnabled()) { if (!setProxy(appSettings.getProxyHost(), appSettings.getProxyPort())) { AppLog.e(this, "Could not enable Proxy"); diff --git a/app/src/main/java/com/github/dfa/diaspora_android/ui/ContextMenuWebView.java b/app/src/main/java/com/github/dfa/diaspora_android/ui/ContextMenuWebView.java index 3be4ce63..02ac27f2 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/ui/ContextMenuWebView.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/ui/ContextMenuWebView.java @@ -35,6 +35,7 @@ import android.support.v4.content.LocalBroadcastManager; import android.util.AttributeSet; import android.view.ContextMenu; import android.view.MenuItem; +import android.webkit.WebView; import android.widget.Toast; import com.github.dfa.diaspora_android.R; @@ -48,7 +49,7 @@ import java.io.File; * or open with another browser */ @SuppressWarnings("deprecation") -public class ContextMenuWebView extends NestedWebView { +public class ContextMenuWebView extends WebView { public static final int ID_SAVE_IMAGE = 10; public static final int ID_IMAGE_EXTERNAL_BROWSER = 11; diff --git a/app/src/main/java/com/github/dfa/diaspora_android/ui/NestedWebView.java b/app/src/main/java/com/github/dfa/diaspora_android/ui/NestedWebView.java deleted file mode 100644 index 4f59a339..00000000 --- a/app/src/main/java/com/github/dfa/diaspora_android/ui/NestedWebView.java +++ /dev/null @@ -1,142 +0,0 @@ -/* - * Copyright (C) 2015 takahirom - * - * 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. - */ - -package com.github.dfa.diaspora_android.ui; - -import android.content.Context; -import android.support.v4.view.MotionEventCompat; -import android.support.v4.view.NestedScrollingChild; -import android.support.v4.view.NestedScrollingChildHelper; -import android.support.v4.view.ViewCompat; -import android.util.AttributeSet; -import android.view.MotionEvent; -import android.webkit.WebView; - -public class NestedWebView extends WebView implements NestedScrollingChild { - private int mLastY; - private final int[] mScrollOffset = new int[2]; - private final int[] mScrollConsumed = new int[2]; - private int mNestedOffsetY; - private NestedScrollingChildHelper mChildHelper; - - public NestedWebView(Context context) { - this(context, null); - } - - public NestedWebView(Context context, AttributeSet attrs) { - this(context, attrs, android.R.attr.webViewStyle); - } - - public NestedWebView(Context context, AttributeSet attrs, int defStyleAttr) { - super(context, attrs, defStyleAttr); - mChildHelper = new NestedScrollingChildHelper(this); - setNestedScrollingEnabled(true); - } - - @Override - public boolean onTouchEvent(MotionEvent ev) { - boolean returnValue = false; - - MotionEvent event = MotionEvent.obtain(ev); - final int action = MotionEventCompat.getActionMasked(event); - if (action == MotionEvent.ACTION_DOWN) { - mNestedOffsetY = 0; - } - int eventY = (int) event.getY(); - event.offsetLocation(0, mNestedOffsetY); - switch (action) { - case MotionEvent.ACTION_MOVE: - int deltaY = mLastY - eventY; - // NestedPreScroll - if (dispatchNestedPreScroll(0, deltaY, mScrollConsumed, mScrollOffset)) { - deltaY -= mScrollConsumed[1]; - mLastY = eventY - mScrollOffset[1]; - event.offsetLocation(0, -mScrollOffset[1]); - mNestedOffsetY += mScrollOffset[1]; - } - returnValue = super.onTouchEvent(event); - - // NestedScroll - if (dispatchNestedScroll(0, mScrollOffset[1], 0, deltaY, mScrollOffset)) { - event.offsetLocation(0, mScrollOffset[1]); - mNestedOffsetY += mScrollOffset[1]; - mLastY -= mScrollOffset[1]; - } - break; - case MotionEvent.ACTION_DOWN: - returnValue = super.onTouchEvent(event); - mLastY = eventY; - // start NestedScroll - startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL); - break; - case MotionEvent.ACTION_UP: - case MotionEvent.ACTION_CANCEL: - returnValue = super.onTouchEvent(event); - // end NestedScroll - stopNestedScroll(); - break; - } - return returnValue; - } - - // Nested Scroll implements - @Override - public void setNestedScrollingEnabled(boolean enabled) { - mChildHelper.setNestedScrollingEnabled(enabled); - } - - @Override - public boolean isNestedScrollingEnabled() { - return mChildHelper.isNestedScrollingEnabled(); - } - - @Override - public boolean startNestedScroll(int axes) { - return mChildHelper.startNestedScroll(axes); - } - - @Override - public void stopNestedScroll() { - mChildHelper.stopNestedScroll(); - } - - @Override - public boolean hasNestedScrollingParent() { - return mChildHelper.hasNestedScrollingParent(); - } - - @Override - public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, - int[] offsetInWindow) { - return mChildHelper.dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, offsetInWindow); - } - - @Override - public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow) { - return mChildHelper.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow); - } - - @Override - public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed) { - return mChildHelper.dispatchNestedFling(velocityX, velocityY, consumed); - } - - @Override - public boolean dispatchNestedPreFling(float velocityX, float velocityY) { - return mChildHelper.dispatchNestedPreFling(velocityX, velocityY); - } - -} \ No newline at end of file diff --git a/app/src/main/res/layout/browser__fragment.xml b/app/src/main/res/layout/browser__fragment.xml index 2ecf4791..15875642 100644 --- a/app/src/main/res/layout/browser__fragment.xml +++ b/app/src/main/res/layout/browser__fragment.xml @@ -3,11 +3,20 @@ xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> - - + android:layout_height="match_parent"> + + + + Date: Sun, 9 Oct 2016 11:39:05 +0200 Subject: [PATCH 2/2] Fixed layout bug and disabled refreshing on new post --- .../dfa/diaspora_android/fragment/BrowserFragment.java | 8 ++++++-- .../dfa/diaspora_android/ui/ContextMenuWebView.java | 1 - app/src/main/res/layout/browser__fragment.xml | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/github/dfa/diaspora_android/fragment/BrowserFragment.java b/app/src/main/java/com/github/dfa/diaspora_android/fragment/BrowserFragment.java index 1bc7195f..a87b8841 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/fragment/BrowserFragment.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/fragment/BrowserFragment.java @@ -30,6 +30,7 @@ import com.github.dfa.diaspora_android.R; import com.github.dfa.diaspora_android.activity.MainActivity; import com.github.dfa.diaspora_android.data.AppSettings; import com.github.dfa.diaspora_android.ui.ContextMenuWebView; +import com.github.dfa.diaspora_android.util.DiasporaUrlHelper; import com.github.dfa.diaspora_android.webview.CustomWebViewClient; import com.github.dfa.diaspora_android.webview.ProgressBarWebChromeClient; import com.github.dfa.diaspora_android.util.AppLog; @@ -62,6 +63,7 @@ public class BrowserFragment extends CustomFragment { protected AppSettings appSettings; protected CustomWebViewClient webViewClient; protected WebSettings webSettings; + protected DiasporaUrlHelper urls; protected String pendingUrl; @@ -81,6 +83,7 @@ public class BrowserFragment extends CustomFragment { if(this.appSettings == null) { this.appSettings = ((App) getActivity().getApplication()).getSettings(); + this.urls = new DiasporaUrlHelper(appSettings); } if(this.webView == null) { @@ -97,7 +100,9 @@ public class BrowserFragment extends CustomFragment { this.swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { - webView.reload(); + if(webView.getUrl() != null && !webView.getUrl().equals(urls.getNewPostUrl())) { + webView.reload(); + } BrowserFragment.this.swipeRefreshLayout.setRefreshing(false); } }); @@ -118,7 +123,6 @@ public class BrowserFragment extends CustomFragment { } webView.setParentActivity(getActivity()); - this.setRetainInstance(true); } diff --git a/app/src/main/java/com/github/dfa/diaspora_android/ui/ContextMenuWebView.java b/app/src/main/java/com/github/dfa/diaspora_android/ui/ContextMenuWebView.java index 02ac27f2..7e0fe46d 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/ui/ContextMenuWebView.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/ui/ContextMenuWebView.java @@ -226,7 +226,6 @@ public class ContextMenuWebView extends WebView { @Override public void loadUrl(String url) { super.loadUrl(url); - // Don't spam intents ;) if (!lasLoadUrl.equals(url)) { Intent updateActivityTitleIntent = new Intent(MainActivity.ACTION_UPDATE_TITLE_FROM_URL); diff --git a/app/src/main/res/layout/browser__fragment.xml b/app/src/main/res/layout/browser__fragment.xml index 15875642..2e7adc33 100644 --- a/app/src/main/res/layout/browser__fragment.xml +++ b/app/src/main/res/layout/browser__fragment.xml @@ -14,7 +14,7 @@ + android:layout_height="wrap_content" />