Skip to content

The app lock

A bank, a health record, a diary: apps that lock themselves after a while away, and open again with the phone's own fingerprint, face or screen lock. In ApkPy it is one line.

from apkpy_lib import *

home = Screen(id="home")
label("Journal", id="title", screen=home)


def opened():
    app_lock.after(5)


lifecycle(home, on_mount=opened)
button("Lock now", id="lock", icon="lock", command=app_lock.now, screen=home)
run(start_screen=home)
Call
app_lock.after(5) Lock the app once it has been away, or untouched, for five minutes. title= is what the phone's prompt says.
app_lock.now() Lock it now — a "Lock" button.
app_lock.off() No more locking: the rule is taken away, and a locked app opened.
app_lock.locked() Whether the app is locked right now — for an if.

after() and now() answer "ok", or "no_lock" on a phone with no screen lock (below); after() answers "invalid", and keeps what it had, for minutes that are not a number from 0. Minutes written in the code are checked when the app is built: app_lock.after(-5) stops the build with the line (U2048).

When it locks

  • Away for the minutes. The person went to the home screen or to another app, and comes back after that long.
  • Untouched for the minutes. The app is on show and nobody has touched it for that long.
  • app_lock.after(0) locks it every time it comes back, and never for being untouched. A part of a minute is a number of minutes too: 0.5 is thirty seconds.

Turning the phone, changing the theme and going from one screen of the app to another are none of these: the app was never away.

Closing the app does not open it. The rule is kept on the phone, in a place of its own — storage.clear() leaves it — and every screen follows it from the moment the app starts, before any of its own code runs. An app closed from the recent apps and opened again after the minutes comes back locked, and so does one that was locked when it was closed. Only app_lock.off() takes the rule away.

Locked

Every screen of the app is covered: its name, a padlock and an Unlock button, in the app's own colours. Over it, the phone's own prompt asks for a fingerprint, a face or the screen lock — PIN, pattern or password.

The phone draws that prompt and checks the person itself. No app can draw it, and nothing typed into it reaches the app. Cancelled, the cover stays and Unlock asks again. Back leaves the app, as the home button does, instead of reaching the screen underneath.

While it is locked:

  • TalkBack does not read what is under the cover;
  • from Android 13, the recent apps show no picture of an app that has a rule;
  • the app's own timers and background jobs go on: the lock covers what is on the screen, it does not stop the app.

On Android 7 to 9 the prompt is the phone's screen-lock check (PIN, pattern or password), which is what those versions offer an app.

A phone with no screen lock

A phone with no PIN, pattern, password, fingerprint or face has nothing the app could ask for, so nothing is locked: after() and now() answer "no_lock". The rule is kept, and holds from the moment the person sets a screen lock. What to say meanwhile is the app's to decide:

def opened():
    if app_lock.after(5) == "no_lock":
        warning.set_value("Set a screen lock on this phone to protect the journal.")

An app that would rather not open at all on such a phone can say so instead; locking the person out of their own data is not something ApkPy does for it.

Where to say it

In the first screen's on_mount, as at the top of this page: said on every start, it is kept as it was. Or in a switch the person turns on and off:

def lock_switch(on):
    if on == "true":
        app_lock.after(1)
    else:
        app_lock.off()

A call at the top of the file runs before there is a screen to cover, and stops the build (U2048); inside a background job, where there is no screen at all, it stops the build too (J7004).

What it is not

It covers the app; it does not encrypt anything. What the app keeps with storage is encrypted already (Data security), and an account on a server still needs the server's own login.

In the Previewer

  • Minimising the window is the app going to the background, and closing it is the app closed: open it again and it is the app opened again, locked if the minutes have gone by.
  • A click, a key or the mouse wheel is a touch.
  • The prompt is the one biometrics.unlock() draws: click the sensor or Use PIN to open, Esc to cancel. Alt+Left while locked minimises the window, as Back leaves the app on the phone.
  • The rule is kept in apkpy_app_lock.json, beside your script. Delete it, or call app_lock.off(), to start again without it.