mirror of
https://github.com/viq/NewsBlur.git
synced 2025-09-18 21:43:31 +00:00
#1319 Built in app browser
This commit is contained in:
parent
96572bc0e1
commit
2c04ddbbf8
4 changed files with 47 additions and 60 deletions
|
@ -2,12 +2,13 @@ package com.newsblur.activity;
|
|||
|
||||
import android.graphics.Bitmap;
|
||||
import android.os.Bundle;
|
||||
import androidx.fragment.app.FragmentActivity;
|
||||
import android.view.View;
|
||||
import android.webkit.WebChromeClient;
|
||||
import android.webkit.WebView;
|
||||
import android.webkit.WebViewClient;
|
||||
|
||||
import androidx.fragment.app.FragmentActivity;
|
||||
|
||||
import com.newsblur.databinding.ActivityInAppBrowserBinding;
|
||||
import com.newsblur.util.PrefsUtils;
|
||||
|
||||
|
@ -53,13 +54,4 @@ public class InAppBrowser extends FragmentActivity {
|
|||
|
||||
binding.webView.loadUrl(url);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
if (binding.webView.canGoBack()) {
|
||||
binding.webView.goBack();
|
||||
} else {
|
||||
finish();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -499,10 +499,8 @@ public class ReadingItemFragment extends NbFragment implements PopupMenu.OnMenuI
|
|||
binding.readingItemTitle.setOnClickListener(new OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
Intent i = new Intent(Intent.ACTION_VIEW);
|
||||
try {
|
||||
i.setData(Uri.parse(story.permalink));
|
||||
startActivity(i);
|
||||
UIUtils.handleUri(requireContext(), Uri.parse(story.permalink));
|
||||
} catch (Throwable t) {
|
||||
// we don't actually know if the user will successfully be able to open whatever string
|
||||
// was in the permalink or if the Intent could throw errors
|
||||
|
|
|
@ -18,6 +18,7 @@ import android.graphics.Color;
|
|||
import android.graphics.Paint;
|
||||
import android.graphics.Shader;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.util.Log;
|
||||
|
@ -541,4 +542,43 @@ public class UIUtils {
|
|||
return result;
|
||||
}
|
||||
|
||||
public static void handleUri(Context context, Uri uri) {
|
||||
DefaultBrowser defaultBrowser = PrefsUtils.getDefaultBrowser(context);
|
||||
if (defaultBrowser == DefaultBrowser.SYSTEM_DEFAULT) {
|
||||
openSystemDefaultBrowser(context, uri);
|
||||
} else if (defaultBrowser == DefaultBrowser.IN_APP_BROWSER) {
|
||||
Intent intent = new Intent(context, InAppBrowser.class);
|
||||
intent.putExtra(InAppBrowser.URI, uri);
|
||||
context.startActivity(intent);
|
||||
} else if (defaultBrowser == DefaultBrowser.CHROME) {
|
||||
openExternalBrowserApp(context, uri, "com.android.chrome");
|
||||
} else if (defaultBrowser == DefaultBrowser.FIREFOX) {
|
||||
openExternalBrowserApp(context, uri, "org.mozilla.firefox");
|
||||
} else if (defaultBrowser == DefaultBrowser.OPERA_MINI) {
|
||||
openExternalBrowserApp(context, uri, "com.opera.mini.native");
|
||||
}
|
||||
}
|
||||
|
||||
public static void openSystemDefaultBrowser(Context context, Uri uri) {
|
||||
try {
|
||||
Intent intent = new Intent(Intent.ACTION_VIEW);
|
||||
intent.setData(uri);
|
||||
context.startActivity(intent);
|
||||
} catch (Exception e) {
|
||||
com.newsblur.util.Log.e(context.getClass().getName(), "device cannot open URLs");
|
||||
}
|
||||
}
|
||||
|
||||
public static void openExternalBrowserApp(Context context, Uri uri, String packageName) {
|
||||
try {
|
||||
Intent intent = new Intent(Intent.ACTION_VIEW);
|
||||
intent.setData(uri);
|
||||
intent.setPackage(packageName);
|
||||
context.startActivity(intent);
|
||||
} catch (Exception e) {
|
||||
com.newsblur.util.Log.e(context.getClass().getName(), "apps not available to open URLs");
|
||||
// fallback to system default if apps cannot be opened
|
||||
openSystemDefaultBrowser(context, uri);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ package com.newsblur.view;
|
|||
|
||||
import android.annotation.TargetApi;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.util.AttributeSet;
|
||||
|
@ -17,11 +16,9 @@ import android.webkit.WebView;
|
|||
import android.webkit.WebViewClient;
|
||||
import android.widget.FrameLayout;
|
||||
|
||||
import com.newsblur.activity.InAppBrowser;
|
||||
import com.newsblur.activity.Reading;
|
||||
import com.newsblur.fragment.ReadingItemFragment;
|
||||
import com.newsblur.util.DefaultBrowser;
|
||||
import com.newsblur.util.PrefsUtils;
|
||||
import com.newsblur.util.UIUtils;
|
||||
|
||||
public class NewsblurWebview extends WebView {
|
||||
|
||||
|
@ -93,14 +90,14 @@ public class NewsblurWebview extends WebView {
|
|||
class NewsblurWebViewClient extends WebViewClient {
|
||||
@Override
|
||||
public boolean shouldOverrideUrlLoading(WebView view, String url) {
|
||||
handleUri(Uri.parse(url));
|
||||
UIUtils.handleUri(context, Uri.parse(url));
|
||||
return true;
|
||||
}
|
||||
|
||||
@TargetApi(Build.VERSION_CODES.N)
|
||||
@Override
|
||||
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
|
||||
handleUri(request.getUrl());
|
||||
UIUtils.handleUri(context, request.getUrl());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -113,46 +110,6 @@ public class NewsblurWebview extends WebView {
|
|||
}
|
||||
}
|
||||
|
||||
private void handleUri(Uri uri) {
|
||||
DefaultBrowser defaultBrowser = PrefsUtils.getDefaultBrowser(context);
|
||||
if (defaultBrowser == DefaultBrowser.SYSTEM_DEFAULT) {
|
||||
openSystemDefaultBrowser(uri);
|
||||
} else if (defaultBrowser == DefaultBrowser.IN_APP_BROWSER) {
|
||||
Intent intent = new Intent(context, InAppBrowser.class);
|
||||
intent.putExtra(InAppBrowser.URI, uri);
|
||||
context.startActivity(intent);
|
||||
} else if (defaultBrowser == DefaultBrowser.CHROME) {
|
||||
openExternalBrowserApp(uri, "com.android.chrome");
|
||||
} else if (defaultBrowser == DefaultBrowser.FIREFOX) {
|
||||
openExternalBrowserApp(uri, "org.mozilla.firefox");
|
||||
} else if (defaultBrowser == DefaultBrowser.OPERA_MINI) {
|
||||
openExternalBrowserApp(uri, "com.opera.mini.native");
|
||||
}
|
||||
}
|
||||
|
||||
private void openSystemDefaultBrowser(Uri uri) {
|
||||
try {
|
||||
Intent intent = new Intent(Intent.ACTION_VIEW);
|
||||
intent.setData(uri);
|
||||
context.startActivity(intent);
|
||||
} catch (Exception e) {
|
||||
com.newsblur.util.Log.e(this.getClass().getName(), "device cannot open URLs");
|
||||
}
|
||||
}
|
||||
|
||||
private void openExternalBrowserApp(Uri uri, String packageName) {
|
||||
try {
|
||||
Intent intent = new Intent(Intent.ACTION_VIEW);
|
||||
intent.setData(uri);
|
||||
intent.setPackage(packageName);
|
||||
context.startActivity(intent);
|
||||
} catch (Exception e) {
|
||||
com.newsblur.util.Log.e(this.getClass().getName(), "apps not available to open URLs");
|
||||
// fallback to system default if apps cannot be opened
|
||||
openSystemDefaultBrowser(uri);
|
||||
}
|
||||
}
|
||||
|
||||
// this WCC implements the bare minimum callbacks to get HTML5 fullscreen video working
|
||||
class NewsblurWebChromeClient extends WebChromeClient {
|
||||
public View customView;
|
||||
|
|
Loading…
Add table
Reference in a new issue