Skip to content

Commit

Permalink
Added Logged Data Widget
Browse files Browse the repository at this point in the history
Solves Issue fossasia#2068
  • Loading branch information
kartikeysaran committed Jan 29, 2021
1 parent b396991 commit 1126381
Show file tree
Hide file tree
Showing 10 changed files with 265 additions and 7 deletions.
10 changes: 10 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:replace="android:icon">
<receiver android:name=".widgets.loggedData.LoggedDataWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>

<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/logged_data_widget_info" />
</receiver>

<uses-library
android:name="org.apache.http.legacy"
android:required="false" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.pslab.widgets.loggedData;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.RemoteViews;

import io.pslab.R;
import io.pslab.activity.DataLoggerActivity;

public class LoggedDataWidget extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
for (int i = 0; i < appWidgetIds.length; i++) {
Intent intent = new Intent(context, logDataWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.logged_data_widget);
rv.setRemoteAdapter(android.R.id.list, intent);
rv.setEmptyView(android.R.id.list, R.id.data_logger_list_view);
appWidgetManager.updateAppWidget(appWidgetIds[i], rv);
}

super.onUpdate(context, appWidgetManager, appWidgetIds);
}
}

155 changes: 155 additions & 0 deletions app/src/main/java/io/pslab/widgets/loggedData/logDataWidgetList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package io.pslab.widgets.loggedData;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.RemoteViews;
import android.widget.RemoteViewsService;

import io.pslab.R;
import io.pslab.adapters.SensorLoggerListAdapter;
import io.pslab.models.PSLabSensor;
import io.pslab.models.SensorDataBlock;
import io.pslab.others.CSVLogger;
import io.pslab.others.LocalDataLog;
import io.realm.Realm;
import io.realm.RealmResults;

public class logDataWidgetList implements RemoteViewsService.RemoteViewsFactory {

private List<SensorDataBlock> results = new ArrayList<>();
private Context context;
private RealmResults<SensorDataBlock> entities;
private Realm realm;
// private int appWidgetId;

public logDataWidgetList(Context context, Intent intent) {

this.context = context;
// appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
}

@Override
public void onCreate() {
refreshEntities();
}

private void refreshEntities() {
realm = LocalDataLog.with().getRealm();
entities = LocalDataLog.with().getAllSensorBlocks();
}

@Override
public RemoteViews getViewAt(int position) {
final RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.logger_data_item);
final SensorDataBlock block = entities.get(position);
assert block!=null;
switch (block.getSensorType()) {
case PSLabSensor.LUXMETER:
remoteViews.setTextViewText(R.id.sensor_name,context.getResources().getString(R.string.lux_meter));
remoteViews.setImageViewResource(R.id.sensor_tile_icon,R.drawable.tile_icon_lux_meter_log);
break;
case PSLabSensor.BAROMETER:
remoteViews.setTextViewText(R.id.sensor_name,context.getResources().getString(R.string.baro_meter));
remoteViews.setImageViewResource(R.id.sensor_tile_icon,R.drawable.tile_icon_barometer_log);
break;
case PSLabSensor.GYROSCOPE:
remoteViews.setTextViewText(R.id.sensor_name,context.getResources().getString(R.string.gyroscope));
remoteViews.setImageViewResource(R.id.sensor_tile_icon,R.drawable.gyroscope_logo);
break;
case PSLabSensor.COMPASS:
remoteViews.setTextViewText(R.id.sensor_name,context.getResources().getString(R.string.gyroscope));
remoteViews.setImageViewResource(R.id.sensor_tile_icon,R.drawable.gyroscope_logo);
break;
case PSLabSensor.ACCELEROMETER:
remoteViews.setTextViewText(R.id.sensor_name,context.getResources().getString(R.string.accelerometer));
remoteViews.setImageViewResource(R.id.sensor_tile_icon,R.drawable.tile_icon_accelerometer);
break;
case PSLabSensor.THERMOMETER:
remoteViews.setTextViewText(R.id.sensor_name,context.getResources().getString(R.string.thermometer));
remoteViews.setImageViewResource(R.id.sensor_tile_icon,R.drawable.thermometer_logo);
break;
case PSLabSensor.ROBOTIC_ARM:
remoteViews.setTextViewText(R.id.sensor_name,context.getResources().getString(R.string.robotic_arm));
remoteViews.setImageViewResource(R.id.sensor_tile_icon,R.drawable.robotic_arm);
break;
case PSLabSensor.WAVE_GENERATOR:
remoteViews.setTextViewText(R.id.sensor_name,context.getResources().getString(R.string.wave_generator));
remoteViews.setImageViewResource(R.id.sensor_tile_icon,R.drawable.tile_icon_wave_generator);
break;
case PSLabSensor.OSCILLOSCOPE:
remoteViews.setTextViewText(R.id.sensor_name,context.getResources().getString(R.string.oscilloscope));
remoteViews.setImageViewResource(R.id.sensor_tile_icon,R.drawable.tile_icon_oscilloscope);
break;
case PSLabSensor.POWER_SOURCE:
remoteViews.setTextViewText(R.id.sensor_name,context.getResources().getString(R.string.power_source));
remoteViews.setImageViewResource(R.id.sensor_tile_icon,R.drawable.tile_icon_power_source);
break;
case PSLabSensor.MULTIMETER:
remoteViews.setTextViewText(R.id.sensor_name,context.getResources().getString(R.string.multimeter));
remoteViews.setImageViewResource(R.id.sensor_tile_icon,R.drawable.tile_icon_multimeter);
break;
case PSLabSensor.LOGIC_ANALYZER:
remoteViews.setTextViewText(R.id.sensor_name,context.getResources().getString(R.string.logical_analyzer));
remoteViews.setImageViewResource(R.id.sensor_tile_icon,R.drawable.tile_icon_logic_analyzer);
break;
case PSLabSensor.GAS_SENSOR:
remoteViews.setTextViewText(R.id.sensor_name,context.getResources().getString(R.string.gas_sensor));
remoteViews.setImageViewResource(R.id.sensor_tile_icon,R.drawable.tile_icon_gas);
break;
case PSLabSensor.SOUND_METER:
remoteViews.setTextViewText(R.id.sensor_name,context.getResources().getString(R.string.sound_meter));
remoteViews.setImageViewResource(R.id.sensor_tile_icon,R.drawable.tile_icon_gas);
break;
default:
break;
}
remoteViews.setTextViewText(R.id.date_time,String.valueOf(CSVLogger.FILE_NAME_FORMAT.format(new Date(block.getBlock()))));
return remoteViews;
}

