Browse Source

ClickHandler removed, avoid call loop duirng long press on a webview between BrowserActivity and LightningView

master
Stefano Pacifici 9 years ago
parent
commit
3615018816
  1. 75
      app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java
  2. 3
      app/src/main/java/acr/browser/lightning/app/AppComponent.java
  3. 1
      app/src/main/java/acr/browser/lightning/bus/BrowserEvents.java
  4. 4
      app/src/main/java/acr/browser/lightning/controller/BrowserController.java
  5. 30
      app/src/main/java/acr/browser/lightning/object/ClickHandler.java
  6. 82
      app/src/main/java/acr/browser/lightning/view/LightningView.java

75
app/src/main/java/acr/browser/lightning/activity/BrowserActivity.java

@ -117,7 +117,6 @@ import acr.browser.lightning.database.BookmarkManager; @@ -117,7 +117,6 @@ import acr.browser.lightning.database.BookmarkManager;
import acr.browser.lightning.database.HistoryDatabase;
import acr.browser.lightning.dialog.BookmarksDialogBuilder;
import acr.browser.lightning.fragment.TabsFragment;
import acr.browser.lightning.object.ClickHandler;
import acr.browser.lightning.object.SearchAdapter;
import acr.browser.lightning.preference.PreferenceManager;
import acr.browser.lightning.receiver.NetworkReceiver;
@ -150,7 +149,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -150,7 +149,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
private SearchAdapter mSearchAdapter;
// Callback
private ClickHandler mClickHandler;
private CustomViewCallback mCustomViewCallback;
private ValueCallback<Uri> mUploadMessage;
private ValueCallback<Uri[]> mFilePathCallback;
@ -240,7 +238,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -240,7 +238,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
mActivity = this;
mClickHandler = new ClickHandler(this);
mBrowserFrame = (FrameLayout) findViewById(R.id.content_frame);
mToolbarLayout = (LinearLayout) findViewById(R.id.toolbar_layout);
// initialize background ColorDrawable
@ -1572,26 +1569,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -1572,26 +1569,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
mActivity.startActivityForResult(chooserIntent, 1);
}
/**
* handles long presses for the browser, tries to get the
* url of the item that was clicked and sends it (it can be null)
* to the click handler that does cool stuff with it
*/
@Override
public void onLongPress() {
if (mClickHandler == null) {
mClickHandler = new ClickHandler(mActivity);
}
Message click = mClickHandler.obtainMessage();
if (click != null) {
click.setTarget(mClickHandler);
final WebView currentWebView = tabsManager.getCurrentWebView();
if (currentWebView != null) {
currentWebView.requestFocusNodeHref(click);
}
}
}
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
final LightningView currentTab = tabsManager.getCurrentTab();
@ -1871,58 +1848,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements @@ -1871,58 +1848,6 @@ public abstract class BrowserActivity extends ThemableBrowserActivity implements
}
}
/**
* handles a long click on the page, parameter String url
* is the url that should have been obtained from the WebView touch node
* thingy, if it is null, this method tries to deal with it and find a workaround
*/
@Override
public void longClickPage(final String url) {
HitTestResult result = null;
String currentUrl = null;
final WebView currentWebView = tabsManager.getCurrentWebView();
if (currentWebView != null) {
result = currentWebView.getHitTestResult();
currentUrl = currentWebView.getUrl();
}
if (currentUrl != null && currentUrl.startsWith(Constants.FILE)) {
if (currentUrl.endsWith(HistoryPage.FILENAME)) {
if (url != null) {
longPressHistoryLink(url);
} else if (result != null && result.getExtra() != null) {
final String newUrl = result.getExtra();
longPressHistoryLink(newUrl);
}
} else if (currentUrl.endsWith(Constants.BOOKMARKS_FILENAME)) {
if (url != null) {
bookmarksDialogBuilder.showLongPressedDialogForUrl(this, url);
} else if (result != null && result.getExtra() != null) {
final String newUrl = result.getExtra();
bookmarksDialogBuilder.showLongPressedDialogForUrl(this, newUrl);
}
}
} else {
if (url != null) {
if (result != null) {
if (result.getType() == HitTestResult.SRC_IMAGE_ANCHOR_TYPE || result.getType() == HitTestResult.IMAGE_TYPE) {
longPressImage(url);
} else {
longPressLink(url);
}
} else {
longPressLink(url);
}
} else if (result != null && result.getExtra() != null) {
final String newUrl = result.getExtra();
if (result.getType() == HitTestResult.SRC_IMAGE_ANCHOR_TYPE || result.getType() == HitTestResult.IMAGE_TYPE) {
longPressImage(newUrl);
} else {
longPressLink(newUrl);
}
}
}
}
private void longPressHistoryLink(final String url) {
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override

3
app/src/main/java/acr/browser/lightning/app/AppComponent.java

@ -15,6 +15,7 @@ import acr.browser.lightning.fragment.LightningPreferenceFragment; @@ -15,6 +15,7 @@ import acr.browser.lightning.fragment.LightningPreferenceFragment;
import acr.browser.lightning.fragment.TabsFragment;
import acr.browser.lightning.object.SearchAdapter;
import acr.browser.lightning.preference.PreferenceManager;
import acr.browser.lightning.view.LightningView;
import dagger.Component;
/**
@ -47,4 +48,6 @@ public interface AppComponent { @@ -47,4 +48,6 @@ public interface AppComponent {
Bus getBus();
Context getApplicationContext();
void inject(LightningView lightningView);
}

1
app/src/main/java/acr/browser/lightning/bus/BrowserEvents.java

@ -1,6 +1,7 @@ @@ -1,6 +1,7 @@
package acr.browser.lightning.bus;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import acr.browser.lightning.R;

4
app/src/main/java/acr/browser/lightning/controller/BrowserController.java

@ -24,7 +24,7 @@ public interface BrowserController { @@ -24,7 +24,7 @@ public interface BrowserController {
void openFileChooser(ValueCallback<Uri> uploadMsg);
void onLongPress();
// void onLongPress();
void onShowCustomView(View view, CustomViewCallback callback);
@ -44,7 +44,7 @@ public interface BrowserController { @@ -44,7 +44,7 @@ public interface BrowserController {
void showActionBar();
void longClickPage(String url);
// void longClickPage(String url);
void showFileChooser(ValueCallback<Uri[]> filePathCallback);

30
app/src/main/java/acr/browser/lightning/object/ClickHandler.java

@ -1,30 +0,0 @@ @@ -1,30 +0,0 @@
/*
* Copyright 2014 A.C.R. Development
*/
package acr.browser.lightning.object;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import acr.browser.lightning.controller.BrowserController;
public class ClickHandler extends Handler {
private BrowserController mBrowserController;
public ClickHandler(Context context) {
try {
mBrowserController = (BrowserController) context;
} catch (ClassCastException e) {
throw new ClassCastException(context + " must implement BrowserController");
}
}
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
String url = msg.getData().getString("url");
mBrowserController.longClickPage(url);
}
}

