7
7
using Android . Content ;
8
8
using Android . Content . PM ;
9
9
using Android . Graphics ;
10
+ using Android . Locations ;
10
11
using Android . OS ;
11
12
using Android . Runtime ;
12
13
using Android . Support . V4 . App ;
13
14
using Android . Widget ;
14
15
15
16
using SimpleTracker . Activities ;
17
+ using SimpleTracker . Broadcasters . Receivers ;
16
18
using SimpleTracker . Dialogs ;
17
19
using SimpleTracker . Resources . layout ;
18
20
@@ -25,6 +27,14 @@ public class MainActivity : BaseApplicationActivity
25
27
{
26
28
private const int GpsRequestCode = 100 ;
27
29
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 ;
28
38
29
39
public override void OnBackPressed ( )
30
40
{
@@ -68,20 +78,24 @@ protected override void OnCreate(Bundle savedInstanceState)
68
78
{
69
79
this . connection = new Connections . GpsTrackerServiceConnection ( this ) ;
70
80
}
81
+
82
+ this . receiver = new LocationSettingsChagnedReceiver ( ) ;
71
83
}
72
84
73
85
protected override void OnResume ( )
74
86
{
75
87
base . OnResume ( ) ;
76
88
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 ( ) ;
85
99
}
86
100
87
101
/// <summary>
@@ -96,15 +110,54 @@ protected override void OnDestroy()
96
110
DisconnectService ( ) ;
97
111
}
98
112
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 ( )
100
126
{
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
+ }
102
155
}
103
156
104
157
private void StopButton_Click ( object sender , EventArgs e )
105
158
{
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 ) ;
108
161
109
162
FindViewById < TextView > ( Resource . Id . textView1 ) . Text += "\n Stopped" ;
110
163
@@ -113,7 +166,9 @@ private void StopButton_Click(object sender, EventArgs e)
113
166
114
167
private void TrackButton_Click ( object sender , EventArgs e )
115
168
{
116
- FindViewById < Button > ( Resource . Id . trackButton ) . Enabled = false ;
169
+ this . isServiceConnecting = true ;
170
+
171
+ DisableButton ( Resource . Id . trackButton ) ;
117
172
118
173
ActivityCompat . RequestPermissions ( this , new string [ ] { Manifest . Permission . AccessFineLocation } , GpsRequestCode ) ;
119
174
}
@@ -137,7 +192,7 @@ public override void OnRequestPermissionsResult(
137
192
138
193
if ( arePermissionsForLocationGranted )
139
194
{
140
- FindViewById < Button > ( Resource . Id . stopTrackButton ) . Enabled = true ;
195
+ EnableButton ( Resource . Id . stopTrackButton ) ;
141
196
142
197
var intent = new Intent ( this , typeof ( Services . GpsTrackerService ) ) ;
143
198
intent . SetAction ( "Start" ) ;
@@ -152,8 +207,8 @@ public override void OnRequestPermissionsResult(
152
207
text . Text = "Please provide GPS permissions." ;
153
208
text . SetTextColor ( Color . Red ) ;
154
209
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 ) ;
157
212
}
158
213
}
159
214
0 commit comments