|
1 | 1 | package io.pslab.activity;
|
2 | 2 |
|
| 3 | +import android.Manifest; |
| 4 | +import android.app.AlertDialog; |
| 5 | +import android.content.Context; |
| 6 | +import android.content.DialogInterface; |
| 7 | +import android.content.Intent; |
| 8 | +import android.content.SharedPreferences; |
3 | 9 | import android.content.pm.ActivityInfo;
|
| 10 | +import android.content.pm.PackageManager; |
| 11 | +import android.location.Location; |
| 12 | +import android.location.LocationManager; |
4 | 13 | import android.os.Bundle;
|
| 14 | +import android.os.Handler; |
| 15 | +import android.support.annotation.NonNull; |
| 16 | +import android.support.design.widget.BottomSheetBehavior; |
| 17 | +import android.support.design.widget.CoordinatorLayout; |
| 18 | +import android.support.v4.app.ActivityCompat; |
| 19 | +import android.support.v4.content.ContextCompat; |
5 | 20 | import android.support.v7.app.AppCompatActivity;
|
6 | 21 | import android.support.v7.widget.LinearLayoutManager;
|
7 | 22 | import android.support.v7.widget.RecyclerView;
|
| 23 | +import android.support.v7.widget.Toolbar; |
| 24 | +import android.view.GestureDetector; |
| 25 | +import android.view.Menu; |
| 26 | +import android.view.MenuInflater; |
| 27 | +import android.view.MenuItem; |
| 28 | +import android.view.View; |
| 29 | +import android.widget.ImageView; |
| 30 | +import android.widget.LinearLayout; |
| 31 | +import android.widget.TextView; |
8 | 32 |
|
| 33 | +import com.github.mikephil.charting.data.Entry; |
| 34 | + |
| 35 | +import java.util.ArrayList; |
| 36 | + |
| 37 | +import butterknife.BindView; |
| 38 | +import butterknife.ButterKnife; |
9 | 39 | import io.pslab.R;
|
10 | 40 | import io.pslab.adapters.AccelerometerAdapter;
|
| 41 | +import io.pslab.others.CSVLogger; |
| 42 | +import io.pslab.others.CustomSnackBar; |
| 43 | +import io.pslab.others.GPSLogger; |
| 44 | +import io.pslab.others.MathUtils; |
| 45 | +import io.pslab.others.SwipeGestureDetector; |
11 | 46 |
|
12 | 47 | public class AccelerometerActivity extends AppCompatActivity {
|
13 | 48 |
|
| 49 | + private static final String PREF_NAME = "AccelerometerPreferences"; |
| 50 | + |
| 51 | + private static final int MY_PERMISSIONS_REQUEST_STORAGE_FOR_DATA = 101; |
| 52 | + private static final int MY_PERMISSIONS_REQUEST_STORAGE_FOR_MAPS = 102; |
| 53 | + |
| 54 | + public boolean recordData = false; |
| 55 | + public boolean locationPref; |
| 56 | + private boolean checkGpsOnResume = false; |
| 57 | + private boolean isRecordingStarted = false; |
| 58 | + private boolean isDataRecorded = false; |
| 59 | + public GPSLogger gpsLogger; |
| 60 | + public CSVLogger accLogger; |
| 61 | + |
| 62 | + private Menu menu; |
| 63 | + AccelerometerAdapter adapter; |
| 64 | + |
| 65 | + BottomSheetBehavior bottomSheetBehavior; |
| 66 | + GestureDetector gestureDetector; |
| 67 | + |
| 68 | + @BindView(R.id.accel_toolbar) |
| 69 | + Toolbar mToolbar; |
| 70 | + @BindView(R.id.accel_coordinator_layout) |
| 71 | + CoordinatorLayout coordinatorLayout; |
| 72 | + @BindView(R.id.bottom_sheet) |
| 73 | + LinearLayout bottomSheet; |
| 74 | + @BindView(R.id.shadow) |
| 75 | + View tvShadow; |
| 76 | + @BindView(R.id.img_arrow) |
| 77 | + ImageView arrowUpDown; |
| 78 | + @BindView(R.id.sheet_slide_text) |
| 79 | + TextView bottomSheetSlideText; |
| 80 | + @BindView(R.id.guide_title) |
| 81 | + TextView bottomSheetGuideTitle; |
| 82 | + @BindView(R.id.custom_dialog_text) |
| 83 | + TextView bottomSheetText; |
| 84 | + @BindView(R.id.custom_dialog_schematic) |
| 85 | + ImageView bottomSheetSchematic; |
| 86 | + @BindView(R.id.custom_dialog_desc) |
| 87 | + TextView bottomSheetDesc; |
| 88 | + |
14 | 89 | @Override
|
15 | 90 | protected void onCreate(Bundle savedInstanceState) {
|
16 | 91 | super.onCreate(savedInstanceState);
|
17 |
| - setContentView(R.layout.activity_accelerometer); |
| 92 | + setContentView(R.layout.activity_accelerometer_main); |
| 93 | + ButterKnife.bind(this); |
18 | 94 | setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
|
19 |
| - AccelerometerAdapter adapter = new AccelerometerAdapter(new String[]{"X axis", "Y axis", "Z axis"}, getApplicationContext()); |
20 | 95 |
|
| 96 | + setUpBottomSheet(); |
| 97 | + setSupportActionBar(mToolbar); |
| 98 | + |
| 99 | + adapter = new AccelerometerAdapter(new String[]{"X axis", "Y axis", "Z axis"}, getApplicationContext()); |
21 | 100 | RecyclerView recyclerView = this.findViewById(R.id.accelerometer_recycler_view);
|
22 | 101 | LinearLayoutManager layoutManager = new LinearLayoutManager(this);
|
23 | 102 | recyclerView.setLayoutManager(layoutManager);
|
24 | 103 | recyclerView.setAdapter(adapter);
|
25 | 104 | }
|
| 105 | + |
| 106 | + @Override |
| 107 | + public boolean onCreateOptionsMenu(Menu menu) { |
| 108 | + MenuInflater inflater = getMenuInflater(); |
| 109 | + inflater.inflate(R.menu.data_log_menu, menu); |
| 110 | + this.menu = menu; |
| 111 | + return true; |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public boolean onOptionsItemSelected(MenuItem item) { |
| 116 | + switch (item.getItemId()) { |
| 117 | + case R.id.record_pause_data: |
| 118 | + if (ContextCompat.checkSelfPermission(this, |
| 119 | + Manifest.permission.WRITE_EXTERNAL_STORAGE) |
| 120 | + != PackageManager.PERMISSION_GRANTED) { |
| 121 | + ActivityCompat.requestPermissions(this, |
| 122 | + new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_STORAGE_FOR_DATA); |
| 123 | + return true; |
| 124 | + } |
| 125 | + if (recordData) { |
| 126 | + item.setIcon(R.drawable.record_icon); |
| 127 | + adapter.setRecordingStatus(false); |
| 128 | + recordData = false; |
| 129 | + CustomSnackBar.showSnackBar(coordinatorLayout, getString(R.string.data_recording_paused), null, null); |
| 130 | + } else { |
| 131 | + isDataRecorded = true; |
| 132 | + item.setIcon(R.drawable.pause_icon); |
| 133 | + adapter.setRecordingStatus(true); |
| 134 | + if (!isRecordingStarted) { |
| 135 | + accLogger = new CSVLogger(getString(R.string.accelerometer)); |
| 136 | + accLogger.writeCSVFile("Timestamp,X,Y,Z\n"); |
| 137 | + isRecordingStarted = true; |
| 138 | + recordData = true; |
| 139 | + } |
| 140 | + if (locationPref) { |
| 141 | + gpsLogger = new GPSLogger(this, (LocationManager) getSystemService(Context.LOCATION_SERVICE)); |
| 142 | + if (gpsLogger.isGPSEnabled()) { |
| 143 | + recordData = true; |
| 144 | + CustomSnackBar.showSnackBar(coordinatorLayout, getString(R.string.data_recording_start) + "\n" + getString(R.string.location_enabled), null, null); |
| 145 | + } else { |
| 146 | + checkGpsOnResume = true; |
| 147 | + } |
| 148 | + gpsLogger.startFetchingLocation(); |
| 149 | + } else { |
| 150 | + recordData = true; |
| 151 | + CustomSnackBar.showSnackBar(coordinatorLayout, getString(R.string.data_recording_start) + "\n" + getString(R.string.location_disabled), null, null); |
| 152 | + } |
| 153 | + } |
| 154 | + break; |
| 155 | + case R.id.record_csv_data: |
| 156 | + if (isDataRecorded) { |
| 157 | + MenuItem item1 = menu.findItem(R.id.record_pause_data); |
| 158 | + item1.setIcon(R.drawable.record_icon); |
| 159 | + |
| 160 | + // Export Data |
| 161 | + ArrayList<Entry> dataX = adapter.getEntries(0); |
| 162 | + ArrayList<Entry> dataY = adapter.getEntries(1); |
| 163 | + ArrayList<Entry> dataZ = adapter.getEntries(2); |
| 164 | + int length = Math.min(Math.min(dataX.size(), dataY.size()), dataZ.size()); |
| 165 | + for (int i = 0; i < length; i++) { |
| 166 | + accLogger.writeCSVFile(dataX.get(i).getX() + "," + dataX.get(i).getY() + "," |
| 167 | + + dataY.get(i).getY() + "," + dataZ.get(i).getY() + "\n"); |
| 168 | + } |
| 169 | + if (locationPref && gpsLogger != null) { |
| 170 | + String data; |
| 171 | + Location location = gpsLogger.getBestLocation(); |
| 172 | + if (location != null) { |
| 173 | + data = "\nLocation" + "," + String.valueOf(location.getLatitude()) + "," + String.valueOf(location.getLongitude() + "\n"); |
| 174 | + } else { |
| 175 | + data = "\nLocation" + "," + "null" + "," + "null"; |
| 176 | + } |
| 177 | + accLogger.writeCSVFile(data); |
| 178 | + gpsLogger.removeUpdate(); |
| 179 | + } |
| 180 | + CustomSnackBar.showSnackBar(coordinatorLayout, |
| 181 | + getString(R.string.csv_store_text) + " " + accLogger.getCurrentFilePath() |
| 182 | + , getString(R.string.delete_capital), new View.OnClickListener() { |
| 183 | + @Override |
| 184 | + public void onClick(View view) { |
| 185 | + new AlertDialog.Builder(AccelerometerActivity.this, R.style.AlertDialogStyle) |
| 186 | + .setTitle(R.string.delete_file) |
| 187 | + .setMessage(R.string.delete_warning) |
| 188 | + .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { |
| 189 | + @Override |
| 190 | + public void onClick(DialogInterface dialogInterface, int i) { |
| 191 | + accLogger.deleteFile(); |
| 192 | + } |
| 193 | + }) |
| 194 | + .setNegativeButton(R.string.cancel, null) |
| 195 | + .create() |
| 196 | + .show(); |
| 197 | + } |
| 198 | + }); |
| 199 | + adapter.setRecordingStatus(false); |
| 200 | + isRecordingStarted = false; |
| 201 | + recordData = false; |
| 202 | + } else { |
| 203 | + CustomSnackBar.showSnackBar(coordinatorLayout, getString(R.string.nothing_to_export), null, null); |
| 204 | + } |
| 205 | + break; |
| 206 | + case R.id.delete_csv_data: |
| 207 | + if (isDataRecorded) { |
| 208 | + MenuItem item1 = menu.findItem(R.id.record_pause_data); |
| 209 | + item1.setIcon(R.drawable.record_icon); |
| 210 | + adapter.setRecordingStatus(false); |
| 211 | + recordData = false; |
| 212 | + isRecordingStarted = false; |
| 213 | + isDataRecorded = false; |
| 214 | + accLogger.deleteFile(); |
| 215 | + CustomSnackBar.showSnackBar(coordinatorLayout, getString(R.string.data_deleted), null, null); |
| 216 | + } else |
| 217 | + CustomSnackBar.showSnackBar(coordinatorLayout, getString(R.string.nothing_to_delete), null, null); |
| 218 | + break; |
| 219 | + case R.id.show_map: |
| 220 | + if (ContextCompat.checkSelfPermission(this, |
| 221 | + Manifest.permission.WRITE_EXTERNAL_STORAGE) |
| 222 | + != PackageManager.PERMISSION_GRANTED) { |
| 223 | + ActivityCompat.requestPermissions(this, |
| 224 | + new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_STORAGE_FOR_MAPS); |
| 225 | + return true; |
| 226 | + } |
| 227 | + Intent MAP = new Intent(getApplicationContext(), MapsActivity.class); |
| 228 | + startActivity(MAP); |
| 229 | + break; |
| 230 | + case R.id.settings: |
| 231 | + startActivity(new Intent(this, SettingsActivity.class)); |
| 232 | + break; |
| 233 | + default: |
| 234 | + break; |
| 235 | + } |
| 236 | + return true; |
| 237 | + } |
| 238 | + |
| 239 | + @Override |
| 240 | + protected void onDestroy() { |
| 241 | + super.onDestroy(); |
| 242 | + if(isRecordingStarted) { |
| 243 | + accLogger.deleteFile(); |
| 244 | + isRecordingStarted = false; |
| 245 | + } |
| 246 | + } |
| 247 | + |
| 248 | + private void setUpBottomSheet() { |
| 249 | + bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet); |
| 250 | + |
| 251 | + final SharedPreferences settings = this.getSharedPreferences(PREF_NAME, MODE_PRIVATE); |
| 252 | + Boolean isFirstTime = settings.getBoolean("AccelerometerFirstTime", true); |
| 253 | + |
| 254 | + bottomSheetGuideTitle.setText(R.string.accelerometer); |
| 255 | + bottomSheetText.setText(R.string.accelerometer_intro); |
| 256 | + bottomSheetSchematic.setImageResource(R.drawable.find_mobile_axis); |
| 257 | + bottomSheetDesc.setText(R.string.accelerometer_description_text); |
| 258 | + |
| 259 | + if (isFirstTime) { |
| 260 | + bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED); |
| 261 | + tvShadow.setAlpha(0.8f); |
| 262 | + arrowUpDown.setRotation(180); |
| 263 | + bottomSheetSlideText.setText(R.string.hide_guide_text); |
| 264 | + SharedPreferences.Editor editor = settings.edit(); |
| 265 | + editor.putBoolean("AccelerometerFirstTime", false); |
| 266 | + editor.apply(); |
| 267 | + } else { |
| 268 | + bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN); |
| 269 | + } |
| 270 | + |
| 271 | + bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() { |
| 272 | + private Handler handler = new Handler(); |
| 273 | + private Runnable runnable = new Runnable() { |
| 274 | + @Override |
| 275 | + public void run() { |
| 276 | + bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN); |
| 277 | + } |
| 278 | + }; |
| 279 | + |
| 280 | + @Override |
| 281 | + public void onStateChanged(@NonNull final View bottomSheet, int newState) { |
| 282 | + switch (newState) { |
| 283 | + case BottomSheetBehavior.STATE_EXPANDED: |
| 284 | + handler.removeCallbacks(runnable); |
| 285 | + bottomSheetSlideText.setText(R.string.hide_guide_text); |
| 286 | + break; |
| 287 | + |
| 288 | + case BottomSheetBehavior.STATE_COLLAPSED: |
| 289 | + handler.postDelayed(runnable, 2000); |
| 290 | + break; |
| 291 | + |
| 292 | + default: |
| 293 | + handler.removeCallbacks(runnable); |
| 294 | + bottomSheetSlideText.setText(R.string.show_guide_text); |
| 295 | + } |
| 296 | + } |
| 297 | + |
| 298 | + @Override |
| 299 | + public void onSlide(@NonNull View bottomSheet, float slideOffset) { |
| 300 | + Float value = (float) MathUtils.map((double) slideOffset, 0.0, 1.0, 0.0, 0.8); |
| 301 | + tvShadow.setAlpha(value); |
| 302 | + arrowUpDown.setRotation(slideOffset * 180); |
| 303 | + } |
| 304 | + }); |
| 305 | + gestureDetector = new GestureDetector(this, new SwipeGestureDetector(bottomSheetBehavior)); |
| 306 | + } |
26 | 307 | }
|
0 commit comments