Skip to content
This repository has been archived by the owner on Sep 8, 2023. It is now read-only.

Commit

Permalink
- fix for useThinAmbient toggle not properly working
Browse files Browse the repository at this point in the history
- moved some things to constants
- modified flex timing on weather update work requests
  • Loading branch information
CorvetteCole committed Feb 28, 2020
1 parent ac1d31c commit ff6e51b
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 31 deletions.
4 changes: 2 additions & 2 deletions wearApp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
applicationId "com.corvettecole.pixelwatchface"
minSdkVersion 23
targetSdkVersion 29
versionCode 1037
versionName "1.4.1.2-dev"
versionCode 1038
versionName "1.4.1.3-dev"
multiDexEnabled true
}
compileOptions {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ public class Constants {

public static final String WEATHER_UPDATE_WORKER = "weather_update_worker";
public static final int WEATHER_UPDATE_INTERVAL = 30;
public static final int WEATHER_BACKOFF_DELAY = 1; // minutes
public static final int WEATHER_BACKOFF_DELAY_ONETIME = 30; // seconds
public static final int WEATHER_BACKOFF_DELAY_PERIODIC = 5; // minutes
public static final int WEATHER_FLEX_PERIOD = 15; // minutes, run within 15 minutes of the 30 minute period

public enum UPDATE_REQUIRED {
WEATHER,
FONT
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public static CurrentWeather getInstance(Context context) {
return instance;
}

// TODO swtich from okhttp to volley
// TODO consider putting the weather update code in the WeatherUpdateWorker
public Future<ListenableWorker.Result> updateForecast(double latitude, double longitude) {
return CallbackToFutureAdapter.getFuture(completer -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;

import static com.corvettecole.pixelwatchface.Constants.WEATHER_BACKOFF_DELAY;
import static com.corvettecole.pixelwatchface.Constants.WEATHER_BACKOFF_DELAY_ONETIME;
import static com.corvettecole.pixelwatchface.Constants.WEATHER_BACKOFF_DELAY_PERIODIC;
import static com.corvettecole.pixelwatchface.Constants.WEATHER_FLEX_PERIOD;
import static com.corvettecole.pixelwatchface.Constants.WEATHER_UPDATE_INTERVAL;
import static com.corvettecole.pixelwatchface.Constants.WEATHER_UPDATE_WORKER;
import static com.corvettecole.pixelwatchface.Utils.drawableToBitmap;
Expand Down Expand Up @@ -271,7 +273,7 @@ public void onTimeTick() {
String TAG = "onTimeTick";
Log.d(TAG, "onTimeTick called");
//if (!mWeatherUpdaterInitialized) {
initWeatherUpdater(false);
initWeatherUpdater(false);
//}
}

Expand All @@ -285,28 +287,17 @@ public void onAmbientModeChanged(boolean inAmbientMode) {
mInfoPaint.setAntiAlias(!inAmbientMode);
}

if (inAmbientMode) {
if (mSettings.isUseThinAmbient()){
mTimePaint.setStyle(Paint.Style.FILL);
mTimePaint.setTypeface(mProductSansThin);
} else {
mTimePaint.setTypeface(mProductSans);
mTimePaint.setStyle(Paint.Style.STROKE);
}
mInfoPaint.setColor(ContextCompat.getColor(getApplicationContext(), R.color.digital_text_ambient));
} else {
mTimePaint.setTypeface(mProductSans);
mTimePaint.setStyle(Paint.Style.FILL);
mInfoPaint.setColor(ContextCompat.getColor(getApplicationContext(), R.color.digital_text));

}
updateFontConfig();

// Whether the timer should be running depends on whether we're visible (as well as
// whether we're in ambient mode), so we may need to start or stop the timer.
updateTimer();
}


//TODO massively optimize this so that calculations and object allocation etc etc are not
// performed here (this needs to run as fast as possible)
// https://developer.android.com/training/wearables/watch-faces/performance
@SuppressLint("DefaultLocale")
@Override
public void onDraw(Canvas canvas, Rect bounds) {
Expand Down Expand Up @@ -402,11 +393,28 @@ public void onDraw(Canvas canvas, Rect bounds) {
}
}

private void initWeatherUpdater(boolean forceUpdate){
private void updateFontConfig() {
if (mAmbient) {
if (mSettings.isUseThinAmbient()) {
mTimePaint.setStyle(Paint.Style.FILL);
mTimePaint.setTypeface(mProductSansThin);
} else {
mTimePaint.setTypeface(mProductSans);
mTimePaint.setStyle(Paint.Style.STROKE);
}
mInfoPaint.setColor(ContextCompat.getColor(getApplicationContext(), R.color.digital_text_ambient));
} else {
mTimePaint.setTypeface(mProductSans);
mTimePaint.setStyle(Paint.Style.FILL);
mInfoPaint.setColor(ContextCompat.getColor(getApplicationContext(), R.color.digital_text));
}
}

private void initWeatherUpdater(boolean forceUpdate) {
String TAG = "initWeatherUpdater";
if (mSettings.isShowTemperature() || mSettings.isShowWeatherIcon()) {
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "requesting permission");
Log.d(TAG, "requesting permission");
requestPermissions();
} else {
Constraints constraints = new Constraints.Builder()
Expand All @@ -416,16 +424,20 @@ private void initWeatherUpdater(boolean forceUpdate){
OneTimeWorkRequest forceWeatherUpdate =
new OneTimeWorkRequest.Builder(WeatherUpdateWorker.class)
.setConstraints(constraints)
.setBackoffCriteria(BackoffPolicy.LINEAR, WEATHER_BACKOFF_DELAY, TimeUnit.MINUTES)
// forced weather update is expected to happen sooner, so
// try again in (30 seconds * attempt count). After 3 failed
// attempts, it would wait 1.5 seconds before retrying again
.setBackoffCriteria(BackoffPolicy.LINEAR, WEATHER_BACKOFF_DELAY_ONETIME, TimeUnit.SECONDS)
.build();
WorkManager.getInstance(getApplicationContext()).enqueue(forceWeatherUpdate);
} else {
Log.d(TAG, "setting up weather periodic request");
PeriodicWorkRequest weatherUpdater =
new PeriodicWorkRequest.Builder(WeatherUpdateWorker.class, WEATHER_UPDATE_INTERVAL, TimeUnit.MINUTES, 15, TimeUnit.MINUTES)
new PeriodicWorkRequest.Builder(WeatherUpdateWorker.class, WEATHER_UPDATE_INTERVAL, TimeUnit.MINUTES, WEATHER_FLEX_PERIOD, TimeUnit.MINUTES)
.setConstraints(constraints)
.addTag(WEATHER_UPDATE_WORKER)
.setBackoffCriteria(BackoffPolicy.LINEAR, WEATHER_BACKOFF_DELAY, TimeUnit.MINUTES) // max 5 hour delay
// periodic weather update is a little more lazy about scheduling. if it fails, try again in (5 minutes * attempt count) with 5 hours as the max
.setBackoffCriteria(BackoffPolicy.LINEAR, WEATHER_BACKOFF_DELAY_PERIODIC, TimeUnit.MINUTES)
.build();
WorkManager.getInstance(getApplicationContext())
.enqueueUniquePeriodicWork(WEATHER_UPDATE_WORKER, ExistingPeriodicWorkPolicy.KEEP, weatherUpdater);
Expand Down Expand Up @@ -481,10 +493,19 @@ public void onDataChanged(DataEventBuffer dataEvents) {
Log.d(TAG, dataMap.toString());
dataMap = dataMap.getDataMap("com.corvettecole.pixelwatchface");
Log.d(TAG, dataMap.toString());
if (mSettings.updateSettings(dataMap)){
initWeatherUpdater(true);

for (Constants.UPDATE_REQUIRED updateRequired : mSettings.updateSettings(dataMap)) {
switch (updateRequired) {
case WEATHER:
initWeatherUpdater(true);
break;
case FONT:
updateFontConfig();
break;
}
}
invalidate();

invalidate();// forces redraw
//syncToPhone();
}
} else if (event.getType() == DataEvent.TYPE_DELETED) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

import com.google.android.gms.wearable.DataMap;

import java.util.ArrayList;

public class Settings {


Expand Down Expand Up @@ -89,12 +91,16 @@ public String getDarkSkyAPIKey() {
return darkSkyAPIKey;
}

public boolean updateSettings(DataMap dataMap) { // returns if weather update required
public ArrayList<Constants.UPDATE_REQUIRED> updateSettings(DataMap dataMap) { // returns if weather update required
String TAG = "updateSettings";
boolean tempShowTemperature = showTemperature;
boolean tempShowWeatherIcon = showWeatherIcon;
boolean tempUseDarkSky = useDarkSky;

boolean tempUseThinAmbient = useThinAmbient;

ArrayList<Constants.UPDATE_REQUIRED> updatesRequired = new ArrayList<>();

Log.d(TAG, "timestamp: " + dataMap.getLong("timestamp"));
use24HourTime = dataMap.getBoolean("use_24_hour_time");

Expand All @@ -107,15 +113,23 @@ public boolean updateSettings(DataMap dataMap) { // returns if weather update r
showTemperatureFractional = dataMap.getBoolean("show_temperature_decimal");

useThinAmbient = dataMap.getBoolean("use_thin_ambient");
showInfoBarAmbient = dataMap.getBoolean("show_infobar_ambient", false);
showInfoBarAmbient = dataMap.getBoolean("show_ 25.0f; infobar_ambient", false);

showBattery = dataMap.getBoolean("show_battery", true);
showWearIcon = dataMap.getBoolean("show_wear_icon", true);

useDarkSky = dataMap.getBoolean("use_dark_sky", false);

savePreferences();
return (tempUseDarkSky != useDarkSky || showTemperature != tempShowTemperature || showWeatherIcon != tempShowWeatherIcon); //detect if weather provider has changed
if (tempUseDarkSky != useDarkSky || showTemperature != tempShowTemperature || showWeatherIcon != tempShowWeatherIcon) { //detect if weather provider has changed
updatesRequired.add(Constants.UPDATE_REQUIRED.WEATHER);
}
if (tempUseThinAmbient != useThinAmbient) { // check if font needs update
updatesRequired.add(Constants.UPDATE_REQUIRED.FONT);
}

return updatesRequired;

}

private void loadPreferences() {
Expand Down

0 comments on commit ff6e51b

Please sign in to comment.