diff --git a/app/src/main/java/com/example/hpostesting/presentation/dashboard/HomeFragment.kt b/app/src/main/java/com/example/hpostesting/presentation/dashboard/HomeFragment.kt index fb1906a..df77e47 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/dashboard/HomeFragment.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/dashboard/HomeFragment.kt @@ -7,6 +7,7 @@ import android.content.DialogInterface import android.content.Intent import android.content.SharedPreferences import android.os.BatteryManager +import android.os.Build import android.os.Bundle import android.util.Base64 import android.util.Log @@ -14,6 +15,7 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.Toast +import androidx.annotation.RequiresApi import androidx.fragment.app.Fragment import androidx.fragment.app.activityViewModels import androidx.navigation.fragment.findNavController @@ -88,6 +90,7 @@ class HomeFragment : Fragment() { return binding.root } + @RequiresApi(Build.VERSION_CODES.P) override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) @@ -135,6 +138,7 @@ class HomeFragment : Fragment() { checkForTokenAndUpdate() } + // Now re-subscribe to allUserData hemoCubeViewModel.allUserData.observe(viewLifecycleOwner) { originalUserDataList -> Log.d("LOCAL_DB OBSERVE", "OBSERVE CALLED") @@ -186,7 +190,7 @@ class HomeFragment : Fragment() { } // Upload results after processing all userData to avoid duplicates and ensure all modifications are done if (resultList.results?.isNotEmpty() == true) { - hemoCubeViewModel.uploadResultfornew(resultList) + hemoCubeViewModel.uploadResult(resultList) } } @@ -262,13 +266,16 @@ class HomeFragment : Fragment() { } + @RequiresApi(Build.VERSION_CODES.P) private fun checkForTokenAndUpdate() { var accessToken = sharedPreference.getString(Constants.ACCESS_TOKEN, "").toString() var password = sharedPreference.getString(Constants.DEVICE_PASSWORD_API, "").toString() var userID = sharedPreference.getString(Constants.DEVICE_ID_API, "").toString() deviceId = sharedPreference.getString(Constants.DEVICE_ID, "").toString() if(userID.isNotEmpty() && password.isNotEmpty()) { + Log.d("istoken",isTokenAvailable.toString()) if (!isTokenAvailable) { + Log.d("istoken1",isTokenAvailable.toString()) if(isTokenExpired(accessToken)) { hemoCubeViewModel.login(createLoginRequestData(userID, password)) } @@ -279,6 +286,14 @@ class HomeFragment : Fragment() { } } else if (userID.isEmpty() && password.isEmpty() && deviceId.isNotEmpty()) { fetchDeviceCredentials() + var accessToken = sharedPreference.getString(Constants.ACCESS_TOKEN, "").toString() + var userID = sharedPreference.getString("username", "").toString() + var password = sharedPreference.getString("password", "").toString() + if(accessToken.isNotEmpty()){ + isTokenAvailable = true + hemoCubeViewModel.startPeriodicCheckUpdate() + hemoCubeViewModel.checkUpdate(createCheckUpdateRequestData()) + } } else { Toast.makeText( requireContext(), @@ -293,6 +308,7 @@ class HomeFragment : Fragment() { updateTokens(response) response.data.data?.accessToken isTokenAvailable = true + Log.d("istoken2",isTokenAvailable.toString()) } is Result.Error -> { @@ -349,9 +365,30 @@ class HomeFragment : Fragment() { is Result.Success -> { val updatedversion = response.data.data?.version.toString() val currentversion = - context?.let { context?.packageManager!!.getPackageInfo(it.packageName, 0) } + context?.let { ctx -> + val packageInfo = ctx.packageManager.getPackageInfo(ctx.packageName, 0) + val versionName = packageInfo.versionName + val versionCode: Long = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + // From Android P (API level 28), versionCode is deprecated and you should use longVersionCode instead. + packageInfo.longVersionCode + } else { + // For older Android versions, use versionCode (cast it to Long for consistency). + packageInfo.versionCode.toLong() + } + + // Use versionName and versionCode as needed + Log.d("AppInfo", "Version Name: $versionName, Version Code: $versionCode") + } + Log.d("versionnow",currentversion.toString()) + Log.d("versionnow",updatedversion.toString()) if(updatedversion > currentversion.toString()){ hemoCubeViewModel.deviceUpdate(createDeviceUpdateRequestData()) + Toast.makeText( + activity, + "new version ${response.data.data?.version} Available", + Toast.LENGTH_LONG + ) + .show() }else{ Toast.makeText( activity, @@ -360,24 +397,17 @@ class HomeFragment : Fragment() { ) .show() } - - Toast.makeText( - activity, - "new version ${response.data.data?.version} Available", - Toast.LENGTH_LONG - ) - .show() } is Result.Error -> { - response.exception.let { message -> -// Toast.makeText( -// activity, -// "$message", -// Toast.LENGTH_LONG -// ) -// .show() - } +// response.exception.let { message -> +//// Toast.makeText( +//// activity, +//// "$message", +//// Toast.LENGTH_LONG +//// ) +//// .show() +// } } is Result.Loading -> { @@ -459,14 +489,18 @@ class HomeFragment : Fragment() { private fun isTokenExpired(token: String): Boolean { - val parts = token.split("\\.".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() - val decodedPayload = String(Base64.decode(parts[1], Base64.DEFAULT)) - val jsonPayload = JSONObject(decodedPayload) + if(token.isNotEmpty()) { + val parts = token.split("\\.".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray() + val decodedPayload = String(Base64.decode(parts[1], Base64.DEFAULT)) + val jsonPayload = JSONObject(decodedPayload) - val exp = jsonPayload.optLong("exp", 0) - val currentTimeSeconds = System.currentTimeMillis() / 1000 + val exp = jsonPayload.optLong("exp", 0) + val currentTimeSeconds = System.currentTimeMillis() / 1000 - return exp <= currentTimeSeconds + return exp <= currentTimeSeconds + }else{ + return false + } } private fun updateTokens(response: Result.Success) { @@ -479,6 +513,7 @@ class HomeFragment : Fragment() { apply() } isTokenAvailable = true + Log.d("istoken3",isTokenAvailable.toString()) } private fun createLoginRequestData(userID: String, password: String): LoginRequest { @@ -620,7 +655,11 @@ class HomeFragment : Fragment() { putString(Constants.NATS_TOKEN, natsToken) apply() } - hemoCubeViewModel.login(createLoginRequestData(username, password)) + if (!isTokenAvailable ) { + Log.d("istoken1", isTokenAvailable.toString()) + hemoCubeViewModel.login(createLoginRequestData(username, password)) + } + } ?: Log.e("fetchDeviceCredentials", "Failed to parse device data.") } else { Log.e("fetchDeviceCredentials", "Document does not exist.") diff --git a/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemocubeActivity.kt b/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemocubeActivity.kt index aff99fc..64f14ea 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemocubeActivity.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/hemocube/HemocubeActivity.kt @@ -131,29 +131,24 @@ open class HemocubeActivity : AppCompatActivity() { } @RequiresApi(Build.VERSION_CODES.O) - @SuppressLint("UnspecifiedImmutableFlag") + @SuppressLint("MutableImplicitPendingIntent") private fun requestUserPermission(manager: UsbManager, device: UsbDevice) { val mPendingIntent: PendingIntent - val pendingIntentFlags = when { - Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> { - PendingIntent.FLAG_IMMUTABLE - } - else -> { + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) { + mPendingIntent = PendingIntent.getBroadcast( + this, 0, Intent(Constants.HEMOCUBE_USB_PERMISSION), PendingIntent.FLAG_MUTABLE + ) + } else { + mPendingIntent = PendingIntent.getBroadcast( + this, + 0, + Intent(Constants.HEMOCUBE_USB_PERMISSION), PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE - } + ) } - mPendingIntent = PendingIntent.getBroadcast( - this, - 0, - Intent(Constants.HEMOCUBE_USB_PERMISSION), - pendingIntentFlags - ) - val filter = IntentFilter(Constants.HEMOCUBE_USB_PERMISSION) registerReceiver(broadcastReceiver, filter, RECEIVER_EXPORTED) - - // Request permission manager.requestPermission(device, mPendingIntent) }