Android Question Screen blue filter - Resolved

padvou

Active Member
Licensed User
Longtime User
Hello,
I've been seeing several applications offering a blue filter on the screen, in fact turning it yellowish and pale, for night use.
How could this be implemented using b4a?
 

padvou

Active Member
Licensed User
Longtime User
You can create a semi-transparent activity. You will need to configure it to pass touch events. Start with implementing the transparent activity and if the results are good then I can help you with the touch events.
Thank you for your response.
But this activity should always remain on top of all everything else, shouldnt it?
 
Upvote 0

padvou

Active Member
Licensed User
Longtime User
Ok, my activity is transparent:
B4X:
SetApplicationAttribute(android:theme, "@android:style/Theme.Translucent")
in the manifest.
I've found these:
B4X:
Dim jo As JavaObject = Activity
    Dim Window As JavaObject = jo.RunMethodJO("getContext", Null).RunMethod("getWindow", Null)
    Window.RunMethod("addFlags", Array As Object(524288)) 'FLAG_SHOW_WHEN_LOCKED
    Window.RunMethod("addFlags", Array As Object(128)) 'FLAG_KEEP_SCREEN_ON
    Window.RunMethod("addFlags", Array As Object(16)) 'FLAG_NOT_TOUCHABLE
but my application's activity is still not passing events.
 
Upvote 0

padvou

Active Member
Licensed User
Longtime User
Works fine here. I see the yellow layer and can touch the screen. The activity is removed when you click on the home button.
I try it on two samsung devices, a T113 (android 4.2.2) and a T280 (android 5.1.1)
On the first one, it is shown but when you click on it, the application exits
On the second one, it is shown but touching it, does nothing, meaning it doesnt pass the events.


Edit:
I tried it also on an HTC 10 (android 7). It runs, passes touch and click on the home screen, but the applications that open are not covered by the transparent activity of this application.
If I exit them, the transparent application is there running.
 
Last edited:
Upvote 0

padvou

Active Member
Licensed User
Longtime User
Today I tried it on an LG Zero (android 6) and the results are that clicks are passed through the application, but when an app shortcut is clicked, it only appears to be clicked, the app doesnt start.

This project apart from the fact that it hasnt worked correctly on any of the four devices I've tried, shows differect behaviour on each one of them
Any ideas why?
Found this in Stackoverflow:
B4X:
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Pass touch events to the background activity
        getWindow().addFlags(
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                        | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                        | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
...
        setContentView(R.layout.main);
}

They say that the layout parameters must be passed before setting the content view.
How can this be accomplished using B4A?
 
Last edited:
Upvote 0

padvou

Active Member
Licensed User
Longtime User
Why does this code create an exception?
B4X:
#if java
import android.view.WindowManager;

protected void _onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

       
        getWindow().addFlags(
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                        | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                        | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);

     
}


#End If
 
Upvote 0

padvou

Active Member
Licensed User
Longtime User
It will be easier to say if you post the exception.
Here it is:
B4X:
Logger connected to:  samsung SM-T110
--------- beginning of /dev/log/system
--------- beginning of /dev/log/main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zerosoftbh.transparent/com.zerosoftbh.transparent.main}: java.lang.RuntimeException: java.lang.IllegalArgumentException: wrong number of arguments; expected 1, got 0
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2262)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
    at android.app.ActivityThread.access$700(ActivityThread.java:158)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:176)
    at android.app.ActivityThread.main(ActivityThread.java:5365)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: java.lang.IllegalArgumentException: wrong number of arguments; expected 1, got 0
    at anywheresoftware.b4a.BA.runHook(BA.java:155)
    at com.zerosoftbh.transparent.main.onCreate(main.java:58)
    at android.app.Activity.performCreate(Activity.java:5326)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225)
    ... 11 more
Caused by: java.lang.IllegalArgumentException: wrong number of arguments; expected 1, got 0
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at anywheresoftware.b4a.BA.runHook(BA.java:152)
    ... 15 more
 
Upvote 0

padvou

Active Member
Licensed User
Longtime User
B4X:
#if java
import android.view.WindowManager;

public void _onCreate() {
   
        getWindow().addFlags(
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                        | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                        | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);

   
}


#End If
This is the closest I got: the transparent activity is running, shortcuts are clicked, but applications do not run.
So if anyone has any kind of suggestion, please do suggest something. :)
 
Upvote 0

JordiCP

Expert
Licensed User
Longtime User
Perhaps I am wrong, but to my understanding, this should be done with a new window which does not belong to the activity. It could be generated from a foreground service which will keep a reference to its instance. This way, the window will "survive" even when your activity is paused.

The drawback is that since Android 6, and on some devices before, drawing over other apps is seen as dangerous, so the permission has to be handled differently. Don't know if because it is a "special one", but it is not included in runtimePermissions library.

Try this example, which uses a workaround. I have tested it with Android 5.1 and 6.0 and works properly . Not sure if it will work with all devices.
 

Attachments

  • ScreenTone.zip
    8.8 KB · Views: 355
Upvote 0

padvou

Active Member
Licensed User
Longtime User
Perhaps I am wrong, but to my understanding, this should be done with a new window which does not belong to the activity. It could be generated from a foreground service which will keep a reference to its instance. This way, the window will "survive" even when your activity is paused.

The drawback is that since Android 6, and on some devices before, drawing over other apps is seen as dangerous, so the permission has to be handled differently. Don't know if because it is a "special one", but it is not included in runtimePermissions library.

Try this example, which uses a workaround. I have tested it with Android 5.1 and 6.0 and works properly . Not sure if it will work with all devices.

Thank you very much!
It's exactly what I was looking for! :)
 
Upvote 0

Shay

Well-Known Member
Licensed User
Longtime User
with sdk 26 it is crashing
B4X:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Activity (main) Resume **
** Activity (main) Pause, UserClosed = true **
*** Service (oservice) Create ***
** Service (oservice) Start **
Error occurred on line: 27 (oService)
java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:131)
    at b4a.example.screenfilter.oservice._init_element(oservice.java:155)
    at b4a.example.screenfilter.oservice._service_start(oservice.java:233)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:732)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:348)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:176)
    at b4a.example.screenfilter.oservice.handleStart(oservice.java:107)
    at b4a.example.screenfilter.oservice.access$000(oservice.java:15)
    at b4a.example.screenfilter.oservice$1.run(oservice.java:78)
    at anywheresoftware.b4a.objects.ServiceHelper$StarterHelper.onStartCommand(ServiceHelper.java:221)
    at b4a.example.screenfilter.oservice.onStartCommand(oservice.java:76)
    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3679)
    at android.app.ActivityThread.-wrap21(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1801)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6944)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@b00b775 -- permission denied for window type 2002
    at android.view.ViewRootImpl.setView(ViewRootImpl.java:982)
    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:381)
    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:100)
    at b4a.example.screenfilter.oservice$CustomImageView.sharedConstructing(oservice.java:276)
    at b4a.example.screenfilter.oservice$CustomImageView.<init>(oservice.java:249)
    at b4a.example.screenfilter.oservice.newInstance(oservice.java:287)
    ... 25 more
 
Upvote 0
Top