Skip to content
This repository was archived by the owner on Nov 20, 2022. It is now read-only.

Commit e218cbe

Browse files
authored
Merge pull request #46 from M-Yankov/fix/gps-disabled-state
Fix buttons state and text content depending on the recording status.
2 parents eef90a0 + e40a234 commit e218cbe

File tree

5 files changed

+106
-17
lines changed

5 files changed

+106
-17
lines changed

SimpleTracker/Activities/BaseApplicationActivity.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Android.Content;
22
using Android.Support.V7.App;
33
using Android.Views;
4+
using Android.Widget;
45

56
using SimpleTracker.Resources.layout;
67

@@ -27,5 +28,22 @@ public override bool OnOptionsItemSelected(IMenuItem item)
2728

2829
return base.OnOptionsItemSelected(item);
2930
}
31+
32+
protected void EnableButton(int buttonResourceId)
33+
{
34+
SetButtonState(buttonResourceId, true);
35+
}
36+
37+
protected void DisableButton(int buttonResourceId)
38+
{
39+
SetButtonState(buttonResourceId, false);
40+
}
41+
42+
private void SetButtonState(int buttonResourceId, bool enabled)
43+
{
44+
Button button = FindViewById<Button>(buttonResourceId);
45+
button.Enabled = enabled;
46+
button.Clickable = enabled;
47+
}
3048
}
3149
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Android.Content;
2+
3+
namespace SimpleTracker.Broadcasters.Receivers
4+
{
5+
public class LocationSettingsChagnedReceiver : BroadcastReceiver
6+
{
7+
public override void OnReceive(Context context, Intent intent)
8+
{
9+
if (context is MainActivity mainActivity)
10+
{
11+
mainActivity.CheckLocationProviderStatus();
12+
}
13+
}
14+
}
15+
}

SimpleTracker/Connections/GpsTrackerServiceConnection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void OnServiceConnected(ComponentName name, IBinder service)
2626

2727
if (this.IsConnected)
2828
{
29-
this.mainActivity.ShowUpdates();
29+
this.mainActivity.TrackingServiceConnected();
3030
}
3131
}
3232

SimpleTracker/MainActivity.cs

Lines changed: 71 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
using Android.Content;
88
using Android.Content.PM;
99
using Android.Graphics;
10+
using Android.Locations;
1011
using Android.OS;
1112
using Android.Runtime;
1213
using Android.Support.V4.App;
1314
using Android.Widget;
1415

1516
using SimpleTracker.Activities;
17+
using SimpleTracker.Broadcasters.Receivers;
1618
using SimpleTracker.Dialogs;
1719
using SimpleTracker.Resources.layout;
1820

@@ -25,6 +27,14 @@ public class MainActivity : BaseApplicationActivity
2527
{
2628
private const int GpsRequestCode = 100;
2729
private Connections.GpsTrackerServiceConnection connection;
30+
private LocationSettingsChagnedReceiver receiver;
31+
32+
/// <summary>
33+
/// A temporary flag indicating whether gps service is starting.
34+
/// It requires more time and for that some UI changes may depend on that status.
35+
/// It is returned to false when the gps service is started.
36+
/// </summary>
37+
private bool isServiceConnecting = false;
2838

2939
public override void OnBackPressed()
3040
{
@@ -68,20 +78,24 @@ protected override void OnCreate(Bundle savedInstanceState)
6878
{
6979
this.connection = new Connections.GpsTrackerServiceConnection(this);
7080
}
81+
82+
this.receiver = new LocationSettingsChagnedReceiver();
7183
}
7284

7385
protected override void OnResume()
7486
{
7587
base.OnResume();
7688

77-
if (this.IsServiceConnected)
78-
{
79-
// Revise the code below: What information to show when user returns to main screen?
80-
// Connect to database
81-
TextView textView = FindViewById<TextView>(Resource.Id.textView1);
82-
textView.Text = $"Recording...";
83-
textView.SetTextColor(Color.DarkGray);
84-
}
89+
RegisterReceiver(receiver, new IntentFilter(LocationManager.ProvidersChangedAction));
90+
91+
this.CheckLocationProviderStatus();
92+
}
93+
94+
protected override void OnPause()
95+
{
96+
UnregisterReceiver(receiver);
97+
98+
base.OnPause();
8599
}
86100

87101
/// <summary>
@@ -96,15 +110,54 @@ protected override void OnDestroy()
96110
DisconnectService();
97111
}
98112

