19
19
20
20
import android .annotation .SuppressLint ;
21
21
import android .app .Activity ;
22
- import android .content .DialogInterface ;
23
22
import android .content .Intent ;
24
23
import android .content .SharedPreferences ;
25
24
import android .net .Uri ;
41
40
42
41
import net .kollnig .missioncontrol .main .AppBlocklistController ;
43
42
import net .kollnig .missioncontrol .main .AppsFragment ;
43
+ import net .kollnig .missioncontrol .main .SettingsActivity ;
44
44
import net .kollnig .missioncontrol .vpn .InConsumer ;
45
45
import net .kollnig .missioncontrol .vpn .OutConsumer ;
46
46
import net .kollnig .missioncontrol .vpn .OutFilter ;
53
53
public class MainActivity extends AppCompatActivity implements AntMonitorActivity ,
54
54
View .OnClickListener {
55
55
private static final String TAG = MainActivity .class .getSimpleName ();
56
- private final String APPS_FRAG_TAG = "appsFragTag" ;
57
- public static String CONSENT_PREF = "consent" ;
56
+ public static String FIRST_START = "first-start" ;
58
57
public static String CONSENT_YES = "yes" ;
59
58
public static String CONSENT_NO = "no" ;
59
+ private final String APPS_FRAG_TAG = "appsFragTag" ;
60
60
SwitchCompat mSwitchMonitoring ;
61
61
Toolbar mToolbar ;
62
62
FragmentManager fm ;
@@ -88,46 +88,49 @@ protected void onCreate (Bundle savedInstanceState) {
88
88
89
89
// Use click events only, for simplicity, disable swipe
90
90
mSwitchMonitoring .setOnClickListener (this );
91
- mSwitchMonitoring .setOnTouchListener (new View .OnTouchListener () {
92
- @ Override
93
- public boolean onTouch (View v , MotionEvent event ) {
94
- return event .getActionMasked () == MotionEvent .ACTION_MOVE ;
95
- }
96
- });
91
+ mSwitchMonitoring .setOnTouchListener ((v , event ) -> event .getActionMasked () == MotionEvent .ACTION_MOVE );
97
92
98
93
// Initialise VPN controller
99
94
mVpnController = VpnController .getInstance (this );
100
95
VpnController .setDnsCacheEnabled (true );
101
96
97
+ // Initialise default settings (only first app start)
98
+ android .support .v7 .preference .PreferenceManager
99
+ .setDefaultValues (this , R .xml .preferences , false );
100
+ final SharedPreferences settingsPref =
101
+ android .support .v7 .preference .PreferenceManager
102
+ .getDefaultSharedPreferences (this );
103
+ Boolean showSystemApps = settingsPref .getBoolean
104
+ (SettingsActivity .KEY_PREF_SYSTEMAPPS_SWITCH , false );
105
+
102
106
// Set up the bottom bottomNavigation
103
107
fm = getSupportFragmentManager ();
104
108
Fragment fApps = fm .findFragmentByTag (APPS_FRAG_TAG );
105
109
if (fApps != null ) {
106
110
fm .beginTransaction ().show (fApps ).commit ();
107
111
} else {
108
- fm .beginTransaction ().add (R .id .main_container , AppsFragment .newInstance (), APPS_FRAG_TAG ).commit ();
112
+ fm .beginTransaction ().add
113
+ (R .id .main_container , AppsFragment .newInstance (showSystemApps ), APPS_FRAG_TAG )
114
+ .commit ();
109
115
}
110
116
111
117
// Ask for consent to contact Google and other servers
112
- final SharedPreferences sharedPref = PreferenceManager .getDefaultSharedPreferences (this );
113
- final String consent = sharedPref .getString (CONSENT_PREF , null );
118
+ SharedPreferences sharedPref = PreferenceManager .getDefaultSharedPreferences (this );
119
+ final Boolean firstStart = sharedPref .getBoolean (FIRST_START , true );
120
+
121
+ if (firstStart ) {
122
+ sharedPref .edit ().putBoolean (FIRST_START , false ).apply ();
114
123
115
- if (consent == null ) {
116
124
AlertDialog .Builder builder = new AlertDialog .Builder (this );
117
125
builder .setMessage (R .string .confirm_google_info )
118
126
.setTitle (R .string .external_servers );
119
- builder .setPositiveButton (R .string .yes , new DialogInterface .OnClickListener () {
120
- public void onClick (DialogInterface dialog , int id ) {
121
- sharedPref .edit ().putString (CONSENT_PREF , CONSENT_YES ).apply ();
122
- dialog .dismiss ();
123
- }
127
+ builder .setPositiveButton (R .string .yes , (dialog , id ) -> {
128
+ settingsPref .edit ().putBoolean
129
+ (SettingsActivity .KEY_PREF_GOOGLEPLAY_SWITCH , true ).apply ();
130
+ dialog .dismiss ();
124
131
});
125
- builder .setNegativeButton (R .string .no , new DialogInterface .OnClickListener () {
126
- @ Override
127
- public void onClick (DialogInterface dialog , int i ) {
128
- sharedPref .edit ().putString (CONSENT_PREF , CONSENT_NO ).apply ();
129
- dialog .dismiss ();
130
- }
132
+ builder .setNegativeButton (R .string .no , (dialog , id ) -> {
133
+ dialog .dismiss ();
131
134
});
132
135
AlertDialog dialog = builder .create ();
133
136
dialog .setCancelable (false ); // avoid back button
@@ -143,12 +146,12 @@ public void onClick (DialogInterface dialog, int i) {
143
146
*/
144
147
private void startMonitoring () {
145
148
// Check if we are connected to the internet
146
- if (!mVpnController .isConnectedToInternet ()) {
149
+ /* if (!mVpnController.isConnectedToInternet()) {
147
150
Toast.makeText(MainActivity.this, R.string.no_service,
148
151
Toast.LENGTH_LONG).show();
149
152
updateMonitoringSwitch(true, false);
150
153
return;
151
- }
154
+ }*/
152
155
153
156
// Check if we have VPN rights from the user
154
157
Intent intent = android .net .VpnService .prepare (MainActivity .this );
@@ -178,6 +181,8 @@ protected void onActivityResult (int request, int result, Intent data) {
178
181
179
182
// Connect - triggers onVpnStateChanged
180
183
mVpnController .connect (null , outFilter , inConsumer , outConsumer );
184
+
185
+ Toast .makeText (this , R .string .instructions_monitoring , Toast .LENGTH_SHORT ).show ();
181
186
} else {
182
187
// enable the switch again so user can try again
183
188
mSwitchMonitoring .setEnabled (true );
@@ -196,11 +201,15 @@ public boolean onCreateOptionsMenu (Menu menu) {
196
201
@ Override
197
202
public boolean onOptionsItemSelected (MenuItem item ) {
198
203
switch (item .getItemId ()) {
199
- case R .id .menu_option_about :
204
+ case R .id .action_about :
200
205
Uri aboutUri = Uri .parse (getString (R .string .about_url ));
201
206
Intent browserIntent = new Intent (Intent .ACTION_VIEW , aboutUri );
202
207
startActivity (browserIntent );
203
208
return true ;
209
+ case R .id .action_settings :
210
+ Intent intent = new Intent (this , SettingsActivity .class );
211
+ startActivity (intent );
212
+ return true ;
204
213
}
205
214
return super .onOptionsItemSelected (item );
206
215
}
0 commit comments