What is the purpose of FLAG_LOCAL_FOCUS_MODE? - android

One of the flags in WindowManager.LayoutParams is FLAG_LOCAL_FOCUS_MODE. It is used together with Window.setLocalFocus method. Unfortunately, the documentation describes the method very succinctly: "Set focus locally". What does it mean? What does it do?

A window with local focus does not report focus changes to the window manager.
While I suspect this is mainly used for automated testing, a real world use case would be an input method for Android TV implemented using Button widgets. You want to be able to focus your buttons in response to dpad navigation, but you don't want these events reported to the window manager because if they are, the window manager will unfocus the window the user is typing in, causing the input method manager to close the input method.

Related

External keyboard event handlers stop responding

I've been working on a project which needs to capture keystrokes from an external keyboard (a handheld barcode scanner really) anywhere in the app. Using react-native, this should be trivial, especially with the library react-native-keyevent.
Works perfectly on the initial load. I then navigate to another part of the app (using react-navigation) and try to scan a barcode; nothing happens. I replaced the react-native-keyevent overrides MainActivity.java with some simple Log.d("KeyEvents", "..."), overriding dispatchKeyEvent, onKeyUp, onKeyMultiple and onKeyDown. I only log, then call super.
Same behaviour. Logs fine until I navigate, then it stops. After investigating the Android docs, it seems Views can override key handlers, and thereby 'steal' keystrokes. However I'm stuck finding out which view steals the focus. Also there's really no keyboard handling in react-native by default, so to my understanding, everything should be passed on to the activity.
Wandering hopelessly around in the react-native codebase, I stumbled upon TVEventHandler which I use to log if a view requests focus:
import TVEventHandler from 'react-native/Libraries/Components/AppleTV/TVEventHandler';
(new TVEventHandler).enable('foo', function(){
console.log(arguments);
})
Right before the handlers stop working, this line indeed logs some focus events with a view tag. However, I don't know how to find out which view has a certain tag.
Can someone point me in the right direction where to look?

View.Activate/Deactivate event listener registered multiple times

I have a simple Apache Flex view based application that runs on Android as follow:
<f:MyView xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/sparks"
activate="activateHandler(event)"
deactivate="deactivateHandler(event)"/>
I expect the activateHandler() should execute only once when the view is activated, however after I pop and then push the same view back the number of activateHandler() execution increased by how many times I did the pop and push operation. Why would this happen and how to force it to operate as expected (i.e only once)?
Expanding on #JileniBouguima's answer, changing activate to creationComplete will resolve this because of how those events work. Your expectation that activateHandler() executes only once is a little off; according to the Activate event documentation, activate fires
every time your application gains operating system focus and becomes active.
By contrast, creationComplete fires once per object, once the component is created.
I am not sure what code exactly is written in the handlers activateHandler and deactivateHandler but in Flex this is a standard practice to remove event listener if you do not need it any more. I am assuming that whenever you pop and push the same view it is adding and removing the listener. I can help you more if you share the handlers code.
Change activate to creationComplete.

Background app to listen to Drag gestures