99-
internal void ShowUpdates()
113+
internal void TrackingServiceConnected()
114+
{
115+
this.isServiceConnecting = false;
116+
this.CheckLocationProviderStatus();
117+
}
118+
119+
/// <summary>
120+
/// Invoked when:
121+
/// 1. OnResume of main activity
122+
/// 2. GPS Status changed from settings while on main activity
123+
/// 3. When tracking is started
124+
/// </summary>
125+
internal void CheckLocationProviderStatus()
100126
{
101-
this.OnResume();
127+
var gpsManager = (LocationManager)GetSystemService(LocationService);
128+
bool gpsEnabled = gpsManager.IsProviderEnabled(LocationManager.GpsProvider);
129+
130+
if (this.IsServiceConnected)
131+
{
132+
// Revise the code below: What information to show when user returns to main screen?
133+
// Connect to database
134+
TextView textView = FindViewById<TextView>(Resource.Id.textView1);
135+
textView.SetTextColor(Color.DarkGray);
136+
137+
textView.Text = gpsEnabled
138+
? "Recording..."
139+
: "Enable GPS to continue recording!";
140+
}
141+
else if (!isServiceConnecting)
142+
{
143+
if (gpsEnabled)
144+
{
145+
EnableButton(Resource.Id.trackButton);
146+
string startText = FindViewById<TextView>(Resource.Id.trackButton).Text;
147+
FindViewById<TextView>(Resource.Id.textView1).Text = $"Press \"{startText}\" to start GPS recording";
148+
}
149+
else
150+
{
151+
FindViewById<TextView>(Resource.Id.textView1).Text = "Turn on GPS, to start tracking";
152+
DisableButton(Resource.Id.trackButton);
153+
}
154+
}
102155
}
103156

104157
private void StopButton_Click(object sender, EventArgs e)
105158
{
106-
FindViewById<Button>(Resource.Id.trackButton).Enabled = true;
107-
FindViewById<Button>(Resource.Id.stopTrackButton).Enabled = false;
159+
EnableButton(Resource.Id.trackButton);
160+
DisableButton(Resource.Id.stopTrackButton);
108161

109162
FindViewById<TextView>(Resource.Id.textView1).Text += "\nStopped";
110163

@@ -113,7 +166,9 @@ private void StopButton_Click(object sender, EventArgs e)
113166

114167
private void TrackButton_Click(object sender, EventArgs e)
115168
{
116-
FindViewById<Button>(Resource.Id.trackButton).Enabled = false;
169+
this.isServiceConnecting = true;
170+
171+
DisableButton(Resource.Id.trackButton);
117172

118173
ActivityCompat.RequestPermissions(this, new string[] { Manifest.Permission.AccessFineLocation }, GpsRequestCode);
119174
}
@@ -137,7 +192,7 @@ public override void OnRequestPermissionsResult(
137192

138193
if (arePermissionsForLocationGranted)
139194
{
140-
FindViewById<Button>(Resource.Id.stopTrackButton).Enabled = true;
195+
EnableButton(Resource.Id.stopTrackButton);
141196

142197
var intent = new Intent(this, typeof(Services.GpsTrackerService));
143198
intent.SetAction("Start");
@@ -152,8 +207,8 @@ public override void OnRequestPermissionsResult(
152207
text.Text = "Please provide GPS permissions.";
153208
text.SetTextColor(Color.Red);
154209

155-
FindViewById<Button>(Resource.Id.trackButton).Enabled = true;
156-
FindViewById<Button>(Resource.Id.stopTrackButton).Enabled = false;
210+
EnableButton(Resource.Id.trackButton);
211+
DisableButton(Resource.Id.stopTrackButton);
157212
}
158213
}
159214

SimpleTracker/SimpleTracker.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
<Compile Include="Adapters\RoutesAdapter.cs" />
8181
<Compile Include="Adapters\RoutesViewHolder.cs" />
8282
<Compile Include="Binders\GpsTrackerServiceBinder.cs" />
83+
<Compile Include="Broadcasters\Receivers\LocationSettingsChagnedReceiver.cs" />
8384
<Compile Include="Common\ApplicationSecrets.cs" />
8485
<Compile Include="Common\Utilities.cs" />
8586
<Compile Include="Connections\GpsTrackerServiceConnection.cs" />

0 commit comments

Comments
 (0)