diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 1c9ea37..1dbcafa 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -100,11 +100,19 @@
+
+
+
+
+
+
-
+ android:resource="@xml/sensor_widget_info"/>
+
@@ -119,6 +127,12 @@
+
+
+
+
+
diff --git a/app/src/main/java/com/axzae/homeassistant/ConnectActivity.java b/app/src/main/java/com/axzae/homeassistant/ConnectActivity.java
index 54b1b76..ac88f91 100644
--- a/app/src/main/java/com/axzae/homeassistant/ConnectActivity.java
+++ b/app/src/main/java/com/axzae/homeassistant/ConnectActivity.java
@@ -41,6 +41,7 @@
import com.axzae.homeassistant.model.HomeAssistantServer;
import com.axzae.homeassistant.provider.DatabaseManager;
import com.axzae.homeassistant.provider.EntityWidgetProvider;
+import com.axzae.homeassistant.provider.SensorWidgetProvider;
import com.axzae.homeassistant.provider.ServiceProvider;
import com.axzae.homeassistant.util.CommonUtil;
import com.crashlytics.android.Crashlytics;
@@ -434,7 +435,12 @@ protected ErrorMessage doInBackground(Void... param) {
mSharedPref = getAppController().getSharedPref();
- int ids[] = AppWidgetManager.getInstance(ConnectActivity.this).getAppWidgetIds(new ComponentName(ConnectActivity.this, EntityWidgetProvider.class));
+ int entityIds[] = AppWidgetManager.getInstance(ConnectActivity.this).getAppWidgetIds(new ComponentName(ConnectActivity.this, EntityWidgetProvider.class));
+ int sensorIds[] = AppWidgetManager.getInstance(ConnectActivity.this).getAppWidgetIds(new ComponentName(ConnectActivity.this, SensorWidgetProvider.class));
+ int[] ids = new int[entityIds.length + sensorIds.length];
+ System.arraycopy(entityIds, 0, ids, 0, entityIds.length);
+ System.arraycopy(sensorIds, 0, ids, entityIds.length, sensorIds.length);
+
if (ids.length > 0) {
ArrayList appWidgetIds = new ArrayList<>();
for (int id : ids) {
@@ -446,6 +452,7 @@ protected ErrorMessage doInBackground(Void... param) {
databaseManager.housekeepWidgets(appWidgetIds);
}
+
//mBundle = getIntent().getExtras();
return null;
}
diff --git a/app/src/main/java/com/axzae/homeassistant/SensorWidgetConfigActivity.java b/app/src/main/java/com/axzae/homeassistant/SensorWidgetConfigActivity.java
new file mode 100644
index 0000000..d44b216
--- /dev/null
+++ b/app/src/main/java/com/axzae/homeassistant/SensorWidgetConfigActivity.java
@@ -0,0 +1,177 @@
+package com.axzae.homeassistant;
+
+import android.appwidget.AppWidgetManager;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+import android.support.annotation.NonNull;
+import android.support.annotation.Nullable;
+import android.support.v7.widget.Toolbar;
+import android.util.Log;
+import android.view.Menu;
+import android.view.MenuInflater;
+import android.view.MenuItem;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.BaseAdapter;
+import android.widget.ListView;
+import android.widget.TextView;
+import android.widget.Toast;
+
+import com.axzae.homeassistant.model.Entity;
+import com.axzae.homeassistant.model.Widget;
+import com.axzae.homeassistant.provider.DatabaseManager;
+import com.axzae.homeassistant.provider.SensorWidgetProvider;
+
+import java.util.ArrayList;
+
+import static android.appwidget.AppWidgetManager.EXTRA_APPWIDGET_ID;
+import static android.appwidget.AppWidgetManager.INVALID_APPWIDGET_ID;
+
+public class SensorWidgetConfigActivity extends BaseActivity {
+
+ private ListView mListView;
+ private EntityListAdapter mAdapter;
+ private DatabaseManager mDatabaseManager;
+
+ @Override
+ public boolean onCreateOptionsMenu(Menu menu) {
+ MenuInflater inflater = getMenuInflater();
+ inflater.inflate(R.menu.menu_widget_config, menu);
+ return super.onCreateOptionsMenu(menu);
+ }
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_widget_config);
+ setResult(RESULT_CANCELED);
+ mDatabaseManager = DatabaseManager.getInstance(this).forceCreate();
+
+ setupToolbar();
+ initListViews();
+ }
+
+ private void setupToolbar() {
+ final Toolbar toolbar = findViewById(R.id.toolbar);
+ setSupportActionBar(toolbar);
+ if (getSupportActionBar() != null) {
+ getSupportActionBar().setDisplayHomeAsUpEnabled(false);
+ getSupportActionBar().setTitle("Select Widget Control");
+ //getSupportActionBar().setSubtitle("Add New Widget");
+ }
+ }
+
+ @Override
+ public boolean onOptionsItemSelected(MenuItem item) {
+ switch (item.getItemId()) {
+ case R.id.action_select:
+ performDone();
+ return true;
+
+ case android.R.id.home:
+ onBackPressed();
+ return true;
+ }
+
+ return true;
+ }
+
+ public void initListViews() {
+ final ArrayList mEntities = mDatabaseManager.getSensors();
+ mAdapter = new EntityListAdapter(mEntities);
+
+ mListView = findViewById(R.id.list_view);
+ mListView.setAdapter(mAdapter);
+ }
+
+ private void performDone() {
+
+ int pos = mListView.getCheckedItemPosition();
+ if (pos == -1) {
+ Toast.makeText(this, "Please select a control", Toast.LENGTH_SHORT).show();
+ return;
+ }
+ Entity selectedItem = (Entity) mAdapter.getItem(pos);
+
+ int appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
+ Intent intent = getIntent();
+ Bundle extras = intent.getExtras();
+ if (extras != null) {
+ appWidgetId = extras.getInt(EXTRA_APPWIDGET_ID, INVALID_APPWIDGET_ID);
+
+ mDatabaseManager.insertWidget(appWidgetId, selectedItem, "SENSOR");
+ Widget widget = Widget.getInstance(selectedItem, appWidgetId);
+ SensorWidgetProvider.updateEntityWidget(this, widget);
+
+ setResult(RESULT_OK);
+ finish();
+ }
+ if (appWidgetId == INVALID_APPWIDGET_ID) {
+ Log.i("I am invalid", "I am invalid");
+ finish();
+ }
+ }
+
+
+ private class EntityListAdapter extends BaseAdapter {
+ private ArrayList items = new ArrayList<>();
+
+ EntityListAdapter(ArrayList items) {
+ this.items = items;
+ }
+
+ @Override
+ public void notifyDataSetChanged() {
+ super.notifyDataSetChanged();
+ }
+
+ @Override
+ public Object getItem(int position) {
+ return items.get(position);
+ }
+
+ @Override
+ public int getCount() {
+ return items.size();
+ }
+
+ @Override
+ public long getItemId(int position) {
+ return position;
+ }
+
+ @Override
+ public boolean hasStableIds() {
+ return true;
+ }
+
+ public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
+ if (convertView == null) {
+ convertView = View.inflate(getContext(), R.layout.item_widget_select, null);
+ }
+
+ Entity entity = items.get(position);
+
+ ViewGroup mItemView = convertView.findViewById(R.id.item);
+ TextView mIconView = convertView.findViewById(R.id.text_mdi);
+ TextView mMainText = convertView.findViewById(R.id.main_text);
+ TextView mLabelText = convertView.findViewById(R.id.sub_text);
+
+ mIconView.setText(entity.getMdiIcon());
+
+ mItemView.setClickable(false);
+ mItemView.setFocusable(false);
+ mMainText.setText(entity.attributes.friendlyName);
+ mLabelText.setText(entity.getDomain());
+
+ //((TextView) convertView).setText(allItems.get(position).colour);
+ return convertView;
+ }
+
+ public Context getContext() {
+ return SensorWidgetConfigActivity.this;
+ }
+ }
+
+}
diff --git a/app/src/main/java/com/axzae/homeassistant/TransparentActivity.java b/app/src/main/java/com/axzae/homeassistant/TransparentActivity.java
index 47725f3..d780e8b 100644
--- a/app/src/main/java/com/axzae/homeassistant/TransparentActivity.java
+++ b/app/src/main/java/com/axzae/homeassistant/TransparentActivity.java
@@ -21,6 +21,7 @@
import com.axzae.homeassistant.model.rest.RxPayload;
import com.axzae.homeassistant.provider.DatabaseManager;
import com.axzae.homeassistant.provider.EntityWidgetProvider;
+import com.axzae.homeassistant.provider.SensorWidgetProvider;
import com.axzae.homeassistant.provider.ServiceProvider;
import com.axzae.homeassistant.service.DataSyncService;
import com.axzae.homeassistant.shared.EntityProcessInterface;
@@ -47,7 +48,7 @@ public class TransparentActivity extends BaseActivity implements DialogInterface
private SharedPreferences mSharedPref;
private Entity mEntity;
private Call> mCall;
- private int mWidgetId;
+ private int mWidgetId, mWidgetType;
private HomeAssistantServer mCurrentServer;
private ArrayList mServers;
private Toast mToast;
@@ -91,7 +92,7 @@ protected void onCreate(Bundle savedInstanceState) {
mServers = DatabaseManager.getInstance(this).getConnections();
mCurrentServer = mServers.get(mSharedPref.getInt("connectionIndex", 0));
mWidgetId = getIntent().getIntExtra("appWidgetId", 0);
-
+ mWidgetType = getIntent().getIntExtra("appWidgetType", 0);
String json = getIntent().getExtras().getString("entity");
mEntity = CommonUtil.inflate(json, Entity.class);
final Entity entity = mEntity;
@@ -116,7 +117,11 @@ public void onResponse(@NonNull Call call, @NonNull Response res
if (restResponse != null) {
getContentResolver().update(Uri.parse("content://com.axzae.homeassistant.provider.EntityContentProvider/"), restResponse.getContentValues(), "ENTITY_ID='" + restResponse.entityId + "'", null);
Widget newWidget = DatabaseManager.getInstance(TransparentActivity.this).getWidgetById(mWidgetId);
- EntityWidgetProvider.updateEntityWidget(TransparentActivity.this, newWidget);
+ if (mWidgetType == 1) {
+ SensorWidgetProvider.updateEntityWidget(TransparentActivity.this, newWidget);
+ } else {
+ EntityWidgetProvider.updateEntityWidget(TransparentActivity.this, newWidget);
+ }
}
}
@@ -176,7 +181,11 @@ public void onResponse(@NonNull Call> call, @NonNull Response<
if (entity.equals(mEntity)) {
Widget widget = Widget.getInstance(entity, mWidgetId);
- EntityWidgetProvider.updateEntityWidget(TransparentActivity.this, widget);
+ if (mWidgetType == 1) {
+ SensorWidgetProvider.updateEntityWidget(TransparentActivity.this, widget);
+ } else {
+ EntityWidgetProvider.updateEntityWidget(TransparentActivity.this, widget);
+ }
}
}
}
diff --git a/app/src/main/java/com/axzae/homeassistant/WidgetConfigActivity.java b/app/src/main/java/com/axzae/homeassistant/WidgetConfigActivity.java
index b61bbbe..3db90fe 100644
--- a/app/src/main/java/com/axzae/homeassistant/WidgetConfigActivity.java
+++ b/app/src/main/java/com/axzae/homeassistant/WidgetConfigActivity.java
@@ -100,7 +100,7 @@ private void performDone() {
if (extras != null) {
appWidgetId = extras.getInt(EXTRA_APPWIDGET_ID, INVALID_APPWIDGET_ID);
- mDatabaseManager.insertWidget(appWidgetId, selectedItem);
+ mDatabaseManager.insertWidget(appWidgetId, selectedItem, "ENTITY");
Widget widget = Widget.getInstance(selectedItem, appWidgetId);
EntityWidgetProvider.updateEntityWidget(this, widget);
diff --git a/app/src/main/java/com/axzae/homeassistant/model/Entity.java b/app/src/main/java/com/axzae/homeassistant/model/Entity.java
index 99fbc8e..c1c276e 100644
--- a/app/src/main/java/com/axzae/homeassistant/model/Entity.java
+++ b/app/src/main/java/com/axzae/homeassistant/model/Entity.java
@@ -9,6 +9,9 @@
import com.google.android.gms.maps.model.LatLng;
import com.google.gson.annotations.SerializedName;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
import java.util.Locale;
public class Entity {
@@ -78,6 +81,22 @@ public String getFriendlyName() {
return (attributes == null || attributes.friendlyName == null) ? "" : attributes.friendlyName;
}
+
+ public String getLastUpdated() {
+ DateFormat iso8601DateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSXXX");
+ SimpleDateFormat widgetDateFormat = new SimpleDateFormat("HH:mm");
+ if (lastUpdated == null) {
+ return "";
+ } else {
+ try {
+ Date parsed = iso8601DateFormat.parse(lastUpdated);
+
+ return widgetDateFormat.format(parsed);
+ } catch (java.text.ParseException e) {
+ return "";
+ }
+ }
+ }
public boolean isHidden() {
return attributes != null && attributes.hidden != null && attributes.hidden;
}
diff --git a/app/src/main/java/com/axzae/homeassistant/provider/DatabaseManager.java b/app/src/main/java/com/axzae/homeassistant/provider/DatabaseManager.java
index 9c40eda..adb7fdd 100644
--- a/app/src/main/java/com/axzae/homeassistant/provider/DatabaseManager.java
+++ b/app/src/main/java/com/axzae/homeassistant/provider/DatabaseManager.java
@@ -20,7 +20,7 @@
import java.util.Comparator;
public class DatabaseManager extends SQLiteOpenHelper {
- private static final int DATABASE_VERSION = 8;
+ private static final int DATABASE_VERSION = 9;
private static final String DATABASE_NAME = "HOMEASSISTANT";
private static DatabaseManager sInstance;
@@ -68,6 +68,10 @@ public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
case 7:
onCreateVer2(database);
}
+ if (oldVersion>5 && oldVersion <9) {
+ database.execSQL("ALTER TABLE " + TABLE_WIDGET + " ADD COLUMN WIDGET_TYPE VARCHAR");
+ database.execSQL("UPDATE " + TABLE_WIDGET + " SET WIDGET_TYPE='ENTITY'");
+ }
}
private void onCreateVer1(SQLiteDatabase database) {
@@ -127,6 +131,7 @@ private void onCreateVer6(SQLiteDatabase database) {
sql += "\n" + "CREATE TABLE " + TABLE_WIDGET;
sql += "\n" + "(";
sql += "\n" + " WIDGET_ID INTEGER,";
+ sql += "\n" + " WIDGET_TYPE VARCHAR,"; //Added in db version 9
sql += "\n" + " ENTITY_ID VARCHAR,";
sql += "\n" + " FRIENDLY_STATE VARCHAR,";
sql += "\n" + " FRIENDLY_NAME VARCHAR,";
@@ -329,6 +334,24 @@ public ArrayList getEntities() {
cursor.close();
return results;
}
+ public ArrayList getSensors() {
+ ArrayList results = new ArrayList<>();
+ // Select All Query
+ String selectQuery = "SELECT * from " + TABLE_ENTITY + " WHERE ENTITY_ID LIKE 'sensor.%' ORDER BY FRIENDLY_NAME ASC, DOMAIN ASC";
+
+ SQLiteDatabase db = this.getReadableDatabase();
+ Cursor cursor = db.rawQuery(selectQuery, null);
+
+ // looping through all rows and adding to list
+ if (cursor.moveToFirst()) {
+ do {
+ results.add(Entity.getInstance(cursor));
+ } while (cursor.moveToNext());
+ }
+
+ cursor.close();
+ return results;
+ }
public int updateSortKeyForGroup(int sortKey, int groupId) {
SQLiteDatabase db = this.getWritableDatabase();
@@ -469,13 +492,14 @@ public void housekeepWidgets(ArrayList appWidgetIds) {
}
}
- public void insertWidget(int widgetId, Entity entity) {
+ public void insertWidget(int widgetId, Entity entity, String widgetType) {
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransaction();
try {
ContentValues initialValues = new ContentValues();
initialValues.put("WIDGET_ID", widgetId);
+ initialValues.put("WIDGET_TYPE", widgetType);
initialValues.put("ENTITY_ID", entity.entityId);
initialValues.put("FRIENDLY_STATE", entity.getFriendlyName());
initialValues.put("FRIENDLY_NAME", entity.getFriendlyName());
@@ -522,9 +546,24 @@ public Widget getWidgetById(int appWidgetId) {
return widget;
}
- public ArrayList getWidgetIdsByEntityId(String entityId) {
+ public ArrayList getEntityWidgetIdsByEntityId(String entityId) {
+ //String selectQuery = "SELECT * from " + TABLE_ENTITY + " WHERE ENTITY_ID='" + entityId + "'";
+ String selectQuery = "SELECT WIDGET_ID FROM widgets WHERE WIDGET_TYPE<>'SENSOR' and ENTITY_ID='" + entityId + "'";
+
+ SQLiteDatabase db = this.getReadableDatabase();
+ Cursor cursor = db.rawQuery(selectQuery, null);
+ ArrayList results = new ArrayList<>();
+ if (cursor.moveToFirst()) {
+ int widgetId = cursor.getInt(cursor.getColumnIndex("WIDGET_ID"));
+ results.add(widgetId);
+ }
+ cursor.close();
+ return results;
+ }
+
+ public ArrayList getSensorWidgetIdsByEntityId(String entityId) {
//String selectQuery = "SELECT * from " + TABLE_ENTITY + " WHERE ENTITY_ID='" + entityId + "'";
- String selectQuery = "SELECT WIDGET_ID FROM widgets WHERE ENTITY_ID='" + entityId + "'";
+ String selectQuery = "SELECT WIDGET_ID FROM widgets WHERE WIDGET_TYPE='SENSOR' and ENTITY_ID='" + entityId + "'";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
diff --git a/app/src/main/java/com/axzae/homeassistant/provider/EntityContentProvider.java b/app/src/main/java/com/axzae/homeassistant/provider/EntityContentProvider.java
index fde1bc7..0a5034a 100644
--- a/app/src/main/java/com/axzae/homeassistant/provider/EntityContentProvider.java
+++ b/app/src/main/java/com/axzae/homeassistant/provider/EntityContentProvider.java
@@ -170,13 +170,18 @@ public int update(@NonNull Uri uri, ContentValues values, String selection, Stri
//Update if there are widgets found.
final String entityId = values.getAsString("ENTITY_ID");
Entity entity = mSqliteOpenHelper.getEntityById(entityId);
- ArrayList widgetIds = mSqliteOpenHelper.getWidgetIdsByEntityId(entityId);
+ ArrayList widgetIds = mSqliteOpenHelper.getEntityWidgetIdsByEntityId(entityId);
for (int widgetId : widgetIds) {
Log.d("YouQi", "Updating Widget: " + widgetId);
Widget widget = Widget.getInstance(entity, widgetId);
EntityWidgetProvider.updateEntityWidget(getContext(), widget);
}
-
+ ArrayList sensorWidgetIds = mSqliteOpenHelper.getSensorWidgetIdsByEntityId(entityId);
+ for (int sensorId : sensorWidgetIds) {
+ Log.d("YouQi", "Updating Sensor Widget: " + sensorId);
+ Widget widget = Widget.getInstance(entity, sensorId);
+ SensorWidgetProvider.updateEntityWidget(getContext(), widget);
+ }
Uri newUri = DummyContentProvider.getUrl(entityId);
Log.d("YouQi", "inform URI: " + newUri);
diff --git a/app/src/main/java/com/axzae/homeassistant/provider/EntityWidgetProvider.java b/app/src/main/java/com/axzae/homeassistant/provider/EntityWidgetProvider.java
index ee0c50c..ba3e81c 100644
--- a/app/src/main/java/com/axzae/homeassistant/provider/EntityWidgetProvider.java
+++ b/app/src/main/java/com/axzae/homeassistant/provider/EntityWidgetProvider.java
@@ -187,6 +187,7 @@ public void onReceive(final Context context, final Intent intent) {
newIntent.putExtra("appWidgetId", widget.appWidgetId);
newIntent.putExtra("entityId", widget.entityId);
newIntent.putExtra("entity", CommonUtil.deflate(dbEntity));
+ newIntent.putExtra("appWidgetType", 0);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.getApplicationContext().startActivity(newIntent);
}
diff --git a/app/src/main/java/com/axzae/homeassistant/provider/SensorWidgetProvider.java b/app/src/main/java/com/axzae/homeassistant/provider/SensorWidgetProvider.java
new file mode 100644
index 0000000..7b42050
--- /dev/null
+++ b/app/src/main/java/com/axzae/homeassistant/provider/SensorWidgetProvider.java
@@ -0,0 +1,205 @@
+package com.axzae.homeassistant.provider;
+
+import android.app.PendingIntent;
+import android.appwidget.AppWidgetManager;
+import android.appwidget.AppWidgetProvider;
+import android.content.Context;
+import android.content.Intent;
+import android.content.SharedPreferences;
+import android.graphics.Bitmap;
+import android.graphics.Canvas;
+import android.graphics.Color;
+import android.graphics.Paint;
+import android.graphics.Rect;
+import android.graphics.Typeface;
+import android.net.Uri;
+import android.os.Bundle;
+import android.support.annotation.NonNull;
+import android.support.v4.content.res.ResourcesCompat;
+import android.util.Log;
+import android.widget.RemoteViews;
+
+import com.axzae.homeassistant.AppController;
+import com.axzae.homeassistant.R;
+import com.axzae.homeassistant.TransparentActivity;
+import com.axzae.homeassistant.model.Entity;
+import com.axzae.homeassistant.model.HomeAssistantServer;
+import com.axzae.homeassistant.model.Widget;
+import com.axzae.homeassistant.util.CommonUtil;
+import com.axzae.homeassistant.util.FaultUtil;
+
+import java.util.ArrayList;
+
+import retrofit2.Call;
+import retrofit2.Callback;
+import retrofit2.Response;
+
+public class SensorWidgetProvider extends AppWidgetProvider {
+
+ private static void setTextSizeForWidth(Paint paint, float desiredWidth, String text) {
+
+ // Pick a reasonably large value for the test. Larger values produce
+ // more accurate results, but may cause problems with hardware
+ // acceleration. But there are workarounds for that, too; refer to
+ // http://stackoverflow.com/questions/6253528/font-size-too-large-to-fit-in-cache
+ final float testTextSize = 48f;
+
+ // Get the bounds of the text, using our testTextSize.
+ paint.setTextSize(testTextSize);
+ Rect bounds = new Rect();
+ paint.getTextBounds(text, 0, text.length(), bounds);
+
+ // Calculate the desired size as a proportion of our testTextSize.
+ float desiredTextSize = testTextSize * desiredWidth / bounds.width();
+
+ // Set the paint for that size.
+ paint.setTextSize(desiredTextSize);
+ }
+
+ public static void updateEntityWidget(Context context, Widget widget) {
+ Log.d("YouQi", "Widget updateEntityWidget: " + CommonUtil.deflate(widget));
+ AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
+
+ String iconText = widget.getMdiIcon(); //MDIFont.getIcon("mdi:weather-hail");
+ int iconColor = ResourcesCompat.getColor(context.getResources(), (widget.isToggleable() && !widget.isCurrentStateActive()) ? R.color.md_grey_500 : R.color.xiaomiPrimaryTextSelected, null);
+
+ Bitmap myBitmap = Bitmap.createBitmap(160, 160, Bitmap.Config.ARGB_8888);
+ myBitmap.eraseColor(Color.TRANSPARENT);
+
+ Typeface typeface = ResourcesCompat.getFont(context, R.font.mdi);
+ Paint paint = new Paint();
+ paint.setFlags(Paint.ANTI_ALIAS_FLAG);
+ paint.setAntiAlias(true);
+ paint.setTypeface(typeface);
+ paint.setColor(iconColor);
+ paint.setTextSize(160);
+ //paint.setStrokeWidth(24); // Text Size
+ //setTextSizeForWidth(paint, 48, iconText);
+ //paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER)); // Text Overlapping Pattern
+
+ Canvas canvas = new Canvas(myBitmap);
+ int yPos = (int) ((canvas.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2));
+ int xPos = (canvas.getWidth() - yPos) / 2;//(canvas.getWidth() / 2);
+ canvas.drawText(iconText, 0, yPos, paint);
+
+ RemoteViews remoteViews = new RemoteViews("com.axzae.homeassistant", R.layout.widget_sensor);
+ remoteViews.setImageViewBitmap(R.id.image_icon, myBitmap);
+ remoteViews.setTextViewText(R.id.text_state, widget.getFriendlyStateRow());
+ remoteViews.setTextColor(R.id.text_state, iconColor);
+ remoteViews.setTextViewText(R.id.text_group, widget.getFriendlyName());
+ remoteViews.setTextViewText(R.id.text_updated, widget.getLastUpdated());
+
+ //https://stackoverflow.com/questions/21311917/onreceive-will-always-receive-the-last-appwidgetid-even-different-instance-widg
+ Intent newIntent = new Intent(context, SensorWidgetProvider.class);
+ newIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
+ newIntent.putExtra("appWidgetId", widget.appWidgetId);
+ newIntent.putExtra("widget", CommonUtil.deflate(widget));
+ PendingIntent pendingIntent = PendingIntent.getBroadcast(context, widget.appWidgetId, newIntent, PendingIntent.FLAG_UPDATE_CURRENT);
+ remoteViews.setOnClickPendingIntent(R.id.item, pendingIntent);
+ appWidgetManager.updateAppWidget(widget.appWidgetId, remoteViews);
+ Log.d("YouQi", "appWidgetManager updating (" + widget.appWidgetId + "): " + widget.getFriendlyState());
+ }
+
+ @Override
+ public void onUpdate(final Context context, final AppWidgetManager appWidgetManager, final int[] appWidgetIds) {
+ //final int count = appWidgetIds.length;
+ Log.d("YouQi", "Widget onUpdate");
+ final DatabaseManager databaseManager = DatabaseManager.getInstance(context);
+ final SharedPreferences mSharedPref = ((AppController) context.getApplicationContext()).getSharedPref();
+ final ArrayList mServers = databaseManager.getConnections();
+ final HomeAssistantServer mCurrentServer = mServers.get(mSharedPref.getInt("connectionIndex", 0));
+
+ for (final int appWidgetId : appWidgetIds) {
+ Log.d("YouQi", "Widget onUpdate appWidgetId: " + appWidgetId);
+ final Widget widget = databaseManager.getWidgetById(appWidgetId);
+ if (widget != null) {
+ //updateEntityWidget(context, widget);
+
+ if (mCurrentServer != null) {
+ Call mCall = ServiceProvider.getApiService(mCurrentServer.getBaseUrl()).getState(mCurrentServer.getPassword(), widget.entityId);
+ mCall.enqueue(new Callback() {
+ @Override
+ public void onResponse(@NonNull Call call, @NonNull Response response) {
+ if (FaultUtil.isRetrofitServerError(response)) {
+ return;
+ }
+
+ Entity restResponse = response.body();
+ if (restResponse != null) {
+ context.getContentResolver().update(Uri.parse("content://com.axzae.homeassistant.provider.EntityContentProvider/"), restResponse.getContentValues(), "ENTITY_ID='" + restResponse.entityId + "'", null);
+ Widget newWidget = databaseManager.getWidgetById(appWidgetId);
+ updateEntityWidget(context, newWidget);
+ }
+ }
+
+ @Override
+ public void onFailure(@NonNull Call call, @NonNull Throwable t) {
+ updateEntityWidget(context, widget);
+ }
+ });
+ } else {
+ updateEntityWidget(context, widget);
+ }
+
+ } else {
+ Log.d("YouQi", "shit happend!");
+ }
+ }
+
+
+ }
+
+ @Override
+ public void onReceive(final Context context, final Intent intent) {
+ super.onReceive(context, intent);
+ Log.d("YouQi", "Widget onReceive");
+
+ Bundle bundle = intent.getExtras();
+ if (bundle != null) {
+ for (String key : bundle.keySet()) {
+ Object value = bundle.get(key);
+ Log.d("YouQi", String.format("%s %s (%s)", key,
+ value.toString(), value.getClass().getName()));
+ }
+ }
+
+ String rawWidget = intent.getStringExtra("widget");
+ if (rawWidget == null) return;
+ Log.d("YouQi", "Widget after rawWidget");
+ final Widget widget = CommonUtil.inflate(rawWidget, Widget.class);
+ final DatabaseManager databaseManager = DatabaseManager.getInstance(context);
+ //final SharedPreferences mSharedPref = ((AppController) context.getApplicationContext()).getSharedPref();
+ //final ArrayList mServers = databaseManager.getConnections();
+ //final HomeAssistantServer mCurrentServer = mServers.get(mSharedPref.getInt("connectionIndex", 0));
+ //final int appWidgetId =widget.appWidgetId;
+
+ Entity dbEntity = databaseManager.getEntityById(widget.entityId);
+ if (dbEntity == null) return;
+ Log.d("YouQi", "Widget after dbEntity");
+
+ {
+ Intent newIntent = new Intent(context, TransparentActivity.class);
+ newIntent.putExtra("appWidgetId", widget.appWidgetId);
+ newIntent.putExtra("entityId", widget.entityId);
+ newIntent.putExtra("entity", CommonUtil.deflate(dbEntity));
+ newIntent.putExtra("appWidgetType", 1);
+ newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+ context.getApplicationContext().startActivity(newIntent);
+ }
+
+ //updateEntityWidget(context, widget);
+
+
+// Widget newWidget = Widget.getInstance(dbEntity, widget.appWidgetId);
+// updateEntityWidget(context, newWidget);
+
+// if (intent.getStringExtra("state") != null && !dbEntity.state.equals(intent.getStringExtra("state"))) {
+// Intent newIntent = new Intent(context, UpdateWidgetService.class);
+// newIntent.putExtra("appWidgetId", appWidgetId);
+// newIntent.putExtra("entityId", entityId);
+// newIntent.putExtra("state", dbEntity.getFriendlyState());
+// newIntent.putExtra("name", dbEntity.getFriendlyName());
+// context.startService(newIntent);
+// }
+ }
+}
\ No newline at end of file
diff --git a/app/src/main/res/drawable-xxxhdpi/sensor_widget_sample.png b/app/src/main/res/drawable-xxxhdpi/sensor_widget_sample.png
new file mode 100644
index 0000000..400f151
Binary files /dev/null and b/app/src/main/res/drawable-xxxhdpi/sensor_widget_sample.png differ
diff --git a/app/src/main/res/layout/widget_sensor.xml b/app/src/main/res/layout/widget_sensor.xml
new file mode 100644
index 0000000..37c567d
--- /dev/null
+++ b/app/src/main/res/layout/widget_sensor.xml
@@ -0,0 +1,82 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/app/src/main/res/xml/sensor_widget_info.xml b/app/src/main/res/xml/sensor_widget_info.xml
new file mode 100644
index 0000000..fa20625
--- /dev/null
+++ b/app/src/main/res/xml/sensor_widget_info.xml
@@ -0,0 +1,12 @@
+
+
+