I need to register a broadcast receiver that will tell me any kind of Drag events throughout the system. My app will run at background and perform any task if any kind of Drag event happens even any other app is running in the foreground. Is it possible? Any idea on how can I do it?
Updates: Do not think I'm going to make keylogger. My app will be visible but will run in background. And all I want is simply to detect Drag events (drag to left, drag to right, drag to up and drag to down).
I'll accept any answer if you can tell me about how can I display 4 buttons those are permant, on top of any other apps because this can also serve me what I want.
Your app can run without a "normal" UI by running as a Service, as per your link, but I think the code you linked may be slightly out of date.
Remember that your service must run in the foreground - the code is supplied there in your link, but not explicitly called. Without running in the foreground, the system could well stop your app rather than running it in the background.
When I created a task switcher using such overlays, I found it was necessary to use a TYPE_SYSTEM_ALERT rather than TYPE_SYSTEM_OVERLAY.
Android 4.x - System Overlay - Cannot capture touch events
I declare my window parameters without FLAG_WATCH_OUTSIDE_TOUCH.
WindowManager.LayoutParams params =
new WindowManager.LayoutParams(width, height, x, y,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);
Your service should also be sure to properly unregister the overlay view from the WindowManager when it ends. Without this, you app will leak memory.
public void onDestroy()
{
super.onDestroy();
if (overlay != null)
{
((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(overlay);
overlay = null;
}
}
I see that this is done in OverlayView.destory() (note the incorrect spelling of that method name - it would be a good idea to use the correct name for that method).
So your real requirement is to be able to pass some input to your app whilst allowing the screen to be dedicated to outputting video.
Have you thought about the following:
detecting tilt or orientation of the device to control direction
having 4 nfc tags, and detecting which of those you are over to change direction (may not give you a quick enough response)
It is also possible to have actions that are selectable directly from notifications, so you could have one or more notification that offer actions to control the direction, and simply allow the notification API to handle the job of appearing in front of the video.
Of course there are apps that manage to overlay their UI in front of other apps. Such an example is Thrutu, though you seem to be pushed for time, and getting such a solution working is not straightforward - see How do I implement a slide-out drawer on the Android's call screen?
I found my answer here. I need to start a service.
The library standout allows you to create floating applications(applications that go over other applications). I used this for a couple android applications I put on the market a while back. It makes creating the floating windows pretty simple. Check it out at:
http://forum.xda-developers.com/showthread.php?t=1688531

how to add an action to copy/pase or listen to copy event on android

Hie!
I'm trying to develop an app that will do one of these 2 options -
whenever a user marks a text in any app (using the regular copy/paste), there will be another option besides copy/cut that will open my app. (preferred)
My app would listen to a copy text event in another app and will show a notification to the user. when they press it, it will open my app.
I haven't found a way to do either of these options.Some claim that I can listen to the copy activity but I couldn't understand how to do it(Android : How to listen to longclick events in any text area in other applications? click-events-in-any-text-area-in-other-application).
I'm not looking for anyone to write the code for me of course, just a pointer to the right direction if this is possible.
Thanks in advance,
Shahar
After short investigation I don't think it is possible. You may look at this guide, but it says copy/paste is implemented using ClipboardManager in Android, which looks like a part of a system. And it doesn't provide any hooks or interfaces to intercept copy/paste events in other app.
Sure you may hack deeper in this mechanismus on rooted device, but this will not work for all users.
Very late response but you can create a custom text selection action, which is an activity with an intent filter with action android.intent.action.PROCESS_TEXT, and whenever a user press on text, a text selection menu appears, your's will be available too,
when the user clicks on your custom text selection action, the activity configures with the android.intent.action.PROCESS_TEXT will be opened for your users.
check this article for implementation steps Custom text selection action

Android webview losing focus

Situation: I have a webview with an html input field in it for typing a location, there is a webview sitting underneath it to display suggestions. There is a keyup handler and an onchange handler on that input field. on keyup there is a message sent out to call an api to populate the suggestion webview.
Problem: After typing one character, the input field loses focus. On android 2.3 the keyboard stays up but continued typing does nothing. on android 4.1 the onchange handler gets triggered (as though the user had hit enter to submit what they had typed instead of seleting a suggestion) and the keyboard and webviews get taken away.
Things I have considered: An external loadURL call could cause this (like 'javascript: var...'), calling 'requestFocus' or 'bringToFront' on some other view could also cause this. Setting a event handler on a view tree listener might help clear things up.
Unfortunately, as far as I have been able to tell, no url change is happening, no other views seem to be requesting focus or are being brought to the front. Putting a handler on the view tree listener did not help--it was triggered after the input field lost focus.
I'm out of ideas to try to find the cause of this focus change.
What can I do to investigate this further and solve this problem?
Is loadUrl being called at all (after the first time)? It turns out that loadUrl automatically closes the keyboard. If you are using loadUrl, I found a workaround! Try https://stackoverflow.com/a/18776064/513038.

Categories

Resources