Skip to content

Commit

Permalink
Merge pull request #1906 from fossasia/development
Browse files Browse the repository at this point in the history
Merge development into master
  • Loading branch information
CloudyPadmal authored Aug 1, 2019
2 parents 5e34f9f + 2b9282f commit 21b6906
Show file tree
Hide file tree
Showing 9 changed files with 407 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
android:theme="@style/AppTheme"
tools:replace="android:icon">
<uses-library android:name="org.apache.http.legacy" android:required="false"/>
<activity android:name=".activity.CreateConfigActivity"/>
<activity
android:name=".activity.ThermometerActivity"
android:screenOrientation="portrait"
Expand Down
185 changes: 185 additions & 0 deletions app/src/main/java/io/pslab/activity/CreateConfigActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
package io.pslab.activity;

import android.os.Bundle;
import android.os.Environment;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.SparseBooleanArray;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import io.pslab.R;
import io.pslab.others.CSVLogger;
import io.pslab.others.CustomSnackBar;

public class CreateConfigActivity extends AppCompatActivity {

private ArrayList<String> instrumentsList;
private ArrayList<String[]> instrumentParamsList;
private ArrayList<String[]> instrumentParamsListTitles;
private ListView paramsListView;
private int selectedItem = 0;
private String intervalUnit = "sec";
private EditText intervalEditText;
private String interval;
private View rootView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_config);
Toolbar toolbar = findViewById(R.id.toolbar);
rootView = findViewById(R.id.create_config_root_view);
paramsListView = findViewById(R.id.params_list);
Spinner selectInstrumentSpinner = findViewById(R.id.select_instrument_spinner);
Spinner intervalUnitSpinner = findViewById(R.id.interval_unit_spinner);
intervalEditText = findViewById(R.id.interval_edit_text);
Button createConfigFileBtn = findViewById(R.id.create_config_btn);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(R.string.nav_config);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
instrumentsList = new ArrayList<>();
instrumentParamsList = new ArrayList<>();
instrumentParamsListTitles = new ArrayList<>();
createArrayLists();
selectInstrumentSpinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, instrumentsList));
selectInstrumentSpinner.setSelection(0, true);
createCheckboxList();
selectInstrumentSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
selectedItem = position;
createCheckboxList();
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
selectedItem = 0;
}
});

intervalUnitSpinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.interval_units)));
intervalUnitSpinner.setSelection(0, true);
intervalUnitSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
intervalUnit = (String) parent.getItemAtPosition(position);
}

@Override
public void onNothingSelected(AdapterView<?> parent) {
intervalUnit = "sec";
}
});

createConfigFileBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
interval = intervalEditText.getText().toString();
if (interval.length() == 0) {
Toast.makeText(CreateConfigActivity.this, getResources().getString(R.string.no_interval_message), Toast.LENGTH_SHORT).show();
} else {
SparseBooleanArray selectedParams = paramsListView.getCheckedItemPositions();
ArrayList<String> selectedParamsList = new ArrayList<>();
for (int i = 0; i < paramsListView.getCount(); i++) {
if (selectedParams.get(i)) {
selectedParamsList.add(instrumentParamsList.get(selectedItem)[i]);
}
}
createConfigFile(selectedParamsList);
}
}
});
}

private void createArrayLists() {

instrumentParamsList.add(getResources().getStringArray(R.array.oscilloscope_params));
instrumentParamsList.add(getResources().getStringArray(R.array.multimeter_params));
instrumentParamsList.add(getResources().getStringArray(R.array.logic_analyzer_params));
instrumentParamsList.add(getResources().getStringArray(R.array.barometer_params));
instrumentParamsList.add(getResources().getStringArray(R.array.luxmeter_params));
instrumentParamsList.add(getResources().getStringArray(R.array.accelerometer_params));

instrumentParamsListTitles.add(getResources().getStringArray(R.array.oscilloscope_params_title));
instrumentParamsListTitles.add(getResources().getStringArray(R.array.multimeter_params_title));
instrumentParamsListTitles.add(getResources().getStringArray(R.array.logic_analyzer_params_title));
instrumentParamsListTitles.add(getResources().getStringArray(R.array.barometer_params));
instrumentParamsListTitles.add(getResources().getStringArray(R.array.luxmeter_params));
instrumentParamsListTitles.add(getResources().getStringArray(R.array.accelerometer_params_title));

instrumentsList.add(getResources().getString(R.string.oscilloscope));
instrumentsList.add(getResources().getString(R.string.multimeter));
instrumentsList.add(getResources().getString(R.string.logical_analyzer));
instrumentsList.add(getResources().getString(R.string.baro_meter));
instrumentsList.add(getResources().getString(R.string.lux_meter));
instrumentsList.add(getResources().getString(R.string.accelerometer));
}

private void createCheckboxList() {
paramsListView.setAdapter(new ArrayAdapter<String>(CreateConfigActivity.this, android.R.layout.simple_list_item_multiple_choice, instrumentParamsListTitles.get(selectedItem)));
paramsListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
}
return super.onOptionsItemSelected(item);
}

