accessibility service permission lost after killing app in android 8.0 - android

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 .

Related

Android, how to ignore an already connected WiFi, just like mobile phone forget WiFi

I know the following method can be used, But sometimes that doesn't work.
public void removeNetwork(String sSid){
WifiConfiguration tempConfig = isExist(sSid);
if (tempConfig != null) {
try {
boolean isRemoved = mWifiManager.removeNetwork(tempConfig.networkId);
boolean isDisabled = mWifiManager.disableNetwork(tempConfig.networkId);
boolean isSaved = mWifiManager.saveConfiguration();
Log.i(TAG, "isRemoved: "+ isRemoved + ", isDisabled" + isDisabled+ ", isSaved" + isSaved);
} catch (Exception e){
Log.i(TAG, "RemoveNetwork Exception: "+e.getMessage());
}
}
}

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);

Android accessibility service for gesture screen lock

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.

Android: Change gateway address of hot-spot

Is there a way to change the gateway address of the AP hot-spot feature of Android from 192.168.43.1 to something else such as 192.168.43.3 without recompiling anything? I have a rooted Android device.
You are trying to change the settings that have been hardcoded for android hotspot
This is from http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.1_r1/android/net/wifi/WifiStateMachine.java
private boolean startTethering(ArrayList<String> available) {
boolean wifiAvailable = false;
checkAndSetConnectivityInstance();
String[] wifiRegexs = mCm.getTetherableWifiRegexs();
for (String intf : available) {
for (String regex : wifiRegexs) {
if (intf.matches(regex)) {
InterfaceConfiguration ifcg = null;
try {
ifcg = mNwService.getInterfaceConfig(intf);
if (ifcg != null) {
/* IP/netmask: 192.168.43.1/255.255.255.0 */
ifcg.setLinkAddress(new LinkAddress(
NetworkUtils.numericToInetAddress("192.168.43.1"), 24));
ifcg.setInterfaceUp();
mNwService.setInterfaceConfig(intf, ifcg);
}
} catch (Exception e) {
loge("Error configuring interface " + intf + ", :" + e);
return false;
}
if(mCm.tether(intf) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
loge("Error tethering on " + intf);
return false;
}
mTetherInterfaceName = intf;
return true;
}
}
}
// We found no interfaces to tether
return false;
}
Try check out this answer
https://android.stackexchange.com/questions/46499/how-configure-the-dhcp-settings-of-wifi-tetheringhotspot-in-android

Detect if my accessibility service is enabled

I was wondering how could I detect if my own service is enabled. So I could check if my service is not enabled, then tell the user to enable it.
Below is the method to check if your accessibility service is enabled or not. 
Note: Change value of YOURAccessibilityService with your Service. 
// To check if service is enabled
private boolean isAccessibilitySettingsOn(Context mContext) {
int accessibilityEnabled = 0;
final String service = getPackageName() + "/" + YOURAccessibilityService.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;
}
And to call this method:
if (!isAccessibilitySettingsOn(getApplicationContext())) {
startActivity(new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS));
}
This will check and launch accessibility settings if not enabled.
This is a modified version of Jakub Bláha's answer in java.
public boolean isAccessServiceEnabled(Context context, Class accessibilityServiceClass)
{
String prefString = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
return prefString!= null && prefString.contains(context.getPackageName() + "/" + accessibilityServiceClass.getName());
}
This is somehow a smaller version, but it is working.
fun isAccessServiceEnabled(context: Context): Boolean {
val prefString =
Settings.Secure.getString(context.contentResolver, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES)
return prefString.contains("${context.packageName}/${context.packageName}.${context.getString(R.string.access_service_name)}")
}
Feel free to correct me if there is something missing.
1:Kotlin Based answer
2:Added a current package name check as well cos it will return true only if accessibility service will be Enabled for current package, Recently it was returning true if any package accessibility service enabled
private fun checkAccessibilityPermission(): Boolean {
var isAccessibilityEnabled = false
(requireActivity().getSystemService(Context.ACCESSIBILITY_SERVICE) as AccessibilityManager).apply {
installedAccessibilityServiceList.forEach { installedService ->
installedService.resolveInfo.serviceInfo.apply {
if (getEnabledAccessibilityServiceList(AccessibilityServiceInfo.FEEDBACK_ALL_MASK).any { it.resolveInfo.serviceInfo.packageName == packageName && it.resolveInfo.serviceInfo.name == name && permission == Manifest.permission.BIND_ACCESSIBILITY_SERVICE && it.resolveInfo.serviceInfo.packageName == requireActivity().packageName })
isAccessibilityEnabled = true
}
}
}
if (isAccessibilityEnabled.not())
startActivity(Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK
})
return isAccessibilityEnabled
}
Kotlin version based on answer of #Pankaj Kumar
inline fun <reified T> Context.isAccessibilityEnabled(): Boolean {
var enabled = 0
try {
enabled = Settings.Secure.getInt(contentResolver, Settings.Secure.ACCESSIBILITY_ENABLED)
} catch (e: SettingNotFoundException) {
Timber.e(e)
}
if (enabled == 1) {
val name = ComponentName(applicationContext, T::class.java)
val services = Settings.Secure.getString(contentResolver, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES)
return services?.contains(name.flattenToString()) ?: false
}
return false
}

Categories

Resources