HTML5WebView fullscreen youtube video consumes back button click - java

In HTML5WebView.java (pastebin link)(source) I have:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
System.out.println("TAG - BACK PRESSED IN WEB VIEW");
return super.onKeyDown(keyCode, event);
}
And in the activity that starts the web view I have:
#Override
public void onBackPressed() {
System.out.println("TAG - BACK PRESSED IN WEB PLAYER ACTIVITY");
super.onBackPressed();
}
Now when I'm playing an embedded youtube video normally (not fullscreen), both methods are called when I press the back button. When I put the video into fullscreen mode (using the youtube player fullscreen button), none of the methods are called. My only guess is, the back button is being consumed by the web view to undo the fullscreen action (but even that doesn't work).
I am trying to get the back button to immediatly kill the web view, even if there is a video in fullscreen mode.
See the problem in action here, had to post it externally because the gif > 2MB

If I was you I would delete the embed play of youtube video's or ask google that won't be seen as abuse. Because the embed play of youtube video's can be seen as a abuse of this:
Your app violates our Device and Network Abuse policy by downloading,
monetizing, or otherwise accessing YouTube videos in violation of the
YouTube Terms of Service or YouTube API Terms of Service.
More Info: https://play.google.com/about/privacy-security/device-network-abuse/ https://www.youtube.com/static?template=terms
For example, your app contains: YouTube background play functionality
This is a violation of the YouTube Terms of Service.
That is what I got in my experience because I also made a app where people could watch some YT embed video's but after some updates the app wouldn't be update becuase I got this error that it is abuse so I am warning you that's all.

Related

How to listen mouse click in background in android

I am creating an android application and want to show Toast on mouse right click and left click.
The default listner is
public boolean onGenericMotionEvent(MotionEvent event) {
if(event.getActionButton() == MotionEvent.ACTION_UP){
Toast.makeText(this, "Left click", Toast.LENGTH_SHORT).show();
}
}
It runs perfectly when my app is opened. How can I do it in background (say while using WhattsApp)?
Thanks in advance :)
When your app is in the background, the way to make it work is by using a Service. Start the service when your app is onPause (on onStop for API level above 23). And in the onStart, check if your Service is running and stop it.
If you are using Fragment then adapt this according to the Fragment's lifecycle.

How to keep webview running in foreground service after pressing home button in android?

I am loading an url in webview which plays some audio song. When user presses the home button, then the audio stops after some time(Probably because activity is going in stopped state).
Well, I think service might solve my problem. But How can I keep the song playing i.e Webview in foreground and show it on notification bar in service.
Edit:-
I don't want to load the URL again because doing that will again go to home page of the website.
I think this can be done using foreground Service. But I am not getting how can I save the state of the webview and keep it running from that state using foreground Service.
The only solution I tried myself is to attach webview to a window with 0 size (to make it invisible):
val params = WindowManager.LayoutParams(
0, 0, TYPE_APPLICATION_OVERLAY, FLAG_NOT_FOCUSABLE or FLAG_NOT_TOUCHABLE, PixelFormat.RGBA_8888
)
(context.getSystemService(Context.WINDOW_SERVICE) as WindowManager).addView(webview, params)
You can perform this code inside your foreground service and remove webview from window when app goes foreground:
windowManager.removeViewImmediate(webivew)
You may invoke this attach/detach code inside onStart/onStop of your activity (or onCreate/onDestroy of your Service, but you will have to close the service when app goes foreground). You only need the foreground service to save your app from being killed by android system.
Another point to think off — you should not create your webview in activity (to prevent memory leak) as your webview has different lifecycle. One way is to create your webview inside Application (with it's context) and inject it inside activity.

Is there possibility to "Auto-start" application when users become inactive?

I have to create an app, which detects user inactivity, and then start activity which displays some videos with WebView, and then when displaying with WebView is finished, it has to play videos from SDCard. I've already handled part with WebView and SDCard (with JavaScriptInterface etc.)
This application has to work with API 19 all the way to the newest one.
The question is - Is there a possibility to detect if user is inactive and start my application, or keep the app running in background, and then start activity in the foreground after the user becomes inactive for certain time?
I'm not trying to play ads, when user is not looking at his screen. Application is for my client, who have stores with all kind of electrical equipments, including smartphones. The goal is to play video presentations with hardware details specific for each smartphone (informations about processor, ram, camera, screen etc.).
In short: I have to make an app which is similar to "Demo Apps" created for example by Samsung (playing some kind of presentations on screen).
So far I've read and tested things like:
1) BroadcastReceiver with combination of ACTION_SCREEN_OFF / ACTION_SCREEN_ON events.
Receiver works properly, I can detect this event and then start activity, but... The screen is already off so i can't see the displayed activity - it's visible running in the foreground after unlocking the phone. Is there a way to unlock the phone when the event is received?
That's my code so far.
EventReceiver Class:
class EventReceiver: BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
StringBuilder().apply {
append("Action: ${intent.action}\n")
append("URI: ${intent.toUri(Intent.URI_INTENT_SCHEME)}\n")
toString().also { log ->
Log.d(TAG, log)
Toast.makeText(context, log, Toast.LENGTH_LONG).show()
}
}
if (intent.action == Intent.ACTION_SCREEN_OFF) {
val i = Intent(context, MainActivity::class.java)
context.startActivity(i)
}
}
}
MainActivity Class:
val br : BroadcastReceiver = EventReceiver()
val filter = IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION).apply {
addAction(Intent.ACTION_SCREEN_OFF)
addAction(Intent.ACTION_SCREEN_ON)
addAction(Intent.ACTION_BOOT_COMPLETED)
}
2) Foreground Services - I read that this is a great way to make some asyc stuff in the background and show notifications to user. Is there a way to start the activity with it?
3) Job Scheduler
4) Daydream / Dream Service - it actually works great with almost every API and manufacturer, but.. there's no way to set the app as Screen Saver on Huawei/Honor smartphones, at least from phone settings, I've read that this is possible with ADB etc. but this is not an option that I can use here.
It seems that none of these fullfill my expectations.

How to show persitent message on android programmatically?

I have searched how to show a persistent message on android screen like Cerberus application does but I can't find. If any one has an idea?
Persistent message mean message keep visible on screen even you switch application or even you lock the phone? Image of screen message
You can disabled home key, back key and whatever key that you wants preventing closing your app with this:
#Override
public boolean dispatchKeyEvent(KeyEvent keyEvent){
return true;
}
Remember launch this block screen when your smartphone turned on with boot complete broadcast receiver.

Android Speech Recognition freezes when another app is in the background

I have an app that uses google voice speech recognition. It works perfectly, but the voice capture popup freezes sometimes (see snapshot) I have narrowed down the problem to the point where as long as a commercial app called X is active in the background, the google voice popup freezes. As soon as I close app X by swiping it away, my app's speech recognition works perfectly again.
This is the code I use to launch the speech recognition popup:
#Override
protected void onResume() {
speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE,app.getDemoLanguageCode());
speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, app.getDemoLanguageCode());
speechIntent.putExtra(RecognizerIntent.EXTRA_ONLY_RETURN_LANGUAGE_PREFERENCE, app.getDemoLanguageCode());
speechIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
speechIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 1);
}
static void openGoogleASR() {
thisActivity.startActivityForResult(speechIntent, SPEECHRECON_CODE);
}
This is how the speech recognition popup looks like when it freezes while app X is active in the background:
Do you know how to properly initialize my speech recognition to make it robust against other misbehaving apps?

Categories