Skip to content

Commit

Permalink
code clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
trudyhood committed Aug 30, 2021
1 parent 377b40e commit 2bd150b
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Samples/VpnHood.Samples.SimpleClient.Win/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ static void Main()
var token = Token.FromAccessKey(accessKey);

var packetCapture = new WinDivertPacketCapture();
var vpnHoodClient = new VpnHoodClient(packetCapture, clientId, token, new ClientOptions {});
var vpnHoodClient = new VpnHoodClient(packetCapture, clientId, token, new ClientOptions());

vpnHoodClient.Connect().Wait();
Console.WriteLine("VpnHood Client Is Running! Open your browser and browse the Internet! Press Ctrl+C to stop.");
Expand Down
38 changes: 20 additions & 18 deletions VpnHood.Client.App.Android/AndroidApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ namespace VpnHood.Client.App.Android
#endif
class AndroidApp : Application, IAppProvider
{
private const int NOTIFICATION_ID = 1000;
private const string NOTIFICATION_CHANNEL_GENERAL_ID = "general";
private const string NOTIFICATION_CHANNEL_GENERAL_NAME = "General";
private readonly Timer _timer = new (1000);
private const int NotificationId = 1000;
private const string NotificationChannelGeneralId = "general";
private const string NotificationChannelGeneralName = "General";
private readonly Timer _timer;
private Notification.Builder? _notifyBuilder;
private AppConnectionState _lastNotifyState = AppConnectionState.None;

Expand All @@ -35,6 +35,8 @@ public AndroidApp(IntPtr javaReference, JniHandleOwnership transfer)
: base(javaReference, transfer)
{
Device = new AndroidDevice();

// init timer
_timer = new Timer(1000);
_timer.Elapsed += Timer_Elapsed;
_timer.Start();
Expand All @@ -51,10 +53,9 @@ private void Timer_Elapsed(object sender, ElapsedEventArgs e)
return;

// connection status
if (connectionState == AppConnectionState.Connected)
_notifyBuilder.SetSubText($"{connectionState}");
else
_notifyBuilder.SetSubText($"{connectionState}...");
_notifyBuilder.SetSubText(connectionState == AppConnectionState.Connected
? $"{connectionState}"
: $"{connectionState}...");

// progress
if (connectionState != AppConnectionState.Connected)
Expand All @@ -67,9 +68,9 @@ private void Timer_Elapsed(object sender, ElapsedEventArgs e)
if (notificationManager != null)
{
if (connectionState != AppConnectionState.None)
notificationManager.Notify(NOTIFICATION_ID, _notifyBuilder.Build());
notificationManager.Notify(NotificationId, _notifyBuilder.Build());
else
notificationManager.Cancel(NOTIFICATION_ID);
notificationManager.Cancel(NotificationId);
}

// set it at the end of method to make sure change is applied without any exception
Expand All @@ -81,8 +82,8 @@ public override void OnCreate()
base.OnCreate();

//app init
VpnHoodApp = VpnHoodApp.Init(this, new AppOptions { });
InitNotifitication();
VpnHoodApp = VpnHoodApp.Init(this, new AppOptions());
InitNotification();
Current = this;
}

Expand All @@ -92,20 +93,21 @@ protected override void Dispose(bool disposing)
{
VpnHoodApp?.Dispose();
VpnHoodApp = null;
_timer.Dispose();
}

base.Dispose(disposing);
}

public PendingIntent? CreatePendingIntent(string name)
public PendingIntent CreatePendingIntent(string name)
{
var intent = new Intent(this, typeof(NotificationBroadcastReceiver));
intent.SetAction(name);
var pendingIntent = PendingIntent.GetBroadcast(this, 0, intent, 0) ?? throw new Exception("Could not acquire Broadcast intent!");
return pendingIntent;
}

