Adding context menu for links.

This commit is contained in:
Sergiotarxz 2023-09-01 00:41:25 +02:00
parent 9c5475d134
commit f347d55413
4 changed files with 209 additions and 56 deletions

View File

@ -7,10 +7,17 @@ import android.webkit.JavascriptInterface;
import android.webkit.ConsoleMessage;
import android.webkit.WebChromeClient;
import android.util.Log;
import android.os.Message;
import android.os.Handler;
import android.os.Looper;
import android.view.ViewTreeObserver;
import android.view.MotionEvent;
import android.view.MenuItem;
import android.view.Menu;
import android.content.Intent;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.app.PendingIntent;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import android.view.View;
@ -23,12 +30,28 @@ import android.graphics.drawable.Icon;
import java.io.InputStream;
import java.net.URL;
import android.content.pm.ApplicationInfo;
import android.widget.PopupMenu;
import android.widget.PopupWindow;
import android.widget.FrameLayout;
import android.util.DisplayMetrics;
import android.view.ContextMenu;
import android.view.MenuInflater;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.content.pm.ShortcutManager;
class MenuId {
public static final int OPEN_LINK = 0;
public static final int OPEN_LINK_BROWSER = 1;
public static final int COPY_LINK = 2;
public static final int OPEN_IMAGE_BROWSER = 4;
public static final int COPY_IMAGE_URL = 5;
public static final int COPY_TEXT = 6;
};
public class MainActivity extends AppCompatActivity {
private WebView webView;
@ -36,49 +59,9 @@ public class MainActivity extends AppCompatActivity {
private RelativeLayout relativeLayout;
String baseUrl = "https://burguillos.info";
private static Bitmap getBitmap(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Bitmap d = BitmapFactory.decodeStream(is);
is.close();
return d;
} catch (Exception e) {
Log.w("BurguillosInfo", e);
return null;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.web);
swipeRefreshLayout = findViewById(R.id.swipeContainer);
relativeLayout = findViewById(R.id.relative);
swipeRefreshLayout.setOnRefreshListener( new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
webView.reload();
}
});
webView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final int action = event.getAction() & MotionEvent.ACTION_MASK;
if (action != MotionEvent.ACTION_DOWN) {
return false;
}
if (event.getY() > 70) {
swipeRefreshLayout.setEnabled(false);
return false;
}
swipeRefreshLayout.setEnabled(true);
return false;
}
});
String hrefContextMenu = null;
String srcContextMenu = null;
String titleContextMenu = null;
class WebCallsAndroidCode {
@JavascriptInterface
@ -128,6 +111,160 @@ public class MainActivity extends AppCompatActivity {
Log.w("BurguillosInfo", "added request pin.");
}
}
@Override
public boolean onContextItemSelected (MenuItem item) {
final int id = item.getItemId();
ClipData clip = null;
ClipboardManager clipboard = (ClipboardManager) getSystemService(this.CLIPBOARD_SERVICE);
Intent targetIntent = null;
Uri targetUri = null;
switch (id) {
case MenuId.OPEN_LINK:
targetUri = Uri.parse(hrefContextMenu);
targetIntent = new Intent();
targetIntent.setAction(Intent.ACTION_VIEW);
targetIntent.setData(targetUri);
startActivity(targetIntent);
break;
case MenuId.OPEN_LINK_BROWSER:
new WebCallsAndroidCode().openInBrowser(hrefContextMenu);
break;
case MenuId.COPY_LINK:
clip = ClipData.newPlainText(hrefContextMenu, hrefContextMenu);
clipboard.setPrimaryClip(clip);
break;
case MenuId.OPEN_IMAGE_BROWSER:
new WebCallsAndroidCode().openInBrowser(srcContextMenu);
break;
case MenuId.COPY_IMAGE_URL:
clip = ClipData.newPlainText(srcContextMenu, srcContextMenu);
clipboard.setPrimaryClip(clip);
break;
case MenuId.COPY_TEXT:
clip = ClipData.newPlainText(titleContextMenu, titleContextMenu);
clipboard.setPrimaryClip(clip);
break;
default:
Log.w("BurguillosInfo", "No recognized id in context menu.");
}
return false;
}
private static Bitmap getBitmap(String url) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Bitmap d = BitmapFactory.decodeStream(is);
is.close();
return d;
} catch (Exception e) {
Log.w("BurguillosInfo", e);
return null;
}
}
public boolean onLongClick(View v) {
return false;
}
@Override
public void onCreateContextMenu (ContextMenu menu,
View v,
ContextMenu.ContextMenuInfo menuinfo) {
new MenuInflater(MainActivity.this).inflate(R.menu.long_click_webview, menu);
if (hrefContextMenu != null) {
menu.add(Menu.NONE, MenuId.OPEN_LINK, Menu.NONE, "Abrir enlace.");
menu.add(Menu.NONE, MenuId.OPEN_LINK_BROWSER, Menu.NONE, "Abrir enlace en el navegador.");
menu.add(Menu.NONE, MenuId.COPY_LINK, Menu.NONE, "Copiar enlace.");
}
if (srcContextMenu != null) {
menu.add(Menu.NONE, MenuId.OPEN_IMAGE_BROWSER, Menu.NONE, "Abrir imagen en el navegador.");
menu.add(Menu.NONE, MenuId.COPY_IMAGE_URL, Menu.NONE, "Copiar enlace de la imagen.");
}
if (titleContextMenu != null) {
menu.add(Menu.NONE, MenuId.COPY_TEXT, Menu.NONE, "Copiar texto del enlace.");
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.web);
Looper looper = Looper.getMainLooper();
final Handler handler = new Handler(looper) {
@Override
public void handleMessage(Message message) {
Bundle bundle = message.getData();
for (String key: bundle.keySet())
{
Log.w ("BurguillosInfo", key + " is a key in the bundle");
}
srcContextMenu = null;
hrefContextMenu = null;
titleContextMenu = null;
CharSequence value = bundle.getCharSequence("src");
if (value != null) {
srcContextMenu = value + "";
}
value = bundle.getCharSequence("url");
if (value != null) {
hrefContextMenu = value + "";
}
value = bundle.getCharSequence("title");
if (value != null) {
titleContextMenu = value + "";
}
MainActivity.this.registerForContextMenu(webView);
MainActivity.this.openContextMenu(webView);
MainActivity.this.unregisterForContextMenu(webView);
}
};
webView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
WebView.HitTestResult hit = webView.getHitTestResult();
if (hit == null
|| hit.getType() == WebView.HitTestResult.UNKNOWN_TYPE
|| hit.getType() == WebView.HitTestResult.EDIT_TEXT_TYPE) {
return false;
}
Log.w("BurguillosInfo", hit.getType()+"");
Message message = handler.obtainMessage();
webView.requestFocusNodeHref(message);
return true;
}
});
swipeRefreshLayout = findViewById(R.id.swipeContainer);
relativeLayout = findViewById(R.id.relative);
swipeRefreshLayout.setOnRefreshListener( new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
webView.reload();
}
});
webView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final int action = event.getAction() & MotionEvent.ACTION_MASK;
if (action != MotionEvent.ACTION_DOWN) {
return false;
}
if (event.getY() > 100) {
swipeRefreshLayout.setEnabled(false);
return false;
}
swipeRefreshLayout.setEnabled(true);
return false;
}
});
webView.addJavascriptInterface(new WebCallsAndroidCode(), "Android");
Intent resultIntent = getIntent();

View File

@ -21,7 +21,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteX="8dp"
tools:layout_editor_absoluteY="8dp" />
tools:layout_editor_absoluteY="8dp"/>
</RelativeLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<!-- <item
android:id="@+id/open_link"
android:title="Open Link"/>
<item
android:id="@+id/open_link_browser"
android:title="Open Link in Browser"/>
<item
android:id="@+id/copy_link"
android:title="Copy Link"/>
<item
android:id="@+id/copy_text"
android:title="Copy Text"/>
-->
</menu>