blob: 8fa2a7f0e877456d7b97146c9ccdf3f36ee316af (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
package com.wireguard.android;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import com.wireguard.config.Config;
/**
* Base class for activities that need to remember the current configuration and wait for a service.
*/
abstract class BaseConfigActivity extends Activity {
protected static final String KEY_CURRENT_CONFIG = "currentConfig";
protected static final String TAG_DETAIL = "detail";
protected static final String TAG_EDIT = "edit";
protected static final String TAG_LIST = "list";
protected static final String TAG_PLACEHOLDER = "placeholder";
private final ServiceConnection callbacks = new ServiceConnectionCallbacks();
private Config currentConfig;
private String initialConfig;
protected Config getCurrentConfig() {
return currentConfig;
}
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Trigger starting the service as early as possible
if (VpnService.getInstance() != null)
onServiceAvailable();
else
bindService(new Intent(this, VpnService.class), callbacks, Context.BIND_AUTO_CREATE);
// Restore the saved configuration if there is one; otherwise grab it from the intent.
if (savedInstanceState != null)
initialConfig = savedInstanceState.getString(KEY_CURRENT_CONFIG);
else
initialConfig = getIntent().getStringExtra(KEY_CURRENT_CONFIG);
}
protected abstract void onCurrentConfigChanged(Config config);
@Override
public void onSaveInstanceState(final Bundle outState) {
super.onSaveInstanceState(outState);
if (currentConfig != null)
outState.putString(KEY_CURRENT_CONFIG, currentConfig.getName());
}
protected abstract void onServiceAvailable();
public void setCurrentConfig(final Config config) {
currentConfig = config;
onCurrentConfigChanged(currentConfig);
}
private class ServiceConnectionCallbacks implements ServiceConnection {
@Override
public void onServiceConnected(final ComponentName component, final IBinder binder) {
// We don't actually need a binding, only notification that the service is started.
unbindService(callbacks);
// Tell the subclass that it is now safe to use the service.
onServiceAvailable();
// Make sure the subclass activity is initialized before setting its config.
if (initialConfig != null && currentConfig == null)
setCurrentConfig(VpnService.getInstance().get(initialConfig));
}
@Override
public void onServiceDisconnected(final ComponentName component) {
// This can never happen; the service runs in the same thread as the activity.
throw new IllegalStateException();
}
}
}
|