Skip to content

Commit

Permalink
Merge pull request #84 from berkethetechnerd/stats-bug-and-improvement
Browse files Browse the repository at this point in the history
Stats bug and improvement
  • Loading branch information
berkethetechnerd authored Feb 21, 2021
2 parents d14da49 + d10db86 commit 1371e61
Show file tree
Hide file tree
Showing 57 changed files with 934 additions and 602 deletions.
32 changes: 17 additions & 15 deletions DeviceTracking/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.alpsproject.devicetracking">

<uses-permission android:name="android.permission.INTERNET" />
Expand All @@ -16,29 +17,21 @@
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:name=".ApplicationClass"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.DeviceTracking"
android:networkSecurityConfig="@xml/network_security_config"
android:name=".ApplicationClass" >
android:supportsRtl="true"
android:allowBackup="true"
tools:targetApi="n"
tools:ignore="AllowBackup">

<service
android:name=".services.TrackerService"
android:enabled="true"
android:exported="false">
</service>

<activity
android:name=".ReportScreenActivity"
android:label="@string/report_title"
android:theme="@style/Theme.DeviceTracking.NoActionBar" />

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

<activity android:name=".DataCollectionActivity" />
android:exported="false" />

<activity android:name=".LoginActivity">
<intent-filter>
Expand All @@ -48,8 +41,17 @@
</intent-filter>
</activity>

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

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

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

<activity
android:name=".ReportScreenActivity"
android:label="@string/report_title"
android:theme="@style/Theme.DeviceTracking.NoActionBar" />

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

