001// SPDX-License-Identifier: GPL-3.0-or-later
002// Copyright © 2026 Aurélien Pierre
003
004package com.aurelienpierre.sioul;
005
006import android.app.Activity;
007import android.content.Intent;
008import android.net.Uri;
009import android.os.Bundle;
010import android.util.Log;
011
012/**
013 * An event's reminder, new mail's notification, or a button of the end of
014 * work's or the night's notice pressed (EventAlarms, MailNotes, DayNotes):
015 * what it opens, the event's file, the Porch, a closing sheet or a block's
016 * question, kept for Sioul ({@link EventAlarms#takeOpened}, which Rust reads
017 * when the window comes up), the day's notice taken away, then Sioul brought
018 * up as by its icon. A window of its own, never shown, as DoseOpener: no
019 * intent naming a file stays with Sioul's window.
020 */
021public final class ReminderOpener extends Activity
022{
023    /** The intent's address: "sioul-open:event", "sioul-open:porch", "sioul-open:review", "sioul-open:need". */
024    static final String SCHEME = "sioul-open";
025    /** The event's file, on an event's reminder; "work" or "night" and its day for a closing sheet ("work:2026-10-14"); a block's key for its question. */
026    static final String KEY = "key";
027    /** The day's notice pressed, to take away (DayNotes: its key). */
028    static final String NOTE = "note";
029
030    /**
031     * Whether a notice of Sioul's may open `kind` with `key`: an event, the
032     * Porch, a block's question ("need"), or a closing sheet ("review") of
033     * the work day ("work") or of the whole day ("night"), with the day it
034     * is for ("work:2026-10-14"). Rust reads the same (eventalarms.rs,
035     * `opened_of`). Pure, checked on a JVM (android/jvm-checks/DayNotesCheck.java).
036     *
037     * @param kind what the notice is about, or null
038     * @param key what in it, or null
039     * @return whether it is opened
040     */
041    static boolean opens(String kind, String key)
042    {
043        if ("event".equals(kind) || "porch".equals(kind) || "need".equals(kind))
044            return true;
045        return "review".equals(kind) && key != null && key.matches("(work|night)(:\\d{4}-\\d{2}-\\d{2})?");
046    }
047
048    @Override
049    protected void onCreate(Bundle state)
050    {
051        super.onCreate(state);
052        Uri address = getIntent().getData();
053        String kind = address != null && SCHEME.equals(address.getScheme()) ? address.getSchemeSpecificPart() : null;
054        String key = getIntent().getStringExtra(KEY);
055        if (state == null && opens(kind, key)) {
056            EventAlarms.opened(this, kind, key);
057            DayNotes.dismiss(this, getIntent().getStringExtra(NOTE));
058            Log.i(DoseAlarms.TAG, "Events: a notification tapped, kept for Sioul (" + kind + ").");
059        }
060        Intent sioul = getPackageManager().getLaunchIntentForPackage(getPackageName());
061        if (sioul != null)
062            startActivity(sioul.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED));
063        finish();
064    }
065}