Skip to content

Commit 7ee1bfc

Browse files
committed
First commit
0 parents  commit 7ee1bfc

38 files changed

+1570
-0
lines changed

.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
*.iml
2+
.gradle
3+
.idea
4+
/local.properties
5+
/.idea/caches/build_file_checksums.ser
6+
/.idea/libraries
7+
/.idea/modules.xml
8+
/.idea/workspace.xml
9+
.DS_Store
10+
/build
11+
/captures
12+
.externalNativeBuild

app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

app/build.gradle

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 28
5+
defaultConfig {
6+
applicationId "com.github.sensorsreport"
7+
minSdkVersion 19
8+
targetSdkVersion 28
9+
versionCode 1
10+
versionName "1.0.0"
11+
}
12+
buildTypes {
13+
release {
14+
minifyEnabled false
15+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
16+
}
17+
}
18+
compileOptions {
19+
sourceCompatibility JavaVersion.VERSION_1_8
20+
targetCompatibility JavaVersion.VERSION_1_8
21+
}
22+
dataBinding {
23+
enabled = true
24+
}
25+
}
26+
27+
dependencies {
28+
implementation fileTree(dir: 'libs', include: ['*.jar'])
29+
implementation 'com.android.support:appcompat-v7:28.0.0'
30+
implementation 'com.android.support:cardview-v7:28.0.0'
31+
implementation "android.arch.lifecycle:extensions:1.1.1"
32+
implementation "android.arch.persistence.room:runtime:1.1.1"
33+
implementation 'com.google.code.gson:gson:2.8.5'
34+
annotationProcessor "android.arch.persistence.room:compiler:1.1.1"
35+
}

app/proguard-rules.pro

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile

