Locking the device with AccessibilityService and TileService

487 Views Asked by At

I have simple example of an app that perform the locking via GLOBAL_ACTION_LOCK_SCREEN (new feature from Pie). In that example are:

1) AccessibilityService.class as follow:

@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
    if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
        doLock();
    }
}

public void doLock() {
    performGlobalAction(GLOBAL_ACTION_LOCK_SCREEN);
}

2) TileService.class as follow:

@Override
public void onStartListening() {
    super.onStartListening();
    Tile t = getQsTile();
    t.setState(Tile.STATE_INACTIVE);
    t.updateTile();
}

@Override
public void onClick() {
    Intent i = new Intent(getApplicationContext(), MainActivity.class);
    i.addFlags(FLAG_ACTIVITY_NEW_TASK);
    startActivityAndCollapse(i);
    super.onClick();
}

3) MainActivity.class as follow:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
    Handler h = new Handler();
    h.postDelayed(new Runnable() {
        @Override
        public void run() {
            MainActivity.this.finish();
        }
    }, 2500);

Now I have question about implementation exactly the same app without MainActivity. How to call AccessibilityService's method doLock() directly from TileService's onClick Method?

I've tried with no luck Messenger, EventBus, etc. Is there any possibility to do it - just click on QS tile and directly call method DoLock (or maybe through onAccessibilityEvent, but I don't know what is event fired in that case)? Thanks in advice. P.S. Sorry if my question too stupid for you, I'm really new in Android development. P.S2 Really appreciate if someone give me some code example, not just hints.

0

There are 0 best solutions below