I think there might be a way to do that if you make your app an AccessibilityService
(but you would have to enable it manually after install).
Then in your AccessibilityService class, inside onAccessibilityEvent
method you can explore views (recursively) and perform clicks - in the example below it will click on TalkBack item in settings - after that it should toggle the toggle button on next screen (the trick is that you can click on parent not the switch view itself) - I haven't tried this code :)
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
AccessibilityNodeInfo source = event.getSource();
if(event.getEventType()==AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED)
explore(source);
}
private void explore(AccessibilityNodeInfo view){
int count = view.getChildCount();
for(int i=0; i<count; i++){
AccessibilityNodeInfo child = view.getChild(i);
if(!MODE_TALK_BACK_SCREEN){
if(child.getText()!=null && child.getText().toString().toLowerCase().contains("TalkBack")){
child.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
MODE_TALK_BACK_SCREEN=true;
return;
}
}else{
if("ToggleButton".equals(child.getClassName().toString())){ //there ony one toggle button on the screen
child.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
performGlobalAction(GLOBAL_ACTION_BACK);
performGlobalAction(GLOBAL_ACTION_BACK);//need to go back two time - i don't know if that will work :)
return;
}
}
explore(child);
child.recycle();
}
so now if you open accessibility settings with Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
it will perform clicks for you - you would have to somehow cover it with full screen toast or service with view
I'm currently working on automatic airplane mode toggle and it works - so should do the job in your case
take a look on my serviceconfig.xml
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:description="@string/accessibility_service_description"
android:packageNames="com.android.settings"
android:accessibilityEventTypes="typeWindowStateChanged"
android:accessibilityFlags="flagDefault"
android:accessibilityFeedbackType="feedbackSpoken"
android:notificationTimeout="100"
android:canRetrieveWindowContent="true"
android:settingsActivity="com.example.android.accessibility.ServiceSettingsActivity"
/>