diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 3064fa7..93abe31 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -13,6 +13,7 @@ + matrix.postRotate(90F) ExifInterface.ORIENTATION_ROTATE_180 -> matrix.postRotate(180F) ExifInterface.ORIENTATION_ROTATE_270 -> matrix.postRotate(270F) } val rotated = Bitmap.createBitmap(img, 0, 0, img.width, img.height, matrix, true) - val scaled = Bitmap.createScaledBitmap(rotated, rotated.width/4, rotated.height/4, true) + val scaled = Bitmap.createScaledBitmap(rotated, rotated.width / 4, rotated.height / 4, true) scaled.compress(Bitmap.CompressFormat.WEBP_LOSSY, 25, FileOutputStream(thumbnailFile)) } @@ -64,8 +74,11 @@ fun String.md5(): String { return digest.toHexString() } -fun stripNetWeight (netWeight: String): Double { - return removeSubstringsFromString(netWeight, arrayOf("Л", "л", "мл", "Мл", "г", "Г", "кг", "Кг", "шт", "Шт", ",", " ", ".")).toDouble() +fun stripNetWeight(netWeight: String): Double { + return removeSubstringsFromString( + netWeight, + arrayOf("Л", "л", "мл", "Мл", "г", "Г", "кг", "Кг", "шт", "Шт", ",", " ", ".") + ).toDouble() } fun removeSubstringsFromString(text: String, toRemove: Array): String { @@ -99,14 +112,31 @@ fun calculateProductFreshness(dateOfProduction: Long, dateOfExpiry: Long): Doubl return lifeSpanLeft / productLifeSpan.toDouble() } -fun getUnitNameById (context: Context, id: Int): String { - return when(id) { - 0 -> { context.getString(R.string.kilogram) } - 1 -> { context.getString(R.string.gram) } - 2 -> { context.getString(R.string.liter) } - 3 -> { context.getString(R.string.milliliter) } - 4 -> { context.getString(R.string.pieces) } - else -> { "" } +fun getUnitNameById(context: Context, id: Int): String { + return when (id) { + 0 -> { + context.getString(R.string.kilogram) + } + + 1 -> { + context.getString(R.string.gram) + } + + 2 -> { + context.getString(R.string.liter) + } + + 3 -> { + context.getString(R.string.milliliter) + } + + 4 -> { + context.getString(R.string.pieces) + } + + else -> { + "" + } } } @@ -137,4 +167,47 @@ fun bytesToHex(bytes: ByteArray): String { hexString.append(hex) } return hexString.toString() +} + +fun isInternetConnectionAvailable(context: Context): Boolean { + if (context.getSystemService(Context.CONNECTIVITY_SERVICE) == null) return false + val connectivityManager = + context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager + if (connectivityManager != null) { + val capabilities = + connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork) + if (capabilities != null) { + if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) { + return true + } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { + return true + } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) { + return true + } + } + } + return false +} + +fun noInternetConnectionAvailableNotification(context: Context) { + AlertDialog.Builder(context) + .setMessage(context.getString(R.string.no_internet_connection)) + .setPositiveButton(R.string.quit) { _, _ -> + exitProcess(0) + } + .setNeutralButton(R.string.logout) { _, _ -> + val sharedPreferences = EncryptedSharedPreferences.create( + "sensitive", + MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC), + context, + EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, + EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM + ) + + sharedPreferences.edit().putString("token", "").apply() + sharedPreferences.edit().putString("server", "").apply() + sharedPreferences.edit().putStringSet("groups", emptySet()).apply() + sharedPreferences.edit().putString("currentGroup", "").apply() + exitProcess(0) + }.show() } \ No newline at end of file diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/GroupActivity.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/GroupActivity.kt index 39c152a..4dbc9dc 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/GroupActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/GroupActivity.kt @@ -8,6 +8,7 @@ import androidx.security.crypto.EncryptedSharedPreferences import androidx.security.crypto.MasterKeys import org.foxarmy.barcodescannerforemployees.Net import org.foxarmy.barcodescannerforemployees.databinding.ActivityGroupBinding +import org.foxarmy.barcodescannerforemployees.noInternetConnectionAvailableNotification class GroupActivity : AppCompatActivity() { @@ -31,24 +32,28 @@ class GroupActivity : AppCompatActivity() { val groupName = binding.groupNameTextEdit.text.toString() val groupPassword = binding.groupPasswordTextEdit.text.toString() // group is set to "successful" - val n = Net() - n.language = sharedPreferences.getString("language", "en-US")!! - n.server = sharedPreferences.getString("server", "")!! - n.token = sharedPreferences.getString("token", "")!! + val net = Net() + net.language = sharedPreferences.getString("language", "en-US")!! + net.server = sharedPreferences.getString("server", "")!! + net.token = sharedPreferences.getString("token", "")!! - val response = n.createGroup(groupName, groupPassword) - val responseText = response.body!!.string() - - if (response.code == 200) { - val currentGroups = sharedPreferences.getStringSet("groups", mutableSetOf()) - currentGroups!!.add(responseText) - sharedPreferences.edit().putStringSet("groups", currentGroups).apply() - sharedPreferences.edit().putString("currentGroup", responseText).apply() - val intent = Intent(this, MainActivity::class.java) - startActivity(intent) - finish() + if (!net.serverIsAvailable(this)) { + noInternetConnectionAvailableNotification(this) } else { - Toast.makeText(this, responseText, Toast.LENGTH_LONG).show() + val response = net.createGroup(groupName, groupPassword) + val responseText = response.body!!.string() + + if (response.code == 200) { + val currentGroups = sharedPreferences.getStringSet("groups", mutableSetOf()) + currentGroups!!.add(responseText) + sharedPreferences.edit().putStringSet("groups", currentGroups).apply() + sharedPreferences.edit().putString("currentGroup", responseText).apply() + val intent = Intent(this, MainActivity::class.java) + startActivity(intent) + finish() + } else { + Toast.makeText(this, responseText, Toast.LENGTH_LONG).show() + } } } @@ -56,25 +61,29 @@ class GroupActivity : AppCompatActivity() { val groupName = binding.groupNameTextEdit.text.toString() val groupPassword = binding.groupPasswordTextEdit.text.toString() - val n = Net() - n.language = sharedPreferences.getString("language", "en-US")!! - n.server = sharedPreferences.getString("server", "")!! - n.token = sharedPreferences.getString("token", "")!! + val net = Net() + net.language = sharedPreferences.getString("language", "en-US")!! + net.server = sharedPreferences.getString("server", "")!! + net.token = sharedPreferences.getString("token", "")!! - val groupId = n.getGroupId(groupName).toInt() - val response = n.joinGroup(groupId, groupPassword) - val responseText = response.body!!.string() - - if (response.code == 200) { - val currentGroups = sharedPreferences.getStringSet("groups", mutableSetOf()) - currentGroups!!.add(groupId.toString()) - sharedPreferences.edit().putStringSet("groups", currentGroups).apply() - sharedPreferences.edit().putString("currentGroup", groupId.toString()).apply() - val intent = Intent(this, MainActivity::class.java) - startActivity(intent) - finish() + if (!net.serverIsAvailable(this)) { + noInternetConnectionAvailableNotification(this) } else { - Toast.makeText(this, responseText, Toast.LENGTH_LONG).show() + val groupId = net.getGroupId(groupName).toInt() + val response = net.joinGroup(groupId, groupPassword) + val responseText = response.body!!.string() + + if (response.code == 200) { + val currentGroups = sharedPreferences.getStringSet("groups", mutableSetOf()) + currentGroups!!.add(groupId.toString()) + sharedPreferences.edit().putStringSet("groups", currentGroups).apply() + sharedPreferences.edit().putString("currentGroup", groupId.toString()).apply() + val intent = Intent(this, MainActivity::class.java) + startActivity(intent) + finish() + } else { + Toast.makeText(this, responseText, Toast.LENGTH_LONG).show() + } } } } diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/LoginActivity.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/LoginActivity.kt index cb94492..aec68d3 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/LoginActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/LoginActivity.kt @@ -10,6 +10,7 @@ import androidx.security.crypto.MasterKeys import org.foxarmy.barcodescannerforemployees.Net import org.foxarmy.barcodescannerforemployees.R import org.foxarmy.barcodescannerforemployees.databinding.ActivityLoginBinding +import org.foxarmy.barcodescannerforemployees.noInternetConnectionAvailableNotification import org.foxarmy.barcodescannerforemployees.parseArray import org.json.JSONObject @@ -40,31 +41,35 @@ class LoginActivity : AppCompatActivity() { val language = resources.getStringArray(R.array.languages)[binding.languageSpinner.selectedItemPosition] sharedPreferences.edit().putString("language", language).apply() - val n = Net() - n.language = sharedPreferences.getString("language", "en-US")!! - n.server = server + val net = Net() + net.language = sharedPreferences.getString("language", "en-US")!! + net.server = server - val response = n.login(username, password) - val responseText = response.body!!.string() - if (response.code != 200) { - Toast.makeText(this, responseText, Toast.LENGTH_SHORT).show() + if (!net.serverIsAvailable(this)) { + noInternetConnectionAvailableNotification(this) } else { - val json = JSONObject(responseText) - sharedPreferences.edit().putString("token", json["token"].toString()).apply() - n.token = json["token"].toString() - sharedPreferences.edit().putInt("userId", json["id"].toString().toInt()).apply() - sharedPreferences.edit().putString("server", server).apply() + val response = net.login(username, password) + val responseText = response.body!!.string() + if (response.code != 200) { + Toast.makeText(this, responseText, Toast.LENGTH_SHORT).show() + } else { + val json = JSONObject(responseText) + sharedPreferences.edit().putString("token", json["token"].toString()).apply() + net.token = json["token"].toString() + sharedPreferences.edit().putInt("userId", json["id"].toString().toInt()).apply() + sharedPreferences.edit().putString("server", server).apply() - val r = n.getMyGroups().body!!.string() - val myGroups = parseArray(r).map { a -> a.toString()} + val r = net.getMyGroups().body!!.string() + val myGroups = parseArray(r).map { a -> a.toString()} - sharedPreferences.edit().putStringSet("groups", myGroups.toSet()).apply() - sharedPreferences.edit().putString("currentGroup", myGroups[0]).apply() + sharedPreferences.edit().putStringSet("groups", myGroups.toSet()).apply() + sharedPreferences.edit().putString("currentGroup", myGroups[0]).apply() - val intent = Intent(this, MainActivity::class.java) - startActivity(intent) - finish() + val intent = Intent(this, MainActivity::class.java) + startActivity(intent) + finish() + } } } @@ -76,22 +81,27 @@ class LoginActivity : AppCompatActivity() { sharedPreferences.edit().putString("language", language).apply() sharedPreferences.edit().putString("server", server).apply() - val n = Net() - n.language = language - n.server = server - val response = n.registerAccount(username, password); - val responseText = response.body!!.string() - if (response.code != 200) { - Toast.makeText(this, responseText, Toast.LENGTH_SHORT).show(); - } else { - sharedPreferences.edit().putInt("userId", responseText.toInt()).apply() - val token = JSONObject(n.login(username, password).body!!.string())["token"].toString() - sharedPreferences.edit().putString("token", token).apply() + val net = Net() + net.language = language + net.server = server - sharedPreferences.edit().putString("server", server).apply() - val intent = Intent(this, GroupActivity::class.java) - startActivity(intent) - finish() + if (!net.serverIsAvailable(this)) { + noInternetConnectionAvailableNotification(this) + } else { + val response = net.registerAccount(username, password); + val responseText = response.body!!.string() + if (response.code != 200) { + Toast.makeText(this, responseText, Toast.LENGTH_SHORT).show(); + } else { + sharedPreferences.edit().putInt("userId", responseText.toInt()).apply() + val token = JSONObject(net.login(username, password).body!!.string())["token"].toString() + sharedPreferences.edit().putString("token", token).apply() + + sharedPreferences.edit().putString("server", server).apply() + val intent = Intent(this, GroupActivity::class.java) + startActivity(intent) + finish() + } } } } diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/MainActivity.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/MainActivity.kt index a61a449..3f88bd1 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/MainActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/MainActivity.kt @@ -16,10 +16,7 @@ import androidx.security.crypto.EncryptedSharedPreferences import androidx.security.crypto.MasterKeys import androidx.viewpager.widget.ViewPager import com.google.android.material.navigation.NavigationView -import org.foxarmy.barcodescannerforemployees.Net -import org.foxarmy.barcodescannerforemployees.R -import org.foxarmy.barcodescannerforemployees.ViewPagerAdapter -import org.foxarmy.barcodescannerforemployees.convertToUnixEpochTimestamp +import org.foxarmy.barcodescannerforemployees.* import org.foxarmy.barcodescannerforemployees.database.AbstractProductDAO import org.foxarmy.barcodescannerforemployees.database.CategoryDAO import org.foxarmy.barcodescannerforemployees.database.DBStorageController @@ -60,9 +57,16 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte actionBarDrawerToggle!!.syncState() binding.navView.setNavigationItemSelectedListener(this) - // to make the Navigation drawer icon always appear on the action bar supportActionBar!!.setDisplayHomeAsUpEnabled(true) + val sharedPreferences = EncryptedSharedPreferences.create( + "sensitive", + MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC), + applicationContext, + EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, + EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM + ) + binding.expiryCalendarFab.setOnClickListener { _ -> val expiryCalendarIntent = Intent(this, ExpiryCalendarActivity::class.java) val extras = Bundle() @@ -78,8 +82,6 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte "StorageFragment" -> { val addAbstractProductIntent = Intent(this, AddAbstractProductActivity::class.java) val extras = Bundle() - // I reuse the same stuff for editing and adding new product. - // if abstractProduct == null, it means that we need to create new object extras.putParcelable("abstractProduct", null) extras.putString("action", "new") addAbstractProductIntent.putExtras(extras) @@ -104,7 +106,14 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte } } } - synchronize() + val net = Net() + net.server = sharedPreferences.getString("server", "")!! + + if (!net.serverIsAvailable(this)) { + noInternetConnectionAvailableNotification(this) + } else { + synchronize() + } } private fun synchronize() { diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/NavigatorActivity.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/NavigatorActivity.kt index c3088d1..37324a0 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/NavigatorActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/NavigatorActivity.kt @@ -31,8 +31,7 @@ class NavigatorActivity : Activity() { if (!isAuthenticated()) { intent = Intent(this, LoginActivity::class.java); } - - + startActivity(intent); finish(); } diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index e045b3d..37c53f9 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -114,6 +114,9 @@ Степень сжатия изображения Текущая группа Отмена + No internet connection available. Please, connect to a network. + Ok + Log out en-US ru-RU diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4eb50aa..37c9170 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -112,6 +112,9 @@ Image compression factor Current group Cancel + No internet connection available. Please, connect to a network. + Ok + Log out en-US ru-RU