Skip to content

Commit

Permalink
Merge pull request #109 from kike-canaries/pax_counter_mini
Browse files Browse the repository at this point in the history
Pax counter mini
  • Loading branch information
hpsaturn authored Sep 9, 2021
2 parents 9efb2c4 + c4a9801 commit f920a0a
Show file tree
Hide file tree
Showing 11 changed files with 149 additions and 25 deletions.
18 changes: 17 additions & 1 deletion app/src/main/java/hpsaturn/pollutionreporter/BaseActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,18 @@ public boolean isFragmentInStack(String tag) {
}
}

public Fragment getFragmentInStack(String tag) {
try {
if (getSupportFragmentManager().getBackStackEntryCount() == 0) return null;
FragmentManager fm = getSupportFragmentManager();
Fragment match = fm.findFragmentByTag(tag);
if (match != null) return match;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

/***********************************************
* MENU OPTIONS
***********************************************/
Expand All @@ -221,6 +233,10 @@ public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) {

case R.id.action_var_filter:
actionVarFilter();
break;

case R.id.action_unpair:
actionUnPair();
break;
Expand All @@ -247,7 +263,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
}

abstract void actionUnPair();

abstract void actionVarFilter();

/**
* A native method that is implemented by the 'native-lib' native library,
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/hpsaturn/pollutionreporter/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package hpsaturn.pollutionreporter;

import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import androidx.annotation.NonNull;
Expand Down Expand Up @@ -52,6 +55,7 @@
import hpsaturn.pollutionreporter.view.ScanFragment;
import hpsaturn.pollutionreporter.view.SettingsFixedStation;
import hpsaturn.pollutionreporter.view.SettingsFragment;
import hpsaturn.pollutionreporter.view.VariableFileterFragment;

/**
* Created by Antonio Vanegas @hpsaturn on 6/11/18.
Expand Down Expand Up @@ -518,6 +522,11 @@ void actionUnPair() {
}
}

@Override
void actionVarFilter() {
showDialogFragment(new VariableFileterFragment(),VariableFileterFragment.TAG);
}

@Override
protected void onDestroy() {
stopRecordTrackService();
Expand Down Expand Up @@ -576,6 +585,8 @@ public boolean onPreferenceStartFragment(PreferenceFragmentCompat caller, Prefer

public void selectedVarsUpdated() {
if (chartFragment!=null)chartFragment.loadSelectedVariables();
ChartFragment infoFragment = (ChartFragment) getFragmentInStack(ChartFragment.TAG_INFO);
if (infoFragment!=null)infoFragment.loadSelectedVariables();
}

public void showTrackInfoFragment(String trackId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public class SensorData {

public float CO2H;

public int PAX;

public int P10;

public int P1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.graphics.Paint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
Expand All @@ -17,8 +18,12 @@
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.listener.ChartTouchListener;
import com.github.mikephil.charting.listener.OnChartGestureListener;
import com.github.mikephil.charting.utils.MPPointD;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
Expand Down Expand Up @@ -236,7 +241,6 @@ public void loadSelectedVariables(){
requireActivity().runOnUiThread(this::loadData);
}


private void calculateReferenceTime(){
ArrayList<SensorData> data = Storage.getSensorData(getActivity());
if (data.isEmpty()) {
Expand All @@ -248,8 +252,6 @@ private void calculateReferenceTime(){
XAxis xAxis = chart.getXAxis();
xAxis.setValueFormatter(xAxisFormatter);
}


/**
* Add data from previous data (recorded track for example)
* @param data
Expand Down Expand Up @@ -281,13 +283,14 @@ private void addValue(long time, SensorData data) {
while (it.hasNext()){
ChartVar var = it.next();
var.addValue(time,data);
boolean loadMap = recordId != null && (var.type.equals("P25") || var.type.equals("CO2"));
boolean loadMap = recordId != null &&
(var.type.equals("P25") || var.type.equals("CO2") || var.type.equals("PAX"));
if(loadMap)addMapSegment(var,data);
}

}

private void refreshDataSets() {

dataSets.clear();

Iterator<ChartVar> it = variables.iterator();
Expand Down
40 changes: 24 additions & 16 deletions app/src/main/java/hpsaturn/pollutionreporter/view/ChartVar.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public class ChartVar {

public String type;

public List<Entry> entries = new ArrayList<Entry>();
public List<Entry> entries = new ArrayList<>();

public LineDataSet dataSet;

public ArrayList<Integer> colors = new ArrayList<Integer>();
public ArrayList<Integer> colors = new ArrayList<>();

public ChartVar(Context ctx, String type,String label) {

Expand All @@ -39,13 +39,16 @@ public ChartVar(Context ctx, String type,String label) {
switch (type) {

case "P25":
dataSet = getMainLineDataSet(entries, R.color.grey, label,1.5F);
dataSet = getMainLineDataSet(entries, R.color.grey, label,1.5F,5.0F);
break;

case "CO2":
dataSet = getMainLineDataSet(entries, R.color.black, label,1.5F);
dataSet = getMainLineDataSet(entries, R.color.black, label,1.5F,3.0F);
break;

case "PAX":
dataSet = getMainLineDataSet(entries, R.color.light_red, label,2.0F,3.0F);
break;

case "P1":
dataSet = getGenericLineDataSet(entries, R.color.brown, label,1F);
Expand All @@ -56,7 +59,7 @@ public ChartVar(Context ctx, String type,String label) {
break;

case "P10":
dataSet = getGenericLineDataSet(entries, R.color.colorAccentWeb, label,1F);
dataSet = getGenericLineDataSet(entries, R.color.colorAccentWeb, label,2F);
break;

case "CO2T":
Expand All @@ -72,7 +75,7 @@ public ChartVar(Context ctx, String type,String label) {
break;

case "hum":
dataSet = getGenericLineDataSet(entries, R.color.blue, label,1F);
dataSet = getGenericLineDataSet(entries, R.color.blue, label,2F);
break;

case "alt":
Expand All @@ -91,13 +94,13 @@ public ChartVar(Context ctx, String type,String label) {

}

private LineDataSet getMainLineDataSet(List<Entry> entry, int color, String label,float width) {
private LineDataSet getMainLineDataSet(List<Entry> entry, int color, String label, float width, float radious) {

LineDataSet dataSet = new LineDataSet(entry,label);
dataSet.setColor(ctx.getResources().getColor(color));
dataSet.setDrawValues(false);
dataSet.setHighlightEnabled(true);
dataSet.setCircleRadius(4.0f);
dataSet.setCircleRadius(radious);
dataSet.setAxisDependency(YAxis.AxisDependency.RIGHT);
dataSet.setLineWidth(width);

Expand Down Expand Up @@ -131,29 +134,34 @@ public void addValue(float time,SensorData data){
if (data.P25 <= 13) colors.add(ctx.getResources().getColor(R.color.green));
else if (data.P25 <= 35) colors.add(ctx.getResources().getColor(R.color.yellow_dark));
else if (data.P25 <= 55) colors.add(ctx.getResources().getColor(R.color.orange));
else if (data.P25 <= 150)colors.add(ctx.getResources().getColor(R.color.red));
else if (data.P25 <= 250)colors.add(ctx.getResources().getColor(R.color.purple));
else if (data.P25 <= 150) colors.add(ctx.getResources().getColor(R.color.red));
else if (data.P25 <= 250) colors.add(ctx.getResources().getColor(R.color.purple));
else colors.add(ctx.getResources().getColor(R.color.brown));
break;
case "PAX":
if (data.PAX <= 20) colors.add(ctx.getResources().getColor(R.color.green));
else if (data.PAX <= 50) colors.add(ctx.getResources().getColor(R.color.yellow_dark));
else if (data.PAX <= 100) colors.add(ctx.getResources().getColor(R.color.orange));
else if (data.PAX <= 400) colors.add(ctx.getResources().getColor(R.color.red));
else if (data.PAX <= 800) colors.add(ctx.getResources().getColor(R.color.purple));
else colors.add(ctx.getResources().getColor(R.color.brown));
break;
}

try {
float value = data.getClass().getField(type).getFloat(data);
// Logger.i(TAG,"--> "+type+ ":"+value);
dataSet.addEntry(new Entry(time,value));

dataSet.addEntry(new Entry(time, value));
} catch (IllegalAccessException | NoSuchFieldException e) {
e.printStackTrace();
}



}

public void refresh(){
switch (type){
case "CO2":

case "P25":
case "PAX":
dataSet.setCircleColors(colors);
break;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void onItemClick(AdapterView<?> adapterView, View view, int position, lon
String recordId = mRecordsAdapter.getItem(position).name;
Logger.i(TAG, "showing record: "+recordId);
chart = ChartFragment.newInstance(recordId);
getMain().addFragmentPopup(chart,ChartFragment.TAG);
getMain().addFragmentPopup(chart,ChartFragment.TAG_INFO);
showingData=true;
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package hpsaturn.pollutionreporter.view;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;

import androidx.fragment.app.DialogFragment;
import androidx.preference.PreferenceManager;

import com.hpsaturn.tools.Logger;

import java.util.Set;

import hpsaturn.pollutionreporter.MainActivity;
import hpsaturn.pollutionreporter.R;

/**
* Created by Antonio Vanegas @hpsaturn on 5/22/21.
*/
public class VariableFileterFragment extends DialogFragment {

public static final String TAG = VariableFileterFragment.class.getSimpleName();


@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {


AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getMain());
Set<String> values = preferences.getStringSet(getString(R.string.key_setting_vars), null);

String[] options = getResources().getStringArray(R.array.pref_vars_values);

boolean[] selected = new boolean[options.length];

for (int i = 0 ; i < options.length ; i++) {
selected[i] = values.contains(options[i]);
}

builder.setMultiChoiceItems(R.array.pref_vars_entries, selected, this::setVaribleFilter);

return builder.create();
}

private void setVaribleFilter(DialogInterface dialog, int item, boolean isChecked) {
Logger.i(TAG, "item: " + item + " isChecked: "+isChecked);

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getMain());
Set<String> values = preferences.getStringSet(getString(R.string.key_setting_vars), null);

String[] options = getResources().getStringArray(R.array.pref_vars_values);

if(isChecked) values.add(options[item]);
else values.remove(options[item]);

SharedPreferences.Editor editor = preferences.edit();
editor.putStringSet(getString(R.string.key_setting_vars),values);
editor.apply();
}

