-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathMainActivity.java
More file actions
689 lines (583 loc) · 23.7 KB
/
Copy pathMainActivity.java
File metadata and controls
689 lines (583 loc) · 23.7 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
package io.netbird.client;
import android.animation.StateListAnimator;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.text.Html;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import com.google.android.material.navigation.NavigationView;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat;
import androidx.core.view.GravityCompat;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import io.netbird.client.databinding.ActivityMainBinding;
import io.netbird.client.tool.RouteChangeListener;
import io.netbird.client.tool.ServiceStateListener;
import io.netbird.client.tool.VPNService;
import io.netbird.client.ui.PreferenceUI;
import io.netbird.gomobile.android.ConnectionListener;
import io.netbird.gomobile.android.NetworkArray;
import io.netbird.gomobile.android.PeerInfoArray;
import io.netbird.gomobile.android.URLOpener;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, ServiceAccessor, StateListenerRegistry {
private StateListAnimator stateAnim;
private enum ConnectionState {
UNKNOWN,
CONNECTED,
CONNECTING,
DISCONNECTING,
DISCONNECTED
}
private final static String LOGTAG = "NBMainActivity";
private VPNService.MyLocalBinder mBinder;
private AppBarConfiguration mAppBarConfiguration;
private ActivityMainBinding binding;
private NavController navController;
private ActivityResultLauncher<Intent> vpnActivityResultLauncher;
private final List<StateListener> serviceStateListeners = new ArrayList<>();
private URLOpener urlOpener;
private QrCodeDialog qrCodeDialog;
private boolean isSSOFinishedWell = false;
private boolean isRunningOnTV = false;
private boolean useDeviceCodeFlow = false;
// Last known state for UI updates
private ConnectionState lastKnownState = ConnectionState.UNKNOWN;
private String lastFqdn = null;
private String lastIp = null;
private long lastPeersCount = 0;
private final ServiceConnection serviceIPC = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName className, IBinder binder) {
Log.d(LOGTAG, "on service connected");
mBinder = (VPNService.MyLocalBinder) binder;
mBinder.setConnectionStateListener(connectionListener);
mBinder.addServiceStateListener(serviceStateListener);
}
@Override
public void onServiceDisconnected(ComponentName name) {
mBinder = null;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
setSupportActionBar(binding.appBarMain.toolbar);
isRunningOnTV = PlatformUtils.isAndroidTV(this);
useDeviceCodeFlow = PlatformUtils.requiresDeviceCodeFlow(this);
if (isRunningOnTV) {
Log.i(LOGTAG, "Running on Android TV - optimizing for D-pad navigation");
}
if (useDeviceCodeFlow && !isRunningOnTV) {
Log.i(LOGTAG, "Running on ChromeOS - using device code flow for authentication");
}
setVersionText();
DrawerLayout drawer = binding.drawerLayout;
NavigationView navigationView = binding.navView;
// Set the listener for menu item selections
navigationView.setNavigationItemSelectedListener(this);
// Update profile menu item with active profile name
updateProfileMenuItem(navigationView);
// On TV, request focus when drawer opens so D-pad navigation works
if (isRunningOnTV) {
drawer.addDrawerListener(new DrawerLayout.SimpleDrawerListener() {
@Override
public void onDrawerOpened(View drawerView) {
// Request focus on the drawer when it opens
navigationView.postDelayed(() -> {
navigationView.setFocusable(true);
navigationView.setFocusableInTouchMode(false);
if (!navigationView.requestFocus()) {
Log.d(LOGTAG, "NavigationView couldn't get focus, trying menu items");
}
// Try to find and focus the first visible menu item
View menuView = navigationView.getChildAt(0);
if (menuView != null) {
View firstFocusable = menuView.findFocus();
if (firstFocusable == null) {
menuView.requestFocus();
}
}
}, 100); // Delay to let drawer animation finish
}
@Override
public void onDrawerClosed(View drawerView) {
// Return focus to main content when drawer closed
View mainContent = findViewById(R.id.nav_host_fragment_content_main);
if (mainContent != null) {
mainContent.requestFocus();
}
}
});
}
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home)
.setOpenableLayout(drawer)
.build();
navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
navController.addOnDestinationChangedListener((controller, destination, arguments) -> {
if (destination.getId() == R.id.nav_home) {
removeToolbarShadow();
// Update profile menu item when returning to home (e.g., after profile switch)
if (binding != null && binding.navView != null) {
updateProfileMenuItem(binding.navView);
}
} else {
resetToolbar();
}
});
if (!useDeviceCodeFlow) {
urlOpener = new CustomTabURLOpener(this, () -> {
if (isSSOFinishedWell) {
return;
}
if (mBinder == null) {
return;
}
mBinder.stopEngine();
});
} else {
urlOpener = new URLOpener() {
@Override
public void open(String url, String userCode) {
qrCodeDialog = QrCodeDialog.newInstance(url, userCode, () -> {
if (isSSOFinishedWell) {
return;
}
if (mBinder == null) {
return;
}
mBinder.stopEngine();
});
qrCodeDialog.show(getSupportFragmentManager(), "QrCodeDialog");
if (!isRunningOnTV) {
try {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browserIntent);
} catch (Exception e) {
Log.e(LOGTAG, "Failed to open browser for device code flow: " + e.getMessage());
}
}
}
@Override
public void onLoginSuccess() {
Log.d(LOGTAG, "onLoginSuccess fired for device code flow.");
if (qrCodeDialog != null && qrCodeDialog.isVisible()) {
qrCodeDialog.dismiss();
qrCodeDialog = null;
}
}
};
}
// VPN permission result launcher
vpnActivityResultLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if((result.getResultCode() != Activity.RESULT_OK)) {
Log.w(LOGTAG, "VPN permission denied");
for (StateListener listener : serviceStateListeners) {
listener.onEngineStopped();
}
Toast.makeText(this, "VPN permission required", Toast.LENGTH_LONG).show();
return;
}
Log.d(LOGTAG, "VPN permission granted");
// Always-on VPN check
if (VPNService.isUsingAlwaysOnVPN(this)) {
showAlwaysOnDialog(() -> {
if (mBinder != null) {
mBinder.runEngine(urlOpener, useDeviceCodeFlow);
}
});
} else {
if (mBinder != null) {
mBinder.runEngine(urlOpener, useDeviceCodeFlow);
}
}
});
if (PreferenceUI.isFirstLaunch(this)) {
PreferenceUI.setFirstLaunchDone(this);
showFirstInstallFragment();
}
}
@Override
public void onStart() {
super.onStart();
Log.d(LOGTAG, "onStart");
startService();
}
@Override
protected void onResume() {
super.onResume();
// Update profile menu item when returning to MainActivity
if (binding != null && binding.navView != null) {
updateProfileMenuItem(binding.navView);
}
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onStop() {
super.onStop();
Log.d(LOGTAG, "onStop");
if (urlOpener instanceof CustomTabURLOpener && ((CustomTabURLOpener) urlOpener).isOpened()) {
return; // Keep service alive for SSO custom tab
}
if (mBinder != null) {
mBinder.removeConnectionStateListener();
mBinder.removeServiceStateListener(serviceStateListener);
unbindService(serviceIPC);
mBinder = null;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mBinder != null) {
mBinder.removeConnectionStateListener();
mBinder.removeServiceStateListener(serviceStateListener);
unbindService(serviceIPC);
mBinder = null;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_docs) {
item.setCheckable(false);
binding.drawerLayout.closeDrawers();
openDocs();
return true;
}
// Use NavigationUI which handles launchSingleTop and saveState/restoreState
// This prevents fragment recreation and preserves state when alternating between destinations
boolean isHandled = NavigationUI.onNavDestinationSelected(item, navController);
binding.drawerLayout.closeDrawers();
return isHandled;
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
@Override
public void switchConnection(boolean status) {
if (mBinder == null) {
Log.w(LOGTAG, "VPN binder is null");
return;
}
if (!status) {
mBinder.stopEngine();
return;
}
Intent prepareIntent = mBinder.prepareVpnIntent(this);
if (prepareIntent != null) {
vpnActivityResultLauncher.launch(prepareIntent);
} else {
mBinder.runEngine(urlOpener, useDeviceCodeFlow);
}
}
@Override
public void stopEngine() {
if (mBinder == null) {
return;
}
mBinder.stopEngine();
}
@Override
public PeerInfoArray getPeersList() {
if (mBinder == null) {
Log.w(LOGTAG, "VPN binder is null");
return new PeerInfoArray();
}
return mBinder.peersInfo();
}
@Override
public NetworkArray getNetworks() {
if (mBinder == null) {
Log.w(LOGTAG, "VPN binder is null");
return new NetworkArray();
}
return mBinder.networks();
}
@Override
public void selectRoute(String route) throws Exception {
if (mBinder == null) {
Log.w(LOGTAG, "VPN binder is null");
return;
}
mBinder.selectRoute(route);
}
@Override
public void deselectRoute(String route) throws Exception {
if (mBinder == null) {
Log.w(LOGTAG, "VPN binder is null");
return;
}
mBinder.deselectRoute(route);
}
@Override
public String debugBundle(boolean anonymize) throws Exception {
if (mBinder == null) {
throw new Exception("VPN service not connected");
}
return mBinder.debugBundle(anonymize);
}
@Override
public void addRouteChangeListener(RouteChangeListener listener) {
if (mBinder == null) {
Log.w(LOGTAG, "VPN binder is null");
return;
}
mBinder.addRouteChangeListener(listener);
}
@Override
public void removeRouteChangeListener(RouteChangeListener listener) {
if (mBinder == null) {
Log.w(LOGTAG, "VPN binder is null");
return;
}
mBinder.removeRouteChangeListener(listener);
}
@Override
public void registerServiceStateListener(StateListener listener) {
if (serviceStateListeners.contains(listener)) {
return;
}
serviceStateListeners.add(listener);
if(lastKnownState == ConnectionState.UNKNOWN) {
return; // No state to notify yet
}
switch (lastKnownState) {
case CONNECTED:
listener.onConnected();
break;
case CONNECTING:
listener.onConnecting();
break;
case DISCONNECTING:
listener.onDisconnecting();
break;
case DISCONNECTED:
listener.onDisconnected();
break;
}
if (lastFqdn != null && lastIp != null) {
listener.onAddressChanged(lastFqdn, lastIp);
}
listener.onPeersListChanged(lastPeersCount);
}
@Override
public void unregisterServiceStateListener(StateListener listener) {
serviceStateListeners.remove(listener);
}
private void openDocs() {
String url = "https://docs.netbird.io"; // Replace with the desired URL
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
private void startService() {
Log.i(LOGTAG, "start VPN service");
Intent intent = new Intent(this, VPNService.class);
intent.setAction(VPNService.INTENT_ACTION_START);
startService(intent);
Intent bindIntent = new Intent(this, VPNService.class);
bindService(bindIntent, serviceIPC, Context.BIND_ABOVE_CLIENT);
}
private void showFirstInstallFragment() {
if (navController != null) {
navController.navigate(R.id.firstInstallFragment);
} else {
Log.w(LOGTAG, "NavController is null, can't navigate to FirstInstallFragment");
}
}
private void removeToolbarShadow() {
stateAnim = binding.appBarMain.appbar.getStateListAnimator();
binding.appBarMain.appbar.setStateListAnimator(null);
binding.appBarMain.appbar.setElevation(0f);
binding.appBarMain.toolbar.setElevation(0);
binding.appBarMain.toolbar.setBackground(new ColorDrawable(ContextCompat.getColor(this, R.color.nb_bg_home)));
}
private void resetToolbar() {
if(stateAnim!=null) {
binding.appBarMain.appbar.setStateListAnimator(stateAnim);
}
binding.appBarMain.appbar.setElevation(10f);
binding.appBarMain.toolbar.setElevation(0);
binding.appBarMain.toolbar.setBackground(new ColorDrawable(ContextCompat.getColor(this, R.color.nb_bg)));
}
private void showAlwaysOnDialog(Runnable onDismissAction) {
final View dialogView = getLayoutInflater().inflate(R.layout.dialog_always_on, null);
final AlertDialog alertDialog = new AlertDialog.Builder(this)
.setView(dialogView)
.create();
// Set bold-formatted text using Html.fromHtml
TextView descriptionText = dialogView.findViewById(R.id.text_description);
descriptionText.setText(Html.fromHtml(getString(R.string.dialog_always_on_desc), Html.FROM_HTML_MODE_LEGACY));
dialogView.findViewById(R.id.btn_close).setOnClickListener(v -> alertDialog.dismiss());
alertDialog.setOnDismissListener(dialog -> {
if (onDismissAction != null) {
onDismissAction.run();
}
});
alertDialog.show();
}
private void setVersionText() {
try {
try {
String packageName = getPackageName();
String versionName = getPackageManager().getPackageInfo(packageName, 0).versionName;
binding.navVersion.setText(versionName);
} catch (Exception e) {
binding.navVersion.setText("");
}
} catch (Exception e) {
}
}
ConnectionListener connectionListener = new ConnectionListener() {
@Override
public synchronized void onAddressChanged(String fqdn, String ip) {
lastFqdn = fqdn;
lastIp = ip;
for (StateListener listener : serviceStateListeners) {
listener.onAddressChanged(fqdn, ip);
}
}
public void onConnected() {
lastKnownState = ConnectionState.CONNECTED;
isSSOFinishedWell = true;
for (StateListener listener : serviceStateListeners) {
listener.onConnected();
}
}
public void onConnecting() {
lastKnownState = ConnectionState.CONNECTING;
isSSOFinishedWell = true;
for (StateListener listener : serviceStateListeners) {
listener.onConnecting();
}
}
public void onDisconnecting() {
lastKnownState = ConnectionState.DISCONNECTING;
for (StateListener listener : serviceStateListeners) {
listener.onDisconnecting();
}
}
public void onDisconnected() {
lastKnownState = ConnectionState.DISCONNECTED;
isSSOFinishedWell = false;
for (StateListener listener : serviceStateListeners) {
listener.onDisconnected();
}
}
@Override
public void onPeersListChanged(long numberOfPeers) {
lastPeersCount = numberOfPeers;
for (StateListener listener : serviceStateListeners) {
listener.onPeersListChanged(numberOfPeers);
}
}
};
ServiceStateListener serviceStateListener = new ServiceStateListener() {
public void onStarted() {
Log.d(LOGTAG, "on engine started");
for (StateListener listener : serviceStateListeners) {
listener.onEngineStarted();
}
}
public void onStopped() {
Log.d(LOGTAG, "on engine stopped");
for (StateListener listener : serviceStateListeners) {
listener.onEngineStopped();
}
}
public void onError(String msg) {
// in case of error the onStopped will be called all the time
Log.e(LOGTAG, "on engine error: " + msg);
runOnUiThread(() -> {
Toast toast = Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG);
toast.show();
});
}
};
private void updateProfileMenuItem(NavigationView navigationView) {
try {
// Get active profile from ProfileManager instead of reading file
io.netbird.client.tool.ProfileManagerWrapper profileManager =
new io.netbird.client.tool.ProfileManagerWrapper(this);
String activeProfile = profileManager.getActiveProfile();
Menu menu = navigationView.getMenu();
MenuItem profileItem = menu.findItem(R.id.nav_profiles);
if (profileItem != null && activeProfile != null) {
profileItem.setTitle(activeProfile);
}
} catch (Exception e) {
Log.e(LOGTAG, "Failed to update profile menu item", e);
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (!isRunningOnTV) {
return super.onKeyDown(keyCode, event);
}
else {
Log.d(LOGTAG, "Key pressed: " + keyCode + " (" + KeyEvent.keyCodeToString(keyCode) + "), repeat: " + event.getRepeatCount());
if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
boolean isOnHomeScreen = navController != null &&
navController.getCurrentDestination() != null &&
navController.getCurrentDestination().getId() == R.id.nav_home;
if (event.getRepeatCount() == 0 && isOnHomeScreen && !binding.drawerLayout.isDrawerOpen(GravityCompat.START)) {
Toast.makeText(this, R.string.tv_menu_hint, Toast.LENGTH_SHORT).show();
}
// drawer is not selectable on Android 16+, so we open via a long press of the left d-pad button instead
if (event.getRepeatCount() > 0 && !binding.drawerLayout.isDrawerOpen(GravityCompat.START)) {
Log.d(LOGTAG, "Long press LEFT detected - opening drawer");
binding.drawerLayout.openDrawer(GravityCompat.START);
binding.navView.requestFocus();
return true;
}
}
if (keyCode == KeyEvent.KEYCODE_BACK && binding.drawerLayout.isDrawerOpen(GravityCompat.START)) {
Log.d(LOGTAG, "Closing drawer with BACK");
binding.drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}