Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ dependencies {
implementation "org.osmdroid:osmdroid-mapsforge:$rootProject.mapsforgeVersion"
implementation "org.osmdroid:osmdroid-geopackage:$rootProject.geoPackageVersion"
implementation "com.android.support:multidex:$rootProject.multiDexVersion"
implementation "io.realm:android-adapters:$rootProject.realmAdapterVersion"

testImplementation "junit:junit:$rootProject.junitVersion"
androidTestImplementation("com.android.support.test:runner:$rootProject.testRunnerRulesVersion") {
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@
android:configChanges="orientation|screenSize|keyboardHidden" />
<activity android:name=".activity.AccelerometerActivity" />

<activity android:name=".activity.DataLoggerActivity"></activity>

<activity
android:name=".activity.Barometer_activity"
android:configChanges="keyboardHidden|screenSize|orientation"/>
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/io/pslab/PSLabApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.app.Application;

import io.realm.Realm;
import io.realm.RealmConfiguration;

/**
* Created by viveksb007 on 4/8/17.
Expand All @@ -14,5 +15,9 @@ public class PSLabApplication extends Application {
public void onCreate() {
super.onCreate();
Realm.init(this);
RealmConfiguration.Builder v = new RealmConfiguration.Builder().name(Realm.DEFAULT_REALM_NAME)
.schemaVersion(0)
.deleteRealmIfMigrationNeeded();
Realm.setDefaultConfiguration(v.build());
}
}
86 changes: 86 additions & 0 deletions app/src/main/java/io/pslab/activity/DataLoggerActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package io.pslab.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.pslab.R;
import io.pslab.adapters.SensorLoggerListAdapter;
import io.pslab.models.SensorLogged;
import io.realm.Realm;
import io.realm.RealmResults;
import io.realm.Sort;

/**
* Created by Avjeet on 05/08/18.
*/

public class DataLoggerActivity extends AppCompatActivity {
public static final String CALLER_ACTIVITY = "Caller";
@BindView(R.id.recycler_view)
RecyclerView recyclerView;

@BindView(R.id.toolbar)
Toolbar toolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data_logger);
ButterKnife.bind(this);
setSupportActionBar(toolbar);
Realm realm = Realm.getDefaultInstance();
String caller = getIntent().getStringExtra(CALLER_ACTIVITY);
if (caller == null)
caller = "";

RealmResults<SensorLogged> results;
String title;
switch (caller) {
case "Lux Meter":
results = realm.where(SensorLogged.class).equalTo("sensor", caller)
.findAll()
.sort("dateTimeStamp", Sort.DESCENDING);
title = caller + " Data";
break;
default:
results = realm.where(SensorLogged.class)
.findAll()
.sort("dateTimeStamp", Sort.DESCENDING);
title = getString(R.string.logged_data);
}
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setTitle(title);
}
SensorLoggerListAdapter adapter = new SensorLoggerListAdapter(results, this);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(linearLayoutManager);

DividerItemDecoration itemDecor = new DividerItemDecoration(this, DividerItemDecoration.HORIZONTAL);
recyclerView.addItemDecoration(itemDecor);
recyclerView.setAdapter(adapter);
}

@Override
public void onBackPressed() {
finish();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
}
40 changes: 23 additions & 17 deletions app/src/main/java/io/pslab/activity/LuxMeterActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import io.pslab.others.MathUtils;
import io.pslab.fragment.SettingsFragment;
import io.pslab.others.SwipeGestureDetector;

import butterknife.BindView;
import butterknife.ButterKnife;

