Skip to content

Commit

Permalink
Fix reconciliation of the DB
Browse files Browse the repository at this point in the history
  • Loading branch information
thuryn committed Jul 25, 2024
1 parent 540317b commit 41bc47f
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 97 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
6.0.2
Fix storing locations into DB

6.0.1
Fix storing of weather forecast

Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ android {
applicationId "org.thosp.yourlocalweather"
minSdkVersion 14
targetSdkVersion 34
versionCode 174
versionName "6.0.1"
versionCode 175
versionName "6.0.2"
vectorDrawables.useSupportLibrary true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
playAccountConfig = playAccountConfigs.defaultAccountConfig
Expand Down
59 changes: 25 additions & 34 deletions app/src/main/java/org/thosp/yourlocalweather/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.Typeface;
import android.location.LocationManager;
import android.net.Uri;
Expand Down Expand Up @@ -53,8 +52,6 @@
import org.thosp.yourlocalweather.model.Weather;
import org.thosp.yourlocalweather.model.WeatherForecastDbHelper;
import org.thosp.yourlocalweather.service.UpdateWeatherService;
import org.thosp.yourlocalweather.service.WeatherRequestDataHolder;
import org.thosp.yourlocalweather.utils.ApiKeys;
import org.thosp.yourlocalweather.utils.AppPreference;
import org.thosp.yourlocalweather.utils.Constants;
import org.thosp.yourlocalweather.utils.PressureWithUnit;
Expand Down Expand Up @@ -87,13 +84,6 @@ public class MainActivity extends BaseActivity
private TextView mSunsetView;
private AppBarLayout mAppBarLayout;
private TextView iconSecondTemperatureView;
private TextView mIconWindView;
private TextView mIconHumidityView;
private TextView mIconPressureView;
private TextView mIconCloudinessView;
private TextView mIconSunriseView;
private TextView mIconSunsetView;
private TextView mIconDewPointView;
private AppCompatImageButton switchLocationButton;

private ConnectionDetector connectionDetector;
Expand Down Expand Up @@ -510,21 +500,21 @@ private void initializeTextView() {
Typeface robotoLight = Typeface.createFromAsset(this.getAssets(),
"fonts/Roboto-Light.ttf");

mIconWeatherView = (ImageView) findViewById(R.id.main_weather_icon);
mTemperatureView = (TextView) findViewById(R.id.main_temperature);
dewPointView = (TextView) findViewById(R.id.main_dew_point);
secondTemperatureView = (TextView) findViewById(R.id.main_second_temperature);
mDescriptionView = (TextView) findViewById(R.id.main_description);
mPressureView = (TextView) findViewById(R.id.main_pressure);
mHumidityView = (TextView) findViewById(R.id.main_humidity);
mWindSpeedView = (TextView) findViewById(R.id.main_wind_speed);
mCloudinessView = (TextView) findViewById(R.id.main_cloudiness);
mLastUpdateView = (TextView) findViewById(R.id.main_last_update);
mSunriseView = (TextView) findViewById(R.id.main_sunrise);
mSunsetView = (TextView) findViewById(R.id.main_sunset);
mAppBarLayout = (AppBarLayout) findViewById(R.id.main_app_bar);
localityView = (TextView) findViewById(R.id.main_locality);
switchLocationButton = (AppCompatImageButton) findViewById(R.id.main_switch_location);
mIconWeatherView = findViewById(R.id.main_weather_icon);
mTemperatureView = findViewById(R.id.main_temperature);
dewPointView = findViewById(R.id.main_dew_point);
secondTemperatureView = findViewById(R.id.main_second_temperature);
mDescriptionView = findViewById(R.id.main_description);
mPressureView = findViewById(R.id.main_pressure);
mHumidityView = findViewById(R.id.main_humidity);
mWindSpeedView = findViewById(R.id.main_wind_speed);
mCloudinessView = findViewById(R.id.main_cloudiness);
mLastUpdateView = findViewById(R.id.main_last_update);
mSunriseView = findViewById(R.id.main_sunrise);
mSunsetView = findViewById(R.id.main_sunset);
mAppBarLayout = findViewById(R.id.main_app_bar);
localityView = findViewById(R.id.main_locality);
switchLocationButton = findViewById(R.id.main_switch_location);

mTemperatureView.setTypeface(robotoThin);
dewPointView.setTypeface(robotoLight);
Expand All @@ -540,28 +530,28 @@ private void initializeTextView() {
/**
* Initialize and configure weather icons
*/
iconSecondTemperatureView = (TextView) findViewById(R.id.main_second_temperature_icon);
iconSecondTemperatureView = findViewById(R.id.main_second_temperature_icon);
iconSecondTemperatureView.setTypeface(weatherFontIcon);
iconSecondTemperatureView.setText(iconSecondTemperature);
mIconWindView = (TextView) findViewById(R.id.main_wind_icon);
TextView mIconWindView = findViewById(R.id.main_wind_icon);
mIconWindView.setTypeface(weatherFontIcon);
mIconWindView.setText(mIconWind);
mIconHumidityView = (TextView) findViewById(R.id.main_humidity_icon);
TextView mIconHumidityView = findViewById(R.id.main_humidity_icon);
mIconHumidityView.setTypeface(weatherFontIcon);
mIconHumidityView.setText(mIconHumidity);
mIconPressureView = (TextView) findViewById(R.id.main_pressure_icon);
TextView mIconPressureView = findViewById(R.id.main_pressure_icon);
mIconPressureView.setTypeface(weatherFontIcon);
mIconPressureView.setText(mIconPressure);
mIconCloudinessView = (TextView) findViewById(R.id.main_cloudiness_icon);
TextView mIconCloudinessView = findViewById(R.id.main_cloudiness_icon);
mIconCloudinessView.setTypeface(weatherFontIcon);
mIconCloudinessView.setText(mIconCloudiness);
mIconSunriseView = (TextView) findViewById(R.id.main_sunrise_icon);
TextView mIconSunriseView = findViewById(R.id.main_sunrise_icon);
mIconSunriseView.setTypeface(weatherFontIcon);
mIconSunriseView.setText(mIconSunrise);
mIconSunsetView = (TextView) findViewById(R.id.main_sunset_icon);
TextView mIconSunsetView = findViewById(R.id.main_sunset_icon);
mIconSunsetView.setTypeface(weatherFontIcon);
mIconSunsetView.setText(mIconSunset);
mIconDewPointView = (TextView) findViewById(R.id.main_dew_point_icon);
TextView mIconDewPointView = findViewById(R.id.main_dew_point_icon);
mIconDewPointView.setTypeface(weatherFontIcon);
mIconDewPointView.setText(mIconDewPoint);
}
Expand Down Expand Up @@ -1166,6 +1156,7 @@ private void updateCurrentLocationAndButtonVisibility() {
switchToNextLocationWhenCurrentIsAutoAndIsDisabled();
Location autoLocation = locationsDbHelper.getLocationByOrderId(0);
int maxOrderId = locationsDbHelper.getMaxOrderId();
appendLog(getBaseContext(), TAG, "updateCurrentLocationAndButtonVisibility:maxOrderId:", maxOrderId);
AppPreference.setCurrentLocationId(MainActivity.this, currentLocation);
runOnUiThread(new Runnable() {
@Override
Expand All @@ -1183,7 +1174,7 @@ public void run() {
}
}
if ((maxOrderId > 1) ||
((maxOrderId == 1) && (locationsDbHelper.getLocationByOrderId(0).isEnabled()))) {
((maxOrderId == 1) && autoLocation.isEnabled())) {
switchLocationButton.setVisibility(View.VISIBLE);
} else {
switchLocationButton.setVisibility(View.GONE);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,20 @@
package org.thosp.yourlocalweather.service;

import static org.thosp.yourlocalweather.utils.LogToFile.appendLog;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.location.Address;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import org.thosp.yourlocalweather.model.Location;
import org.thosp.yourlocalweather.model.LocationsDbHelper;
import org.thosp.yourlocalweather.utils.ForecastUtil;
import org.thosp.yourlocalweather.utils.WidgetUtils;

import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import static org.thosp.yourlocalweather.utils.LogToFile.appendLog;

public class AbstractCommonService extends Service {

private static final String TAG = "AbstractCommonService";
Expand Down Expand Up @@ -164,7 +150,7 @@ protected void sendMessageToReconciliationDbService(boolean force) {
Intent intent = new Intent("org.thosp.yourlocalweather.action.START_RECONCILIATION");
intent.setPackage("org.thosp.yourlocalweather");
intent.putExtra("force", force);
ContextCompat.startForegroundService(getBaseContext(), intent);
startService(intent);
}

protected void sendIntent(String intent) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
package org.thosp.yourlocalweather.service;

import static org.thosp.yourlocalweather.utils.LogToFile.appendLog;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;

import org.thosp.yourlocalweather.YourLocalWeather;
import org.thosp.yourlocalweather.model.Location;
import org.thosp.yourlocalweather.model.LocationsContract;
import org.thosp.yourlocalweather.model.LocationsDbHelper;
import org.thosp.yourlocalweather.model.LocationsFileDbHelper;
import org.thosp.yourlocalweather.utils.NotificationUtils;

import static org.thosp.yourlocalweather.utils.LogToFile.appendLog;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ReconciliationDbService extends AbstractCommonService {

Expand All @@ -39,19 +33,20 @@ public void run() {
}
};


@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, final int startId) {
appendLog(getBaseContext(), TAG, "onStartCommand:", intent);
int ret = super.onStartCommand(intent, flags, startId);
if (intent == null) {
return ret;
}
YourLocalWeather.executor.submit(() -> {
startForeground(NotificationUtils.NOTIFICATION_ID, NotificationUtils.getNotificationForActivity(getBaseContext()));
//startForeground(NotificationUtils.NOTIFICATION_ID, NotificationUtils.getNotificationForActivity(getBaseContext()));
appendLog(getBaseContext(), TAG, "onStartCommand:intent.getAction():", intent.getAction());
switch (intent.getAction()) {
case "org.thosp.yourlocalweather.action.START_RECONCILIATION": startReconciliation(intent.getBooleanExtra("force", false)); return;
Expand Down Expand Up @@ -85,6 +80,7 @@ protected void startReconciliation(boolean force) {
LocationsFileDbHelper locationsFileDbHelper = LocationsFileDbHelper.getInstance(getApplicationContext());
SQLiteDatabase db = locationsFileDbHelper.getWritableDatabase();
for (Location location: locationsDbHelper.getAllRows()) {
appendLog(getBaseContext(), TAG, "reconciliation from in memory db to file db of location:", location.getId());
Location locationInFile = locationsFileDbHelper.getLocationById(location.getId());
if (locationInFile == null) {
insertLocation(db, location);
Expand All @@ -93,11 +89,13 @@ protected void startReconciliation(boolean force) {
}
}
for (Location location: locationsFileDbHelper.getAllRows()) {
appendLog(getBaseContext(), TAG, "reconciliation from file db to in memory db of location:", location.getId());
Location locationInRam = locationsDbHelper.getLocationById(location.getId());
if (locationInRam == null) {
locationsFileDbHelper.deleteRecordFromTable(location);
}
}
appendLog(getBaseContext(), TAG, "reconciliation has finished");
nextReconciliationTime = System.currentTimeMillis() + MIN_RECONCILIATION_TIME_SPAN_IN_MS;
}

Expand All @@ -119,6 +117,7 @@ private void insertLocation(SQLiteDatabase db, Location location) {
LocationsContract.Locations.TABLE_NAME,
null,
values);
appendLog(getBaseContext(), TAG, "inserted location:", location.getId(), ", order:", location.getOrderId(), ", rowId:", newLocationRowId);
}

private void updateLocation(SQLiteDatabase db, Location location, Location locationInFile) {
Expand All @@ -130,12 +129,13 @@ private void updateLocation(SQLiteDatabase db, Location location, Location locat
this,
TAG,
"update location:", location.getId());
db.updateWithOnConflict(
long existingLocationRowId = db.updateWithOnConflict(
LocationsContract.Locations.TABLE_NAME,
values,
LocationsContract.Locations._ID +"=" + locationInFile.getId(),
null,
SQLiteDatabase.CONFLICT_IGNORE);
appendLog(getBaseContext(), TAG, "updated location:", location.getId(), ", order:", location.getOrderId(), ", rowId:", existingLocationRowId);
}

private ContentValues prepareValues(Location location, Location locationInFile) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
package org.thosp.yourlocalweather.service;

import static org.thosp.yourlocalweather.utils.LogToFile.appendLog;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.app.job.JobService;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.ServiceInfo;
import android.net.TrafficStats;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.os.SystemClock;

import androidx.core.content.ContextCompat;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;

Expand All @@ -30,20 +28,14 @@
import org.thosp.yourlocalweather.R;
import org.thosp.yourlocalweather.WeatherJSONParser;
import org.thosp.yourlocalweather.YourLocalWeather;
import org.thosp.yourlocalweather.licence.LicenseNotValidException;
import org.thosp.yourlocalweather.licence.TooEarlyUpdateException;
import org.thosp.yourlocalweather.model.CompleteWeatherForecast;
import org.thosp.yourlocalweather.model.CurrentWeatherDbHelper;
import org.thosp.yourlocalweather.model.LicenseKey;
import org.thosp.yourlocalweather.model.LicenseKeysDbHelper;
import org.thosp.yourlocalweather.model.Location;
import org.thosp.yourlocalweather.model.LocationsDbHelper;
import org.thosp.yourlocalweather.model.Weather;
import org.thosp.yourlocalweather.model.WeatherForecastDbHelper;
import org.thosp.yourlocalweather.utils.ApiKeys;
import org.thosp.yourlocalweather.utils.AppPreference;
import org.thosp.yourlocalweather.utils.Constants;
import org.thosp.yourlocalweather.utils.ForecastUtil;
import org.thosp.yourlocalweather.utils.GraphUtils;
import org.thosp.yourlocalweather.utils.NotificationUtils;
import org.thosp.yourlocalweather.utils.Utils;
Expand All @@ -53,21 +45,9 @@
import java.text.ParseException;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import cz.msebera.android.httpclient.Header;

import static org.thosp.yourlocalweather.utils.LogToFile.appendLog;
import static org.thosp.yourlocalweather.utils.LogToFile.appendLogLastUpdateTime;

import androidx.core.app.ServiceCompat;
import androidx.core.content.ContextCompat;
import androidx.core.net.TrafficStatsCompat;

public class UpdateWeatherService extends AbstractCommonService {

private static final String TAG = "UpdateWeatherService";
Expand Down Expand Up @@ -497,7 +477,7 @@ private void sendResult(String result, Context context, Long locationId, int upd
}
appendLog(getBaseContext(),
TAG,
"sendResult: updateWidgets:",updateRequest.getUpdateSource());
"sendResult: updateWidgets:", (updateRequest != null) ? updateRequest.getUpdateSource() : "");
WidgetUtils.updateWidgets(getBaseContext());
sendMessageToReconciliationDbService(false);
} catch (Throwable exception) {
Expand Down
Loading

0 comments on commit 41bc47f

Please sign in to comment.