Skip to content

Commit

Permalink
Merge pull request #4 from pk-218/dev
Browse files Browse the repository at this point in the history
Phase 1 changes
  • Loading branch information
pk-218 authored Mar 25, 2022
2 parents a6ee24f + f1e0d8b commit 96686c1
Show file tree
Hide file tree
Showing 20 changed files with 325 additions and 98 deletions.
2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ dependencies {
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
implementation 'androidx.navigation:navigation-fragment-ktx:2.4.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2'
implementation 'androidx.navigation:navigation-ui-ktx:2.4.1'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tech.kotlinx.knox">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
26 changes: 26 additions & 0 deletions app/src/main/java/tech/kotlinx/knox/ChatFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package tech.kotlinx.knox

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import tech.kotlinx.knox.adapter.MessageAdapter
import tech.kotlinx.knox.data.model.Datasource

class ChatFragment : Fragment() {

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {

val view = inflater.inflate(R.layout.fragment_chat, container, false)
val messages = Datasource().loadMessages()
val recyclerView=view.findViewById<RecyclerView>(R.id.message_view)
recyclerView.adapter= context?.let { MessageAdapter(it, messages) }

return view
}
}
96 changes: 96 additions & 0 deletions app/src/main/java/tech/kotlinx/knox/ConnectionDetailsFragment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package tech.kotlinx.knox

import android.content.Context
import android.content.Context.WIFI_SERVICE
import android.net.ConnectivityManager
import android.net.wifi.WifiManager
import android.os.Bundle
import android.text.format.Formatter
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import tech.kotlinx.knox.databinding.FragmentConnectionDetailsBinding
import java.net.NetworkInterface
import java.util.*


/**
* A simple [Fragment] subclass as the second destination in the navigation.
*/
class ConnectionDetailsFragment : Fragment() {

private var _binding: FragmentConnectionDetailsBinding? = null

// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentConnectionDetailsBinding.inflate(inflater, container, false)
return binding.root

}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

var userIpAddress: String? = "0.0.0.0"
val connectivityManager: ConnectivityManager = context?.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
@Suppress("DEPRECATION")
when (connectivityManager.activeNetworkInfo?.type) {
ConnectivityManager.TYPE_WIFI -> userIpAddress = getWifiIpAddress()
ConnectivityManager.TYPE_MOBILE -> userIpAddress = getMobileDataIpAddress()
}
binding.senderIpAddressField.setText(userIpAddress)

binding.enterChatButton.setOnClickListener {
findNavController().navigate(R.id.action_SecondFragment_to_FirstFragment)
}
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}

@Suppress("DEPRECATION")
private fun getWifiIpAddress(): String? {
val wifiMgr = context?.getSystemService(WIFI_SERVICE) as WifiManager?
val wifiInfo = wifiMgr!!.connectionInfo
val ip = wifiInfo.ipAddress
return Formatter.formatIpAddress(ip)
}

private fun getMobileDataIpAddress(): String {
try {
val interfaces = Collections.list(NetworkInterface.getNetworkInterfaces())
for (networkInterface in interfaces) {
val addresses = Collections.list(networkInterface.inetAddresses)
for (address in addresses) {
if (!address.isLoopbackAddress) {
val result = address.hostAddress!!
val isIPv4 = result.indexOf(':') < 0
return if (isIPv4)
result
else {
val delimiter: Int = result.indexOf('%')
if (delimiter < 0)
result.uppercase(Locale.getDefault())
else
result.substring(0, delimiter).uppercase(Locale.getDefault());
}
}
}
}
} catch (e: Exception) {
Log.d("MobileIPError", e.toString())
}
return "0.0.0.0"
}
}
44 changes: 0 additions & 44 deletions app/src/main/java/tech/kotlinx/knox/SecondFragment.kt

This file was deleted.

33 changes: 33 additions & 0 deletions app/src/main/java/tech/kotlinx/knox/adapter/messageAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package tech.kotlinx.knox.adapter
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import tech.kotlinx.knox.R
import tech.kotlinx.knox.data.model.Message

class MessageAdapter(
private val context: Context,
private val messageList:List<Message>
) : RecyclerView.Adapter<MessageAdapter.ItemViewHolder>() {

class ItemViewHolder(private val view: View) : RecyclerView.ViewHolder(view) {
val textView: TextView = view.findViewById(R.id.message_box)
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
val adapterLayout = LayoutInflater.from(parent.context)
.inflate(R.layout.message_box, parent, false)

return ItemViewHolder(adapterLayout)
}

override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
val item = messageList[position]
holder.textView.text = item.getMessage()
}

override fun getItemCount() = messageList.size
}
13 changes: 13 additions & 0 deletions app/src/main/java/tech/kotlinx/knox/data/model/Datasource.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package tech.kotlinx.knox.data.model

import java.util.*

class Datasource {
fun loadMessages():List<Message> {
return listOf<Message>(
Message("Hi",1, Date(2,2,2)),
Message("Hi",1, Date(2,2,2)),
Message("Hi",1, Date(2,2,2))
)
}
}
38 changes: 38 additions & 0 deletions app/src/main/java/tech/kotlinx/knox/ui/viewmodels/ChatViewModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package tech.kotlinx.knox.ui.viewmodels

import android.util.Log
import androidx.lifecycle.ViewModel
import java.io.BufferedReader
import java.io.InputStreamReader
import java.io.PrintWriter
import java.net.Socket
import java.util.*

class ChatViewModel : ViewModel() {
val TAG = "Chat View Model"
private suspend fun sendMessage(msg : String?, receiverIpAddress : String?, receiverPort : Int) {
try {
val clientSocket : Socket = Socket(receiverIpAddress, receiverPort)
val outToServer = clientSocket.getOutputStream()
val output = PrintWriter(outToServer)
output.println(msg)
output.flush()
clientSocket.close()

} catch(e : Exception) {
e.printStackTrace()
}
}

private suspend fun receiveMessage(vararg sockets: Socket): String? {
var text : String? = null
try {
val input = BufferedReader(InputStreamReader(sockets[0].getInputStream()))
text = input.readLine()
Log.i(TAG, "Received => $text")
} catch (e: java.lang.Exception) {
e.printStackTrace()
}
return text
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 5 additions & 3 deletions app/src/main/res/drawable/round_border.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke
android:width="1dp"
android:color="@color/white" />
android:color="@color/white"
/>

<solid android:color="#ffffff" />

<padding
android:left="1dp"
android:right="1dp"
android:bottom="1dp"
android:top="1dp" />
android:top="1dp"
/>

<corners android:radius="5dp" />
</shape>
15 changes: 15 additions & 0 deletions app/src/main/res/layout/fragment_chat.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".ChatFragment">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/message_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
app:layoutManager="LinearLayoutManager" />
</FrameLayout>
Loading

0 comments on commit 96686c1

Please sign in to comment.