-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from pk-218/dev
Phase 1 changes
- Loading branch information
Showing
20 changed files
with
325 additions
and
98 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
96
app/src/main/java/tech/kotlinx/knox/ConnectionDetailsFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
33 changes: 33 additions & 0 deletions
33
app/src/main/java/tech/kotlinx/knox/adapter/messageAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
app/src/main/java/tech/kotlinx/knox/data/model/Datasource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
38
app/src/main/java/tech/kotlinx/knox/ui/viewmodels/ChatViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.