how to send widevine data from webview to external player - java

i have webview where if you create a url ending in m3u8 it will open an external player,
#Override
// Force links to be opened inside WebView and not in Default Browser
// Thanks http://stackoverflow.com/a/33681975/1815624
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.endsWith(".m3u8") || url.endsWith(".ts") || url.endsWith(".mpd") || url.endsWith(".mp4"))
{
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(url), "video/*");
intent.setPackage("com.genuine.leone");
view.getContext().startActivity(intent);
return true; // to tell the WebView that it should not load the URL because you handled it by yourself
}
catch (ActivityNotFoundException e) {
// Player is not installed, you may want to open the play store link
}
}
the problem now, m3u8 requires a widevine license to play, how to send a widevine license url to the external player
i have to research but not find anything, any idea?

Related

java.net.ProtocalException while using certain URL in VideoView android

I have the below list of test video links which i am am trying to set it on my VideoView. The first two works fine but the third one shows java.net.ProtocalException and I have the same problem when i user the link from my local Spring Web Application Server. How to play such link in videoView?
The list of links
1. VIDEO_SOURCE = "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/SubaruOutbackOnStreetAndDirt.mp4";
2. VIDEO_SOURCE = "https://media.geeksforgeeks.org/wp-content/uploads/20201217192146/Screenrecorder-2020-12-17-19-17-36-828.mp4?_=1";
3. VIDEO_SOURCE = "https://www.learningcontainer.com/wp-content/uploads/2020/05/sample-mp4-file.mp4";
VideoView Code
if( URLUtil.isValidUrl(VIDEO_SOURCE)){
try{
videoUri = getMedia(VIDEO_SOURCE);
// Set up the media controller widget and attach it to the video view.
MediaController controller = new MediaController(_CourseContents.this);
// anchor view for the videoView
controller.setAnchorView(mVideoView);
// sets the media player to the videoView
controller.setMediaPlayer(mVideoView);
// sets the media controller to the videoView
mVideoView.setMediaController(controller);
mVideoView.setVideoURI(videoUri);
try {
HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
} catch (Exception e) {
e.printStackTrace();
}
progressBar.setVisibility(View.GONE);
}
catch(Exception e){
Toast.makeText(getApplicationContext(), "Cannot parse video source:" + e.getMessage(), Toast.LENGTH_LONG).show();
finish();
}}else {
Toast.makeText(getApplicationContext(), "Invalid video source.", Toast.LENGTH_LONG).show();
finish();
}
The Error

Download instead of load url in WebView when url contains x

