Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display prompt for enabling GPS #252

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion app/src/main/java/org/traccar/client/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@
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;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
Expand All @@ -32,6 +35,8 @@
import android.preference.PreferenceActivity;
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;
Expand Down Expand Up @@ -60,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) {
Expand Down Expand Up @@ -230,6 +236,20 @@ private void initPreferences() {
findPreference(KEY_DEVICE).setSummary(sharedPreferences.getString(KEY_DEVICE, null));
}

private void promptForEnablingLocationService(final Activity activity)
{
final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setMessage(R.string.prompt_location_service)
.setPositiveButton("OK",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still hardcoded string here.

new DialogInterface.OnClickListener() {
public void onClick(DialogInterface d, int id) {
activity.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
d.dismiss();
}
});
builder.create().show();
}

private void startTrackingService(boolean checkPermission, boolean permission) {
if (checkPermission) {
Set<String> missingPermissions = new HashSet<>();
Expand All @@ -255,6 +275,9 @@ private void startTrackingService(boolean checkPermission, boolean permission) {
startService(new Intent(this, TrackingService.class));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we wait till user enables providers before starting the service?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, because if user not enable providers, it will be useless. Traccar-client cannot update location and send it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You said yes, but you haven't fixed it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry, I'm still trying but have not been successful. I try to use AsyncTask (https://developer.android.com/reference/android/os/AsyncTask.html)

alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
15000, 15000, alarmIntent);
if (TextUtils.isEmpty(Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED))) {
promptForEnablingLocationService(this);
}
} else {
sharedPreferences.edit().putBoolean(KEY_STATUS, false).commit();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
Expand Down Expand Up @@ -286,5 +309,4 @@ public void onRequestPermissionsResult(int requestCode, String[] permissions, in
startTrackingService(false, granted);
}
}

}
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@
<string name="status_connectivity_change">Connectivity change</string>
<string name="hidden_app_name">Device Settings</string>
<string name="hidden_alert">The app has been hidden. To open it again please dial 8722227 (TRACCAR).</string>
<string name="prompt_location_service">Enable location service to find current location. Click OK to go to.</string>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use some better wording. Something like:

Please enable location services. Click OK to open settings screen.

</resources>