001// SPDX-License-Identifier: GPL-3.0-or-later
002// Copyright © 2026 Aurélien Pierre
003
004package com.aurelienpierre.sioul;
005
006import android.app.PendingIntent;
007import android.content.Intent;
008import android.os.Build;
009import android.service.quicksettings.Tile;
010import android.service.quicksettings.TileService;
011
012/**
013 * "Pause" among the quick settings: a tap opens Sioul, which starts the pause,
014 * or shows it when it is on already; never an end (ending is the pause
015 * screen's one button: no exit by accident). Shown active while the pause is
016 * on. On a locked phone the tap asks to unlock first: no pause from a pocket.
017 */
018public final class PauseTile extends TileService
019{
020    @Override
021    public void onStartListening()
022    {
023        show();
024    }
025
026    @Override
027    public void onTileAdded()
028    {
029        show();
030    }
031
032    @Override
033    public void onClick()
034    {
035        if (isLocked())
036            unlockAndRun(this::open);
037        else
038            open();
039    }
040
041    private void show()
042    {
043        Tile tile = getQsTile();
044        if (tile == null)
045            return;
046        tile.setState(PauseMode.paused(this) ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE);
047        tile.updateTile();
048    }
049
050    /** Sioul brought up through PauseOpener, which keeps the press; the shade closed. */
051    @SuppressWarnings("deprecation")
052    private void open()
053    {
054        Intent opener = new Intent(this, PauseOpener.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
055        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
056            startActivityAndCollapse(PendingIntent.getActivity(this, 0, opener, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT));
057        else
058            startActivityAndCollapse(opener);
059    }
060}