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.graphics.Bitmap;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import androidx.fragment.app.FragmentActivity;
|
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.webkit.WebChromeClient;
|
import android.webkit.WebChromeClient;
|
||||||
import android.webkit.WebView;
|
import android.webkit.WebView;
|
||||||
import android.webkit.WebViewClient;
|
import android.webkit.WebViewClient;
|
||||||
|
|
||||||
|
import androidx.fragment.app.FragmentActivity;
|
||||||
|
|
||||||
import com.newsblur.databinding.ActivityInAppBrowserBinding;
|
import com.newsblur.databinding.ActivityInAppBrowserBinding;
|
||||||
import com.newsblur.util.PrefsUtils;
|
import com.newsblur.util.PrefsUtils;
|
||||||
|
|
||||||
|
@ -53,13 +54,4 @@ public class InAppBrowser extends FragmentActivity {
|
||||||
|
|
||||||
binding.webView.loadUrl(url);
|
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() {
|
binding.readingItemTitle.setOnClickListener(new OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
Intent i = new Intent(Intent.ACTION_VIEW);
|
|
||||||
try {
|
try {
|
||||||
i.setData(Uri.parse(story.permalink));
|
UIUtils.handleUri(requireContext(), Uri.parse(story.permalink));
|
||||||
startActivity(i);
|
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
// we don't actually know if the user will successfully be able to open whatever string
|
// 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
|
// 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.Paint;
|
||||||
import android.graphics.Shader;
|
import android.graphics.Shader;
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
|
import android.net.Uri;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
@ -541,4 +542,43 @@ public class UIUtils {
|
||||||
return result;
|
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.annotation.TargetApi;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
|
@ -17,11 +16,9 @@ import android.webkit.WebView;
|
||||||
import android.webkit.WebViewClient;
|
import android.webkit.WebViewClient;
|
||||||
import android.widget.FrameLayout;
|
import android.widget.FrameLayout;
|
||||||
|
|
||||||
import com.newsblur.activity.InAppBrowser;
|
|
||||||
import com.newsblur.activity.Reading;
|
import com.newsblur.activity.Reading;
|
||||||
import com.newsblur.fragment.ReadingItemFragment;
|
import com.newsblur.fragment.ReadingItemFragment;
|
||||||
import com.newsblur.util.DefaultBrowser;
|
import com.newsblur.util.UIUtils;
|
||||||
import com.newsblur.util.PrefsUtils;
|
|
||||||
|
|
||||||
public class NewsblurWebview extends WebView {
|
public class NewsblurWebview extends WebView {
|
||||||
|
|
||||||
|
@ -93,14 +90,14 @@ public class NewsblurWebview extends WebView {
|
||||||
class NewsblurWebViewClient extends WebViewClient {
|
class NewsblurWebViewClient extends WebViewClient {
|
||||||
@Override
|
@Override
|
||||||
public boolean shouldOverrideUrlLoading(WebView view, String url) {
|
public boolean shouldOverrideUrlLoading(WebView view, String url) {
|
||||||
handleUri(Uri.parse(url));
|
UIUtils.handleUri(context, Uri.parse(url));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@TargetApi(Build.VERSION_CODES.N)
|
@TargetApi(Build.VERSION_CODES.N)
|
||||||
@Override
|
@Override
|
||||||
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
|
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
|
||||||
handleUri(request.getUrl());
|
UIUtils.handleUri(context, request.getUrl());
|
||||||
return true;
|
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
|
// this WCC implements the bare minimum callbacks to get HTML5 fullscreen video working
|
||||||
class NewsblurWebChromeClient extends WebChromeClient {
|
class NewsblurWebChromeClient extends WebChromeClient {
|
||||||
public View customView;
|
public View customView;
|
||||||
|
|
Loading…
Add table
Reference in a new issue