From 0954b9d0d847ef759f84ffde9ff1443b52d3c082 Mon Sep 17 00:00:00 2001 From: bernard Date: Thu, 9 Feb 2017 03:36:23 +0700 Subject: [PATCH 1/2] Display prompt for enabling GPS --- .../java/org/traccar/client/MainActivity.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/app/src/main/java/org/traccar/client/MainActivity.java b/app/src/main/java/org/traccar/client/MainActivity.java index 5c891a7c..056e195b 100644 --- a/app/src/main/java/org/traccar/client/MainActivity.java +++ b/app/src/main/java/org/traccar/client/MainActivity.java @@ -16,10 +16,12 @@ package org.traccar.client; import android.Manifest; +import android.app.Activity; import android.app.AlarmManager; import android.app.AlertDialog; import android.app.PendingIntent; import android.content.ComponentName; +import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.OnSharedPreferenceChangeListener; @@ -32,6 +34,7 @@ import android.preference.PreferenceActivity; import android.preference.PreferenceManager; import android.preference.TwoStatePreference; +import android.provider.Settings; import android.util.Log; import android.util.Patterns; import android.view.Menu; @@ -230,6 +233,25 @@ private void initPreferences() { findPreference(KEY_DEVICE).setSummary(sharedPreferences.getString(KEY_DEVICE, null)); } + private void displayPromptForEnablingGPS( + final Activity activity) + { + final AlertDialog.Builder builder = new AlertDialog.Builder(activity); + final String action = Settings.ACTION_LOCATION_SOURCE_SETTINGS; + final String message = "Enable location service to find current location. Click OK to go to."; + + builder.setMessage(message) + .setPositiveButton("OK", + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface d, int id) { + activity.startActivity(new Intent(action)); + d.dismiss(); + } + }); + + builder.create().show(); + } + private void startTrackingService(boolean checkPermission, boolean permission) { if (checkPermission) { Set missingPermissions = new HashSet<>(); @@ -255,6 +277,10 @@ private void startTrackingService(boolean checkPermission, boolean permission) { startService(new Intent(this, TrackingService.class)); alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 15000, 15000, alarmIntent); + String locationProviders = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); + if (locationProviders == null || locationProviders.equals("")) { + displayPromptForEnablingGPS(this); + } } else { sharedPreferences.edit().putBoolean(KEY_STATUS, false).commit(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { From 9b0a7d27f5d5e977ca9ea403d67dc437afedb4e3 Mon Sep 17 00:00:00 2001 From: bernard Date: Fri, 10 Feb 2017 16:57:45 +0700 Subject: [PATCH 2/2] Fix camel-cased, remove uneccessary variable, not use hardcode settings, use TextUtils to check Location --- .../java/org/traccar/client/MainActivity.java | 20 ++++++++----------- app/src/main/res/values/strings.xml | 1 + 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/org/traccar/client/MainActivity.java b/app/src/main/java/org/traccar/client/MainActivity.java index 056e195b..af41accc 100644 --- a/app/src/main/java/org/traccar/client/MainActivity.java +++ b/app/src/main/java/org/traccar/client/MainActivity.java @@ -26,6 +26,7 @@ import android.content.SharedPreferences; import android.content.SharedPreferences.OnSharedPreferenceChangeListener; import android.content.pm.PackageManager; +import android.content.res.Resources; import android.os.Build; import android.os.Bundle; import android.preference.CheckBoxPreference; @@ -35,6 +36,7 @@ import android.preference.PreferenceManager; import android.preference.TwoStatePreference; import android.provider.Settings; +import android.text.TextUtils; import android.util.Log; import android.util.Patterns; import android.view.Menu; @@ -63,6 +65,7 @@ public class MainActivity extends PreferenceActivity implements OnSharedPreferen private AlarmManager alarmManager; private PendingIntent alarmIntent; + private Object message; @Override public void onCreate(Bundle savedInstanceState) { @@ -233,22 +236,17 @@ private void initPreferences() { findPreference(KEY_DEVICE).setSummary(sharedPreferences.getString(KEY_DEVICE, null)); } - private void displayPromptForEnablingGPS( - final Activity activity) + private void promptForEnablingLocationService(final Activity activity) { final AlertDialog.Builder builder = new AlertDialog.Builder(activity); - final String action = Settings.ACTION_LOCATION_SOURCE_SETTINGS; - final String message = "Enable location service to find current location. Click OK to go to."; - - builder.setMessage(message) + builder.setMessage(R.string.prompt_location_service) .setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface d, int id) { - activity.startActivity(new Intent(action)); + activity.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); d.dismiss(); } }); - builder.create().show(); } @@ -277,9 +275,8 @@ private void startTrackingService(boolean checkPermission, boolean permission) { startService(new Intent(this, TrackingService.class)); alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 15000, 15000, alarmIntent); - String locationProviders = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); - if (locationProviders == null || locationProviders.equals("")) { - displayPromptForEnablingGPS(this); + if (TextUtils.isEmpty(Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED))) { + promptForEnablingLocationService(this); } } else { sharedPreferences.edit().putBoolean(KEY_STATUS, false).commit(); @@ -312,5 +309,4 @@ public void onRequestPermissionsResult(int requestCode, String[] permissions, in startTrackingService(false, granted); } } - } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4e71662c..87de62e7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -44,4 +44,5 @@ Connectivity change Device Settings The app has been hidden. To open it again please dial 8722227 (TRACCAR). + Enable location service to find current location. Click OK to go to.