Skip to content

Commit

Permalink
Merge branch 'main' into disclamer_text
Browse files Browse the repository at this point in the history
  • Loading branch information
eceatata authored Feb 7, 2021
2 parents 9becd43 + c7678f1 commit 6cd52fb
Show file tree
Hide file tree
Showing 14 changed files with 131 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class DataCollectionActivity : BaseActivity(), SensorStatusDelegate {
private lateinit var selectedScreenUsageView: SensorView
private lateinit var selectedGpsView: SensorView
private lateinit var selectedNfcView: SensorView
private lateinit var selectedTorchView: SensorView

private val isWifiSelected: Boolean
get() = intent.getBooleanExtra(C.SENSOR_WIFI, false)
Expand All @@ -39,14 +40,19 @@ class DataCollectionActivity : BaseActivity(), SensorStatusDelegate {
private val isNfcSelected: Boolean
get() = intent.getBooleanExtra(C.SENSOR_NFC, false)
|| SharedPreferencesManager.read(C.RUNNING_SENSOR_NFC, false)
private val isTorchSelected: Boolean
get() = intent.getBooleanExtra(C.SENSOR_TORCH, false)
|| SharedPreferencesManager.read(C.RUNNING_SENSOR_TORCH, false)


private val arrOfSelectedSensors: Array<Boolean>
get() = arrayOf(
isWifiSelected,
isBluetoothSelected,
isScreenUsageSelected,
isGpsSelected,
isNfcSelected
isNfcSelected,
isTorchSelected
)

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -88,6 +94,7 @@ class DataCollectionActivity : BaseActivity(), SensorStatusDelegate {
selectedScreenUsageView = findViewById(R.id.data_collection_list_screen_usage)
selectedGpsView = findViewById(R.id.data_collection_list_gps)
selectedNfcView = findViewById(R.id.data_collection_list_nfc)
selectedTorchView = findViewById(R.id.data_collection_list_torch)

if(!isWifiSelected) {
selectedWifiView.visibility = View.GONE
Expand Down Expand Up @@ -128,6 +135,13 @@ class DataCollectionActivity : BaseActivity(), SensorStatusDelegate {
selectedNfcView.configureSensor(getResIcon(R.drawable.ic_nfc_sensor), getString(R.string.sensor_nfc))
selectedNfcView.changeSensorStatus(SettingsManager.isNfcEnabled(this))
}

if(!isTorchSelected) {
selectedTorchView.visibility = View.GONE
} else {
selectedTorchView.switchToStatusView()
selectedTorchView.configureSensor(getResIcon(R.drawable.ic_light_sensor), getString(R.string.sensor_torch))
}
}

private fun startStopButton() {
Expand All @@ -140,6 +154,7 @@ class DataCollectionActivity : BaseActivity(), SensorStatusDelegate {
if(isScreenUsageSelected) { DataCollectionManager.startCollectionForSensor(DeviceSensor.ACCESS_SCREEN_USAGE, this) }
if(isGpsSelected) { DataCollectionManager.startCollectionForSensor(DeviceSensor.ACCESS_GPS, this) }
if(isNfcSelected) { DataCollectionManager.startCollectionForSensor(DeviceSensor.ACCESS_NFC, this) }
if(isTorchSelected) { DataCollectionManager.startCollectionForSensor(DeviceSensor.ACCESS_TORCH, this) }
}

fun stopDataCollection() {
Expand All @@ -151,6 +166,7 @@ class DataCollectionActivity : BaseActivity(), SensorStatusDelegate {
if(isScreenUsageSelected){ DataCollectionManager.stopCollectionForSensor(DeviceSensor.ACCESS_SCREEN_USAGE) }
if(isGpsSelected) { DataCollectionManager.stopCollectionForSensor(DeviceSensor.ACCESS_GPS) }
if(isNfcSelected) { DataCollectionManager.stopCollectionForSensor(DeviceSensor.ACCESS_NFC) }
if(isTorchSelected) { DataCollectionManager.stopCollectionForSensor(DeviceSensor.ACCESS_TORCH) }
}

if(!isRunning()) { // Starting
Expand Down Expand Up @@ -184,6 +200,10 @@ class DataCollectionActivity : BaseActivity(), SensorStatusDelegate {

override fun didNfcDisable() = selectedNfcView.changeSensorStatus(false)

override fun didTorchEnable() = selectedTorchView.changeSensorStatus(true)

override fun didTorchDisable() = selectedTorchView.changeSensorStatus(false)

override fun onCreateOptionsMenu(menu: Menu): Boolean {
val inflater: MenuInflater = menuInflater
inflater.inflate(R.menu.menu_report, menu)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class SensorSelectionActivity : BaseActivity(), PermissionDelegate {
private lateinit var sensorScreenUsageView: SensorView
private lateinit var sensorGpsView: SensorView
private lateinit var sensorNfcView: SensorView
private lateinit var sensorTorchView: SensorView
private lateinit var btnNext: Button

private var grantedSensors: Int = 0
Expand All @@ -32,6 +33,7 @@ class SensorSelectionActivity : BaseActivity(), PermissionDelegate {
if (sensorScreenUsageView.isSensorSelected()) numberOfSensors++
if (sensorGpsView.isSensorSelected()) numberOfSensors++
if (sensorNfcView.isSensorSelected()) numberOfSensors++
if (sensorTorchView.isSensorSelected()) numberOfSensors++
return numberOfSensors
}

Expand All @@ -56,6 +58,7 @@ class SensorSelectionActivity : BaseActivity(), PermissionDelegate {
sensorScreenUsageView.deselectSensor()
sensorGpsView.deselectSensor()
sensorNfcView.deselectSensor()
sensorTorchView.deselectSensor()
}

private fun checkIfAlreadyRunning() {
Expand All @@ -80,6 +83,9 @@ class SensorSelectionActivity : BaseActivity(), PermissionDelegate {
sensorNfcView = findViewById(R.id.sensor_view_nfc)
sensorNfcView.configureSensor(getResIcon(R.drawable.ic_nfc_sensor), getString(R.string.sensor_nfc))

sensorTorchView = findViewById(R.id.sensor_view_torch)
sensorTorchView.configureSensor(getResIcon(R.drawable.ic_light_sensor), getString(R.string.sensor_torch))

btnNext = findViewById(R.id.btn_next_data_collection)
btnNext.setOnClickListener { requestPermissions() }
}
Expand All @@ -88,6 +94,14 @@ class SensorSelectionActivity : BaseActivity(), PermissionDelegate {
grantedSensors = 0
rejectedSensors = 0

if (sensorTorchView.isSensorSelected()) {
if (!PermissionManager.checkPermission(DeviceSensor.ACCESS_TORCH)) {
PermissionManager.askPermission(this, DeviceSensor.ACCESS_TORCH)
} else {
grantedSensors++
}
}

if (sensorNfcView.isSensorSelected()) {
if (!PermissionManager.checkPermission(DeviceSensor.ACCESS_NFC)) {
PermissionManager.askPermission(this, DeviceSensor.ACCESS_NFC)
Expand Down Expand Up @@ -157,6 +171,7 @@ class SensorSelectionActivity : BaseActivity(), PermissionDelegate {
dataCollection.putExtra(C.SENSOR_SCREEN_USAGE, sensorScreenUsageView.isSensorSelected())
dataCollection.putExtra(C.SENSOR_GPS, sensorGpsView.isSensorSelected())
dataCollection.putExtra(C.SENSOR_NFC, sensorNfcView.isSensorSelected())
dataCollection.putExtra(C.SENSOR_TORCH, sensorTorchView.isSensorSelected())
startActivity(dataCollection)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ interface SensorStatusDelegate {
fun didGpsDisable()
fun didNfcEnable()
fun didNfcDisable()
fun didTorchEnable()
fun didTorchDisable()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ enum class DeviceSensor {
ACCESS_BLUETOOTH,
ACCESS_SCREEN_USAGE,
ACCESS_GPS,
ACCESS_NFC
ACCESS_NFC,
ACCESS_TORCH
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ 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
Expand All @@ -17,13 +19,15 @@ 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) }
if (arrOfSensors[1]) { registerForBluetooth(receiver) }
if (arrOfSensors[2]) { registerForScreenUsage(receiver) }
if (arrOfSensors[3]) { registerForGps(receiver) }
if (arrOfSensors[4]) { registerForNfc(receiver) }
if (arrOfSensors[5]) { registerForTorch(receiver) }

(receiver as? SensorStatusDelegate)?.let {
receivers.add(it)
Expand All @@ -36,6 +40,12 @@ 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)
}
}

(receiver as? SensorStatusDelegate)?.let {
if (receivers.contains(it)) {
Expand Down Expand Up @@ -71,6 +81,11 @@ object Broadcaster {
receiver.registerReceiver(nfcReceiver, nfcFilter)
}

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

fun registerForShutdown(receiver: Context) {
val shutdownFilter = IntentFilter()
shutdownFilter.addAction(Intent.ACTION_REBOOT)
Expand Down Expand Up @@ -180,6 +195,21 @@ 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
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
Expand All @@ -206,6 +236,10 @@ object Broadcaster {
if (SharedPreferencesManager.read(ConstantsManager.RUNNING_SENSOR_NFC, false)) {
DataCollectionManager.stopCollectionForSensor(DeviceSensor.ACCESS_NFC)
}

if (SharedPreferencesManager.read(ConstantsManager.RUNNING_SENSOR_TORCH, false)) {
DataCollectionManager.stopCollectionForSensor(DeviceSensor.ACCESS_TORCH)
}
}
}
}
Expand All @@ -219,6 +253,7 @@ object Broadcaster {
DeviceSensor.ACCESS_SCREEN_USAGE -> broadcastScreenStateChange(status)
DeviceSensor.ACCESS_GPS -> broadcastGpsChange(status)
DeviceSensor.ACCESS_NFC -> broadcastNfcChange(status)
DeviceSensor.ACCESS_TORCH -> broadcastTorchChange(status)
}
}

Expand Down Expand Up @@ -261,4 +296,12 @@ object Broadcaster {
receivers.forEach { it.didNfcDisable() }
}
}

private fun broadcastTorchChange(status: Boolean) {
if (status) {
receivers.forEach { it.didTorchEnable() }
} else {
receivers.forEach { it.didTorchDisable() }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,31 @@ object ConstantsManager {
const val SENSOR_SCREEN_USAGE = "Screen Usage"
const val SENSOR_GPS = "Location"
const val SENSOR_NFC = "NFC"
const val SENSOR_TORCH = "TORCH"

const val CONSENT_OF_USER = "ConsentForDisclaimer"
private const val CONSENT_OF_WIFI = CONSENT_OF_USER + SENSOR_WIFI
private const val CONSENT_OF_BLUETOOTH = CONSENT_OF_USER + SENSOR_BLUETOOTH
private const val CONSENT_OF_SCREEN_USAGE = CONSENT_OF_USER + SENSOR_SCREEN_USAGE
private const val CONSENT_OF_GPS= CONSENT_OF_USER + SENSOR_GPS
private const val CONSENT_OF_NFC= CONSENT_OF_USER + SENSOR_NFC
private const val CONSENT_OF_TORCH= CONSENT_OF_USER + SENSOR_TORCH

const val RUNNING_DATA_COLLECTION = "RunningBackground"
const val RUNNING_SENSOR_WIFI = RUNNING_DATA_COLLECTION + SENSOR_WIFI
const val RUNNING_SENSOR_BLUETOOTH = RUNNING_DATA_COLLECTION + SENSOR_BLUETOOTH
const val RUNNING_SENSOR_SCREEN_USAGE = RUNNING_DATA_COLLECTION + SENSOR_SCREEN_USAGE
const val RUNNING_SENSOR_GPS = RUNNING_DATA_COLLECTION + SENSOR_GPS
const val RUNNING_SENSOR_NFC = RUNNING_DATA_COLLECTION + SENSOR_NFC
const val RUNNING_SENSOR_TORCH = RUNNING_DATA_COLLECTION + SENSOR_TORCH

private const val ID = "ID"
private const val RUNNING_SENSOR_WIFI_ID = RUNNING_SENSOR_WIFI + ID
private const val RUNNING_SENSOR_BLUETOOTH_ID = RUNNING_SENSOR_BLUETOOTH + ID
private const val RUNNING_SENSOR_SCREEN_USAGE_ID = RUNNING_SENSOR_SCREEN_USAGE + ID
private const val RUNNING_SENSOR_GPS_ID = RUNNING_SENSOR_GPS + ID
private const val RUNNING_SENSOR_NFC_ID = RUNNING_SENSOR_NFC + ID
private const val RUNNING_SENSOR_TORCH_ID = RUNNING_SENSOR_TORCH + ID

fun getSensorName(forSensor: DeviceSensor): String {
return when (forSensor) {
Expand All @@ -41,6 +45,7 @@ object ConstantsManager {
DeviceSensor.ACCESS_SCREEN_USAGE -> SENSOR_SCREEN_USAGE
DeviceSensor.ACCESS_GPS -> SENSOR_GPS
DeviceSensor.ACCESS_NFC -> SENSOR_NFC
DeviceSensor.ACCESS_TORCH -> SENSOR_TORCH
}
}

Expand All @@ -51,6 +56,7 @@ object ConstantsManager {
DeviceSensor.ACCESS_SCREEN_USAGE -> RUNNING_SENSOR_SCREEN_USAGE_ID
DeviceSensor.ACCESS_GPS -> RUNNING_SENSOR_GPS_ID
DeviceSensor.ACCESS_NFC -> RUNNING_SENSOR_NFC_ID
DeviceSensor.ACCESS_TORCH -> RUNNING_SENSOR_TORCH_ID
}
}

Expand All @@ -61,6 +67,7 @@ object ConstantsManager {
DeviceSensor.ACCESS_SCREEN_USAGE -> RUNNING_SENSOR_SCREEN_USAGE
DeviceSensor.ACCESS_GPS -> RUNNING_SENSOR_GPS
DeviceSensor.ACCESS_NFC -> RUNNING_SENSOR_NFC
DeviceSensor.ACCESS_TORCH -> RUNNING_SENSOR_TORCH
}
}

Expand All @@ -71,6 +78,7 @@ object ConstantsManager {
DeviceSensor.ACCESS_SCREEN_USAGE -> CONSENT_OF_SCREEN_USAGE
DeviceSensor.ACCESS_GPS -> CONSENT_OF_GPS
DeviceSensor.ACCESS_NFC -> CONSENT_OF_NFC
DeviceSensor.ACCESS_TORCH -> CONSENT_OF_TORCH
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ object DataCollectionManager {
DeviceSensor.ACCESS_SCREEN_USAGE -> startScreenUsageCollection()
DeviceSensor.ACCESS_GPS -> startGpsCollection(activity)
DeviceSensor.ACCESS_NFC -> startNfcCollection(activity)
DeviceSensor.ACCESS_TORCH -> startTorchCollection(activity)
}

SharedPreferencesManager.write(C.getRunningSensorKey(sensor), true)
Expand Down Expand Up @@ -78,6 +79,14 @@ object DataCollectionManager {
}
}

private fun startTorchCollection(activity: Activity) {
if (SettingsManager.isTorchEnabled(activity)) {
createNewSensorEntry(DeviceSensor.ACCESS_TORCH)
} else {
SettingsManager.askForSensor(activity, DeviceSensor.ACCESS_TORCH)
}
}

private fun createNewSensorEntry(forSensor: DeviceSensor) {
if (RealmManager.queryForOpenEntryInSensor(forSensor)) return // Due to multiple broadcast wake

Expand Down
Loading

0 comments on commit 6cd52fb

Please sign in to comment.