Simulating click via Android Accessibility Service - java

I am trying to simulate a click function in an android application utilizing the Accessability Service. I have set up the service in the manifest, and enabled it in my phones settings. I have a class MouseAccessabilityService which contains a the following click function:
// Clikc function that clicks at a specific x and y coordinate
#RequiresApi(api = Build.VERSION_CODES.N)
public static GestureDescription click(float x, float y) {
// for a single tap a duration of 1 ms is enough
final int DURATION = 1;
Path clickPath = new Path();
clickPath.moveTo(x, y);
GestureDescription.StrokeDescription clickStroke =
new GestureDescription.StrokeDescription(clickPath, 0, DURATION);
GestureDescription.Builder clickBuilder = new GestureDescription.Builder();
clickBuilder.addStroke(clickStroke);
return clickBuilder.build();
}
I am calling the click function in the MainActivity via a button press that triggers the click (for testing purposes):
Buttonclick = (Button)findViewById(R.id.clickbutton);
Buttonclick.setOnClickListener(new View.OnClickListener() {
#RequiresApi(api = Build.VERSION_CODES.N)
#Override
public void onClick(View view) {
//changeButtonText.setText("Second Text");
MouseAccessabilityService.click(centerX, centerY);
}
});
The X and Y is being passed as an integer variable from some other functionality. I can not seem to get the click() function to work from within my app and press on a button.
Am I on the right page? Can this functionality even work? I am at a standstill with this and would appreciate any help.
Thanks.

You can use the dispatchGesture function.
Try replacing your click function's return with :
return dispatchGesture(clickBuilder.build(), null, null);
https://codelabs.developers.google.com/codelabs/developing-android-a11y-service/#7

Related

android - java get id of the item just clicked without set "onClick" to the item

How do I get an item id when I click on it without setting an "onClick" event in the xmk file?
I tried that:
private View.OnClickListener globalClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
int clickedPosition = (int) v.getTag();
// do something with position
}
}
But i don't understand how to use it
Hello!
Rebuild the method as follows:
private View.OnClickListener globalClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
}
}
Well, to get the target ID that you click, it is better to first define the attribute android:tag="target_id" in the XML file, and then access it within the called method when the target is clicked, using the v.getTag(). Of course, you can also get the clicked ID via v.getId() without doing so. You can check the ID from:
use if (v.getTag () == R.id.target_id), or parse it as follows:
String viewID = getResources (). GetResourceName (v.getId ())
Your code creates a listener without actually attaching it to anything, so it never receives click events. If you have, for example, a button that you want to respond to clicks, you can register your listener with it like so:
button.setOnClickListener(globalClickListener);

How to make button do some work(write a method) as well as show a pop up window

To show a pop up window i used this way :
Button b = (Button) findViewById(R.id.finish) ;
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,Pop.class));
}
});
code of pop.java
public class Pop extends Activity {
#Override
protected void onCreate(Bundle savedInstanceSate) {
super.onCreate(savedInstanceSate);
setContentView(R.layout.popup);
DisplayMetrics dm = new DisplayMetrics() ;
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels ;
int height = dm.heightPixels ;
getWindow().setLayout( (int)(width*.6),(int)(height*.4) ) ;
I want my button to do some work too as soon as it is pressed so where should i write the method for that and can i use onclick() for that ?
Thank You :)
Your first code snippet must be placed inside the onCreate() callback of the calling activity (in your case it seems MainActivity), after the setContentView() call.
If you need to display a popup I suggest you to use Dialogs https://developer.android.com/guide/topics/ui/dialogs.html
You can write your method after the onClick, before changing the activity or within the new activity in the onCreate method.

How to perform an action after button click

I need to perform an action after onClick method of OnClickListener has run.
Here is my code for onClickListener:
View.OnClickListener imgButton0Handler0 = new View.OnClickListener() {
int identifier=0;
public void onClick(View v) {
//check if tile is found and return if it is
if(isFound[identifier]==true) return;
//set tile as open
checkField[identifier]=1;
//set background on predetermined
button0.setBackgroundResource(tiles[identifier]);
}
};
After this has run, and the background is set I would like to call a method checker(int identifier) which will check for other open tiles and change backgrounds accordingly.
This method needs to be run separately because the background is only displayed after onClick finishes, and I need predetermined background shown for a short time before checker method changes it to something else.
How can I accomplish this?
Have You Tried Post Delayed see this,
View.OnClickListener imgButton0Handler0 = new View.OnClickListener() {
int identifier=0;
public void onClick(View v) {
//check if tile is found and return if it is
if(isFound[identifier]==true) return;
//set tile as open
checkField[identifier]=1;
//set background on predetermined
button0.setBackgroundResource(tiles[identifier]);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
checker(identifier) // your method call
}
}, 3000); // 3 second
}
};

LikeView (android view) in libgdx

I'm trying to place a Like button in my app. After much searching I found that it is impossible to use me own custom button, so I am left only with implementing the default like button from the facebook sdk.
Since this LikeView seems to be a native android like view I don't really know how to put this into my libGDX app.
I would like to use this button only in a specific Screen and set its bounds so that it fits with the rest of my UI. Does anyone have an example of how to create this like button without using the XML (as it is done in all the documentation I have found so far).
Adding the following functions to my application makes it show in the correct position. Unfortunately the LikeView does not get the correct size, but is centered inside the view, which means changing the width/height just moves it.
public void GenerateLikeButton()
{
application.runOnUiThread(new Runnable(){
#Override
public void run() {
float x = 560 * game.global_scale;
int width = (int) (440 * game.global_scale);
int height = (int) (152* game.global_scale);
float y_from_bottom = game.screen_height - ((56+152+70) * game.global_scale + game.ad_height);
Gdx.app.log("like", "from bottom: "+ y_from_bottom);
likeButton = new LikeView(application);
likeButton.setLikeViewStyle(LikeView.Style.BUTTON);
likeButton.setX(x);
likeButton.setY(y_from_bottom-height);
likeButton.setObjectId(LIKE_URL);
likeButton.setVisibility(View.GONE);
application.layout.addView(likeButton,width,height);
likeButton.invalidate();
}
});
}
#Override
public void ShowLikeButton(final boolean visible)
{
application.runOnUiThread(new Runnable()
{
#Override
public void run()
{
if(visible)
likeButton.setVisibility(View.VISIBLE);
else
likeButton.setVisibility(View.GONE);
}
});
}

Display different Button/Button Background depending on SharedPreferences Value

Basically i want a Button to change to another one depending on the int Value stored in sharedprefs.
I have stage select in my game, if the user got enough score in level, then he can start the next one and i want to change the Button depending on that.
I'm setting up my Button's with custom Background created in XML Selector file located in drawable folder.
After that how i can call it in Java?
I tried a bit with if statement but cant find the right solution.
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
Button button = (Button) findViewById(R.id.button_id);
if (settings.getBoolean("nextLevel", true) {
button.setText("Level_2");
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
}
});
}
else {
//Everything should be the same.
}
Check also the developer docs concerning SharedPreferences in android: http://developer.android.com/guide/topics/data/data-storage.html#pref

Categories