Skip to content

Commit

Permalink
starting the service with IllegalStateException check; make singleton…
Browse files Browse the repository at this point in the history
…s thread save
  • Loading branch information
thuryn committed Jun 4, 2018
1 parent 3d4c071 commit 484f7f3
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 29 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ android {
applicationId "org.thosp.yourlocalweather"
minSdkVersion 14
targetSdkVersion 26
versionCode 53
versionName "3.1.9"
versionCode 54
versionName "3.1.10"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
playAccountConfig = playAccountConfigs.defaultAccountConfig
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class CurrentWeatherDbHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "CurrentWeather.db";
private static CurrentWeatherDbHelper instance;

public static CurrentWeatherDbHelper getInstance(Context ctx) {
public synchronized static CurrentWeatherDbHelper getInstance(Context ctx) {
if (instance == null) {
instance = new CurrentWeatherDbHelper(ctx.getApplicationContext());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class LocationsDbHelper extends SQLiteOpenHelper {
private Context context;
private static LocationsDbHelper instance;

public static LocationsDbHelper getInstance(Context ctx) {
public synchronized static LocationsDbHelper getInstance(Context ctx) {
if (instance == null) {
instance = new LocationsDbHelper(ctx.getApplicationContext());
}
Expand Down Expand Up @@ -473,10 +473,13 @@ public void run() {
}).start();
}

public void setNoLocationFound(Context context) {
public void setNoLocationFound() {
appendLog(context, TAG, "setNoLocationFound:entered");
new Thread(new Runnable() {
public void run() {
appendLog(context, TAG, "setNoLocationFound:run");
SQLiteDatabase db = getWritableDatabase();
appendLog(context, TAG, "setNoLocationFound:writableDB");
ContentValues values = new ContentValues();
values.put(LocationsContract.Locations.COLUMN_NAME_ADDRESS_FOUND, 0);
values.put(LocationsContract.Locations.COLUMN_NAME_LAST_UPDATE_TIME_IN_MS, System.currentTimeMillis());
Expand All @@ -486,15 +489,20 @@ public void run() {
LocationsContract.Locations.COLUMN_NAME_ORDER_ID +"=0",
null,
SQLiteDatabase.CONFLICT_IGNORE);
appendLog(context, TAG, "setNoLocationFound:updated");
LocationUpdateService.autolocationForSensorEventAddressFound = false;
appendLog(context, TAG, "setNoLocationFound:finished");
}
}).start();
}

public void updateLocationSource(final long locationId, final String updateSource) {
appendLog(context, TAG, "updateLocationSource:entered:" + locationId + ":" + updateSource);
new Thread(new Runnable() {
public void run() {
appendLog(context, TAG, "updateLocationSource:run");
SQLiteDatabase db = getWritableDatabase();
appendLog(context, TAG, "updateLocationSource:writableDB");
ContentValues values = new ContentValues();
values.put(LocationsContract.Locations.COLUMN_NAME_LOCATION_UPDATE_SOURCE, updateSource);

Expand All @@ -504,6 +512,7 @@ public void run() {
LocationsContract.Locations._ID + "=" + locationId,
null,
SQLiteDatabase.CONFLICT_IGNORE);
appendLog(context, TAG, "updateLocationSource:updated");
}
}).start();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ReverseGeocodingCacheDbHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "ReverseGeocodingCache.db";
private static ReverseGeocodingCacheDbHelper instance;

public static ReverseGeocodingCacheDbHelper getInstance(Context ctx) {
public synchronized static ReverseGeocodingCacheDbHelper getInstance(Context ctx) {
if (instance == null) {
instance = new ReverseGeocodingCacheDbHelper(ctx.getApplicationContext());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class WeatherForecastDbHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "WeatherForecast.db";
private static WeatherForecastDbHelper instance;

public static WeatherForecastDbHelper getInstance(Context ctx) {
public synchronized static WeatherForecastDbHelper getInstance(Context ctx) {
if (instance == null) {
instance = new WeatherForecastDbHelper(ctx.getApplicationContext());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,29 +274,28 @@ public void onLocationChanged(Location location, Address address) {
if (!additionalSourceSetted) {
networkSourceBuilder.append(location.getProvider().substring(0, 1));
}
String updateSource = networkSourceBuilder.toString();
updateSource = networkSourceBuilder.toString();
appendLog(getBaseContext(), TAG, "send update source to " + updateSource);
locationsDbHelper.updateLocationSource(currentLocation.getId(), updateSource);
} else if ("-".equals(currentLocation.getLocationSource())) {
locationsDbHelper.updateLocationSource(currentLocation.getId(), "N");
updateSource = "N";
}
currentLocation = locationsDbHelper.getLocationById(currentLocation.getId());
locationsDbHelper.updateAutoLocationGeoLocation(location.getLatitude(), location.getLongitude(), currentLocation.getLocationSource(), location.getAccuracy(), getLocationTimeInMilis(location));
locationsDbHelper.updateAutoLocationGeoLocation(location.getLatitude(), location.getLongitude(), updateSource, location.getAccuracy(), getLocationTimeInMilis(location));
appendLog(getBaseContext(), TAG, "put new location from location update service, latitude=" + location.getLatitude() + ", longitude=" + location.getLongitude());
if (address != null) {
locationsDbHelper.updateAutoLocationAddress(PreferenceUtil.getLanguage(getBaseContext()), address);
} else {
String geocoder = AppPreference.getLocationGeocoderSource(this);
boolean resolveAddressByOS = !("location_geocoder_unifiednlp".equals(geocoder) || "location_geocoder_local".equals(geocoder));
locationsDbHelper.setNoLocationFound(getBaseContext());
locationsDbHelper.setNoLocationFound();
Utils.getAndWriteAddressFromGeocoder(new Geocoder(this, new Locale(PreferenceUtil.getLanguage(this))),
address,
location.getLatitude(),
location.getLongitude(),
resolveAddressByOS,
this);
}
appendLog(getBaseContext(), TAG, "send intent to get weather, updateSource " + currentLocation.getLocationSource());
appendLog(getBaseContext(), TAG, "send intent to get weather, updateSource " + updateSource);
sendIntentToGetWeather(currentLocation, false);
}

Expand Down Expand Up @@ -495,7 +494,7 @@ private void setNoLocationFound(boolean isInteractive) {
if (lastLocationUpdate > now.getTimeInMillis()) {
return;
}
locationDbHelper.setNoLocationFound(this);
locationDbHelper.setNoLocationFound();
updateWidgets(isInteractive);
}

Expand Down Expand Up @@ -724,18 +723,22 @@ private void updateWidgets(boolean isInteractive) {
}

private void startBackgroundService(Intent intent, boolean isInteractive) {
if (isInteractive) {
getBaseContext().startService(intent);
} else {
PendingIntent pendingIntent = PendingIntent.getService(getBaseContext(),
0,
intent,
PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) getBaseContext().getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + 10,
pendingIntent);
try {
if (isInteractive) {
getBaseContext().startService(intent);
return;
}
} catch (IllegalStateException ise) {
//
}
PendingIntent pendingIntent = PendingIntent.getService(getBaseContext(),
0,
intent,
PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) getBaseContext().getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + 10,
pendingIntent);
}

private void sendIntentToMain(boolean isInteractive) {
Expand Down Expand Up @@ -796,7 +799,7 @@ private void processSensorEvent(SensorEvent sensorEvent) {
return;
}
LocationsDbHelper locationsDbHelper = LocationsDbHelper.getInstance(getApplicationContext());
locationsDbHelper.setNoLocationFound(getBaseContext());
locationsDbHelper.setNoLocationFound();
gravity[0] = 0;
gravity[1] = 0;
gravity[2] = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ public static void getAndWriteAddressFromGeocoder(Geocoder geocoder,
if(address != null) {
locationDbHelper.updateAutoLocationAddress(PreferenceUtil.getLanguage(context), address);
} else {
locationDbHelper.setNoLocationFound(context);
locationDbHelper.setNoLocationFound();
}
} catch (IOException | NumberFormatException ex) {
Log.e(Utils.class.getName(), "Unable to get address from latitude and longitude", ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,13 @@ private void sendWeatherUpdate(Context context) {
startLocationUpdateIntent.setPackage("org.thosp.yourlocalweather");
startLocationUpdateIntent.putExtra("locationId", currentLocation.getId());
startLocationUpdateIntent.putExtra("isInteractive", true);
context.startService(startLocationUpdateIntent);
startServiceWithCheck(context, startLocationUpdateIntent);
appendLog(context, TAG, "send intent START_LOCATION_UPDATE:" + startLocationUpdateIntent);
} else if (currentLocation.getOrderId() != 0) {
Intent intentToCheckWeather = new Intent(context, CurrentWeatherService.class);
intentToCheckWeather.putExtra("locationId", currentLocation.getId());
intentToCheckWeather.putExtra("isInteractive", true);
context.startService(intentToCheckWeather);
startServiceWithCheck(context, intentToCheckWeather);
}
}

Expand Down Expand Up @@ -247,4 +247,21 @@ private void changeLocation(int widgetId,
}
widgetSettingsDbHelper.saveParamLong(widgetId, "locationId", currentLocation.getId());
}

private void startServiceWithCheck(Context context, Intent intent) {
try {
context.startService(intent);
} catch (IllegalStateException ise) {
intent.putExtra("isInteractive", false);
PendingIntent pendingIntent = PendingIntent.getService(context,
0,
intent,
PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + 10,
pendingIntent);
}

}
}

0 comments on commit 484f7f3

Please sign in to comment.