Skip to content

Commit c92676c

Browse files
committed
wip
Signed-off-by: Andrey Parfenov <a1994ndrey@gmail.com>
1 parent 582f1d2 commit c92676c

19 files changed

Lines changed: 153 additions & 130 deletions

app/src/main/AndroidManifest.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.example.brainflowplot">
44

5+
# for devices which use network
6+
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
7+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
8+
# to use read/write file from/to SD card
9+
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
10+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
11+
512
<application
613
android:allowBackup="true"
714
android:icon="@mipmap/ic_launcher"
Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package com.example.brainflowplot;
22

3+
import android.content.Context;
4+
import android.content.Intent;
5+
import android.content.SharedPreferences;
36
import android.os.Bundle;
7+
import android.util.Log;
8+
import android.view.View;
9+
import android.widget.Toast;
410

511
import com.google.android.material.bottomnavigation.BottomNavigationView;
612

@@ -9,22 +15,86 @@
915
import androidx.navigation.Navigation;
1016
import androidx.navigation.ui.AppBarConfiguration;
1117
import androidx.navigation.ui.NavigationUI;
18+
import androidx.preference.PreferenceManager;
19+
20+
import brainflow.BoardShim;
21+
import brainflow.BrainFlowError;
22+
import brainflow.BrainFlowInputParams;
1223

1324
public class DataActivity extends AppCompatActivity {
1425

26+
public BoardShim boardShim = null;
27+
public int samplingRate = 0;
28+
public int[] channels = null;
29+
30+
private boolean isTryingToConnect = false;
31+
1532
@Override
1633
protected void onCreate(Bundle savedInstanceState) {
1734
super.onCreate(savedInstanceState);
1835
setContentView(R.layout.activity_data);
1936
BottomNavigationView navView = findViewById(R.id.nav_view);
2037
// Passing each menu ID as a set of Ids because each
2138
// menu should be considered as top level destinations.
39+
40+
// comment out these two methods for theme wo actionbar
41+
/*
2242
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
23-
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
43+
44+
R.id.navigation_dataplot, R.id.navigation_psdplot, R.id.navigation_bandpowerplot)
2445
.build();
46+
*/
2547
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
26-
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
48+
// NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
2749
NavigationUI.setupWithNavController(navView, navController);
50+
51+
// read settings
52+
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
53+
int boardId = Integer.valueOf(prefs.getString(getString(R.string.board_id_key), "-1"));
54+
String ipAddr = prefs.getString(getString(R.string.ip_address_key), "");
55+
int ipPort = 0;
56+
try {
57+
ipPort = Integer.valueOf(prefs.getString(getString(R.string.ip_port_key), "0"));
58+
} catch (Exception e) {
59+
// do nothing
60+
}
61+
String dataType = prefs.getString(getString(R.string.data_type_key), "");
62+
63+
boolean connected = false;
64+
try {
65+
BrainFlowInputParams params = new BrainFlowInputParams();
66+
params.ip_address = ipAddr;
67+
params.ip_port = ipPort;
68+
boardShim = new BoardShim(boardId, params);
69+
boardShim.prepare_session();
70+
boardShim.start_stream();
71+
connected = true;
72+
} catch (Exception e) {
73+
Context context = getApplicationContext();
74+
CharSequence text = "Error occurred, validate provided parameters and your board";
75+
int duration = Toast.LENGTH_LONG;
76+
Toast toast = Toast.makeText(context, text, duration);
77+
toast.show();
78+
Log.e(getString(R.string.log_tag), e.getMessage());
79+
connected = false;
80+
}
81+
if (!connected) {
82+
// if failed to connect back to settings page
83+
Intent myIntent = new Intent(this, SettingsActivity.class);
84+
startActivity(myIntent);
85+
}
86+
}
87+
88+
@Override
89+
protected void onDestroy() {
90+
try {
91+
if (boardShim != null) {
92+
boardShim.release_session();
93+
}
94+
} catch (BrainFlowError e) {
95+
Log.e(getString(R.string.log_tag), e.getMessage());
96+
}
97+
super.onDestroy();
2898
}
2999

30100
}

app/src/main/java/com/example/brainflowplot/SettingsActivity.java

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,15 @@
11
package com.example.brainflowplot;
22

3-
import android.content.Context;
43
import android.content.Intent;
5-
import android.content.SharedPreferences;
64
import android.os.Bundle;
7-
import android.util.Log;
85
import android.view.View;
9-
import android.widget.Toast;
106

117
import androidx.appcompat.app.ActionBar;
128
import androidx.appcompat.app.AppCompatActivity;
139
import androidx.preference.PreferenceFragmentCompat;
14-
import androidx.preference.PreferenceManager;
15-
16-
import brainflow.BoardShim;
17-
import brainflow.BrainFlowError;
18-
import brainflow.BrainFlowInputParams;
1910

