Skip to content
Merged
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package me.ghui.v2er.module.vshare;

import android.annotation.SuppressLint;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

Expand All @@ -26,6 +30,7 @@
*/
public class VshareWebActivity extends AppCompatActivity {

private static final String TAG = "VshareWebActivity";
private static final String VSHARE_BASE_URL = "https://v2er.app/vshare";

@BindView(R.id.webview)
Expand Down Expand Up @@ -114,8 +119,12 @@ private void setupWebView() {
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
// Let the WebView handle the navigation
return false;
return handleUrlLoading(request.getUrl().toString());
}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return handleUrlLoading(url);
}

@Override
Expand Down Expand Up @@ -149,6 +158,88 @@ public void onProgressChanged(WebView view, int newProgress) {
});
}

/**
* Handle URL loading for WebView
* Returns true if the URL was handled externally, false if WebView should load it
*/
private boolean handleUrlLoading(String url) {
if (url == null) {
return false;
}

Uri uri = Uri.parse(url);
String scheme = uri.getScheme();

// Handle intent:// URLs (e.g., Google Play Store links)
if ("intent".equals(scheme)) {
try {
Intent intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);

// Check if there's an app that can handle this intent
if (getPackageManager().resolveActivity(intent, 0) != null) {
Copy link

Copilot AI Oct 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The resolveActivity() call with flags=0 is deprecated in API level 30+. Use PackageManager.ResolveInfo flags or PackageManager.MATCH_DEFAULT_ONLY for better compatibility.

Copilot uses AI. Check for mistakes.
startActivity(intent);
return true;
}

// Fallback: Try to open the browser_fallback_url if available
String fallbackUrl = intent.getStringExtra("browser_fallback_url");
if (fallbackUrl != null) {
mWebView.loadUrl(fallbackUrl);
return true;
}

// Last resort: Try to open in Google Play if it's a Play Store intent
String packageName = intent.getPackage();
if (packageName != null) {
Intent marketIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("market://details?id=" + packageName));
marketIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (getPackageManager().resolveActivity(marketIntent, 0) != null) {
Copy link

Copilot AI Oct 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The resolveActivity() call with flags=0 is deprecated in API level 30+. Use PackageManager.ResolveInfo flags or PackageManager.MATCH_DEFAULT_ONLY for better compatibility.

Copilot uses AI. Check for mistakes.
startActivity(marketIntent);
return true;
}
}
} catch (Exception e) {
Log.e(TAG, "Error handling intent URL: " + url, e);
Toast.makeText(this, "Unable to open app", Toast.LENGTH_SHORT).show();
}
return true;
}

// Handle market:// URLs (Google Play Store)
if ("market".equals(scheme)) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
return true;
} catch (ActivityNotFoundException e) {
Log.e(TAG, "Google Play Store not found", e);
// Fallback to web version
String webUrl = url.replace("market://", "https://play.google.com/store/apps/");
mWebView.loadUrl(webUrl);
return true;
}
}

// Handle other app-specific schemes (e.g., mailto:, tel:, etc.)
if (!"http".equals(scheme) && !"https".equals(scheme)) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
return true;
} catch (ActivityNotFoundException e) {
Log.e(TAG, "No app found to handle scheme: " + scheme, e);
Toast.makeText(this, "No app found to open this link", Toast.LENGTH_SHORT).show();
return true;
}
}

// Let WebView handle normal http/https URLs
return false;
}

@Override
public void onBackPressed() {
// Handle back navigation in WebView
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/layout/activity_vshare_web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
android:layout_height="match_parent"
android:fitsSystemWindows="true" />

<ProgressBar
android:id="@+id/progress_bar"
Expand Down
Loading