Expand Down Expand Up @@ -85,13 +84,16 @@ public class LuxMeterActivity extends AppCompatActivity {
private boolean checkGpsOnResume = false;
public boolean locationPref;
private LuxMeterFragmentData selectedFragment;
public static final String NAME = "realmData";
private SharedPreferences realmPreferences;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lux_main);
ButterKnife.bind(this);
setSupportActionBar(toolbar);
realmPreferences = getSharedPreferences(NAME, Context.MODE_PRIVATE);
setUpBottomSheet();
bottomNavigationView.setOnNavigationItemSelectedListener
(new BottomNavigationView.OnNavigationItemSelectedListener() {
Expand Down Expand Up @@ -215,35 +217,31 @@ public boolean onPrepareOptionsMenu(Menu menu) {
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.record_data:
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_STORAGE_FOR_DATA);
return true;
}
if (recordData) {
((LuxMeterFragmentData)selectedFragment).stopSensorFetching();
((LuxMeterFragmentData) selectedFragment).stopSensorFetching();
invalidateOptionsMenu();
Long uniqueRef = realmPreferences.getLong("uniqueCount", 0);
selectedFragment.saveDataInRealm(uniqueRef);
CustomSnackBar.showSnackBar(coordinatorLayout, getString(R.string.exp_data_saved), null, null);
SharedPreferences.Editor editor = realmPreferences.edit();
editor.putLong("uniqueCount", uniqueRef + 1);
editor.commit();
recordData = false;
} else {
luxLogger = new CSVLogger(getString(R.string.lux_meter));
luxLogger.writeCSVFile("Timestamp,X,Y,Z\n");
recordData = true;
((LuxMeterFragmentData)selectedFragment).startSensorFetching();
invalidateOptionsMenu();
if (locationPref) {
gpsLogger = new GPSLogger(this, (LocationManager) getSystemService(Context.LOCATION_SERVICE));
if (gpsLogger.isGPSEnabled()) {
recordData = true;
CustomSnackBar.showSnackBar(coordinatorLayout, getString(R.string.data_recording_start) + "\n" + getString(R.string.location_enabled), null, null);
((LuxMeterFragmentData) selectedFragment).startSensorFetching();
invalidateOptionsMenu();
} else {
checkGpsOnResume = true;
}
gpsLogger.startFetchingLocation();
} else {
recordData = true;
CustomSnackBar.showSnackBar(coordinatorLayout, getString(R.string.data_recording_start) + "\n" + getString(R.string.location_disabled), null, null);
((LuxMeterFragmentData) selectedFragment).startSensorFetching();
invalidateOptionsMenu();
}
}
break;
Expand All @@ -257,9 +255,16 @@ public boolean onOptionsItemSelected(MenuItem item) {
}
Intent MAP = new Intent(getApplicationContext(), MapsActivity.class);
startActivity(MAP);
startActivity(new Intent(this, ShowLoggedData.class));
break;
case R.id.settings:
startActivity(new Intent(this, SettingsActivity.class));
break;
case R.id.show_logged_data:
Intent intent = new Intent(this,DataLoggerActivity.class);
intent.putExtra(DataLoggerActivity.CALLER_ACTIVITY,"Lux Meter");
startActivity(intent);

break;
default:
break;
Expand All @@ -273,7 +278,8 @@ protected void onResume() {
if (checkGpsOnResume) {
if (gpsLogger.isGPSEnabled()) {
recordData = true;
CustomSnackBar.showSnackBar(coordinatorLayout, getString(R.string.data_recording_start), null, null);
((LuxMeterFragmentData) selectedFragment).startSensorFetching();
invalidateOptionsMenu();
} else {
recordData = false;
Toast.makeText(getApplicationContext(), getString(R.string.gps_not_enabled),
Expand Down
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 @@ -270,6 +270,12 @@ public boolean onNavigationItemSelected(@NonNull MenuItem item) {
case R.id.nav_app_version:
setTitleColor(R.color.gray);
break;
case R.id.sensor_data_logger:
if (drawer != null) {
drawer.closeDrawers();
}
startActivity(new Intent(MainActivity.this, DataLoggerActivity.class));
break;
default:
navItemIndex = 0;
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/io/pslab/activity/ShowLoggedData.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

/**
* Created by viveksb007 on 12/8/17.
* deprecated
*/

public class ShowLoggedData extends AppCompatActivity {
Expand Down
86 changes: 86 additions & 0 deletions app/src/main/java/io/pslab/adapters/SensorLoggerListAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package io.pslab.adapters;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.text.SimpleDateFormat;
import java.util.Date;

import io.pslab.R;
import io.pslab.models.LuxData;
import io.pslab.models.SensorLogged;
import io.realm.Realm;
import io.realm.RealmRecyclerViewAdapter;
import io.realm.RealmResults;

/**
* Created by Avjeet on 03-08-2018.
*/
public class SensorLoggerListAdapter extends RealmRecyclerViewAdapter<SensorLogged, SensorLoggerListAdapter.ViewHolder> {


private Context context;
private SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm:ss");
private Realm realm;

public SensorLoggerListAdapter(RealmResults<SensorLogged> results, Context context) {
super(results, true, true);
this.context = context;
realm = Realm.getDefaultInstance();
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.logger_data_item, parent, false);
return new ViewHolder(itemView);
}

@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
final SensorLogged temp = getItem(position);
holder.sensor.setText(temp.getSensor());
Date date = new Date(temp.getDateTimeStamp());
holder.dateTime.setText(String.valueOf(sdf.format(date)));
holder.cardView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
RealmResults<LuxData> results = realm.where(LuxData.class).equalTo("foreignKey", getItem(holder.getAdapterPosition()).getUniqueRef()).findAll();
Toast.makeText(context, "Results size :" + String.valueOf(results.size()), Toast.LENGTH_SHORT).show();
}
});
holder.deleteIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
RealmResults<SensorLogged> sensorItem = realm.where(SensorLogged.class).equalTo("uniqueRef", getItem(holder.getAdapterPosition()).getUniqueRef()).findAll();
RealmResults<LuxData> results = realm.where(LuxData.class).equalTo("foreignKey", getItem(holder.getAdapterPosition()).getUniqueRef()).findAll();
realm.beginTransaction();
results.deleteAllFromRealm();
sensorItem.deleteAllFromRealm();
realm.commitTransaction();
}
});
}

public class ViewHolder extends RecyclerView.ViewHolder {
private TextView sensor, dateTime;
ImageView deleteIcon;
private CardView cardView;

public ViewHolder(View itemView) {
super(itemView);
dateTime = itemView.findViewById(R.id.date_time);
sensor = itemView.findViewById(R.id.sensor_name);
deleteIcon = itemView.findViewById(R.id.delete_item);
cardView = itemView.findViewById(R.id.data_item_card);
}
}
}
Loading