diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 8ae1d72..17c82e8 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -64,6 +64,14 @@ android:name=".activities.GroupActivity" android:exported="false" android:theme="@style/Theme.BarcodeScannerForEmployees"/> + + { "" } } } + +fun parseArray(input: String): IntArray { + return input.trim('[', ']').split(",").map { it.trim().toInt() }.toIntArray() +} \ 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 704e755..944ac3c 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/GroupActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/GroupActivity.kt @@ -40,14 +40,43 @@ class GroupActivity : AppCompatActivity() { val response = n.createGroup(groupName, groupPassword) val responseText = response.body!!.string() - Toast.makeText(this, responseText, Toast.LENGTH_LONG).show() if (response.code == 200) { - Toast.makeText(this, responseText, Toast.LENGTH_LONG).show() - sharedPreferences.edit().putStringSet("groups", setOf(responseText)) + 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() + } + } + + binding.joinGroupButton.setOnClickListener { + 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 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", responseText).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 e2292a9..d6d8b9d 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,8 @@ 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.parseArray +import org.json.JSONObject class LoginActivity : AppCompatActivity() { @@ -49,8 +51,17 @@ class LoginActivity : AppCompatActivity() { if (response.code != 200) { Toast.makeText(this, responseText, Toast.LENGTH_SHORT).show() } else { - sharedPreferences.edit().putString("token", responseText).apply() - sharedPreferences.edit().putString("servre", server).apply() + 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 r = n.getMyGroups().body!!.string() + val myGroups = parseArray(r) + + sharedPreferences.edit().putStringSet("groups", myGroups.map { a -> a.toString()}.toSet()) val intent = Intent(this, MainActivity::class.java) startActivity(intent) finish() @@ -73,8 +84,9 @@ class LoginActivity : AppCompatActivity() { if (response.code != 200) { Toast.makeText(this, responseText, Toast.LENGTH_SHORT).show(); } else { - val token = n.login(username, password) - sharedPreferences.edit().putString("token", token.body!!.string()).apply() + sharedPreferences.edit().putInt("userId", responseText.toInt()).apply() + val token = JSONObject(n.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) 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 199751b..ac4978b 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/MainActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/MainActivity.kt @@ -118,7 +118,7 @@ class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelecte intent = Intent(this, AccountSettingsActivity::class.java) } R.id.nav_groups -> { - intent = Intent(this, AccountSettingsActivity::class.java) + intent = Intent(this, MyGroupsActivity::class.java) } R.id.nav_settings -> { //TODO: Settings diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/ManageGroupActivity.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/ManageGroupActivity.kt new file mode 100644 index 0000000..4cfe4e5 --- /dev/null +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/ManageGroupActivity.kt @@ -0,0 +1,78 @@ +package org.foxarmy.barcodescannerforemployees.activities + +import android.content.Context +import android.os.Bundle +import android.view.View +import androidx.appcompat.app.AppCompatActivity +import androidx.security.crypto.EncryptedSharedPreferences +import androidx.security.crypto.MasterKeys +import org.foxarmy.barcodescannerforemployees.Net +import org.foxarmy.barcodescannerforemployees.databinding.ActivityManageGroupBinding +import org.foxarmy.barcodescannerforemployees.parseArray +import org.foxarmy.barcodescannerforemployees.views.GroupMemberView + +class ManageGroupActivity : AppCompatActivity(){ + private lateinit var binding: ActivityManageGroupBinding + private var isAdmin: Boolean = true + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + binding = ActivityManageGroupBinding.inflate(layoutInflater) + setContentView(binding.root) + + val groupId = intent.extras!!.getInt("groupId") + + val masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC) + + val sharedPreferences = EncryptedSharedPreferences.create( + "sensitive", + masterKeyAlias, + applicationContext, + EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, + EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM + ) + + val net = Net() + + net.token = sharedPreferences.getString("token", "")!! + net.server = sharedPreferences.getString("server", "")!! + net.language = sharedPreferences.getString("language", "en-US")!! + + + val users = parseArray(net.getUsersInGroup(groupId).body!!.string()) + + for (user in users) { + val groupMemberView = GroupMemberView(this, this as Context, net.getUsernameById(user).body!!.string(), user) + + binding.groupsContent.addView(groupMemberView) + } + + isAdmin = amIAnAdminIn(groupId) + + if (!isAdmin) { + binding.renameButton.visibility = View.GONE + } + } + + fun amIAnAdminIn(groupId: Int): Boolean { + val masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC) + + val sharedPreferences = EncryptedSharedPreferences.create( + "sensitive", + masterKeyAlias, + applicationContext, + EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, + EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM + ) + + val net = Net() + + net.token = sharedPreferences.getString("token", "")!! + net.server = sharedPreferences.getString("server", "")!! + net.language = sharedPreferences.getString("language", "en-US")!! + + val result = sharedPreferences.getInt("userId", 0) == net.getGroupAdminId(groupId).body!!.string().toInt() + return result + } +} \ No newline at end of file diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/MyGroupsActivity.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/MyGroupsActivity.kt new file mode 100644 index 0000000..f729a6f --- /dev/null +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/MyGroupsActivity.kt @@ -0,0 +1,57 @@ +package org.foxarmy.barcodescannerforemployees.activities + +import android.content.Context +import android.content.Intent +import android.os.Bundle +import android.widget.LinearLayout +import androidx.appcompat.app.AppCompatActivity +import androidx.security.crypto.EncryptedSharedPreferences +import androidx.security.crypto.MasterKeys +import org.foxarmy.barcodescannerforemployees.Net +import org.foxarmy.barcodescannerforemployees.R +import org.foxarmy.barcodescannerforemployees.databinding.ActivityMyGroupsBinding +import org.foxarmy.barcodescannerforemployees.parseArray +import org.foxarmy.barcodescannerforemployees.views.GroupView + +class MyGroupsActivity : AppCompatActivity(){ + private lateinit var binding: ActivityMyGroupsBinding + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + binding = ActivityMyGroupsBinding.inflate(layoutInflater) + setContentView(binding.root) + + val masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC) + + val sharedPreferences = EncryptedSharedPreferences.create( + "sensitive", + masterKeyAlias, + applicationContext, + EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, + EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM + ) + + val net = Net() + + net.token = sharedPreferences.getString("token", "")!! + net.server = sharedPreferences.getString("server", "")!! + net.language = sharedPreferences.getString("language", "en-US")!! + + val groups = parseArray(net.getMyGroups().body!!.string()) + + val container = findViewById(R.id.groupsLayout) + + for (group in groups) { + val groupView = GroupView(this, this as Context, net.getGroupName(group), group) + + + container.addView(groupView) + } + + binding.newGroup.setOnClickListener { + val intent = Intent(this, GroupActivity::class.java) + startActivity(intent) + } + } +} \ No newline at end of file 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 a3467fb..998c212 100644 --- a/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/NavigatorActivity.kt +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/activities/NavigatorActivity.kt @@ -3,6 +3,7 @@ package org.foxarmy.barcodescannerforemployees.activities import android.app.Activity import android.content.Intent import android.os.Bundle +import android.util.Log import androidx.security.crypto.EncryptedSharedPreferences import androidx.security.crypto.MasterKeys @@ -22,7 +23,7 @@ class NavigatorActivity : Activity() { startActivity(intent); -// finish(); + finish(); } private fun isAuthenticated(): Boolean { @@ -52,6 +53,7 @@ class NavigatorActivity : Activity() { ) val groups = sharedPreferences.getStringSet("groups", emptySet()) + Log.d("QWERTYUIOP", groups.toString()) return groups!!.isNotEmpty() } } \ No newline at end of file diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/GroupMemberView.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/GroupMemberView.kt new file mode 100644 index 0000000..42aed1f --- /dev/null +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/GroupMemberView.kt @@ -0,0 +1,25 @@ +package org.foxarmy.barcodescannerforemployees.views + +import android.app.Activity +import android.content.Context +import android.view.LayoutInflater +import android.widget.LinearLayout +import android.widget.TextView +import org.foxarmy.barcodescannerforemployees.R + +class GroupMemberView : LinearLayout { + + private var username: String + private var userId: Int + + constructor(activity: Activity, context: Context, username: String, userId: Int) : super(context) { + + this.username = username + this.userId = userId + + val inflater: LayoutInflater = activity.layoutInflater + inflater.inflate(R.layout.group_member_view, this) + + findViewById(R.id.userNameTextView).text = username + } +} \ No newline at end of file diff --git a/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/GroupView.kt b/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/GroupView.kt new file mode 100644 index 0000000..37e4893 --- /dev/null +++ b/app/src/main/java/org/foxarmy/barcodescannerforemployees/views/GroupView.kt @@ -0,0 +1,37 @@ +package org.foxarmy.barcodescannerforemployees.views + +import android.app.Activity +import android.content.Context +import android.content.Intent +import android.os.Bundle +import android.view.LayoutInflater +import android.widget.LinearLayout +import android.widget.TextView +import androidx.core.content.ContextCompat +import org.foxarmy.barcodescannerforemployees.R +import org.foxarmy.barcodescannerforemployees.activities.ManageGroupActivity + +class GroupView : LinearLayout { + private var name: String + private var id: Int + + constructor(activity: Activity, context: Context, name: String, id: Int) : super(context) { + + this.name = name + this.id = id + + val inflater: LayoutInflater = activity.layoutInflater + inflater.inflate(R.layout.group_view, this) + + findViewById(R.id.nameTextView).text = name + + setOnClickListener { + val intent = Intent(activity, ManageGroupActivity::class.java) + val extras = Bundle() + extras.putInt("groupId", id) + intent.putExtras(extras) + ContextCompat.startActivity(context, intent, extras) + } + } + +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_manage_group.xml b/app/src/main/res/layout/activity_manage_group.xml new file mode 100644 index 0000000..1c4fec9 --- /dev/null +++ b/app/src/main/res/layout/activity_manage_group.xml @@ -0,0 +1,37 @@ + + + +