private void InitNotifitication()
private void InitNotification()
{
// check notification manager
var notificationManager = (NotificationManager?)GetSystemService(NotificationService);
Expand All @@ -125,14 +127,14 @@ private void InitNotifitication()
//create channel
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
var channel = new NotificationChannel(NOTIFICATION_CHANNEL_GENERAL_ID, NOTIFICATION_CHANNEL_GENERAL_NAME, NotificationImportance.Low);
var channel = new NotificationChannel(NotificationChannelGeneralId, NotificationChannelGeneralName, NotificationImportance.Low);
channel.EnableVibration(false);
channel.EnableLights(false);
channel.SetShowBadge(false);
channel.LockscreenVisibility = NotificationVisibility.Public;
channel.Importance = NotificationImportance.Low;
notificationManager.CreateNotificationChannel(channel);
_notifyBuilder = new Notification.Builder(this, NOTIFICATION_CHANNEL_GENERAL_ID);
_notifyBuilder = new Notification.Builder(this, NotificationChannelGeneralId);
}
else
{
Expand All @@ -146,8 +148,8 @@ private void InitNotifitication()
_notifyBuilder.AddAction(new Notification.Action(0, Resources?.GetText(Resource.String.disconnect) ?? "Disconnect", CreatePendingIntent("disconnect")));
_notifyBuilder.AddAction(new Notification.Action(0, Resources?.GetText(Resource.String.manage) ?? "Manage", pendingOpenIntent));
_notifyBuilder.SetSmallIcon(Resource.Mipmap.ic_notification);
_notifyBuilder.SetOngoing(true); // ingored by StartForeground
_notifyBuilder.SetAutoCancel(false); // ingored by StartForeground
_notifyBuilder.SetOngoing(true); // ignored by StartForeground
_notifyBuilder.SetAutoCancel(false); // ignored by StartForeground
}
}
}
16 changes: 8 additions & 8 deletions VpnHood.Client.App.Android/MainActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ namespace VpnHood.Client.App.Android
public class MainActivity : Activity
{
private VpnHoodAppUI? _appUi;
private const int REQUEST_VpnPermission = 10;
private const int RequestVpnPermission = 10;
private AndroidDevice Device => (AndroidDevice?)AndroidApp.Current?.Device ?? throw new InvalidOperationException($"{nameof(Device)} is not initialized!");

public WebView? WebView { get; private set; }
public Color BackgroudColor => Resources?.GetColor(Resource.Color.colorBackground, null) ?? Color.DarkBlue;
public Color BackgroundColor => Resources?.GetColor(Resource.Color.colorBackground, null) ?? Color.DarkBlue;

protected override void OnCreate(Bundle? savedInstanceState)
{
Expand All @@ -43,7 +43,7 @@ protected override void OnCreate(Bundle? savedInstanceState)
// Initialize UI
var zipStream = Resources?.Assets?.Open("SPA.zip") ?? throw new Exception("Could not load SPA.zip resource!");
_appUi = VpnHoodAppUI.Init(zipStream);
InitWebUI();
InitWebUi();
}

private void Device_OnRequestVpnPermission(object sender, EventArgs e)
Expand All @@ -55,13 +55,13 @@ private void Device_OnRequestVpnPermission(object sender, EventArgs e)
}
else
{
StartActivityForResult(intent, REQUEST_VpnPermission);
StartActivityForResult(intent, RequestVpnPermission);
}
}

protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent? data)
{
if (requestCode == REQUEST_VpnPermission && resultCode == Result.Ok)
if (requestCode == RequestVpnPermission && resultCode == Result.Ok)
Device.VpnPermissionGranted();
else
Device.VpnPermissionRejected();
Expand Down Expand Up @@ -91,10 +91,10 @@ private void InitSplashScreen()
SetContentView(imageView);
}

private void InitWebUI()
private void InitWebUi()
{
WebView = new WebView(this);
WebView.SetBackgroundColor(BackgroudColor);
WebView.SetBackgroundColor(BackgroundColor);
WebView.Settings.JavaScriptEnabled = true;
WebView.Settings.DomStorageEnabled = true;
WebView.Settings.JavaScriptCanOpenWindowsAutomatically = true;
Expand All @@ -117,7 +117,7 @@ private void WebViewClient_PageLoaded(object sender, EventArgs e)
{
if (WebView == null) throw new Exception("WebView has not been loaded yet!");
SetContentView(WebView);
Window?.SetStatusBarColor(BackgroudColor);
Window?.SetStatusBarColor(BackgroundColor);
}

public override void OnBackPressed()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public override void OnReceive(Context? context, Intent? intent)
switch (intent?.Action)
{
case "disconnect":
VpnHoodApp.Instance?.Disconnect(true);
VpnHoodApp.Instance.Disconnect(true);
return;
}
}
Expand Down
2 changes: 1 addition & 1 deletion VpnHood.ZTest/Tests/Test_FileAccessServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public void CRUD()
var sessionRequestEx2 = CreateSessionRequestEx(accessItem2, Guid.NewGuid());

var accessItem3 = accessServer1.AccessItem_Create(hostEndPoint);
var clientInfo3 = new ClientInfo { };
var clientInfo3 = new ClientInfo();
var sessionRequestEx3 = CreateSessionRequestEx(accessItem3, Guid.NewGuid());

// ************
Expand Down
1 change: 1 addition & 0 deletions VpnHood.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=netsh/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=nowindow/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=openwindow/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=vpnhood/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=_0020delete_0020rul/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

0 comments on commit 2bd150b

Please sign in to comment.