I have an Android app that is pretty much a WebView containing a mobile website. I have minimum control over the code of the website but can inject javascript.
The website contains download links to PDF, DOC, ZIP etc. files, with most of them only accessible if the user is logged in. At first I simply made the app open links containing "FileHandler.axd" (the script that handles files) in the normal browser, but if users are not logged in through the app ánd browser they'll just get an error.
So now I'm trying to make the app use the download manager if a link is tapped but am having some trouble as it's not really working at all. I don't have a lot of experience developing Android apps so any lead on where I'm doing it wrong would be greatly appreciated.
These are my permissions:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I marked the code I added last with a "DOWNLOAD MANAGER CODE BELOW" comment block.
This is part of my MainActivity.java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
if(CheckNetwork.isInternetAvailable(MainActivity.this)) {
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
// Get current UserAgent
String curua = mWebView.getSettings().getUserAgentString();
// Add -comapp string to useragent for detection
mWebView.getSettings().setUserAgentString(curua + "-comapp");
// Use remote resource
mWebView.loadUrl("https://www.onsplatform.tv/comapp");
// Stop local links and redirects from opening in browser instead of WebView
mWebView.setWebViewClient(new MyAppWebViewClient(){
#Override
public void onPageFinished(WebView view, String url) {
//hide loading image
findViewById(R.id.imageLoading1).setVisibility(View.GONE);
}
});
/////////////////////////////////
// DOWNLOAD MANAGER CODE BELOW //
/////////////////////////////////
if(mWebView.getUrl().contains("FileHandler.axd")){
String url = mWebView.getUrl();
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "download");
// You can change the name of the downloads, by changing "download" to everything you want, such as the mWebview title...
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
}
} else {
//no connection
Toast toast = Toast.makeText(MainActivity.this, "geen internet / no internet", Toast.LENGTH_LONG);
toast.show();
}
}
Edit
I'm getting a feeling I put the code in the wrong place. I'm guessing I need a public method for the WebViewClient? But which one, onLoadResource()? I've been reading the documentation but I don't really know if I'm looking at the right stuff.
I'm guessing there's a sort of event handler that fires when a link is loading, maybe being able to intercept that, start the download manager for that url and prevent the url from loading in the webview?
I have a class that extends WebViewClient, I'm guessing that would be the place:
public class MyAppWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().endsWith("onsplatform.tv")) { // && !url.toLowerCase().contains("filehandler")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}

Loading PDF from internal storage Error invalid format

So I've been practising download manager and i was trying out some program.
A first button will download a said Pdf to a location in my phone.
The second button is supposed to open the Pdf file with a Pdf reader installed by the user such as WP Reader etc..
The download works fine, but when i open the pdf , it says invalid format.
I uploaded a sample Pdf to the google drive , so I know that the uploaded file isnt corrupted in any case. There muse be some problem when the file is being downloaded from the server. Please help me find the mistake. And I am relatively new to Android.
And the download button uses an Onclicklistener while the loadPdf is given in the xml file android:onClick="downloadPdf" .
public class MainActivity extends AppCompatActivity {
String myHTTPUrl = "https://drive.google.com/open?id=0B5Pev9zz5bVjZTFFZ1dLZVp1WVU";
String TAG = "My app";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button download = (Button) findViewById(R.id.download);
download.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.v(TAG,"download method called");
downloadPdf();
Toast.makeText(MainActivity.this,"Listener Called",Toast.LENGTH_SHORT).show();
Log.v(TAG,"On Click Listener Called");
}
});
}
public void loadPdf(View view) {
Log.v(TAG,"Pdf load called");
File pdfFile = new File(Environment.getExternalStorageDirectory()+"/notes","ssp.pdf");
if(pdfFile.exists())
{
Uri path = Uri.fromFile(pdfFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent intent = Intent.createChooser(pdfIntent, "Open File");
try
{
startActivity(intent);
}
catch(ActivityNotFoundException e)
{
Toast.makeText(MainActivity.this, "No Application available to view pdf", Toast.LENGTH_LONG).show();
}
}
else
{
Toast.makeText(MainActivity.this,"Pdf not found",Toast.LENGTH_SHORT).show();
}
}
public void downloadPdf()
{
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(myHTTPUrl));
request.setTitle("Solid State Physics");
request.setDescription("File is being Downloaded...");
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir("/notes","ssp.pdf");
manager.enqueue(request);
}}
EDITED :
This is the exact error I'm facing with screenshot
Error : File format error , Cannot be opened
Screenshot of the error.
You're not downloading the PDF, you're downloading the web-page displaying the contents of the PDF. If you want to download the PDF file directly, use the following URL:
String myHTTPUrl = "https://drive.google.com/uc?export=download&id=0B5Pev9zz5bVjZTFFZ1dLZVp1WVU";
and your application should work.
(Don't forget to delete the invalid notes/ssp.pdf file first, otherwise the DownloadManager will download the file under a different name, like notes/ssp-1.pdf.)

How can I open play store market:// in webview [duplicate]

I have some links in my webview that are market:// links. When my users tap on them, it gives them a page cannot be found error.
How can I allow all links that begin with market:// to automatically open the Google play store when they are tapped? I tried:
final Intent intent = new Intent("android.intent.action.VIEW");
intent.setData(Uri.parse("market://details?id="));
startActivity(intent);
}
but that didn't seem to do anything. I am pretty new to this so any help would be appreciated. Also, FYI, I cannot change the market:// links to play.google.com myself. They are from my advertiser.
Is there anyway I can include it in this code:
public boolean shouldOverrideUrlLoading(WebView paramWebView, String paramString) {
if (DEBUG)
Log.e("shouldOverride", paramString);
if (Uri.parse(paramString).getHost()!=null && (!Uri.parse(paramString).getHost().equals("market.android.com")) && (!paramString.contains("facebook.com")) && (!Uri.parse(paramString).getHost().contains("twitter.com")) && (!Uri.parse(paramString).getHost().equals("play.google.com"))
&& (!Uri.parse(paramString).getHost().contains("bit.ly")) && (!Uri.parse(paramString).getHost().contains("plus.google.com")) && (!Uri.parse(paramString).getHost().contains("youtube.com"))){
if(isAppOrGamePage(paramString)){
final Intent intent = new Intent(MainActivity.this, PageActivity.class);
intent.putExtra("app_url", paramString);
startActivity(intent);
} else
return false;
} else {
final Intent intent = new Intent("android.intent.action.VIEW", Uri.parse(paramString));
startActivity(intent);
}
return true;
}
}
You can decide what to do by looking the scheme of the url, if Google Play Store app is installed you can open the detail page in Play Store app, else you can show Google Play web page of the application
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getScheme().equals("market")) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
Activity host = (Activity) view.getContext();
host.startActivity(intent);
return true;
} catch (ActivityNotFoundException e) {
// Google Play app is not installed, you may want to open the app store link
Uri uri = Uri.parse(url);
view.loadUrl("http://play.google.com/store/apps/" + uri.getHost() + "?" + uri.getQuery());
return false;
}
}
return false;
}
});
you can use this code like this also if its help you:
// It will not work in android simulator as it does not have Google Play Store
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id="+APP_ID)));
if (url.startsWith("market://")||url.startsWith("vnd:youtube")||url.startsWith("tel:")||url.startsWith("mailto:"))
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
}

upload photo to Facebook from Android

I'm trying to upload photo to Facebook from my Android App and it works fine, photo is uploaded, problem is, Facebook app doesn't open, my App simply uploads photo directly to News Feed, but without option to maybe discard it, or change text or antything, so what i would like is to open facebook post with my image attached but not posted until i click the "post"
here is my code :
Bitmap img = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
Request uploadRequest = Request.newUploadPhotoRequest(
Session.getActiveSession(), img, new Request.Callback() {
#Override
public void onCompleted(Response response) {
Toast.makeText(MyActivity.this,
"Photo uploaded successfully",
Toast.LENGTH_LONG).show();
}
});
uploadRequest.executeAsync();
I googled around for hours, and this facebook sdk is messed up, i can't seem to find any proper answer, if i use shareDialog facebook provides, i can't share local images, if i dont use share dialog i can't open facebook app, and i don't want to use OpenGraph stories .. Any help would be gravely appreciated.
You can post imade or text into facebook by using facebook SDK wich is explaned here
and to post an image
private void postImageToWall(String accessToken,byte[] image, String text){
Bundle params = new Bundle();
params.putString(Facebook.TOKEN, accessToken);
//text with the image
params.putString("message",text);
// The byte array is the data of a picture.
params.putByteArray("picture", image);
try {
facebook.request("me/photos", params, "POST");
uploadSucceed = true;
} catch (FileNotFoundException fileNotFoundException) {
showToast(fileNotFoundException.getMessage());
} catch (MalformedURLException malformedURLException) {
showToast(malformedURLException.getMessage());
} catch (IOException ioException) {
showToast(ioException.getMessage());
}
}

Categories

Resources