22
22
23
23
package eu .basicairdata .graziano .gpslogger ;
24
24
25
+ import static android .content .Context .POWER_SERVICE ;
26
+
25
27
import android .Manifest ;
26
28
import android .content .Intent ;
27
29
import android .content .pm .PackageManager ;
32
34
import androidx .cardview .widget .CardView ;
33
35
import androidx .core .content .ContextCompat ;
34
36
import androidx .fragment .app .Fragment ;
37
+
38
+ import android .os .PowerManager ;
35
39
import android .provider .Settings ;
36
40
import android .view .LayoutInflater ;
37
41
import android .view .View ;
38
42
import android .view .ViewGroup ;
39
43
import android .view .ViewTreeObserver ;
40
44
import android .widget .FrameLayout ;
45
+ import android .widget .ImageButton ;
46
+ import android .widget .ImageView ;
41
47
import android .widget .LinearLayout ;
42
48
import android .widget .TableLayout ;
43
49
import android .widget .TextView ;
@@ -93,7 +99,9 @@ public class FragmentGPSFix extends Fragment {
93
99
private CardView cvWarningLocationDenied ;
94
100
private CardView cvWarningGPSDisabled ;
95
101
private CardView cvWarningBackgroundRestricted ;
102
+ private CardView cvWarningBatteryOptimised ;
96
103
private LinearLayout llTimeSatellites ;
104
+ private ImageView iwWarningBatteryOptimisedClose ;
97
105
98
106
private PhysicalData phdLatitude ;
99
107
private PhysicalData phdLongitude ;
@@ -110,6 +118,7 @@ public class FragmentGPSFix extends Fragment {
110
118
private boolean EGMAltitudeCorrection ;
111
119
private boolean isValidAltitude ;
112
120
private boolean isBackgroundActivityRestricted ;
121
+ private PowerManager powerManager ;
113
122
114
123
/**
115
124
* The Observer that calculate the new available height when the layout is changed.
@@ -185,6 +194,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
185
194
cvWarningLocationDenied = view .findViewById (R .id .card_view_warning_location_denied );
186
195
cvWarningGPSDisabled = view .findViewById (R .id .card_view_warning_enable_location_service );
187
196
cvWarningBackgroundRestricted = view .findViewById (R .id .card_view_warning_background_restricted );
197
+ cvWarningBatteryOptimised = view .findViewById (R .id .card_view_warning_battery_optimised );
188
198
189
199
// TableLayouts
190
200
tlCoordinates = view .findViewById (R .id .id_TableLayout_Coordinates ) ;
@@ -195,9 +205,15 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
195
205
tlTime = view .findViewById (R .id .id_TableLayout_Time );
196
206
tlSatellites = view .findViewById (R .id .id_TableLayout_Satellites );
197
207
208
+ // ImageViews
209
+
210
+ iwWarningBatteryOptimisedClose = view .findViewById (R .id .id_warning_battery_optimised_close );
211
+
198
212
// LinearLayouts
199
213
llTimeSatellites = view .findViewById (R .id .id_linearLayout_Time_Satellites );
200
214
215
+ powerManager = (PowerManager ) gpsApp .getSystemService (POWER_SERVICE );
216
+
201
217
cvWarningGPSDisabled .setOnClickListener (new View .OnClickListener () {
202
218
@ Override
203
219
public void onClick (View v ) {
@@ -215,6 +231,14 @@ public void onClick(View v) {
215
231
}
216
232
});
217
233
234
+ iwWarningBatteryOptimisedClose .setOnClickListener (new View .OnClickListener () {
235
+ @ Override
236
+ public void onClick (View v ) {
237
+ gpsApp .setBatteryOptimisedWarningVisible (false );
238
+ update ();
239
+ }
240
+ });
241
+
218
242
cvWarningBackgroundRestricted .setOnClickListener (new View .OnClickListener () {
219
243
@ Override
220
244
public void onClick (View v ) {
@@ -235,6 +259,25 @@ public void onClick(View v) {
235
259
}
236
260
});
237
261
262
+ cvWarningBatteryOptimised .setOnClickListener (new View .OnClickListener () {
263
+ @ Override
264
+ public void onClick (View v ) {
265
+ if (!isAWarningClicked && (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M )) {
266
+ isAWarningClicked = true ;
267
+ // Go to Settings screen
268
+ Intent intent = new Intent ();
269
+ intent .setAction (Settings .ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS );
270
+
271
+ try {
272
+ getContext ().startActivity (intent );
273
+ } catch (Exception e ) {
274
+ isAWarningClicked = false ;
275
+ // Unable to open Intent
276
+ }
277
+ }
278
+ }
279
+ });
280
+
238
281
cvWarningLocationDenied .setOnClickListener (new View .OnClickListener () {
239
282
@ Override
240
283
public void onClick (View v ) {
@@ -351,6 +394,7 @@ public void update() {
351
394
352
395
tvGPSFixStatus .setVisibility (View .INVISIBLE );
353
396
cvWarningBackgroundRestricted .setVisibility (View .GONE );
397
+ cvWarningBatteryOptimised .setVisibility (View .GONE );
354
398
cvWarningGPSDisabled .setVisibility (View .GONE );
355
399
cvWarningLocationDenied .setVisibility (View .GONE );
356
400
} else {
@@ -394,6 +438,14 @@ public void update() {
394
438
cvWarningBackgroundRestricted .setVisibility (View .GONE );
395
439
}
396
440
441
+ if ((Build .VERSION .SDK_INT >= Build .VERSION_CODES .M )
442
+ && !powerManager .isIgnoringBatteryOptimizations (gpsApp .getPackageName ())
443
+ && gpsApp .isBatteryOptimisedWarningVisible ()) {
444
+ cvWarningBatteryOptimised .setVisibility (View .VISIBLE );
445
+ } else {
446
+ cvWarningBatteryOptimised .setVisibility (View .GONE );
447
+ }
448
+
397
449
if (ContextCompat .checkSelfPermission (getActivity (), Manifest .permission .ACCESS_FINE_LOCATION ) != PackageManager .PERMISSION_GRANTED ) {
398
450
tvGPSFixStatus .setText (R .string .gps_not_accessible );
399
451
cvWarningLocationDenied .setVisibility (View .VISIBLE );
0 commit comments