@Override
public RemoteViews getLoadingView() {
return null;
}

@Override
public int getCount() {

return entities.size();
}

@Override
public long getItemId(int position) {

return position;
}

@Override
public void onDataSetChanged() {

refreshEntities();
}

@Override
public void onDestroy() { }


@Override
public int getViewTypeCount() {

return 1;
}

@Override
public boolean hasStableIds() {

return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.pslab.widgets.loggedData;

import android.content.Intent;
import android.widget.RemoteViewsService;

public class logDataWidgetService extends RemoteViewsService {

@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {

return new logDataWidgetList(getApplicationContext(), intent);
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions app/src/main/res/layout/logged_data_widget.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/widget_margin">
<TextView
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:text="@string/logged_data"
android:background="@color/colorPrimary"
android:textSize="20sp"
android:textColor="@color/white"
android:gravity="center"
android:id="@+id/loggedDataWidget_textView"
/>
<TextView
android:id="@+id/data_logger_blank_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_below="@+id/loggedDataWidget_textView"
android:text="@string/no_logs_to_display" />

<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/loggedDataWidget_textView"
android:id="@+id/data_logger_list_view"
/>
</RelativeLayout>
10 changes: 10 additions & 0 deletions app/src/main/res/values-v14/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<!--
Refer to App Widget Documentation for margin information
http://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout
-->
<dimen name="widget_margin">0dp</dimen>

</resources>
8 changes: 7 additions & 1 deletion app/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
<dimen name="multimeter_length_0">0dp</dimen>
<dimen name="multimeter_knob_width_xhdpi">192dp</dimen>
<dimen name="multimeter_knob_height_xhdpi">190dp</dimen>

<dimen name="multimeter_knobcircle_radius_1_xl_tablet">173dp</dimen>
<dimen name="multimeter_knobcircle_radius_2_xl_tablet">167dp</dimen>
<dimen name="multimeter_knobcircle_radius_3_xl_tablet">176dp</dimen>
Expand Down Expand Up @@ -396,4 +396,10 @@
<dimen name="sound_guage_font_size">8sp</dimen>
<dimen name="sound_sensor_label_title_size">16sp</dimen>
<dimen name="sound_width_0">0dp</dimen>

<!--
Refer to App Widget Documentation for margin information
http://developer.android.com/guide/topics/appwidgets/index.html#CreatingLayout
-->
<dimen name="widget_margin">8dp</dimen>
</resources>
8 changes: 2 additions & 6 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
<string name="app_name">PSLab</string>
<string name="openDrawer">open_drawer</string>
<string name="closeDrawer">close_drawer</string>

<string name="Toast_double_tap">Press again to close PSLab</string>
<string name="Toast_magnetic_not_present">"The sensor required for this instrument is not present in your device"</string>

<string name="nav_device">Connect Device</string>
<string name="nav_instruments">Instruments</string>
<string name="nav_settings">Settings</string>
Expand Down Expand Up @@ -1031,14 +1029,12 @@
<item>0</item>
<item>1</item>
</string-array>

<string name="google_map">Google Map</string>
<string name="osm_map">OpenStreetMap</string>
<string name="no_location_specified">Location not specified</string>
<string name="compass_default_0">0</string>
<string name="unknown">Unknown</string>
<string name="rateapp">Rate App</string>

<string name="sound_meter">Sound Meter</string>
<string name="sound_meter_desc">To measure the loudness in the environment in decibel(dB)</string>
<string name="sound_meter_intro">Sound meter Introduction</string>
Expand All @@ -1048,12 +1044,12 @@
<string name="avg_dB">Avg (dB)</string>
<string name="sound_chart_label">Sound Level(dB)</string>
<string name="in_progress">This feature is in progress</string>

<string name="developers">Developers</string>
<string name="github_developers_link">https://github.com/fossasia/pslab-android/graphs/contributors</string>
<string name="limit_dangerous">\"Dangerous\"</string>
<string name="limit_average">Average</string>

<string name="no_log_data_found">No Logged Data Found</string>
<string name="appwidget_text">EXAMPLE</string>
<string name="add_widget">Add widget</string>

</resources>
10 changes: 10 additions & 0 deletions app/src/main/res/xml/logged_data_widget_info.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialKeyguardLayout="@layout/logged_data_widget"
android:initialLayout="@layout/logged_data_widget"
android:minWidth="250dp"
android:minHeight="110dp"
android:previewImage="@drawable/example_appwidget_preview"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="86400000"
android:widgetCategory="home_screen"></appwidget-provider>

0 comments on commit 1126381

Please sign in to comment.