Allow getting the network location within pebble settings for sunrise/sunset calculation

It is also possible to set the location manually
On Android >=6 the required permission will be requested when pressing the button in settings.
here
Andreas Shimokawa 2016-05-26 14:39:54 +02:00
parent fa6100fcec
commit 7ef005f6a3
5 changed files with 66 additions and 16 deletions

View File

@ -17,6 +17,7 @@
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="com.fsck.k9.permission.READ_MESSAGES" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-feature
android:name="android.hardware.bluetooth"

View File

@ -1,17 +1,28 @@
package nodomain.freeyourgadget.gadgetbridge.activities;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceCategory;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.widget.Toast;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
@ -19,13 +30,14 @@ import nodomain.freeyourgadget.gadgetbridge.devices.miband.MiBandPreferencesActi
import nodomain.freeyourgadget.gadgetbridge.util.FileUtils;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_GENDER;
import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_HEIGHT_CM;
import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_SLEEP_DURATION;
import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_WEIGHT_KG;
import static nodomain.freeyourgadget.gadgetbridge.model.ActivityUser.PREF_USER_YEAR_OF_BIRTH;
public class SettingsActivity extends AbstractSettingsActivity {
private static final Logger LOG = LoggerFactory.getLogger(SettingsActivity.class);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@ -114,6 +126,33 @@ public class SettingsActivity extends AbstractSettingsActivity {
category.removePreference(pref);
}
pref = findPreference("location_aquire");
pref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(SettingsActivity.this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 0);
}
String provider = locationManager.getBestProvider(criteria, false);
if (provider != null) {
Location location = locationManager.getLastKnownLocation(provider);
String latitude = String.format(Locale.US, "%.6g", location.getLatitude());
String longitude = String.format(Locale.US, "%.6g", location.getLongitude());
LOG.info("got location. Lat: " + latitude + " Lng: " + longitude);
EditTextPreference pref_latitude = (EditTextPreference) findPreference("location_latitude");
EditTextPreference pref_longitude = (EditTextPreference) findPreference("location_longitude");
pref_latitude.setText(latitude);
pref_longitude.setText(longitude);
pref_latitude.setSummary(latitude);
pref_longitude.setSummary(longitude);
} else {
LOG.warn("No location provider found, did you deny location permission?");
}
return true;
}
});
// Get all receivers of Media Buttons
Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
@ -146,6 +185,8 @@ public class SettingsActivity extends AbstractSettingsActivity {
"pebble_emu_addr",
"pebble_emu_port",
"pebble_reconnect_attempts",
"location_latitude",
"location_longitude",
"canned_reply_suffix",
"canned_reply_1",
"canned_reply_2",

View File

@ -19,11 +19,10 @@ import java.util.GregorianCalendar;
import nodomain.freeyourgadget.gadgetbridge.BuildConfig;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.model.CalendarEventSpec;
import nodomain.freeyourgadget.gadgetbridge.service.DeviceCommunicationService;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
public class AlarmReceiver extends BroadcastReceiver {
private static final Logger LOG = LoggerFactory.getLogger(DeviceCommunicationService.class);
private static final Logger LOG = LoggerFactory.getLogger(AlarmReceiver.class);
public AlarmReceiver() {
Context context = GBApplication.getContext();
@ -55,7 +54,7 @@ public class AlarmReceiver extends BroadcastReceiver {
float latitude = prefs.getFloat("location_latitude", 0);
float longitude = prefs.getFloat("location_longitude", 0);
LOG.info("got longitude/latitude from preferences: " + latitude + "/" + longitude);
GregorianCalendar[] sunriseTransitSetTomorrow = SPA.calculateSunriseTransitSet(dateTimeTomorrow, latitude, longitude, DeltaT.estimate(dateTimeTomorrow));
CalendarEventSpec calendarEventSpec = new CalendarEventSpec();

View File

@ -42,8 +42,6 @@
<string name="pref_header_datetime">Date and Time</string>
<string name="pref_title_datetime_syctimeonconnect">Sync time</string>
<string name="pref_summary_datetime_syctimeonconnect">Sync time to device when connecting and when time or timezone changes on Android</string>
<string name="pref_title_location_latitude">Latitude</string>
<string name="pref_title_location_longitude">Longitude</string>
<string name="pref_title_theme">Theme</string>
<string name="pref_theme_light">Light</string>
@ -77,11 +75,18 @@
<string name="pref_title_pebble_activitytracker">Preferred Activitytracker</string>
<string name="pref_title_enable_pebblekit">Allow 3rd Party Android App Access</string>
<string name="pref_summary_enable_pebblekit">Enable experimental support for Android Apps using PebbleKit</string>
<string name="pref_header_location">Location</string>
<string name="pref_title_location_aquire">Aquire Location</string>
<string name="pref_title_location_latitude">Latitude</string>
<string name="pref_title_location_longitude">Longitude</string>
<string name="pref_title_pebble_forceprotocol">Force Notification Protocol</string>
<string name="pref_summary_pebble_forceprotocol">This option forces using the latest notification protocol depending on the firmware version. ENABLE ONLY IF YOU KNOW WHAT YOU ARE DOING!</string>
<string name="pref_title_pebble_forceuntested">Enable untested features</string>
<string name="pref_summary_pebble_forceuntested">Enable features that are untested. ENABLE ONLY IF YOU KNOW WHAT YOU ARE DOING!</string>
<string name="pref_title_pebble_reconnect_attempts">Reconnection Attempts</string>
<string name="not_connected">not connected</string>
<string name="connecting">connecting</string>
<string name="connected">connected</string>

View File

@ -32,14 +32,6 @@
android:key="datetime_synconconnect"
android:summary="@string/pref_summary_datetime_syctimeonconnect"
android:title="@string/pref_title_datetime_syctimeonconnect" />
<EditTextPreference
android:defaultValue="0"
android:key="location_latitude"
android:title="@string/pref_title_location_latitude" />
<EditTextPreference
android:defaultValue="0"
android:key="location_longitude"
android:title="@string/pref_title_location_longitude" />
</PreferenceCategory>
<PreferenceCategory
android:key="pref_key_notifications"
@ -204,7 +196,6 @@
android:key="pref_key_pebble"
android:title="@string/pref_title_pebble_settings">
<PreferenceCategory
android:key="pref_key_general"
android:title="@string/pref_header_general">
<CheckBoxPreference
android:defaultValue="false"
@ -226,7 +217,20 @@
android:summary="%s" />
</PreferenceCategory>
<PreferenceCategory
android:key="pref_key_development"
android:title="@string/pref_header_location">
<Preference
android:key="location_aquire"
android:title="@string/pref_title_location_aquire"/>
<EditTextPreference
android:defaultValue="0"
android:key="location_latitude"
android:title="@string/pref_title_location_latitude" />
<EditTextPreference
android:defaultValue="0"
android:key="location_longitude"
android:title="@string/pref_title_location_longitude" />
</PreferenceCategory>
<PreferenceCategory
android:title="@string/pref_header_development">
<CheckBoxPreference
android:defaultValue="false"