@Override
public void onDestroy() {
Logger.d(TAG,"onDestroy");
getMain().selectedVarsUpdated();
super.onDestroy();
}

private MainActivity getMain() {
return ((MainActivity) getActivity());
}

}
5 changes: 5 additions & 0 deletions app/src/main/res/menu/menu_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
xmlns:tools="http://schemas.android.com/tools"
tools:context="hpsaturn.pollutionreporter.MainActivity">

<item
android:id="@+id/action_var_filter"
android:title="@string/menu_var_filter"
app:showAsAction="never" />

<item
android:id="@+id/action_unpair"
android:title="@string/menu_unpair"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<item>CO2</item>
<item>CO2T</item>
<item>CO2H</item>
<item>PAX</item>
<item>Temp</item>
<item>Humi</item>
<item>Alt</item>
Expand All @@ -53,6 +54,7 @@
<item>CO2</item>
<item>CO2T</item>
<item>CO2H</item>
<item>PAX</item>
<item>tmp</item>
<item>hum</item>
<item>alt</item>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,5 @@
<string name="key_setting_altitude_offset">key_setting_altitude_offset</string>
<string name="summary_altitude_offset">Set Altitude to enable. 0 disable.</string>
<string name="title_altitude_offset">CO2 Altitude compensation</string>
<string name="menu_var_filter">Variables…</string>
</resources>
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
mCompileSdkVersion=30
mMinSdkVersion=19
mTargetSdkVersion=30
mVersionCode=578
mVersionName=0.6.9
mVersionCode=581
mVersionName=0.7.0
android.useAndroidX=true
android.enableJetifier=true

0 comments on commit f920a0a

Please sign in to comment.