Android accessibility service for gesture screen lock - android

I'm looking at creating my gesture lock screen, which will be shown once screen is on.
It can not achieved using screen on\off broadcast receiver as they won't work if registered from manifest. And you can not keep service running all time.
My Service looks like.
public class CustomAccessibilityService extends AccessibilityService {
#Override
public void onAccessibilityEvent(AccessibilityEvent event) {
// event.getSource(), findFocus(int), getWindows(), or getRootInActiveWindow().
Toast.makeText(this, "onAccessibilityEvent" + event.getSource(), Toast.LENGTH_SHORT).show();
}
#Override
public void onInterrupt() {
Toast.makeText(this, "onInterrupt", Toast.LENGTH_SHORT).show();
}
#Override
protected boolean onGesture(int gestureId) {
Toast.makeText(this, "onGesture" + gestureId, Toast.LENGTH_SHORT).show();
return super.onGesture(gestureId);
}
}
Manifest file
<service
android:name=".CustomAccessibilityService"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
<meta-data
android:name="android.accessibilityservice"
android:resource="#xml/service_configuration" />
</service>
And Service_configuration.xml as below.
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeViewAccessibilityFocused|typeViewAccessibilityFocusCleared"
android:accessibilityFeedbackType="feedbackAllMask"
android:canRetrieveWindowContent="true"
android:notificationTimeout="100"
android:packageNames="com.xxxx.gesturelockscreen" />
I am stuck on it.
I know that we need to activate accessibility service (Gesture lock screen) and I am using below code to check if it is enabled.
if (!isAccessibilityEnabled()) {
Toast.makeText(this, "Not Enabled!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
startActivity(intent);
} else {
//Good to go!
}
public boolean isAccessibilityEnabled() {
int accessibilityEnabled = 0;
final String ACCESSIBILITY_SERVICE_NAME = "com.xxx.gesturelockscreen/com.xxx.gesturelockscreen.CustomAccessibilityService";
boolean accessibilityFound = false;
try {
accessibilityEnabled = Settings.Secure.getInt(this.getContentResolver(), android.provider.Settings.Secure.ACCESSIBILITY_ENABLED);
Log.d(TAG, "ACCESSIBILITY: " + accessibilityEnabled);
} catch (Settings.SettingNotFoundException e) {
Log.d(TAG, "Error finding setting, default accessibility to not found: " + e.getMessage());
}
TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');
if (accessibilityEnabled == 1) {
Log.d(TAG, "***ACCESSIBILIY IS ENABLED***: ");
String settingValue = Settings.Secure.getString(getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
Log.d(TAG, "Setting: " + settingValue);
if (settingValue != null) {
TextUtils.SimpleStringSplitter splitter = mStringColonSplitter;
splitter.setString(settingValue);
while (splitter.hasNext()) {
String accessabilityService = splitter.next();
Log.d(TAG, "Setting: " + accessabilityService);
if (accessabilityService.equalsIgnoreCase(ACCESSIBILITY_SERVICE_NAME)) {
Log.d(TAG, "We've found the correct setting - accessibility is switched on!");
return true;
}
}
}
Log.d(TAG, "***END***");
} else {
Log.d(TAG, "***ACCESSIBILIY IS DISABLED***");
}
return accessibilityFound;
}
Now I want when screen is on my custom accessibility service's need to be called.
Please help, any help is appreciated, thanks in advance.

Related

accessibility service permission lost after killing app in android 8.0

Hi i have created accessibility service permission using below code .
public static boolean isAccessibilitySettingsOn(Context mContext) {
int accessibilityEnabled = 0;
final String service = mContext.getPackageName() + "/" + YouTubeFilterService.class.getCanonicalName();
try {
accessibilityEnabled = Settings.Secure.getInt(
mContext.getApplicationContext().getContentResolver(),
android.provider.Settings.Secure.ACCESSIBILITY_ENABLED);
Log.v(TAG, "accessibilityEnabled = " + accessibilityEnabled);
} catch (Settings.SettingNotFoundException e) {
Log.e(TAG, "Error finding setting, default accessibility to not found: "
+ e.getMessage());
}
TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');
if (accessibilityEnabled == 1) {
Log.v(TAG, "***ACCESSIBILITY IS ENABLED*** -----------------");
String settingValue = Settings.Secure.getString(
mContext.getApplicationContext().getContentResolver(),
Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
if (settingValue != null) {
mStringColonSplitter.setString(settingValue);
while (mStringColonSplitter.hasNext()) {
String accessibilityService = mStringColonSplitter.next();
Log.v(TAG, "-------------- > accessibilityService :: " + accessibilityService + " " + service);
if (accessibilityService.equalsIgnoreCase(service)) {
Log.v(TAG, "We've found the correct setting - accessibility is switched on!");
return true;
}
}
}
} else {
Log.v(TAG, "***ACCESSIBILITY IS DISABLED***");
}
return false;
}
This is working properly in android 8 above when i give permission its save permission status but in android 8.0 and android 8.1 when i give permission to ON and again if i kill and launch then its asking again permission while it should be save permission status please help me what i am doing wrong .

isGestureDetectionAvailable() always returning False

I am trying to detect gestures on a fingerprint scanner. I have made an Accessibility service and yet I get back "False" for this method isGestureDetectionAvailable(). However, my device is capable of detecting the gestures.
I have applied everything according to the code mentioned on the official Android developer website.
This is my Android Manifest Code.
<application
.....>
<service
android:name=".AccessibilityService"
android:enabled="true"
android:exported="true"
android:label="My application"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action
android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
<meta-data
android:name="android.accessibilityservice"
android:resource="#xml/my_gesture_service" />
</service>
This is my Service.
public class AccessibilityService extends
android.accessibilityservice.AccessibilityService {
private static final String TAG =
AccessibilityService.class.getSimpleName();
#Override
public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
Log.d("ACCEVENT", accessibilityEvent.toString());
}
#Override
public void onInterrupt() {
Log.d("ACCEVENT", "onAccessibilityEvent Inturupt");
}
#Override
protected void onServiceConnected() {
super.onServiceConnected();
Log.d(TAG, "onServiceConnected");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
FingerprintGestureController gestureController = getFingerprintGestureController();
Toast.makeText(getApplicationContext(), "Is available: " + gestureController.isGestureDetectionAvailable(), Toast.LENGTH_LONG).show();
Log.e(TAG, "Is available: " + gestureController.isGestureDetectionAvailable() );
FingerprintGestureController.FingerprintGestureCallback callback = new
FingerprintGestureController.FingerprintGestureCallback() {
#Override
public void onGestureDetectionAvailabilityChanged(boolean available) {
super.onGestureDetectionAvailabilityChanged(available);
Toast.makeText(getApplicationContext(), "Gesture available change to: " + available, Toast.LENGTH_SHORT).show();
Log.d(TAG, "onGestureDetectionAvailabilityChanged " + available);
}
#Override
public void onGestureDetected(int gesture) {
super.onGestureDetected(gesture);
Toast.makeText(getApplicationContext(), "Gesture: " + gesture, Toast.LENGTH_SHORT).show();
Log.d(TAG, "onGestureDetected " + gesture);
}
};
gestureController.registerFingerprintGestureCallback(callback, null);
}
}
#Override
public boolean onUnbind(Intent intent) {
Log.d(TAG, "onUnbind " );
return super.onUnbind(intent);
}
}
This is my XML code for the service
<accessibility-service
xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityFeedbackType="feedbackGeneric"
android:accessibilityFlags="flagDefault|flagRequestFingerprintGestures"
android:canRequestFingerprintGestures="true" />
How do you know that your device supports gestures? Does TalkBack work with them? Otherwise, it's possible that the hardware vendor didn't report that the hardware detects gestures.

Fingerprint gesture availability always false for android 9

I'm trying to use fingerprint gestures in an accessibility service. It is working fine in android 8.1 devices (Nexus 5x emulator, Moto G5s Plus) but not working in android 9 devices (Nexus 5x emulator, Samsung M30s). I've added all the required lines mentioned in this question:
Android O - fingerprint gesture callback not working
Do I have to add something extra for android 9? Can someone please help me?
activity_service.xml:
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityFeedbackType="feedbackGeneric"
android:canRequestFingerprintGestures="true"
android:accessibilityFlags="flagDefault|flagRequestFingerprintGestures"
android:canPerformGestures="true"/>
MyAccessibilityService.java:
public class MyAccessibilityService extends AccessibilityService {
private static final String TAG = MyAccessibilityService.class.getSimpleName();
#Override
public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
Log.d(TAG, "onAccessibilityEvent");
}
#Override
public void onInterrupt() {
Log.d(TAG, "onInterrupt");
}
#Override
protected boolean onGesture(int gestureId) {
Log.d(TAG, "onGesture " + gestureId);
return super.onGesture(gestureId);
}
#Override
protected boolean onKeyEvent(KeyEvent event) {
Log.d(TAG, "onKeyEvent " + event.getKeyCode());
return super.onKeyEvent(event);
}
#Override
public void onDestroy() {
Toast.makeText(getApplicationContext(), "onDestroy" , Toast.LENGTH_SHORT).show();
super.onDestroy();
}
#Override
protected void onServiceConnected() {
super.onServiceConnected();
Log.d(TAG, "onServiceConnected");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
FingerprintGestureController gestureController = getFingerprintGestureController();
Toast.makeText(getApplicationContext(), "Is available: " + gestureController.isGestureDetectionAvailable(), Toast.LENGTH_LONG).show();
Log.e(TAG, "Is available: " + gestureController.isGestureDetectionAvailable() );
FingerprintGestureController.FingerprintGestureCallback callback = new
FingerprintGestureController.FingerprintGestureCallback() {
#Override
public void onGestureDetectionAvailabilityChanged(boolean available) {
super.onGestureDetectionAvailabilityChanged(available);
Toast.makeText(getApplicationContext(), "Gesture available change to: " + available, Toast.LENGTH_SHORT).show();
Log.d(TAG, "onGestureDetectionAvailabilityChanged " + available);
}
#Override
public void onGestureDetected(int gesture) {
super.onGestureDetected(gesture);
switch (gesture){
case GESTURE_SWIPE_DOWN:
CameraManager mCameraManager;
mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
String mCameraId = "";
try {
mCameraId = mCameraManager.getCameraIdList()[0];
mCameraManager.setTorchMode(mCameraId, true);
}
catch (Exception e){
}
break;
case GESTURE_SWIPE_UP:
mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
mCameraId = mCameraManager.getCameraIdList()[0];
mCameraManager.setTorchMode(mCameraId, false);
}
catch (Exception e){
}
break;
default:
Toast.makeText(getApplicationContext()
, "Gesture: " + gesture, Toast.LENGTH_SHORT).show();
}
Log.d(TAG, "onGestureDetected " + gesture);
}
};
gestureController.registerFingerprintGestureCallback(callback, new Handler());
}
}
#Override
public boolean onUnbind(Intent intent) {
Log.d(TAG, "onUnbind " );
return super.onUnbind(intent);
}}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.bignerdranch.com.myapplication">
<uses-feature android:name="android.hardware.fingerprint"/>
<uses-feature android:name="android.hardware.camera.flash" />
<uses-permission android:name="android.permission.USE_BIOMETRIC" />
<uses-permission android:name="android.permission." />
<uses-permission android:name="android.permission.USE_FINGERPRINT" />
<permission android:name="android.permission.FLASHLIGHT"
android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
android:protectionLevel="normal" />
<application>
<service
android:name=".MyAccessibilityService"
android:label="#string/app_name"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
<meta-data
android:name="android.accessibilityservice"
android:resource="#xml/activity_service" />
</service>
</application>
</manifest>
You need to add this line in the gesture service xml
android:accessibilityFlags="flagDefault|flagRequestFingerprintGestures"
It could also be device related. Check the following answer for information.
Android M FingerprintManager.isHardwareDetected() returns false on a Samsung Galaxy S5
Can you provide your code samples for further debugging if this doesn't work?

