Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sensor widget #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,19 @@
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/entity_widget_info"/>
</receiver>
<receiver android:name=".provider.SensorWidgetProvider">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/entity_widget_info"/>
</receiver>
android:resource="@xml/sensor_widget_info"/>

</receiver>
<receiver
android:name=".service.DataSyncService$TerminateConnectionReceiver"
android:exported="false">
Expand All @@ -119,6 +127,12 @@
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
</intent-filter>
</activity>
<activity
android:name=".SensorWidgetConfigActivity">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
</intent-filter>
</activity>
<activity android:name=".GroupActivity"/>
<activity android:name=".MapActivity"/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> appWidgetIds = new ArrayList<>();
for (int id : ids) {
Expand All @@ -446,6 +452,7 @@ protected ErrorMessage doInBackground(Void... param) {
databaseManager.housekeepWidgets(appWidgetIds);

}

//mBundle = getIntent().getExtras();
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Entity> 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<Entity> items = new ArrayList<>();

EntityListAdapter(ArrayList<Entity> 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;
}
}

}
17 changes: 13 additions & 4 deletions app/src/main/java/com/axzae/homeassistant/TransparentActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -47,7 +48,7 @@ public class TransparentActivity extends BaseActivity implements DialogInterface
private SharedPreferences mSharedPref;
private Entity mEntity;
private Call<ArrayList<Entity>> mCall;
private int mWidgetId;
private int mWidgetId, mWidgetType;
private HomeAssistantServer mCurrentServer;
private ArrayList<HomeAssistantServer> mServers;
private Toast mToast;
Expand Down Expand Up @@ -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;
Expand All @@ -116,7 +117,11 @@ public void onResponse(@NonNull Call<Entity> call, @NonNull Response<Entity> 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);
}
}
}

Expand Down Expand Up @@ -176,7 +181,11 @@ public void onResponse(@NonNull Call<ArrayList<Entity>> 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);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/com/axzae/homeassistant/model/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
Expand Down
Loading