</application>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,22 @@ class DataCollectionActivity : BaseActivity(), SensorStatusDelegate {

private val isWifiSelected: Boolean
get() = intent.getBooleanExtra(C.SENSOR_WIFI, false)
|| SharedPreferencesManager.read(C.RUNNING_SENSOR_WIFI, false)
|| SharedPreferencesManager.read(C.getRunningSensorKey(DeviceSensor.ACCESS_WIFI), false)
private val isBluetoothSelected: Boolean
get() = intent.getBooleanExtra(C.SENSOR_BLUETOOTH, false)
|| SharedPreferencesManager.read(C.RUNNING_SENSOR_BLUETOOTH, false)
|| SharedPreferencesManager.read(C.getRunningSensorKey(DeviceSensor.ACCESS_BLUETOOTH), false)
private val isScreenUsageSelected: Boolean
get() = intent.getBooleanExtra(C.SENSOR_SCREEN_USAGE, false)
|| SharedPreferencesManager.read(C.RUNNING_SENSOR_SCREEN_USAGE, false)
|| SharedPreferencesManager.read(C.getRunningSensorKey(DeviceSensor.ACCESS_SCREEN_USAGE), false)
private val isGpsSelected: Boolean
get() = intent.getBooleanExtra(C.SENSOR_GPS, false)
|| SharedPreferencesManager.read(C.RUNNING_SENSOR_GPS, false)
|| SharedPreferencesManager.read(C.getRunningSensorKey(DeviceSensor.ACCESS_GPS), false)
private val isNfcSelected: Boolean
get() = intent.getBooleanExtra(C.SENSOR_NFC, false)
|| SharedPreferencesManager.read(C.RUNNING_SENSOR_NFC, false)
|| SharedPreferencesManager.read(C.getRunningSensorKey(DeviceSensor.ACCESS_NFC), false)
private val isTorchSelected: Boolean
get() = intent.getBooleanExtra(C.SENSOR_TORCH, false)
|| SharedPreferencesManager.read(C.RUNNING_SENSOR_TORCH, false)
|| SharedPreferencesManager.read(C.getRunningSensorKey(DeviceSensor.ACCESS_TORCH), false)


private val arrOfSelectedSensors: Array<Boolean>
Expand Down Expand Up @@ -76,9 +76,6 @@ class DataCollectionActivity : BaseActivity(), SensorStatusDelegate {
}

private fun initUI() {
tvTitle = findViewById(R.id.tv_data_collection_title)
tvTitle.text = getString(R.string.data_collection_title)

btnStartStop = findViewById(R.id.btn_start_stop)
btnStartStop.setOnClickListener { startStopButton() }

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,43 @@ import com.alpsproject.devicetracking.helper.SharedPreferencesManager
import com.alpsproject.devicetracking.model.SensorData
import com.google.gson.JsonObject
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.Body
import retrofit2.http.Headers
import retrofit2.http.POST

object API {
object SensorMetricsAPI {

fun sendSensorEntry(sensorData: SensorData, onResult: (SensorData?) -> Unit) {
val retrofit = APIServiceBuilder.buildService(APIServices::class.java)
retrofit.postSensorEntry(getJsonObject(sensorData)).enqueue(
val jsonSensorData = getJsonObject(sensorData)

retrofit.postSensorEntry(jsonSensorData).enqueue(
object : Callback<JsonObject> {
override fun onFailure(call: Call<JsonObject>, t: Throwable) {
onResult(null)
onResult(null) // On failure, returns null
}

override fun onResponse(call: Call<JsonObject>, response: Response<JsonObject>) {
Logger.logAPINotification()
onResult(sensorData)
onResult(sensorData) // On success, returns the same object
}
}
)
}

private fun getJsonObject(sensorData: SensorData): JsonObject {
val jsonObject = JsonObject()
jsonObject.addProperty("sensor_name", sensorData.sensorName)
jsonObject.addProperty("start_time", CalendarManager.convertDateForBackend(sensorData.startTime))
jsonObject.addProperty("end_time", CalendarManager.convertDateForBackend(sensorData.endTime))

return jsonObject
private fun getJsonObject(sensorData: SensorData) = JsonObject().apply {
addProperty("sensor_name", sensorData.sensorName)
addProperty("start_time", CalendarManager.dateToBackendString(sensorData.startTime))
addProperty("end_time", CalendarManager.dateToBackendString(sensorData.endTime))
}
}

object APIServiceBuilder {
private object APIServiceBuilder {

private val retrofit: Retrofit
private val address: String
Expand All @@ -53,8 +54,6 @@ object APIServiceBuilder {

init {
val client = OkHttpClient.Builder().build()
val interceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BODY

retrofit = Retrofit.Builder()
.baseUrl(address)
Expand All @@ -66,4 +65,12 @@ object APIServiceBuilder {
fun <T> buildService(service: Class<T>): T {
return retrofit.create(service)
}
}

private interface APIServices {

@Headers("Content-Type: application/json")
@POST("/data")
fun postSensorEntry(@Body sensorEntry: JsonObject): Call<JsonObject>

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,15 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.graphics.Camera
import android.hardware.camera2.CameraManager
import android.location.LocationManager
import android.net.ConnectivityManager
import android.net.wifi.WifiManager
import android.nfc.NfcAdapter
import com.alpsproject.devicetracking.R
import com.alpsproject.devicetracking.delegates.SensorStatusDelegate
import com.alpsproject.devicetracking.enums.DeviceSensor


object Broadcaster {

private val receivers: ArrayList<SensorStatusDelegate> = ArrayList()
public var isTorchEnabled = false

fun registerForBroadcasting(receiver: Context, arrOfSensors: Array<Boolean>) {
if (arrOfSensors[0]) { registerForWifi(receiver) }
Expand All @@ -40,12 +34,7 @@ object Broadcaster {
if (arrOfSensors[2]) { receiver.unregisterReceiver(screenUsageReceiver) }
if (arrOfSensors[3]) { receiver.unregisterReceiver(gpsReceiver) }
if (arrOfSensors[4]) { receiver.unregisterReceiver(nfcReceiver) }
if (arrOfSensors[5]) {
val cameraManager = SettingsManager.getCameraManager(receiver)
cameraManager?.let {
it.unregisterTorchCallback(torchReceiver)
}
}
if (arrOfSensors[5]) { SettingsManager.getCameraManager(receiver)?.unregisterTorchCallback(torchReceiver) }

(receiver as? SensorStatusDelegate)?.let {
if (receivers.contains(it)) {
Expand Down Expand Up @@ -82,8 +71,7 @@ object Broadcaster {
}

private fun registerForTorch(receiver: Context) {
val cameraManager = SettingsManager.getCameraManager(receiver) ?: return
cameraManager.registerTorchCallback(torchReceiver, null)
SettingsManager.getCameraManager(receiver)?.registerTorchCallback(torchReceiver, null)
}

fun registerForShutdown(receiver: Context) {
Expand Down Expand Up @@ -174,21 +162,20 @@ object Broadcaster {
}

private val nfcReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent) {
val action = intent.action
if (action == NfcAdapter.ACTION_ADAPTER_STATE_CHANGED) {
val state = intent.getIntExtra(
NfcAdapter.EXTRA_ADAPTER_STATE,
NfcAdapter.STATE_OFF
)
when (state) {
NfcAdapter.STATE_OFF -> {
Logger.logSensorUpdate(DeviceSensor.ACCESS_NFC, false)
broadcastSensorChange(DeviceSensor.ACCESS_NFC, false)
}
NfcAdapter.STATE_ON -> {
override fun onReceive(context: Context?, intent: Intent?) {
intent?.let {
if (it.action.equals(NfcAdapter.ACTION_ADAPTER_STATE_CHANGED)) {
val state = intent.getIntExtra(
NfcAdapter.EXTRA_ADAPTER_STATE,
NfcAdapter.STATE_OFF
)

if (state == NfcAdapter.STATE_ON) {
Logger.logSensorUpdate(DeviceSensor.ACCESS_NFC, true)
broadcastSensorChange(DeviceSensor.ACCESS_NFC, true)
} else if (state == NfcAdapter.STATE_OFF) {
Logger.logSensorUpdate(DeviceSensor.ACCESS_NFC, false)
broadcastSensorChange(DeviceSensor.ACCESS_NFC, false)
}
}
}
Expand All @@ -197,48 +184,62 @@ object Broadcaster {

private val torchReceiver = object : android.hardware.camera2.CameraManager.TorchCallback() {
override fun onTorchModeChanged(cameraId: String, enabled: Boolean) {
super.onTorchModeChanged(cameraId, enabled)
if(enabled){
isTorchEnabled = true
SettingsManager.setTorchStatus(enabled)

if (enabled) {
Logger.logSensorUpdate(DeviceSensor.ACCESS_TORCH, true)
broadcastSensorChange(DeviceSensor.ACCESS_TORCH, true)
} else {
isTorchEnabled = false
Logger.logSensorUpdate(DeviceSensor.ACCESS_TORCH, false)
broadcastSensorChange(DeviceSensor.ACCESS_TORCH, false)
}
}
}

private val shutdownReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val action = intent.action
if (action.equals(Intent.ACTION_SHUTDOWN) || action.equals(Intent.ACTION_REBOOT)) {
SharedPreferencesManager.write(ConstantsManager.RUNNING_DATA_COLLECTION, false)
Logger.logServiceNotification("Phone is shutting down. Cutting off the data collection!")

if (SharedPreferencesManager.read(ConstantsManager.RUNNING_SENSOR_WIFI, false)) {
DataCollectionManager.stopCollectionForSensor(DeviceSensor.ACCESS_WIFI)
}
override fun onReceive(context: Context?, intent: Intent?) {
intent?.let {
val action = it.action

if (SharedPreferencesManager.read(ConstantsManager.RUNNING_SENSOR_BLUETOOTH, false)) {
DataCollectionManager.stopCollectionForSensor(DeviceSensor.ACCESS_BLUETOOTH)
}
if (action.equals(Intent.ACTION_SHUTDOWN) || action.equals(Intent.ACTION_REBOOT)) {
Logger.logServiceNotification("Phone is shutting down. Cutting off the data collection!")
SharedPreferencesManager.write(ConstantsManager.RUNNING_DATA_COLLECTION, false)

if (SharedPreferencesManager.read(ConstantsManager.RUNNING_SENSOR_SCREEN_USAGE, false)) {
DataCollectionManager.stopCollectionForSensor(DeviceSensor.ACCESS_SCREEN_USAGE)
}
val arrOfSensors: Array<Boolean> = Array(6) { false }

if (SharedPreferencesManager.read(ConstantsManager.RUNNING_SENSOR_GPS, false)) {
DataCollectionManager.stopCollectionForSensor(DeviceSensor.ACCESS_GPS)
}
if (SharedPreferencesManager.read(ConstantsManager.getRunningSensorKey(DeviceSensor.ACCESS_WIFI), false)) {
DataCollectionManager.stopCollectionForSensor(DeviceSensor.ACCESS_WIFI)
arrOfSensors[0] = true
}

if (SharedPreferencesManager.read(ConstantsManager.RUNNING_SENSOR_NFC, false)) {
DataCollectionManager.stopCollectionForSensor(DeviceSensor.ACCESS_NFC)
}
if (SharedPreferencesManager.read(ConstantsManager.getRunningSensorKey(DeviceSensor.ACCESS_BLUETOOTH), false)) {
DataCollectionManager.stopCollectionForSensor(DeviceSensor.ACCESS_BLUETOOTH)
arrOfSensors[1] = true
}

if (SharedPreferencesManager.read(ConstantsManager.getRunningSensorKey(DeviceSensor.ACCESS_SCREEN_USAGE), false)) {
DataCollectionManager.stopCollectionForSensor(DeviceSensor.ACCESS_SCREEN_USAGE)
arrOfSensors[2] = true
}

if (SharedPreferencesManager.read(ConstantsManager.RUNNING_SENSOR_TORCH, false)) {
DataCollectionManager.stopCollectionForSensor(DeviceSensor.ACCESS_TORCH)
if (SharedPreferencesManager.read(ConstantsManager.getRunningSensorKey(DeviceSensor.ACCESS_GPS), false)) {
DataCollectionManager.stopCollectionForSensor(DeviceSensor.ACCESS_GPS)
arrOfSensors[3] = true
}

if (SharedPreferencesManager.read(ConstantsManager.getRunningSensorKey(DeviceSensor.ACCESS_NFC), false)) {
DataCollectionManager.stopCollectionForSensor(DeviceSensor.ACCESS_NFC)
arrOfSensors[4] = true
}

if (SharedPreferencesManager.read(ConstantsManager.getRunningSensorKey(DeviceSensor.ACCESS_TORCH), false)) {
DataCollectionManager.stopCollectionForSensor(DeviceSensor.ACCESS_TORCH)
arrOfSensors[5] = true
}

context?.let {
unregisterForBroadcasting(context, arrOfSensors)
}
}
}
}
Expand Down
Loading

0 comments on commit 1371e61

Please sign in to comment.