Text not changing depending on the conditions given

In my program, I have a switch, a text view and an accessibility service running.
When the switch is turned ON, the second screen opens from where I need to switch on my service and by doing this, the text in the first screen should change to ON(the text should change to ON only if the service is enabled).
But the problem is that the text in the first screen is changing to ON, as soon as I switch ON the switch, but before I enable my service from the second screen.
This is my code for the switch:
private void setSwitchAndText(){
boolean ifON;
ifON = isAccessibilitySettingsOn(getApplicationContext());
if(ifON) {
mySwitch.setChecked(true);
} else {
mySwitch.setChecked(false);
}
myTextview.setText(ifON ? R.string.on : R.string.off);
}
and this is how I check whether the service is enable or not:
private boolean isAccessibilitySettingsOn(Context mContext) {
int accessibilityEnabled = 0;
final String service = getPackageName() + "/" + myService.class.getCanonicalName();
boolean accessibilityFound = false;
try {
accessibilityEnabled = Settings.Secure.getInt(this.getContentResolver(),android.provider.Settings.Secure.ACCESSIBILITY_ENABLED);
Log.d(LOGTAG, "ACCESSIBILITY: " + accessibilityEnabled);
} catch (Settings.SettingNotFoundException e) {
Log.d(LOGTAG, "Error finding setting, default accessibility to not found: " + e.getMessage());
}
TextUtils.SimpleStringSplitter mStringColonSplitter = new TextUtils.SimpleStringSplitter(':');
if (accessibilityEnabled == 1) {
Log.d(TAG, "***ACCESSIBILITY IS ENABLED*** -----------------");
String settingValue = Settings.Secure.getString(
mContext.getApplicationContext().getContentResolver(),
Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
if (settingValue != null) {
mStringColonSplitter.setString(settingValue);
while (mStringColonSplitter.hasNext()) {
String accessibilityService = mStringColonSplitter.next();
Log.d(TAG, "-------------- > accessibilityService :: " + accessibilityService + " " + service);
if (accessibilityService.equalsIgnoreCase(service)) {
Log.d(TAG, "We've found the correct setting - accessibility is switched on!");
return true;
}
}
}
} else {
Log.v(TAG, "***ACCESSIBILITY IS DISABLED***");
}
return accessibilityFound;
}
and this is the relevant code in the onCreate:
myTextview = (TextView) findViewById(R.id.my_text);
mySwitch = (Switch) findViewById(R.id.my_switch);
mySwitch.setChecked(false);
mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
myTextview.setText(isChecked ? R.string.on : R.string.off);
if (isChecked) {
askToStartAccessibilityService(getResources().getString(R.string.on));
} else {
askToStartAccessibilityService(getResources().getString(R.string.off));
}
}
});
The way your detecting active accessibility services is pretty intense and there's no reason for this. Try this code instead:
AccessibilityManager accessibilityManager = (AccessibilityManager) getSystemService(Context.ACCESSIBILITY_SERVICE);
List<AccessibilityServiceInfo> serviceInfoList = accessibilityManager.getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_SPOKEN);
return (serviceInfoList.size() > 0);