82
app/src/main/java/acr/browser/lightning/view/LightningView.java

@ -20,6 +20,7 @@ import android.net.MailTo; @@ -20,6 +20,7 @@ import android.net.MailTo;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
@ -63,9 +64,12 @@ import javax.inject.Inject; @@ -63,9 +64,12 @@ import javax.inject.Inject;
import acr.browser.lightning.R;
import acr.browser.lightning.app.BrowserApp;
import acr.browser.lightning.bus.BrowserEvents;
import acr.browser.lightning.bus.TabEvents;
import acr.browser.lightning.constant.Constants;
import acr.browser.lightning.constant.HistoryPage;
import acr.browser.lightning.constant.StartPage;
import acr.browser.lightning.controller.BrowserController;
import acr.browser.lightning.dialog.BookmarksDialogBuilder;
import acr.browser.lightning.download.LightningDownloadListener;
import acr.browser.lightning.preference.PreferenceManager;
import acr.browser.lightning.utils.AdBlock;
@ -87,7 +91,6 @@ public class LightningView { @@ -87,7 +91,6 @@ public class LightningView {
private static String mDefaultUserAgent;
// TODO fix so that mWebpageBitmap can be static - static changes the icon when switching from light to dark and then back to light
private final Bitmap mWebpageBitmap;
PreferenceManager mPreferences;
private final AdBlock mAdBlock;
private final IntentUtils mIntentUtils;
private final Paint mPaint = new Paint();
@ -106,13 +109,20 @@ public class LightningView { @@ -106,13 +109,20 @@ public class LightningView {
};
private final PermissionsManager mPermissionsManager;
private static final String[] PERMISSIONS = new String[]{Manifest.permission.ACCESS_FINE_LOCATION};
private final WebViewHandler webViewHandler = new WebViewHandler();
@Inject
Bus eventBus;
@Inject
PreferenceManager mPreferences;
private final Bus eventBus;
@Inject
BookmarksDialogBuilder bookmarksDialogBuilder;
@SuppressLint("NewApi")
public LightningView(Activity activity, String url, boolean darkTheme, boolean isIncognito) {
eventBus = BrowserApp.getAppComponent().getBus();
mPreferences = BrowserApp.getAppComponent().getPreferenceManager();
BrowserApp.getAppComponent().inject(this);
mActivity = activity;
mWebView = new WebView(activity);
mIsIncognitoTab = isIncognito;
@ -613,6 +623,52 @@ public class LightningView { @@ -613,6 +623,52 @@ public class LightningView {
}
}
/**
* handles a long click on the page, parameter String url
* is the url that should have been obtained from the WebView touch node
* thingy, if it is null, this method tries to deal with it and find a workaround
*/
private void longClickPage(final String url) {
final WebView.HitTestResult result = mWebView.getHitTestResult();
String currentUrl = mWebView.getUrl();
if (currentUrl != null && currentUrl.startsWith(Constants.FILE)) {
if (currentUrl.endsWith(HistoryPage.FILENAME)) {
if (url != null) {
// TODO longPressHistoryLink(url);
} else if (result != null && result.getExtra() != null) {
final String newUrl = result.getExtra();
// TODO longPressHistoryLink(newUrl);
}
} else if (currentUrl.endsWith(Constants.BOOKMARKS_FILENAME)) {
if (url != null) {
bookmarksDialogBuilder.showLongPressedDialogForUrl(mActivity, url);
} else if (result != null && result.getExtra() != null) {
final String newUrl = result.getExtra();
bookmarksDialogBuilder.showLongPressedDialogForUrl(mActivity, newUrl);
}
}
} else {
if (url != null) {
if (result != null) {
if (result.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE || result.getType() == WebView.HitTestResult.IMAGE_TYPE) {
// TODO longPressImage(url);
} else {
// TODO longPressLink(url);
}
} else {
// TODO longPressLink(url);
}
} else if (result != null && result.getExtra() != null) {
final String newUrl = result.getExtra();
if (result.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE || result.getType() == WebView.HitTestResult.IMAGE_TYPE) {
// TODO longPressImage(newUrl);
} else {
// TODO longPressLink(newUrl);
}
}
}
}
public boolean canGoBack() {
return mWebView != null && mWebView.canGoBack();
}
@ -1149,8 +1205,13 @@ public class LightningView { @@ -1149,8 +1205,13 @@ public class LightningView {
@Override
public void onLongPress(MotionEvent e) {
if (mCanTriggerLongPress)
mBrowserController.onLongPress();
if (mCanTriggerLongPress) {
Message msg = webViewHandler.obtainMessage();
if (msg != null) {
msg.setTarget(webViewHandler);
mWebView.requestFocusNodeHref(msg);
}
}
}
/**
@ -1172,4 +1233,13 @@ public class LightningView { @@ -1172,4 +1233,13 @@ public class LightningView {
mCanTriggerLongPress = true;
}
}
private class WebViewHandler extends Handler {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
final String url = msg.getData().getString("url");
longClickPage(url);
}
}
}

Loading…
Cancel
Save