-
Notifications
You must be signed in to change notification settings - Fork 802
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1906 from fossasia/development
Merge development into master
- Loading branch information
Showing
9 changed files
with
407 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
185 changes: 185 additions & 0 deletions
185
app/src/main/java/io/pslab/activity/CreateConfigActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.