app/src/main/AndroidManifest.xml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
package="com.github.sensorsreport">
5+
6+
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
7+
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
8+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
9+
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
10+
11+
<application
12+
android:allowBackup="true"
13+
android:icon="@mipmap/ic_launcher"
14+
android:label="@string/app_name"
15+
android:roundIcon="@mipmap/ic_launcher_round"
16+
android:supportsRtl="true"
17+
android:theme="@style/AppTheme"
18+
tools:ignore="AllowBackup,GoogleAppIndexingWarning">
19+
20+
<activity android:name=".MainActivity">
21+
<intent-filter>
22+
<action android:name="android.intent.action.MAIN" />
23+
24+
<category android:name="android.intent.category.LAUNCHER" />
25+
</intent-filter>
26+
</activity>
27+
28+
<service
29+
android:name=".service.ReportService"
30+
android:exported="false" />
31+
</application>
32+
</manifest>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.github.sensorsreport;
2+
3+
import android.databinding.BindingAdapter;
4+
import android.support.v7.widget.AppCompatSpinner;
5+
6+
public class BindingAdapters {
7+
8+
@BindingAdapter("spinnerEnabled")
9+
public static void enabled(AppCompatSpinner spinner, boolean enabled) {
10+
spinner.setEnabled(enabled);
11+
}
12+
}
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
package com.github.sensorsreport;
2+
3+
import android.Manifest;
4+
import android.arch.lifecycle.ViewModelProviders;
5+
import android.content.Intent;
6+
import android.content.SharedPreferences;
7+
import android.content.pm.PackageManager;
8+
import android.databinding.DataBindingUtil;
9+
import android.os.Bundle;
10+
import android.os.Environment;
11+
import android.preference.PreferenceManager;
12+
import android.support.annotation.NonNull;
13+
import android.support.v4.app.ActivityCompat;
14+
import android.support.v4.content.ContextCompat;
15+
import android.support.v7.app.AppCompatActivity;
16+
import android.view.View;
17+
import android.widget.AdapterView;
18+
import android.widget.ArrayAdapter;
19+
import android.widget.Toast;
20+
21+
import com.github.sensorsreport.data.SensorData;
22+
import com.github.sensorsreport.data.db.AppDatabase;
23+
import com.github.sensorsreport.data.db.SensorDataDao;
24+
import com.github.sensorsreport.databinding.MainActivityBinding;
25+
import com.github.sensorsreport.service.ReportService;
26+
import com.google.gson.Gson;
27+
import com.google.gson.reflect.TypeToken;
28+
29+
import java.io.File;
30+
import java.io.FileWriter;
31+
import java.io.IOException;
32+
import java.lang.reflect.Type;
33+
import java.util.List;
34+
import java.util.concurrent.Executor;
35+
import java.util.concurrent.Executors;
36+
37+
public class MainActivity extends AppCompatActivity implements
38+
SharedPreferences.OnSharedPreferenceChangeListener {
39+
40+
private static final String REPORT_REQUEST_TIME = "report_request_time";
41+
private static final String SELECTED_INTERVAL = "selected_interval";
42+
43+
private static final String[] INTERVALS = {"200", "500", "700", "1000", "1500", "2000"};
44+
45+
private MainActivityBinding binding;
46+
47+
private MainViewModel viewModel;
48+
49+
private SharedPreferences sharedPreferences;
50+
51+
private Executor diskExecutor;
52+
53+
private long reportStartTime = 0;
54+
55+
@Override
56+
protected void onCreate(Bundle savedInstanceState) {
57+
super.onCreate(savedInstanceState);
58+
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
59+
60+
diskExecutor = Executors.newSingleThreadExecutor();
61+
62+
requestPermissions();
63+
64+
viewModel = ViewModelProviders.of(this).get(MainViewModel.class);
65+
66+
viewModel.getDataReport().observe(this, sensorData -> binding.setData(sensorData));
67+
68+
Intent reportServiceIntent =
69+
new Intent(MainActivity.this, ReportService.class);
70+
71+
SensorDataDao sensorDataDao = AppDatabase.getDatabase(this).sensorDataDao();
72+
73+
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
74+
75+
boolean reportRequested = sharedPreferences
76+
.getBoolean(ReportService.REQUESTING_REPORT, false);
77+
binding.setStarted(reportRequested);
78+
79+
if (reportRequested) {
80+
reportStartTime = sharedPreferences
81+
.getLong(REPORT_REQUEST_TIME, System.currentTimeMillis());
82+
}
83+
84+
Type listOfSensorData = new TypeToken<List<SensorData>>() {}.getType();
85+
86+
binding.startButton.setOnClickListener(view -> {
87+
reportStartTime = System.currentTimeMillis();
88+
ContextCompat.startForegroundService(this, reportServiceIntent);
89+
sharedPreferences.edit().putLong(REPORT_REQUEST_TIME, reportStartTime).apply();
90+
});
91+
92+
binding.stopButton.setOnClickListener(view -> {
93+
stopService(reportServiceIntent);
94+
if (!isExternalStorageWritable()) {
95+
Toast.makeText(this, "External storage is not available!",
96+
Toast.LENGTH_SHORT).show();
97+
} else {
98+
diskExecutor.execute(() -> {
99+
List<SensorData> dataList = sensorDataDao.getSensorsReport(reportStartTime);
100+
String json = new Gson().toJson(dataList, listOfSensorData);
101+
102+
File file = new File(Environment.getExternalStoragePublicDirectory(
103+
Environment.DIRECTORY_DOWNLOADS),
104+
"sensors-report-" + reportStartTime + ".json");
105+
try {
106+
FileWriter fileWriter = new FileWriter(file);
107+
fileWriter.write(json);
108+
fileWriter.flush();
109+
fileWriter.close();
110+
} catch (IOException e) {
111+
e.printStackTrace();
112+
}
113+
});
114+
}
115+
});
116+
}
117+
118+
@Override
119+
protected void onStart() {
120+
super.onStart();
121+
sharedPreferences.registerOnSharedPreferenceChangeListener(this);
122+
}
123+
124+
@Override
125+
protected void onStop() {
126+
sharedPreferences.unregisterOnSharedPreferenceChangeListener(this);
127+
super.onStop();
128+
}
129+
130+
private void requestPermissions() {
131+
ActivityCompat.requestPermissions(MainActivity.this,
132+
new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
133+
Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1000);
134+
}
135+
136+
public boolean isExternalStorageWritable() {
137+
String state = Environment.getExternalStorageState();
138+
if (Environment.MEDIA_MOUNTED.equals(state)) {
139+
return true;
140+
}
141+
return false;
142+
}
143+
144+
@Override
145+
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
146+
@NonNull int[] grantResults) {
147+
if (requestCode == 1000) {
148+
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED &&
149+
grantResults[1] == PackageManager.PERMISSION_GRANTED) {
150+
binding.container.setEnabled(true);
151+
populateIntervalSpinner();
152+
} else {
153+
binding.container.setEnabled(false);
154+
}
155+
}
156+
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
157+
}
158+
159+
private void populateIntervalSpinner() {
160+
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
161+
android.R.layout.simple_spinner_dropdown_item, INTERVALS);
162+
binding.reportInterval.setAdapter(adapter);
163+
binding.reportInterval.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
164+
@Override
165+
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
166+
viewModel.setReportInterval(Long.parseLong(INTERVALS[i]));
167+
sharedPreferences.edit().putInt(SELECTED_INTERVAL, i).apply();
168+
}
169+
170+
@Override
171+
public void onNothingSelected(AdapterView<?> adapterView) {
172+
173+
}
174+
});
175+
binding.reportInterval.setSelection(sharedPreferences.getInt(SELECTED_INTERVAL, 0));
176+
}
177+
178+
@Override
179+
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
180+
if (s.equals(ReportService.REQUESTING_REPORT)) {
181+
binding.setStarted(sharedPreferences.getBoolean(s, false));
182+
}
183+
}
184+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.github.sensorsreport;
2+
3+
import android.app.Application;
4+
import android.arch.lifecycle.AndroidViewModel;
5+
import android.arch.lifecycle.LiveData;
6+
import android.arch.lifecycle.MutableLiveData;
7+
import android.arch.lifecycle.Transformations;
8+
9+
import com.github.sensorsreport.data.SensorLiveData;
10+
import com.github.sensorsreport.data.SensorData;
11+
12+
public class MainViewModel extends AndroidViewModel {
13+
14+
private MutableLiveData<Long> reportInterval = new MutableLiveData<>();
15+
private final LiveData<SensorData> dataReport;
16+
17+
public MainViewModel(Application application) {
18+
super(application);
19+
20+
dataReport = Transformations.switchMap(reportInterval, input ->
21+
SensorLiveData.getInstance(application).reportInterval(input));
22+
}
23+
24+
LiveData<SensorData> getDataReport() {
25+
return dataReport;
26+
}
27+
28+
void setReportInterval(long reportInterval) {
29+
this.reportInterval.setValue(reportInterval);
30+
}
31+
}

0 commit comments

Comments
 (0)