Skip to content

Commit

Permalink
Display real data on wear
Browse files Browse the repository at this point in the history
  • Loading branch information
Protino committed Feb 27, 2017
1 parent d99a4f4 commit 25fc622
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 17 deletions.
1 change: 0 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ android {
targetSdkVersion 25
versionCode 1
versionName "1.0"
multiDexEnabled true

buildConfigField 'String', 'OPEN_WEATHER_MAP_API_KEY', MyOpenWeatherMapApiKey
resValue 'string', 'GEO_API_KEY', MyGeoApiKey
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package io.github.protino;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

import com.google.android.gms.wearable.DataEvent;
import com.google.android.gms.wearable.DataEventBuffer;
import com.google.android.gms.wearable.DataItem;
import com.google.android.gms.wearable.DataMap;
import com.google.android.gms.wearable.DataMapItem;
import com.google.android.gms.wearable.WearableListenerService;

/**
Expand All @@ -12,6 +17,21 @@

public class DataLayerListenerService extends WearableListenerService {
private static final String LOG_TAG = DataLayerListenerService.class.getSimpleName();
private SharedPreferences sharedPreferences;
public static String weatherIdPrefKey;
public static String maxTempPrefKey;
public static String minTempPrefKey;

//Lifecycle start
@Override
public void onCreate() {
super.onCreate();
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
weatherIdPrefKey = getString(R.string.WEATHER_ID_PREF_KEY);
maxTempPrefKey = getString(R.string.MAX_TEMP_PREF_KEY);
minTempPrefKey = getString(R.string.MIN_TEMP_PREF_KEY);
}
//Lifecycle end

@Override
public void onDataChanged(DataEventBuffer dataEventBuffer) {
Expand All @@ -20,7 +40,14 @@ public void onDataChanged(DataEventBuffer dataEventBuffer) {
if (event.getType() == DataEvent.TYPE_DELETED) {
Log.d(LOG_TAG, "DataItem deleted: " + event.getDataItem());
} else if (event.getType() == DataEvent.TYPE_CHANGED) {
Log.d(LOG_TAG, "DataItem changed: " + event.getDataItem());
DataItem dataItem = event.getDataItem();
DataMap dataMap = DataMapItem.fromDataItem(dataItem).getDataMap();

SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(weatherIdPrefKey, dataMap.getInt(weatherIdPrefKey));
editor.putString(maxTempPrefKey, dataMap.getString(maxTempPrefKey));
editor.putString(minTempPrefKey, dataMap.getString(minTempPrefKey));
editor.apply();
}
}
}
Expand Down
44 changes: 44 additions & 0 deletions wear/src/main/java/io/github/protino/Utility.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.github.protino;

/**
* Created by Gurupad Mamadapur on 27-Feb-17.
*/

public class Utility {

/**
* Helper method to provide the art resource id according to the weather condition id returned
* by the OpenWeatherMap call.
*
* @param weatherId from OpenWeatherMap API response
* @return resource id for the corresponding image. -1 if no relation is found.
*/
public static int getArtResourceForWeatherCondition(int weatherId) {
// Based on weather code data found at:
// http://bugs.openweathermap.org/projects/api/wiki/Weather_Condition_Codes
if (weatherId >= 200 && weatherId <= 233) {
return R.drawable.art_storm;
} else if (weatherId >= 300 && weatherId <= 321) {
return R.drawable.art_light_rain;
} else if (weatherId >= 500 && weatherId <= 504) {
return R.drawable.art_rain;
} else if (weatherId == 511) {
return R.drawable.art_snow;
} else if (weatherId >= 520 && weatherId <= 531) {
return R.drawable.art_rain;
} else if (weatherId >= 600 && weatherId <= 622) {
return R.drawable.art_snow;
} else if (weatherId >= 701 && weatherId <= 761) {
return R.drawable.art_fog;
} else if (weatherId == 761 || weatherId == 781) {
return R.drawable.art_storm;
} else if (weatherId == 800) {
return R.drawable.art_clear;
} else if (weatherId == 801) {
return R.drawable.art_light_clouds;
} else if (weatherId >= 802 && weatherId <= 804) {
return R.drawable.art_clouds;
}
return R.drawable.ic_cloud_off_white_18dp;
}
}
71 changes: 57 additions & 14 deletions wear/src/main/java/io/github/protino/WeatherWatchFaceService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.github.protino;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
Expand All @@ -10,6 +12,7 @@
import android.graphics.Rect;
import android.graphics.Typeface;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.wearable.watchface.CanvasWatchFaceService;
import android.support.wearable.watchface.WatchFaceStyle;
import android.text.format.DateFormat;
Expand All @@ -35,11 +38,15 @@ public Engine onCreateEngine() {
return new Engine();
}

private class Engine extends CanvasWatchFaceService.Engine{
private class Engine extends CanvasWatchFaceService.Engine implements SharedPreferences.OnSharedPreferenceChangeListener {


private final String LOG_TAG = Engine.class.getSimpleName();
private final String colon = " : ";
private final Context context = WeatherWatchFaceService.this.getApplicationContext();
private String weatherIdPrefKey;
private String maxTempPrefKey;
private String minTempPrefKey;
private Bitmap weatherIconBitmap;
private Date date;
private java.text.DateFormat dateFormat;
Expand All @@ -59,23 +66,28 @@ private class Engine extends CanvasWatchFaceService.Engine{
private int highTempTextColor;
private Paint highTempTextPaint;
private Paint weatherIconBitmapPaint;
private SharedPreferences sharedPreferences;
private int weatherIconId;

//Lifecycle start
@Override
public void onCreate(SurfaceHolder holder) {
super.onCreate(holder);


sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
sharedPreferences.registerOnSharedPreferenceChangeListener(this);
weatherIdPrefKey = getString(R.string.WEATHER_ID_PREF_KEY);
maxTempPrefKey = getString(R.string.MAX_TEMP_PREF_KEY);
minTempPrefKey = getString(R.string.MIN_TEMP_PREF_KEY);

setWatchFaceStyle(new WatchFaceStyle.Builder(WeatherWatchFaceService.this)
.setCardPeekMode(WatchFaceStyle.PEEK_MODE_VARIABLE)
.setBackgroundVisibility(WatchFaceStyle.BACKGROUND_VISIBILITY_INTERRUPTIVE)
.setShowSystemUiTime(false)
.build());

Resources resources = WeatherWatchFaceService.this.getResources();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
weatherIconBitmap = BitmapFactory.decodeResource(resources, R.drawable.art_clear, options);

Resources resources = context.getResources();

twoDigitFormat = resources.getString(R.string.two_digit_format);
timeTextSize = resources.getDimension(R.dimen.time_text_size);
Expand Down Expand Up @@ -109,17 +121,32 @@ public void onCreate(SurfaceHolder holder) {
lowTempTextPaint = createTextPaint(lowTempTextColor, NORMAL_TYPEFACE);
lowTempTextPaint.setTextSize(lowTempTextSize);


highTempText = "30°";
lowTempText = "24°";

weatherIconId = sharedPreferences.getInt(weatherIdPrefKey, -1);
weatherIconBitmap = getWeatherIconBitmapFromId(resources, weatherIconId);
highTempText = sharedPreferences.getString(maxTempPrefKey, "");
lowTempText = sharedPreferences.getString(minTempPrefKey, "");

date = new Date();
calendar = Calendar.getInstance();

initFormats();
}

@Override
public void onDestroy() {
super.onDestroy();
sharedPreferences.unregisterOnSharedPreferenceChangeListener(this);
}

private Bitmap getWeatherIconBitmapFromId(Resources resources, int weatherIconId) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
return BitmapFactory.decodeResource(
resources,
Utility.getArtResourceForWeatherCondition(weatherIconId),
options);
}

private Paint createTextPaint(int timeColor, Typeface typeface) {
Paint paint = new Paint();
paint.setColor(timeColor);
Expand All @@ -129,7 +156,7 @@ private Paint createTextPaint(int timeColor, Typeface typeface) {
}

private void initFormats() {
dateFormat = DateFormat.getDateFormat(WeatherWatchFaceService.this);
dateFormat = DateFormat.getDateFormat(context);
dateFormat.setCalendar(calendar);
}

Expand Down Expand Up @@ -160,7 +187,7 @@ public void onDraw(Canvas canvas, Rect bounds) {
calendar.setTimeInMillis(now);

/* Data */
boolean is24Hour = DateFormat.is24HourFormat(WeatherWatchFaceService.this);
boolean is24Hour = DateFormat.is24HourFormat(context);
String hourString;
if (is24Hour) {
hourString = formatTwoDigitNumber(calendar.get(Calendar.HOUR_OF_DAY));
Expand Down Expand Up @@ -217,10 +244,11 @@ public void onDraw(Canvas canvas, Rect bounds) {


/** place weatherIconBitmap overlapping both upperRect and lowerRect
centered along x-axis. {@link weatherIconBitmapOffset}
centered along x-axis.
*/
rect = new Rect();
int weatherIconBitmapOffset = (int) (boundsWidth / 8.0);
int halfLength = (weatherIconId == -1) ? 12 : 10;
int weatherIconBitmapOffset = boundsWidth / halfLength;
int weatherIconBitmapLeft = lowerRect.centerX() - weatherIconBitmapOffset;
int weatherIconBitmapTop = upperRectYOffset - weatherIconBitmapOffset;
int weatherIconBitmapBottom = upperRectYOffset + weatherIconBitmapOffset;
Expand Down Expand Up @@ -262,5 +290,20 @@ public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int he
super.onSurfaceChanged(holder, format, width, height);
Log.d(LOG_TAG, "onSurfaceChanged: " + width + " " + height);
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (key.equals(weatherIdPrefKey)) {
weatherIconId = sharedPreferences.getInt(weatherIdPrefKey, -1);
weatherIconBitmap = getWeatherIconBitmapFromId(getResources(), weatherIconId);
invalidate();
} else if (key.equals(maxTempPrefKey)) {
highTempText = sharedPreferences.getString(maxTempPrefKey, "");
invalidate();
} else if (key.equals(minTempPrefKey)) {
lowTempText = sharedPreferences.getString(minTempPrefKey, "");
invalidate();
}
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 5 additions & 1 deletion wear/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@

<string name="weather_data_item_path">/wearable_weather_data</string>


<string name="watch_name">Sunshine Watch</string>

<string name="two_digit_format">%02d</string>

<!--Preference keys -->
<string name="WEATHER_ID_PREF_KEY" translatable="false">weather_id</string>
<string name="MAX_TEMP_PREF_KEY" translatable="false">max_temp</string>
<string name="MIN_TEMP_PREF_KEY" translatable="false">min_temp</string>

</resources>

0 comments on commit 25fc622

Please sign in to comment.