Skip to content
Open
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 @@ -46,6 +46,7 @@ android {
buildFeatures {
dataBinding true
}

buildTypes {

release {
Expand Down
8 changes: 2 additions & 6 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"
android:usesPermissionFlags="neverForLocation"
tools:targetApi="s"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"
android:usesPermissionFlags="neverForLocation"
tools:targetApi="s" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>
Expand Down
11 changes: 2 additions & 9 deletions app/src/main/java/Constants.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
/**
* Device Types (internal unique strings used to classify which device was used for a given experiment/scan)
*/
const val DEVICE_TYPE_LS1 = "LinkSquare"

const val DEVICE_TYPE_NIR = "LinkSquareNIR"

const val DEVICE_TYPE_NANO = "InnoSpectra Nano"

const val packageId = "org.phenoapps.prospector"

/***
Expand Down Expand Up @@ -46,6 +37,8 @@ const val DEVICE_LINK_SQUARE = "$packageId.DEVICE_TYPE_LINK_SQUARE"

const val DEVICE_INNO_SPECTRA = "$packageId.DEVICE_TYPE_INNO_SPECTRA"

const val DEVICE_INDIGO = "$packageId.DEVICE_TYPE_INDIGO"

const val EXPORT_TYPE = "$packageId.EXPORT_TYPE"

const val DEVICE_INFO = "$packageId.DEVICE_INFO"
Expand Down
147 changes: 115 additions & 32 deletions app/src/main/java/org/phenoapps/prospector/activities/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,18 @@ import ALPHA_DESC
import BULB_FRAMES
import DATE_ASC
import DATE_DESC
import DEVICE_TYPE_LS1
import DEVICE_TYPE_NANO
import DEVICE_TYPE_NIR
import FIRST_CONNECT_ERROR_ON_LOAD
import LED_FRAMES
import android.Manifest
import android.app.Activity
import android.bluetooth.BluetoothAdapter
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.location.LocationManager
import android.net.Uri
import android.os.*
import android.provider.Settings
import android.util.Log
import android.widget.ListAdapter
import android.widget.ListView
import android.view.View
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import androidx.appcompat.app.AlertDialog
Expand All @@ -35,6 +28,11 @@ import androidx.navigation.Navigation
import androidx.preference.PreferenceManager
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.*
import org.phenoapps.interfaces.spectrometers.Spectrometer
import org.phenoapps.interfaces.spectrometers.Spectrometer.Companion.DEVICE_TYPE_INDIGO
import org.phenoapps.interfaces.spectrometers.Spectrometer.Companion.DEVICE_TYPE_LS1
import org.phenoapps.interfaces.spectrometers.Spectrometer.Companion.DEVICE_TYPE_NANO
import org.phenoapps.interfaces.spectrometers.Spectrometer.Companion.DEVICE_TYPE_NIR
import org.phenoapps.prospector.BuildConfig
import org.phenoapps.prospector.NavigationRootDirections
import org.phenoapps.prospector.R
Expand All @@ -45,8 +43,9 @@ import org.phenoapps.prospector.data.viewmodels.MainActivityViewModel
import org.phenoapps.prospector.data.viewmodels.devices.InnoSpectraViewModel
import org.phenoapps.prospector.data.viewmodels.devices.LinkSquareViewModel
import org.phenoapps.prospector.databinding.ActivityMainBinding
import org.phenoapps.prospector.interfaces.Spectrometer
import org.phenoapps.prospector.utils.*
import org.phenoapps.security.Security
import org.phenoapps.viewmodels.spectrometers.Indigo
import org.phenoapps.utils.IntentUtil
import java.io.File

Expand Down Expand Up @@ -74,6 +73,8 @@ class MainActivity : AppCompatActivity(), CoroutineScope by MainScope() {

private val sViewModel: MainActivityViewModel by viewModels()

val advisor by Security().secureBluetoothActivity()

/**
* This activity view model is used throughout all the fragments to update connection status.
*/
Expand Down Expand Up @@ -135,13 +136,22 @@ class MainActivity : AppCompatActivity(), CoroutineScope by MainScope() {
}

val maker = mPrefs.getString(mKeyUtil.deviceMaker, DEVICE_TYPE_LS1)
sDeviceViewModel = if (maker == DEVICE_TYPE_LS1) {
sDeviceViewModel = when (maker) {
in setOf(DEVICE_TYPE_LS1, DEVICE_TYPE_NIR) -> {

LinkSquareViewModel()
LinkSquareViewModel()

} else {
}
DEVICE_TYPE_NANO -> {

InnoSpectraViewModel()
InnoSpectraViewModel()

}
else -> {

Indigo()

}
}

mBinding = DataBindingUtil.setContentView(this@MainActivity, R.layout.activity_main)
Expand Down Expand Up @@ -505,25 +515,50 @@ class MainActivity : AppCompatActivity(), CoroutineScope by MainScope() {

fun switchInnoSpectra() {

stopDeviceConnection()
val current = mPrefs.getString(mKeyUtil.deviceMaker, null)

mPrefs.edit().putString(mKeyUtil.deviceMaker, DEVICE_TYPE_NANO).apply()
if (current != DEVICE_TYPE_NANO) {

sDeviceViewModel = InnoSpectraViewModel()
stopDeviceConnection()

runtimeBluetoothCheck()
mPrefs.edit().putString(mKeyUtil.deviceMaker, DEVICE_TYPE_NANO).apply()

sDeviceViewModel = InnoSpectraViewModel()

runtimeBluetoothCheck()
}
}

fun switchLinkSquare() {

stopDeviceConnection()
val current = mPrefs.getString(mKeyUtil.deviceMaker, null)

mPrefs.edit().putString(mKeyUtil.deviceMaker, DEVICE_TYPE_LS1).apply()
if (current !in setOf(DEVICE_TYPE_LS1, DEVICE_TYPE_NIR)) {

sDeviceViewModel = LinkSquareViewModel()
stopDeviceConnection()

startDeviceConnection()
mPrefs.edit().putString(mKeyUtil.deviceMaker, DEVICE_TYPE_LS1).apply()

sDeviceViewModel = LinkSquareViewModel()

startDeviceConnection()
}
}

fun switchIndigo() {

val current = mPrefs.getString(mKeyUtil.deviceMaker, null)

if (current != DEVICE_TYPE_INDIGO) {

stopDeviceConnection()

mPrefs.edit().putString(mKeyUtil.deviceMaker, DEVICE_TYPE_INDIGO).apply()

sDeviceViewModel = Indigo()

startDeviceConnection()
}
}

private fun runtimeBluetoothCheck() {
Expand Down Expand Up @@ -736,23 +771,55 @@ class MainActivity : AppCompatActivity(), CoroutineScope by MainScope() {
}

fun startDeviceConnection() {
launch {
withContext(Dispatchers.IO) {
sDeviceViewModel?.connect(this@MainActivity.applicationContext)

val maker = mPrefs.getString(mKeyUtil.deviceMaker, DEVICE_TYPE_LS1)
sDeviceViewModel = when (maker) {
in setOf(DEVICE_TYPE_LS1, DEVICE_TYPE_NIR) -> {

LinkSquareViewModel()

}
DEVICE_TYPE_NANO -> {

InnoSpectraViewModel()

}
else -> {

Indigo()

}
}
}

private fun stopDeviceConnection() {
launch {
withContext(Dispatchers.IO) {
if (sDeviceViewModel?.isConnected() == true)
sDeviceViewModel?.disconnect(this@MainActivity)
if (sDeviceViewModel?.isConnected() != true) {

if (sDeviceViewModel is Indigo) {

(sDeviceViewModel as? Indigo)?.let { indigo ->
advisor.withNearby { adapter ->
launch {
withContext(Dispatchers.IO) {
indigo.connect(adapter, this@MainActivity)
}
}
}
}

} else {
launch {
withContext(Dispatchers.IO) {
sDeviceViewModel?.connect(this@MainActivity.applicationContext)
}
}
}
}
}

private fun stopDeviceConnection() {
if (sDeviceViewModel?.isConnected() == true)
sDeviceViewModel?.forceDisconnect()
}

override fun onDestroy() {

stopDeviceConnection()
Expand All @@ -769,15 +836,27 @@ class MainActivity : AppCompatActivity(), CoroutineScope by MainScope() {

mConnectionHandlerThread.quit()

cancel()

super.onDestroy()

}

override fun onPause() {

launch {
withContext(Dispatchers.IO) {
sDeviceViewModel?.reset(this@MainActivity)
if (sDeviceViewModel is Indigo) {
(sDeviceViewModel as? Indigo)?.let { indigo ->
advisor.withNearby { adapter ->

indigo.reset(adapter, this)

}
}
} else {
launch {
withContext(Dispatchers.IO) {
sDeviceViewModel?.reset(this@MainActivity)
}
}
}

Expand Down Expand Up @@ -884,4 +963,8 @@ class MainActivity : AppCompatActivity(), CoroutineScope by MainScope() {
private fun updateLastOpenedTime() {
mPrefs.edit().putLong(mKeyUtil.lastTimeAppOpened, System.nanoTime()).apply()
}

init {
advisor.initialize()
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.phenoapps.prospector.data.models

import DEVICE_TYPE_NIR
import androidx.annotation.Keep
import androidx.room.*
import org.phenoapps.interfaces.spectrometers.Spectrometer.Companion.DEVICE_TYPE_NIR
import org.phenoapps.prospector.utils.DateUtil

@Keep
Expand Down
Loading