Skip to content

Commit b7b9abd

Browse files
committed
android: ensure in secure state to interact with quicktile
Updates tailscale/tailscale#14628 Signed-off-by: davfsa <[email protected]>
1 parent 99712c1 commit b7b9abd

File tree

1 file changed

+76
-72
lines changed

1 file changed

+76
-72
lines changed

android/src/main/java/com/tailscale/ipn/QuickToggleService.java

+76-72
Original file line numberDiff line numberDiff line change
@@ -10,90 +10,94 @@
1010
import android.service.quicksettings.TileService;
1111

1212
public class QuickToggleService extends TileService {
13-
// lock protects the static fields below it.
14-
private static final Object lock = new Object();
13+
// lock protects the static fields below it.
14+
private static final Object lock = new Object();
1515

16-
// isRunning tracks whether the VPN is running.
17-
private static boolean isRunning;
16+
// isRunning tracks whether the VPN is running.
17+
private static boolean isRunning;
1818

19-
// currentTile tracks getQsTile while service is listening.
20-
private static Tile currentTile;
19+
// currentTile tracks getQsTile while service is listening.
20+
private static Tile currentTile;
2121

22-
public static void updateTile() {
23-
var app = UninitializedApp.get();
24-
Tile t;
25-
boolean act;
26-
synchronized (lock) {
27-
t = currentTile;
28-
act = isRunning && app.isAbleToStartVPN();
29-
}
30-
if (t == null) {
31-
return;
32-
}
33-
t.setLabel("Tailscale");
34-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
35-
t.setSubtitle(act ? app.getString(R.string.connected) : app.getString(R.string.not_connected));
36-
}
37-
t.setState(act ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE);
38-
t.updateTile();
22+
public static void updateTile() {
23+
var app = UninitializedApp.get();
24+
Tile t;
25+
boolean act;
26+
synchronized (lock) {
27+
t = currentTile;
28+
act = isRunning && app.isAbleToStartVPN();
3929
}
30+
if (t == null) {
31+
return;
32+
}
33+
t.setLabel("Tailscale");
34+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
35+
t.setSubtitle(act ? app.getString(R.string.connected) : app.getString(R.string.not_connected));
36+
}
37+
t.setState(act ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE);
38+
t.updateTile();
39+
}
4040

41-
static void setVPNRunning(boolean running) {
42-
synchronized (lock) {
43-
isRunning = running;
44-
}
45-
updateTile();
41+
static void setVPNRunning(boolean running) {
42+
synchronized (lock) {
43+
isRunning = running;
4644
}
45+
updateTile();
46+
}
4747

48-
@Override
49-
public void onStartListening() {
50-
synchronized (lock) {
51-
currentTile = getQsTile();
52-
}
53-
updateTile();
48+
@Override
49+
public void onStartListening() {
50+
synchronized (lock) {
51+
currentTile = getQsTile();
5452
}
53+
updateTile();
54+
}
5555

56-
@Override
57-
public void onStopListening() {
58-
synchronized (lock) {
59-
currentTile = null;
60-
}
56+
@Override
57+
public void onStopListening() {
58+
synchronized (lock) {
59+
currentTile = null;
6160
}
61+
}
62+
63+
@Override
64+
public void onClick() {
65+
unlockAndRun(this::secureOnTileClick);
66+
}
6267

63-
@SuppressWarnings("deprecation")
64-
@Override
65-
public void onClick() {
66-
boolean r;
67-
synchronized (lock) {
68-
r = UninitializedApp.get().isAbleToStartVPN();
69-
}
70-
if (r) {
71-
// Get the application to make sure it initializes
72-
App.get();
73-
onTileClick();
74-
} else {
75-
// Start main activity.
76-
Intent i = getPackageManager().getLaunchIntentForPackage(getPackageName());
77-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
78-
// Request code for opening activity.
79-
startActivityAndCollapse(PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE));
80-
} else {
81-
// Deprecated, but still required for older versions.
82-
startActivityAndCollapse(i);
83-
}
84-
}
68+
@SuppressWarnings("deprecation")
69+
private void secureOnTileClick() {
70+
boolean r;
71+
synchronized (lock) {
72+
r = UninitializedApp.get().isAbleToStartVPN();
8573
}
74+
if (r) {
75+
// Get the application to make sure it initializes
76+
App.get();
77+
onTileClick();
78+
} else {
79+
// Start main activity.
80+
Intent i = getPackageManager().getLaunchIntentForPackage(getPackageName());
81+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
82+
// Request code for opening activity.
83+
startActivityAndCollapse(PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE));
84+
} else {
85+
// Deprecated, but still required for older versions.
86+
startActivityAndCollapse(i);
87+
}
88+
}
89+
}
8690

87-
private void onTileClick() {
88-
UninitializedApp app = UninitializedApp.get();
89-
boolean needsToStop;
90-
synchronized (lock) {
91-
needsToStop = app.isAbleToStartVPN() && isRunning;
92-
}
93-
if (needsToStop) {
94-
app.stopVPN();
95-
} else {
96-
app.startVPN();
97-
}
91+
private void onTileClick() {
92+
UninitializedApp app = UninitializedApp.get();
93+
boolean needsToStop;
94+
synchronized (lock) {
95+
needsToStop = app.isAbleToStartVPN() && isRunning;
96+
}
97+
if (needsToStop) {
98+
app.stopVPN();
99+
} else {
100+
app.startVPN();
98101
}
102+
}
99103
}

0 commit comments

Comments
 (0)