AIDL service not connecting after bindService()

I am trying to develop a setup of 2 applications (service app + client app) using AIDL. I have currently a setup of 3 modules:
android-agent-framework (android library module holding only the AIDL file)
android-agent (the service)
android-example-client (the client)
android-agent and android-agent-framework have a dependency to the first one to get access to the interface.
Whenever the client calls bindService() it gets false as return and in the ServiceConnection the onServiceConnected() is not called. Also in the service implementation the onBind() is not called. There is no error in the logs.
Here is the code:
android-agent activity:
public class MyCompanyStartActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.i(MyCompanyStartActivity.class.toString(), "Create MyCompanyStartActivity");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ComponentName service = startService(new Intent(this, MyCompanyRequestService.class));
Log.i("tag", service.getClassName() + "::" + service.getPackageName());
}
}
android-agent service:
public class MyCompanyRequestService extends Service {
#Override
public IBinder onBind(Intent intent) {
Log.i(MyCompanyRequestService.class.toString(), "Starting SmartRest Service");
return mBinder;
}
private final IMyCompanyRequestService.Stub mBinder = new IMyCompanyRequestService.Stub() {
#Override
public void sendData(String xid, String authentication, String data) throws RemoteException{
Log.i(MyCompanyRequestService.class.toString(), "sending data: " + data);
}
};
}
android-agent manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.android.agent" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MyCompanyStartActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Services -->
<service
android:name="com.mycompany.android.agent.framework.MyCompanyRequestService"
android:process=":remote"
android:exported="true"
android:enabled="true">
<intent-filter>
<action android:name="MyCompanyRequestService"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</service>
<!-- Permissions -->
</application>
</manifest>
android-example-client activity:
public class ClientStarter extends Activity {
protected IMyCompanyRequestService mycompanyRequestService = null;
#Override
public void onCreate(Bundle savedInstanceState) {
Log.i("tag","create client");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onStart() {
super.onStart();
if (mycompanyRequestService == null) {
printServices();
Intent it = new Intent("MyCompanyRequestService");
it.setPackage("com.mycompany.android.agent.framework");
Log.i("tag","before binding service: " + it.getAction() + "::" + it.getPackage());
boolean serviceBinding = getApplicationContext().bindService(it, connection, Context.BIND_AUTO_CREATE);
Log.i("tag", "service is bound: " + serviceBinding);
}
Handler handler = new Handler();
handler.postDelayed(new Runner(), 10000);
}
#Override
protected void onDestroy() {
super.onDestroy();
unbindService(connection);
}
private ServiceConnection connection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName name, IBinder service) {
Log.i("service", "Service connected");
mycompanyRequestService = IMyCompanyRequestService.Stub.asInterface(service);
Toast.makeText(getApplicationContext(), "Service Connected", Toast.LENGTH_SHORT).show();
Log.i("service", "Service connected");
}
#Override
public void onServiceDisconnected(ComponentName name) {
Log.i("service", "Service disconnected");
mycompanyRequestService = null;
Toast.makeText(getApplicationContext(), "Service Disconnected", Toast.LENGTH_SHORT).show();
Log.i("service", "Service disconnected");
}
};
private void printServices() {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
Log.d("service", service.service.getClassName());
}
}
private class Runner implements Runnable {
#Override
public void run() {
Log.i("tag","starting");
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location loc;
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
Log.e(ClientStarter.class.toString(), "Error", e);
} while(true) {
try {
if (mycompanyRequestService != null) {
loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Log.i(ClientStarter.class.toString(), loc.getLatitude() + " - " + loc.getLongitude() + " - " + loc.getAltitude());
mycompanyRequestService.sendData("test", "auth", String.valueOf(loc.getLatitude()) + "," + String.valueOf(loc.getLongitude()) + "," + String.valueOf(loc.getAltitude()));
} else {
Log.i(ClientStarter.class.toString(), "service not yet available");
}
Thread.sleep(5000);
} catch (InterruptedException e) {
Log.e(ClientStarter.class.toString(), "Error", e);
} catch (RemoteException e) {
Log.e(ClientStarter.class.toString(), "Error", e);
}
}
}
}
}
The printServices() call before trying to bind the service actually lists the service so it is running.
The log does not contain any errors and the client is in the end running in the loop but the service is still null.
Maybe someone encountered a similar issue before.
After going another round through all files I found my mistake.
I needed to change:
Intent it = new Intent("MyCompanyRequestService");
it.setPackage("com.mycompany.android.agent.framework");
to:
Intent it = new Intent("MyCompanyRequestService");
it.setPackage("com.mycompany.android.agent");
The package of the Intent needs to match the package of the app and not the package of the service.
Another reason why you could face this issue (at least I did) is that – from API level 30 – you are also required to declare the apps that you communicate to in the manifest, for example:
<queries>
<package android:name="com.your.app" />
</queries>

Categories

Resources