2011
public class SettingsActivity extends AppCompatActivity {
2112

22-
public BoardShim boardShim = null;
23-
public int samplingRate = 0;
24-
public int[] channels = null;
25-
26-
private boolean isTryingToConnect = false;
27-
2813
@Override
2914
protected void onCreate(Bundle savedInstanceState) {
3015
super.onCreate(savedInstanceState);
@@ -43,60 +28,6 @@ protected void onCreate(Bundle savedInstanceState) {
4328
public void prepareSession(View view) {
4429
Intent myIntent = new Intent(this, DataActivity.class);
4530
startActivity(myIntent);
46-
/*
47-
// don't run it twice
48-
if (isTryingToConnect) {
49-
return;
50-
}
51-
isTryingToConnect = true;
52-
53-
Context context = getApplicationContext();
54-
CharSequence text = "Trying to establish connection";
55-
int duration = Toast.LENGTH_SHORT;
56-
Toast toast = Toast.makeText(context, text, duration);
57-
toast.show();
58-
59-
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
60-
int boardId = Integer.valueOf(prefs.getString(getString(R.string.board_id_key), "-1"));
61-
String ipAddr = prefs.getString(getString(R.string.ip_address_key), "");
62-
int ipPort = 0;
63-
try {
64-
ipPort = Integer.valueOf(prefs.getString(getString(R.string.ip_port_key), "0"));
65-
} catch (Exception e) {
66-
// do nothing
67-
}
68-
String dataType = prefs.getString(getString(R.string.data_type_key), "");
69-
70-
try {
71-
BrainFlowInputParams params = new BrainFlowInputParams();
72-
params.ip_address = ipAddr;
73-
params.ip_port = ipPort;
74-
boardShim = new BoardShim(boardId, params);
75-
boardShim.prepare_session();
76-
boardShim.start_stream();
77-
} catch (Exception e) {
78-
text = "Error occurred, validate provided parameters and your board";
79-
duration = Toast.LENGTH_LONG;
80-
toast = Toast.makeText(context, text, duration);
81-
toast.show();
82-
Log.e(getString(R.string.log_tag), e.getMessage());
83-
}
84-
85-
isTryingToConnect = false;
86-
87-
*/
88-
}
89-
90-
@Override
91-
protected void onDestroy() {
92-
try {
93-
if (boardShim != null) {
94-
boardShim.release_session();
95-
}
96-
} catch (BrainFlowError e) {
97-
Log.e(getString(R.string.log_tag), e.getMessage());
98-
}
99-
super.onDestroy();
10031
}
10132

10233
public static class SettingsFragment extends PreferenceFragmentCompat {

app/src/main/java/com/example/brainflowplot/ui/notifications/NotificationsFragment.java renamed to app/src/main/java/com/example/brainflowplot/ui/bandpowerplot/BandPowerPlotFragment.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.example.brainflowplot.ui.notifications;
1+
package com.example.brainflowplot.ui.bandpowerplot;
22

33
import android.os.Bundle;
44
import android.view.LayoutInflater;
@@ -14,17 +14,17 @@
1414

1515
import com.example.brainflowplot.R;
1616

17-
public class NotificationsFragment extends Fragment {
17+
public class BandPowerPlotFragment extends Fragment {
1818

19-
private NotificationsViewModel notificationsViewModel;
19+
private BandPowerPlotViewModel bandPowerPlotViewModel;
2020

2121
public View onCreateView(@NonNull LayoutInflater inflater,
2222
ViewGroup container, Bundle savedInstanceState) {
23-
notificationsViewModel =
24-
ViewModelProviders.of(this).get(NotificationsViewModel.class);
25-
View root = inflater.inflate(R.layout.fragment_notifications, container, false);
23+
bandPowerPlotViewModel =
24+
ViewModelProviders.of(this).get(BandPowerPlotViewModel.class);
25+
View root = inflater.inflate(R.layout.fragment_bandpowerplot, container, false);
2626
final TextView textView = root.findViewById(R.id.text_notifications);
27-
notificationsViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
27+
bandPowerPlotViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
2828
@Override
2929
public void onChanged(@Nullable String s) {
3030
textView.setText(s);

app/src/main/java/com/example/brainflowplot/ui/notifications/NotificationsViewModel.java renamed to app/src/main/java/com/example/brainflowplot/ui/bandpowerplot/BandPowerPlotViewModel.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
package com.example.brainflowplot.ui.notifications;
1+
package com.example.brainflowplot.ui.bandpowerplot;
22

33
import androidx.lifecycle.LiveData;
44
import androidx.lifecycle.MutableLiveData;
55
import androidx.lifecycle.ViewModel;
66

7-
public class NotificationsViewModel extends ViewModel {
7+
public class BandPowerPlotViewModel extends ViewModel {
88

99
private MutableLiveData<String> mText;
1010

11-
public NotificationsViewModel() {
11+
public BandPowerPlotViewModel() {
1212
mText = new MutableLiveData<>();
1313
mText.setValue("This is notifications fragment");
1414
}

app/src/main/java/com/example/brainflowplot/ui/home/HomeFragment.java renamed to app/src/main/java/com/example/brainflowplot/ui/dataplot/DataPlotFragment.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.example.brainflowplot.ui.home;
1+
package com.example.brainflowplot.ui.dataplot;
22

33
import android.os.Bundle;
44
import android.view.LayoutInflater;
@@ -14,17 +14,17 @@
1414

1515
import com.example.brainflowplot.R;
1616

17-
public class HomeFragment extends Fragment {
17+
public class DataPlotFragment extends Fragment {
1818

19-
private HomeViewModel homeViewModel;
19+
private DataPlotViewModel dataPlotViewModel;
2020

2121
public View onCreateView(@NonNull LayoutInflater inflater,
2222
ViewGroup container, Bundle savedInstanceState) {
23-
homeViewModel =
24-
ViewModelProviders.of(this).get(HomeViewModel.class);
25-
View root = inflater.inflate(R.layout.fragment_home, container, false);
23+
dataPlotViewModel =
24+
ViewModelProviders.of(this).get(DataPlotViewModel.class);
25+
View root = inflater.inflate(R.layout.fragment_dataplot, container, false);
2626
final TextView textView = root.findViewById(R.id.text_home);
27-
homeViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
27+
dataPlotViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
2828
@Override
2929
public void onChanged(@Nullable String s) {
3030
textView.setText(s);

app/src/main/java/com/example/brainflowplot/ui/home/HomeViewModel.java renamed to app/src/main/java/com/example/brainflowplot/ui/dataplot/DataPlotViewModel.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
package com.example.brainflowplot.ui.home;
1+
package com.example.brainflowplot.ui.dataplot;
22

33
import androidx.lifecycle.LiveData;
44
import androidx.lifecycle.MutableLiveData;
55
import androidx.lifecycle.ViewModel;
66

7-
public class HomeViewModel extends ViewModel {
7+
public class DataPlotViewModel extends ViewModel {
88

99
private MutableLiveData<String> mText;
1010

11-
public HomeViewModel() {
11+
public DataPlotViewModel() {
1212
mText = new MutableLiveData<>();
1313
mText.setValue("This is home fragment");
1414
}

app/src/main/java/com/example/brainflowplot/ui/dashboard/DashboardFragment.java renamed to app/src/main/java/com/example/brainflowplot/ui/psdplot/PSDPlotFragment.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.example.brainflowplot.ui.dashboard;
1+
package com.example.brainflowplot.ui.psdplot;
22

33
import android.os.Bundle;
44
import android.view.LayoutInflater;
@@ -14,17 +14,17 @@
1414

1515
import com.example.brainflowplot.R;
1616

17-
public class DashboardFragment extends Fragment {
17+
public class PSDPlotFragment extends Fragment {
1818

19-
private DashboardViewModel dashboardViewModel;
19+
private PSDPlotViewModel PSDPlotViewModel;
2020

2121
public View onCreateView(@NonNull LayoutInflater inflater,
2222
ViewGroup container, Bundle savedInstanceState) {
23-
dashboardViewModel =
24-
ViewModelProviders.of(this).get(DashboardViewModel.class);
25-
View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
23+
PSDPlotViewModel =
24+
ViewModelProviders.of(this).get(PSDPlotViewModel.class);
25+
View root = inflater.inflate(R.layout.fragment_psdplot, container, false);
2626
final TextView textView = root.findViewById(R.id.text_dashboard);
27-
dashboardViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
27+
PSDPlotViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
2828
@Override
2929
public void onChanged(@Nullable String s) {
3030
textView.setText(s);

app/src/main/java/com/example/brainflowplot/ui/dashboard/DashboardViewModel.java renamed to app/src/main/java/com/example/brainflowplot/ui/psdplot/PSDPlotViewModel.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
package com.example.brainflowplot.ui.dashboard;
1+
package com.example.brainflowplot.ui.psdplot;
22

33
import androidx.lifecycle.LiveData;
44
import androidx.lifecycle.MutableLiveData;
55
import androidx.lifecycle.ViewModel;
66

7-
public class DashboardViewModel extends ViewModel {
7+
public class PSDPlotViewModel extends ViewModel {
88

99
private MutableLiveData<String> mText;
1010

11-
public DashboardViewModel() {
11+
public PSDPlotViewModel() {
1212
mText = new MutableLiveData<>();
1313
mText.setValue("This is dashboard fragment");
1414
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<vector android:alpha="0.8" android:height="24dp"
2+
android:tint="?attr/colorControlNormal" android:viewportHeight="24"
3+
android:viewportWidth="24" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
4+
<path android:fillColor="@android:color/white" android:pathData="M5,9.2h3L8,19L5,19zM10.6,5h2.8v14h-2.8zM16.2,13L19,13v6h-2.8z"/>
5+
</vector>

0 commit comments

Comments
 (0)