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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
package com.wireguard.android;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import com.wireguard.config.Config;
/**
* Activity that allows creating/viewing/editing/deleting WireGuard configurations.
*/
public class ConfigActivity extends BaseConfigActivity {
private ConfigDetailFragment detailFragment;
private ConfigEditFragment editFragment;
private final FragmentManager fm = getFragmentManager();
private boolean isEditing;
private boolean isServiceAvailable;
private boolean isSplitLayout;
private boolean isStateSaved;
private ConfigListFragment listFragment;
private int mainContainer;
private boolean wasUpdateSkipped;
@Override
public void onBackPressed() {
super.onBackPressed();
if ((isEditing && isSplitLayout) || (!isEditing && !isSplitLayout)) {
if (getActionBar() != null)
getActionBar().setDisplayHomeAsUpEnabled(false);
}
// Ensure the current config is cleared when going back to the single-pane-layout list.
if (isEditing)
isEditing = false;
else
setCurrentConfig(null);
}
@Override
public void onCreate(final Bundle savedInstanceState) {
setContentView(R.layout.config_activity);
isSplitLayout = findViewById(R.id.detail_fragment) != null;
mainContainer = isSplitLayout ? R.id.detail_fragment : R.id.master_fragment;
Log.d(getClass().getSimpleName(), "onCreate isSplitLayout=" + isSplitLayout);
super.onCreate(savedInstanceState);
}
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
protected void onCurrentConfigChanged(final Config config) {
Log.d(getClass().getSimpleName(), "onCurrentConfigChanged config=" + (config != null ?
config.getName() : null) + " fragment=" + fm.findFragmentById(mainContainer) +
(!isServiceAvailable || isStateSaved ? " SKIPPING" : ""));
// Avoid performing fragment transactions when it would be illegal or the service is null.
if (!isServiceAvailable || isStateSaved) {
// Signal that updates need to be performed once the activity is resumed.
wasUpdateSkipped = true;
return;
} else {
// Now that an update is being performed, reset the flag.
wasUpdateSkipped = false;
}
// If the config change came from the intent or ConfigEditFragment, forward it to the list.
// listFragment is guaranteed not to be null at this point by onServiceAvailable().
if (listFragment.getCurrentConfig() != config)
listFragment.setCurrentConfig(config);
// Update the activity's title if the list of configurations is not visible.
if (!isSplitLayout)
setTitle(config != null ? config.getName() : getString(R.string.app_name));
// Update the fragment in the main container.
if (isEditing)
onBackPressed();
if (config != null) {
final boolean shouldPush = !isSplitLayout &&
fm.findFragmentById(mainContainer) instanceof ConfigListFragment;
switchToFragment(mainContainer, TAG_DETAIL, shouldPush);
} else if (isSplitLayout) {
switchToFragment(mainContainer, TAG_PLACEHOLDER, false);
}
}
@Override
public boolean onOptionsItemSelected(final MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
case R.id.menu_action_edit:
isEditing = true;
switchToFragment(mainContainer, TAG_EDIT, true);
return true;
case R.id.menu_action_save:
// This menu item is handled by the current fragment.
return false;
case R.id.menu_settings:
startActivity(new Intent(this, SettingsActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public void onPostResume() {
super.onPostResume();
final boolean wasStateSaved = isStateSaved;
isStateSaved = false;
if (wasStateSaved || wasUpdateSkipped)
onCurrentConfigChanged(getCurrentConfig());
}
@Override
public void onSaveInstanceState(final Bundle outState) {
// We cannot save fragments that might switch between containers if the layout changes.
if (fm.getBackStackEntryCount() > 0) {
final int bottomEntryId = fm.getBackStackEntryAt(0).getId();
fm.popBackStackImmediate(bottomEntryId, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
if (isSplitLayout) {
final Fragment oldFragment = fm.findFragmentById(mainContainer);
if (oldFragment != null) {
fm.beginTransaction().remove(oldFragment).commit();
fm.executePendingTransactions();
}
}
isStateSaved = true;
super.onSaveInstanceState(outState);
}
@Override
protected void onServiceAvailable() {
super.onServiceAvailable();
isServiceAvailable = true;
// Create the initial fragment set.
final Fragment masterFragment = fm.findFragmentById(R.id.master_fragment);
if (masterFragment instanceof ConfigListFragment)
listFragment = (ConfigListFragment) masterFragment;
else
switchToFragment(R.id.master_fragment, TAG_LIST, false);
// This must run even if no update was skipped, so the restored config gets applied.
onCurrentConfigChanged(getCurrentConfig());
}
private void switchToFragment(final int container, final String tag, final boolean push) {
if (tag.equals(TAG_PLACEHOLDER)) {
final Fragment oldFragment = fm.findFragmentById(container);
if (oldFragment != null)
fm.beginTransaction().remove(oldFragment).commit();
return;
}
final BaseConfigFragment fragment;
switch (tag) {
case TAG_DETAIL:
if (detailFragment == null)
detailFragment = new ConfigDetailFragment();
fragment = detailFragment;
break;
case TAG_EDIT:
if (editFragment == null)
editFragment = new ConfigEditFragment();
fragment = editFragment;
break;
case TAG_LIST:
if (listFragment == null)
listFragment = new ConfigListFragment();
fragment = listFragment;
break;
default:
throw new IllegalArgumentException();
}
if (fragment.getCurrentConfig() != getCurrentConfig())
fragment.setCurrentConfig(getCurrentConfig());
if (fm.findFragmentById(container) != fragment) {
final FragmentTransaction transaction = fm.beginTransaction();
if (push) {
transaction.addToBackStack(null);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
if (getActionBar() != null && (!isSplitLayout || isEditing))
getActionBar().setDisplayHomeAsUpEnabled(true);
}
transaction.replace(container, fragment, null).commit();
}
}
}
|