show weather icon
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
apply plugin: 'com.android.application'
|
||||
// This does not break the build when Android Studio is missing the JRebel for Android plugin.
|
||||
apply plugin: 'com.zeroturnaround.jrebel.android'
|
||||
|
||||
|
||||
android {
|
||||
@@ -6,7 +8,7 @@ android {
|
||||
buildToolsVersion "23.0.2"
|
||||
|
||||
defaultConfig {
|
||||
applicationId "com.example.android.sunshine.wear"
|
||||
applicationId "com.example.android.sunshine.app"
|
||||
minSdkVersion 21
|
||||
targetSdkVersion 23
|
||||
versionCode 1
|
||||
|
||||
@@ -21,17 +21,35 @@ import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.res.Resources;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Rect;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.support.wearable.watchface.CanvasWatchFaceService;
|
||||
import android.support.wearable.watchface.WatchFaceStyle;
|
||||
import android.text.format.Time;
|
||||
import android.util.Log;
|
||||
import android.view.SurfaceHolder;
|
||||
import android.view.WindowInsets;
|
||||
|
||||
import com.google.android.gms.common.ConnectionResult;
|
||||
import com.google.android.gms.common.api.GoogleApiClient;
|
||||
import com.google.android.gms.common.api.PendingResult;
|
||||
import com.google.android.gms.common.api.ResultCallback;
|
||||
import com.google.android.gms.wearable.Asset;
|
||||
import com.google.android.gms.wearable.DataApi;
|
||||
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.Wearable;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.TimeZone;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -78,13 +96,16 @@ public class SunshineWatchFaceService extends CanvasWatchFaceService {
|
||||
}
|
||||
}
|
||||
|
||||
private class SunshineWatchFaceEngine extends CanvasWatchFaceService.Engine {
|
||||
private class SunshineWatchFaceEngine extends CanvasWatchFaceService.Engine implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, DataApi.DataListener {
|
||||
final Handler mUpdateTimeHandler = new EngineHandler(this);
|
||||
WatchFaceDrawHelper mDrawerHelper;
|
||||
boolean mRegisteredTimeZoneReceiver = false;
|
||||
|
||||
boolean mAmbient;
|
||||
Time mTime;
|
||||
int tempHigh, tempLow;
|
||||
Bitmap weatherIcon;
|
||||
|
||||
final BroadcastReceiver mTimeZoneReceiver = new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
@@ -99,6 +120,8 @@ public class SunshineWatchFaceService extends CanvasWatchFaceService {
|
||||
*/
|
||||
boolean mLowBitAmbient;
|
||||
|
||||
GoogleApiClient mApiClient;
|
||||
|
||||
@Override
|
||||
public void onCreate(SurfaceHolder holder) {
|
||||
super.onCreate(holder);
|
||||
@@ -111,6 +134,13 @@ public class SunshineWatchFaceService extends CanvasWatchFaceService {
|
||||
.build());
|
||||
|
||||
mTime = new Time();
|
||||
|
||||
mApiClient = new GoogleApiClient.Builder(getApplicationContext())
|
||||
.addApi(Wearable.API)
|
||||
.addConnectionCallbacks(this)
|
||||
.addOnConnectionFailedListener(this)
|
||||
.build();
|
||||
mApiClient.connect();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -192,11 +222,10 @@ public class SunshineWatchFaceService extends CanvasWatchFaceService {
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void onDraw(Canvas canvas, Rect bounds) {
|
||||
mTime.setToNow();
|
||||
mDrawerHelper.draw(canvas, bounds, mTime, 25, 16);
|
||||
mDrawerHelper.draw(canvas, bounds, mTime, tempHigh, tempLow, weatherIcon);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -230,5 +259,62 @@ public class SunshineWatchFaceService extends CanvasWatchFaceService {
|
||||
mUpdateTimeHandler.sendEmptyMessageDelayed(MSG_UPDATE_TIME, delayMs);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnected(Bundle bundle) {
|
||||
Log.d("SunshineWatchFaceSync", "connected");
|
||||
Wearable.DataApi.addListener(mApiClient, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnectionSuspended(int i) {
|
||||
Log.d("SunshineWatchFaceSync", "connection suspended: " + i);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnectionFailed(ConnectionResult connectionResult) {
|
||||
Log.d("SunshineWatchFaceSync",
|
||||
"connection failed: " + connectionResult.toString());
|
||||
}
|
||||
|
||||
public static final String KEY_HIGHEST_TEMP = "highestTemp";
|
||||
public static final String KEY_LOWEST_TEMP = "lowestTemp";
|
||||
public static final String KEY_WEATHER_ICON = "weatherIcon";
|
||||
|
||||
@Override
|
||||
public void onDataChanged(DataEventBuffer dataEvents) {
|
||||
Log.d("SunshineWatchFaceSync", "data changed");
|
||||
for (DataEvent event : dataEvents) {
|
||||
if (event.getType() == DataEvent.TYPE_CHANGED) {
|
||||
// DataItem changed
|
||||
DataItem item = event.getDataItem();
|
||||
if (item.getUri().getPath().compareTo("/dailyTemp") == 0) {
|
||||
DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
|
||||
tempHigh = dataMap.getInt(KEY_HIGHEST_TEMP);
|
||||
tempLow = dataMap.getInt(KEY_LOWEST_TEMP);
|
||||
|
||||
|
||||
GoogleApiClient client = mApiClient;
|
||||
Asset asset = dataMap.getAsset(KEY_WEATHER_ICON);
|
||||
|
||||
PendingResult<DataApi.GetFdForAssetResult> pendingResult = Wearable.DataApi.getFdForAsset(client, asset);
|
||||
pendingResult.setResultCallback(new ResultCallback<DataApi.GetFdForAssetResult>() {
|
||||
@Override
|
||||
public void onResult(DataApi.GetFdForAssetResult assetResult) {
|
||||
BitmapFactory.Options options = new BitmapFactory.Options();
|
||||
options.inSampleSize = 6; // TODO calculate this
|
||||
InputStream assetInputStream = assetResult.getInputStream();
|
||||
weatherIcon = BitmapFactory.decodeStream(assetInputStream, null, options);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Log.d("SunshineWatchFaceSync", "new dataitem: highest: " + tempHigh + " lowest: " + tempLow);
|
||||
}
|
||||
} else if (event.getType() == DataEvent.TYPE_DELETED) {
|
||||
Log.d("SunshineWatchFaceSync", "dailyTemp deleted");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.example.android.sunshine.wear;
|
||||
|
||||
import android.content.res.Resources;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Paint;
|
||||
@@ -23,23 +24,30 @@ public class WatchFaceDrawHelper {
|
||||
private Paint mDateTextPaint;
|
||||
private Paint mHighestTempPaint;
|
||||
private Paint mLowestTempPaint;
|
||||
private Paint mIconPaint;
|
||||
|
||||
float timeXOffset;
|
||||
float timeYOffset;
|
||||
float mTimeXOffset;
|
||||
float mTimeYOffset;
|
||||
float mDateXOffset;
|
||||
float mDateYOffset;
|
||||
float mWeatherYOffset;
|
||||
float mWeatherXOffset;
|
||||
float mWeatherXOffset2;
|
||||
float mTempYOffset;
|
||||
float mWeatherIconXOffset;
|
||||
float mWeatherIconYOffset;
|
||||
float mTempHighXOffset;
|
||||
float mTempLowXOffset;
|
||||
|
||||
public WatchFaceDrawHelper(Resources resources, boolean isRound) {
|
||||
timeYOffset = resources.getDimension(R.dimen.time_y_offset);
|
||||
mTimeYOffset = resources.getDimension(R.dimen.time_y_offset);
|
||||
mDateYOffset = resources.getDimension(R.dimen.date_y_offset);
|
||||
mWeatherYOffset = resources.getDimension(R.dimen.weather_y_offset);
|
||||
mTempYOffset = resources.getDimension(R.dimen.temp_y_offset);
|
||||
mWeatherIconYOffset = resources.getDimension(R.dimen.weather_icon_y_offset);
|
||||
|
||||
mBackgroundPaint = new Paint();
|
||||
mBackgroundPaint.setColor(resources.getColor(R.color.background));
|
||||
|
||||
mIconPaint = new Paint();
|
||||
// mIconPaint.setColor(resources.getColor(android.R.color.transparent));
|
||||
|
||||
timeTextPaint = createTextPaint(resources.getColor(R.color.textcolor_primary));
|
||||
mDateTextPaint = createTextPaint(resources.getColor(R.color.textcolor_secondary));
|
||||
|
||||
@@ -47,14 +55,16 @@ public class WatchFaceDrawHelper {
|
||||
mHighestTempPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
|
||||
mLowestTempPaint = createTextPaint(resources.getColor(R.color.textcolor_primary));
|
||||
|
||||
timeXOffset = resources.getDimension(isRound
|
||||
mTimeXOffset = resources.getDimension(isRound
|
||||
? R.dimen.time_x_offset_round : R.dimen.time_x_offset);
|
||||
mDateXOffset = resources.getDimension(isRound
|
||||
? R.dimen.date_x_offset_round : R.dimen.date_x_offset);
|
||||
mWeatherXOffset = resources.getDimension(isRound
|
||||
? R.dimen.weather_x_offset_round : R.dimen.weather_x_offset);
|
||||
mWeatherXOffset2 = resources.getDimension(isRound
|
||||
? R.dimen.weather_x_offset_round_2 : R.dimen.weather_x_offset_2);
|
||||
mWeatherIconXOffset = resources.getDimension(isRound
|
||||
? R.dimen.weather_icon_x_offset_round : R.dimen.weather_icon_x_offset);
|
||||
mTempHighXOffset = resources.getDimension(isRound
|
||||
? R.dimen.temp_high_x_offset_round : R.dimen.temp_high_x_offset);
|
||||
mTempLowXOffset = resources.getDimension(isRound
|
||||
? R.dimen.temp_low_x_offset_round : R.dimen.temp_low_x_offset);
|
||||
|
||||
float timeTextSize = resources.getDimension(isRound
|
||||
? R.dimen.time_text_size_round : R.dimen.time_text_size);
|
||||
@@ -91,7 +101,7 @@ public class WatchFaceDrawHelper {
|
||||
this.isLowBitAmbient = isLowBitAmbient;
|
||||
}
|
||||
|
||||
public void draw(Canvas canvas, Rect bounds, Time time, int highestTemp, int lowestTemp) {
|
||||
public void draw(Canvas canvas, Rect bounds, Time time, int highestTemp, int lowestTemp, Bitmap weatherIcon) {
|
||||
// Draw the background.
|
||||
if (isInAmbientMode) {
|
||||
canvas.drawColor(Color.BLACK);
|
||||
@@ -104,17 +114,21 @@ public class WatchFaceDrawHelper {
|
||||
String timeText = isInAmbientMode
|
||||
? String.format(TIME_FORMAT, time.hour, time.minute)
|
||||
: String.format(TIME_FORMAT_AMBIENT, time.hour, time.minute, time.second);
|
||||
canvas.drawText(timeText, timeXOffset, timeYOffset, timeTextPaint);
|
||||
canvas.drawText(timeText, mTimeXOffset, mTimeYOffset, timeTextPaint);
|
||||
|
||||
// Draw current date
|
||||
String dateText = time.format(DATE_FORMAT);
|
||||
canvas.drawText(dateText, mDateXOffset, mDateYOffset, mDateTextPaint);
|
||||
|
||||
// Draw weather
|
||||
String currentHigh = String.format(TEMP_FORMAT, highestTemp);
|
||||
canvas.drawText(currentHigh, mWeatherXOffset, mWeatherYOffset, mHighestTempPaint);
|
||||
String currentLow = String.format(TEMP_FORMAT, lowestTemp);
|
||||
canvas.drawText(currentLow, mWeatherXOffset + mWeatherXOffset2, mWeatherYOffset, mLowestTempPaint);
|
||||
if(!(highestTemp == 0 && lowestTemp == 0)) {
|
||||
String currentHigh = String.format(TEMP_FORMAT, highestTemp);
|
||||
canvas.drawText(currentHigh, mTempHighXOffset, mTempYOffset, mHighestTempPaint);
|
||||
String currentLow = String.format(TEMP_FORMAT, lowestTemp);
|
||||
canvas.drawText(currentLow, mTempLowXOffset, mTempYOffset, mLowestTempPaint);
|
||||
}
|
||||
|
||||
if(weatherIcon != null)
|
||||
canvas.drawBitmap(weatherIcon, mWeatherIconXOffset, mWeatherIconYOffset, mIconPaint);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,12 +16,17 @@
|
||||
<dimen name="weather_text_size_round">25dp</dimen>
|
||||
<dimen name="weather_text_size_2">15dp</dimen>
|
||||
<dimen name="weather_text_size_round_2">20dp</dimen>
|
||||
<dimen name="weather_x_offset">45dp</dimen>
|
||||
<dimen name="weather_x_offset_round">55dp</dimen>
|
||||
<dimen name="weather_x_offset_2">70dp</dimen>
|
||||
<dimen name="weather_x_offset_round_2">100dp</dimen>
|
||||
<dimen name="weather_y_offset">140dp</dimen>
|
||||
|
||||
<dimen name="weather_indicator_size">40dp</dimen>
|
||||
<dimen name="weather_icon_x_offset">15dp</dimen>
|
||||
<dimen name="weather_icon_x_offset_round">55dp</dimen>
|
||||
<dimen name="weather_icon_y_offset">120dp</dimen>
|
||||
|
||||
<dimen name="temp_high_x_offset">70dp</dimen>
|
||||
<dimen name="temp_high_x_offset_round">100dp</dimen>
|
||||
<dimen name="temp_low_x_offset">125dp</dimen>
|
||||
<dimen name="temp_low_x_offset_round">140dp</dimen>
|
||||
<dimen name="temp_y_offset">140dp</dimen>
|
||||
|
||||
|
||||
|
||||
</resources>
|
||||
|
||||
Reference in New Issue
Block a user