private void createConfigFile(ArrayList<String> params) {
String instrumentName = instrumentsList.get(selectedItem);
String fileName = "pslab_config.txt";
String basepath = Environment.getExternalStorageDirectory().getAbsolutePath();

File baseDirectory = new File(basepath + File.separator + CSVLogger.CSV_DIRECTORY);
if (!baseDirectory.exists()) {
try {
baseDirectory.mkdir();
} catch (Exception e) {
e.printStackTrace();
}
}

File configFile = new File(basepath + File.separator + CSVLogger.CSV_DIRECTORY + File.separator + fileName);
if (!configFile.exists()) {
try {
configFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
FileWriter writer = new FileWriter(configFile);
writer.write("instrument: " + instrumentName + "\n");
writer.write("interval: " + interval + " " + intervalUnit + "\n");
String param = String.join(",", params);
writer.write("params: " + param);
writer.flush();
writer.close();
CustomSnackBar.showSnackBar(rootView, getString(R.string.file_created_success_message), null, null, Snackbar.LENGTH_SHORT);
} catch (IOException e) {
e.printStackTrace();
CustomSnackBar.showSnackBar(rootView, getString(R.string.file_created_fail_message), null, null, Snackbar.LENGTH_SHORT);
}

}
}
6 changes: 6 additions & 0 deletions app/src/main/java/io/pslab/activity/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,12 @@ public boolean onNavigationItemSelected(@NonNull MenuItem item) {
shareIntent.putExtra(Intent.EXTRA_TEXT, shareMessage);
startActivity(shareIntent);
return true;
case R.id.nav_generate_config:
if (drawer != null) {
drawer.closeDrawers();
}
startActivity(new Intent(MainActivity.this, CreateConfigActivity.class));
break;
default:
navItemIndex = 0;
}
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/create_config_icon.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M20,6h-8l-2,-2L4,4c-1.11,0 -1.99,0.89 -1.99,2L2,18c0,1.11 0.89,2 2,2h16c1.11,0 2,-0.89 2,-2L22,8c0,-1.11 -0.89,-2 -2,-2zM19,14h-3v3h-2v-3h-3v-2h3L14,9h2v3h3v2z"/>
</vector>
103 changes: 103 additions & 0 deletions app/src/main/res/layout/activity_create_config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/create_config_root_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.CreateConfigActivity">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/top_app_bar_layout"
android:padding="@dimen/home_fragment_padding">

<TextView
android:id="@+id/select_instrument_title"
style="@style/Base.TextAppearance.AppCompat.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/create_config_margin4"
android:text="@string/select_instrument_title" />

<android.support.v7.widget.AppCompatSpinner
android:id="@+id/select_instrument_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/select_instrument_title" />

<TextView
android:id="@+id/time_interval_title"
style="@style/Base.TextAppearance.AppCompat.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/select_instrument_title"
android:layout_marginTop="@dimen/create_config_margin3"
android:text="@string/time_interval_title" />

<EditText
android:id="@+id/interval_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/select_instrument_title"
android:layout_marginStart="@dimen/margin_btn"
android:layout_marginTop="@dimen/create_config_margin1"
android:layout_toEndOf="@id/time_interval_title"
android:hint="@string/time_interval_hint"
android:inputType="number" />

<android.support.v7.widget.AppCompatSpinner
android:id="@+id/interval_unit_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/select_instrument_title"
android:layout_marginStart="@dimen/margin_btn"
android:layout_marginTop="@dimen/create_config_margin1"
android:layout_toEndOf="@+id/interval_edit_text" />

<TextView
android:id="@+id/select_params_title"
style="@style/Base.TextAppearance.AppCompat.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/time_interval_title"
android:layout_centerHorizontal="true"
android:layout_marginTop="@dimen/create_config_margin2"
android:text="@string/select_params_title" />

<ListView
android:id="@+id/params_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/create_config_btn"
android:layout_below="@+id/select_params_title"
android:layout_margin="@dimen/create_config_margin1" />

<Button
android:id="@+id/create_config_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@color/colorPrimary"
android:padding="@dimen/margin_btn"
android:text="@string/create_config_btn_text"
android:textColor="@color/white" />
</RelativeLayout>

<android.support.design.widget.AppBarLayout
android:id="@+id/top_app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>
</RelativeLayout>
4 changes: 4 additions & 0 deletions app/src/main/res/menu/activity_main_drawer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
android:id="@+id/sensor_data_logger"
android:icon="@drawable/ic_wifi_tethering_black_24dp"
android:title="@string/logged_data" />
<item
android:id="@+id/nav_generate_config"
android:icon="@drawable/create_config_icon"
android:title="@string/nav_config" />
<item
android:id="@+id/nav_settings"
android:icon="@drawable/ic_settings_black_24dp"
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,10 @@
<dimen name="seekarc_padding">30dp</dimen>
<dimen name="seekarc_edit_text">36sp</dimen>

<dimen name="create_config_margin1">10dp</dimen>
<dimen name="create_config_margin2">20dp</dimen>
<dimen name="create_config_margin3">22dp</dimen>
<dimen name="create_config_margin4">12dp</dimen>
<dimen name="sensor_sht21_margin">5dp</dimen>
<dimen name="sht21_height">40dp</dimen>
<dimen name="card_sht21_height">180dp</dimen>
Expand Down
Loading

0 comments on commit 21b6906

Please sign in to comment.