more changes

This commit is contained in:
Tucan444 2021-03-22 20:14:02 +01:00
parent 4ef7318c27
commit c5ed73adb4
18 changed files with 219 additions and 114 deletions

@ -14,8 +14,9 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:usesCleartextTraffic="true"
android:theme="@style/Theme.WikiSpotWithActionBar">
<activity android:name=".activities.MainActivity">
android:theme="@style/Theme.WikiSpot">
<activity android:name=".activities.MainActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

@ -42,7 +42,7 @@ fun Context.getThemeId(): Int {
// for client
fun Context.getDataFromServer(): String {
// requesting data
val url = "http://192.168.1.230:8000/devices_list"
val url = "${ServerManagement.baseUrl}devices_list"
val request = Request.Builder().url(url).build()
val client = OkHttpClient()
var receivedResponse = ""

@ -6,8 +6,10 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
import androidx.navigation.ui.setupWithNavController
import com.example.wikispot.*
import com.example.wikispot.fragments.*
import com.example.wikispot.modelClasses.SettingsSaveManager
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.fragment_home.*
class MainActivity : AppCompatActivity() {
@ -21,13 +23,7 @@ class MainActivity : AppCompatActivity() {
}
override fun onCreate(savedInstanceState: Bundle?) {
/*if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE),
ManifestRelatedVariables.REQUEST_READ_EXTERNAL)
} */
loadSettings()
ServerManagement.serverManager.addActivityConnection(this, "main",0)
ServerManagement.serverManager.getData(this, this, 0, "", "", true)
setTheme(getThemeId())
super.onCreate(savedInstanceState)
@ -37,8 +33,36 @@ class MainActivity : AppCompatActivity() {
mainBottomNavigationView.setupWithNavController(navController)
handleExtras()
}
println("[debug] ${getDataFromServer()}")
override fun onResume() {
super.onResume()
// server communication
val dataReceiver: (String) -> Unit = {data: String ->
println("Data here: $data")
when (mainFragmentHost.childFragmentManager.fragments[0]) {
is chatFragment -> {}
is exploreFragment -> {}
is homeFragment -> {
val view = mainFragmentHost.childFragmentManager.fragments[0].homeFragmentTextIdTest
view.post {
view.text = data
}
}
is mapFragment -> {}
is settingsFragment -> {}
}
}
ServerManagement.serverManager.addReceiverConnection(dataReceiver, this, "mainConnection", 0, "test0.json")
}
override fun onPause() {
super.onPause()
ServerManagement.serverManager.deleteConnection("mainConnection")
}
private fun handleExtras() {
@ -55,5 +79,4 @@ class MainActivity : AppCompatActivity() {
val settingsSaveManager = SettingsSaveManager(this)
settingsSaveManager.loadSettings()
}
}

@ -1,18 +0,0 @@
package com.example.wikispot
import android.app.Activity
import android.os.Build
import androidx.annotation.RequiresApi
import kotlinx.android.synthetic.main.activity_main.*
// for serverManager
fun Activity.receiveData(data: String) {
when (this.localClassName) {
"activities.MainActivity" -> { this.receiveDataForMainActivity(data) }
}
}
fun Activity.receiveDataForMainActivity(data: String) {
}

@ -45,11 +45,10 @@ class anotherDebugFragment : Fragment(R.layout.fragment_another_debug) {
createViewConnectionBtn.setOnClickListener {
val attributePath = attributePathInput.text.toString()
val setWholeContent = wholeContentCheckBox.isChecked
val filePath = filePathInput.text.toString()
ServerManagement.serverManager.deleteConnection("debug", "view")
ServerManagement.serverManager.addViewConnection(requireContext(), dataContentView, "debug",0, filePath, attributePath, setWholeContent)
ServerManagement.serverManager.addViewConnection(requireContext(), dataContentView, "debug",0, filePath, attributePath)
}
// handling navigation between debug fragments

@ -6,7 +6,28 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.wikispot.R
import com.example.wikispot.getStringFromSharedPreferences
import com.example.wikispot.saveString
import kotlinx.android.synthetic.main.fragment_home.*
class homeFragment : Fragment(R.layout.fragment_home) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
loadCache()
}
override fun onPause() {
super.onPause()
saveCache()
}
private fun loadCache() {
homeFragmentTextIdTest.text = requireContext().getStringFromSharedPreferences("title", "homeFragmentCache" )
}
private fun saveCache() {
requireContext().saveString("title", homeFragmentTextIdTest.text.toString(), "homeFragmentCache")
}
}

@ -1,150 +1,30 @@
package com.example.wikispot.modelClasses
import android.app.Activity
import android.content.Context
import android.widget.TextView
import com.example.wikispot.ServerManagement
import com.example.wikispot.receiveData
import okhttp3.*
import org.json.JSONArray
import org.json.JSONObject
import java.io.IOException
class ServerManager {
private var activityConnections = mutableListOf<ActivityConnection>()
private var receiverConnections = mutableListOf<ReceiverConnection>()
private var viewConnections = mutableListOf<ViewConnection>()
fun getData(activity: Activity, context: Context, serverId: Int, path: String, attributePath: String, getWholeContent: Boolean=false, numberOfAttempts: Int=2) {
val dataRequestThread = Thread(DataRequest(activity, context, serverId, path, attributePath, getWholeContent, numberOfAttempts))
fun getData(dataReceiver: (String) -> Unit, context: Context, serverId: Int, path: String, attributePath: String="", numberOfAttempts: Int=2) {
val dataRequestThread = Thread(DataRequest(dataReceiver, context, serverId, path, attributePath, numberOfAttempts))
dataRequestThread.start()
}
inner class DataRequest(val activity: Activity, val context: Context, val serverId: Int, val path: String="", val attributePath: String, var getWholeContent: Boolean=false, val numberOfAttempts: Int=2): Runnable{
inner class DataRequest(val dataReceiver: (String) -> Unit, val context: Context, val serverId: Int, val path: String="", val attributePath: String, private val numberOfAttempts: Int=2): Runnable{
override fun run() {
for (n in 0 until numberOfAttempts) {
var url = "http://192.168.1.230:8000/devices_list"
var url = "${ServerManagement.baseUrl}devices_list"
if (path != "") {
url = "http://192.168.1.230:8000/files/$serverId/$path"
}
val request = Request.Builder().url(url).build()
val client = OkHttpClient()
client.newCall(request).enqueue(object : Callback {
override fun onResponse(call: Call, response: Response) {
response.body?.let {
val receivedString = response.body!!.string()
if (receivedString == "Internal Server Error") {
return
}
try {
JSONArray(receivedString)
val jsonManager = JsonManager(context, receivedString)
if (path == "") {
jsonManager.getJsonObject(serverId)
} else {
if (attributePath == "") {
getWholeContent = true
}
}
if (getWholeContent) {
activity.receiveData(jsonManager.currentJsonObject.toString())
println("[debug] testing ${activity.localClassName} ; ${activity.componentName} ; ${activity.packageName}")
} else if(attributePath != "") {
activity.receiveData(jsonManager.getAttributeContentByPath(attributePath))
} else {
println("[debug] path or whole content needs to be chosen")
}
} catch (exception: Throwable) {
activity.receiveData(receivedString)
}
}
}
override fun onFailure(call: Call, e: IOException) {
println("Request Failed")
println(e)
}
})
Thread.sleep(ServerManagement.dataRequestOnAttemptWait)
}
}
}
// connections
fun deleteConnection(connectionName: String, connectionType: String="any") { // other types are any, activity and view
if ((connectionType == "any") or (connectionType == "activity")) {
for (i in 0 until activityConnections.size) { // checking in connections
if (activityConnections[i].connectionName == connectionName) {
activityConnections[i].running = false
activityConnections.removeAt(i)
}
}
}
if ((connectionType == "any") or (connectionType == "view")) {
for (i in 0 until viewConnections.size) { // checking in connections
if (viewConnections[i].connectionName == connectionName) {
viewConnections[i].running = false
viewConnections.removeAt(i)
}
}
}
}
fun addActivityConnection(activity: Activity, connectionName: String, serverId: Int, path: String?=null) {
activityConnections.add(ActivityConnection(activity, connectionName, serverId, path))
}
inner class ActivityConnection(val activity: Activity, val connectionName: String, val serverId: Int, val path: String?=null) {
var running = true
init {
val checkingServerDataThread = Thread(CheckingServerData())
checkingServerDataThread.start()
}
inner class CheckingServerData : Runnable {
override fun run() {
while (running) {
println("[debug] connection thread running")
Thread.sleep(ServerManagement.activityConnectionOnCheckWait)
}
}
}
}
fun addViewConnection(context: Context, view: TextView, connectionName: String, serverId: Int, path: String="", attributePath: String, getWholeContent: Boolean=false) {
viewConnections.add(ViewConnection(context, view, connectionName, serverId, path, attributePath, getWholeContent))
}
inner class ViewConnection(val context: Context, val view: TextView, val connectionName: String, val serverId: Int, val path: String="", var attributePath: String, var getWholeContent: Boolean=false) {
var running = true
init {
val checkingServerDataThread = Thread(CheckingServerData())
checkingServerDataThread.start()
}
inner class CheckingServerData: Runnable {
override fun run() {
while (running) {
var url = "http://192.168.1.230:8000/devices_list"
if (path != "") {
url = "http://192.168.1.230:8000/files/$serverId/$path"
url = "${ServerManagement.baseUrl}files/$serverId/$path"
}
val request = Request.Builder().url(url).build()
@ -170,16 +50,217 @@ class ServerManager {
}
}
if (getWholeContent) {
view.post {
view.text = jsonManager.currentJsonObject.toString()
if(attributePath != "") {
dataReceiver(jsonManager.getAttributeContentByPath(attributePath))
} else {
dataReceiver(jsonManager.currentJsonObject.toString())
}
} else if(attributePath != "") {
} catch (exception: Throwable) {
try {
JSONObject(receivedString)
val jsonManager = JsonManager(context, receivedString, "JSONObject")
if(attributePath != "") {
dataReceiver(jsonManager.getAttributeContentByPath(attributePath))
} else {
dataReceiver(jsonManager.currentJsonObject.toString())
}
} catch (exception: Throwable) {
dataReceiver(receivedString)
}
}
}
}
override fun onFailure(call: Call, e: IOException) {
println("Request Failed")
println(e)
}
})
Thread.sleep(ServerManagement.dataRequestOnAttemptWait)
}
}
}
// connections
fun deleteConnection(connectionName: String, connectionType: String="any") { // other types are any, activity and view
if ((connectionType == "any") or (connectionType == "activity")) {
for (i in 0 until receiverConnections.size) { // checking in connections
if (receiverConnections[i].connectionName == connectionName) {
receiverConnections[i].running = false
receiverConnections.removeAt(i)
}
}
}
if ((connectionType == "any") or (connectionType == "view")) {
for (i in 0 until viewConnections.size) { // checking in connections
if (viewConnections[i].connectionName == connectionName) {
viewConnections[i].running = false
viewConnections.removeAt(i)
}
}
}
}
fun addReceiverConnection(dataReceiver: (String) -> Unit, context: Context, connectionName: String, serverId: Int, path: String?=null, attributePath: String="") {
receiverConnections.add(ReceiverConnection(dataReceiver, context, connectionName, serverId, path, attributePath))
}
inner class ReceiverConnection(val dataReceiver: (String) -> Unit, val context: Context, val connectionName: String, val serverId: Int, val path: String?=null, val attributePath: String) {
var running = true
init {
val checkingServerDataThread = Thread(CheckingServerData())
checkingServerDataThread.start()
}
inner class CheckingServerData : Runnable {
override fun run() {
while (running) {
var url = "${ServerManagement.baseUrl}devices_list"
if (path != "") {
url = "${ServerManagement.baseUrl}files/$serverId/$path"
}
val request = Request.Builder().url(url).build()
val client = OkHttpClient()
client.newCall(request).enqueue(object : Callback {
override fun onResponse(call: Call, response: Response) {
response.body?.let {
val receivedString = response.body!!.string()
if (receivedString == "Internal Server Error") {
return
}
try {
JSONArray(receivedString)
val jsonManager = JsonManager(context, receivedString)
if (path == "") {
jsonManager.getJsonObject(serverId)
} else {
if (attributePath == "") {
throw Throwable()
}
}
if(attributePath != "") {
dataReceiver(jsonManager.getAttributeContentByPath(attributePath))
} else {
dataReceiver(jsonManager.currentJsonObject.toString())
}
} catch (exception: Throwable) {
try {
JSONObject(receivedString)
val jsonManager = JsonManager(context, receivedString, "JSONObject")
if(attributePath != "") {
dataReceiver(jsonManager.getAttributeContentByPath(attributePath))
} else {
dataReceiver(jsonManager.currentJsonObject.toString())
}
} catch (exception: Throwable) {
dataReceiver(receivedString)
}
}
}
}
override fun onFailure(call: Call, e: IOException) {
println("Request Failed")
println(e)
}
})
Thread.sleep(ServerManagement.dataRequestOnAttemptWait)
}
}
}
}
fun addViewConnection(context: Context, view: TextView, connectionName: String, serverId: Int, path: String="", attributePath: String="") {
viewConnections.add(ViewConnection(context, view, connectionName, serverId, path, attributePath))
}
inner class ViewConnection(val context: Context, val view: TextView, val connectionName: String, val serverId: Int, val path: String="", var attributePath: String) {
var running = true
init {
val checkingServerDataThread = Thread(CheckingServerData())
checkingServerDataThread.start()
}
inner class CheckingServerData: Runnable {
override fun run() {
while (running) {
var url = "${ServerManagement.baseUrl}devices_list"
if (path != "") {
url = "${ServerManagement.baseUrl}files/$serverId/$path"
}
val request = Request.Builder().url(url).build()
val client = OkHttpClient()
client.newCall(request).enqueue(object : Callback {
override fun onResponse(call: Call, response: Response) {
response.body?.let {
val receivedString = response.body!!.string()
if (receivedString == "Internal Server Error") {
return
}
try {
JSONArray(receivedString)
val jsonManager = JsonManager(context, receivedString)
if (path == "") {
jsonManager.getJsonObject(serverId)
} else {
if (attributePath == "") {
throw Throwable()
}
}
if(attributePath != "") {
view.post {
view.text = jsonManager.getAttributeContentByPath(attributePath)
}
} else {
println("[debug] path or whole content needs to be chosen")
view.post {
view.text = jsonManager.currentJsonObject.toString()
}
}
} catch (exception: Throwable) {
try {
JSONObject(receivedString)
val jsonManager = JsonManager(context, receivedString, "JSONObject")
if(attributePath != "") {
view.post {
view.text = jsonManager.getAttributeContentByPath(attributePath)
}
} else {
view.post {
view.text = jsonManager.currentJsonObject.toString()
}
}
} catch (exception: Throwable) {
@ -189,6 +270,7 @@ class ServerManager {
}
}
}
}
override fun onFailure(call: Call, e: IOException) {
println("Request Failed")

@ -21,12 +21,13 @@ object ServerManagement {
const val activityConnectionOnCheckWait: Long = 4000
const val viewConnectionOnCheckWait: Long = 5000
const val dataRequestOnAttemptWait: Long = 2000
const val baseUrl = "http://192.168.1.230:8000/"
}
object ThemeOptions {
var darkTheme = false
var actionBar = true
var actionBar = false
}

@ -28,4 +28,5 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/main_activity_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>

@ -73,21 +73,6 @@
android:textAlignment="center" />
</ScrollView>
<CheckBox
android:id="@+id/wholeContentCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:shadowColor="#B4B4B4"
android:text="Whole Content"
android:textColor="#FFFFFF"
android:textColorHighlight="#FFFFFF"
android:textColorHint="#FFFFFF"
android:textColorLink="#FFFFFF"
app:layout_constraintStart_toStartOf="@+id/attributePathInput"
app:layout_constraintTop_toBottomOf="@+id/attributePathInput"
app:useMaterialThemeColors="false" />
<EditText
android:id="@+id/attributePathInput"
android:layout_width="wrap_content"
@ -103,11 +88,10 @@
android:id="@+id/createViewConnectionBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="8dp"
android:text="Create View Connection"
android:textSize="12sp"
app:layout_constraintStart_toEndOf="@+id/wholeContentCheckBox"
app:layout_constraintStart_toStartOf="@+id/attributePathInput"
app:layout_constraintTop_toBottomOf="@+id/attributePathInput" />
<Button
@ -118,8 +102,8 @@
android:text="Stop Connection"
android:textSize="10sp"
app:layout_constraintBottom_toBottomOf="@+id/attributePathInput"
app:layout_constraintEnd_toEndOf="@+id/createViewConnectionBtn"
app:layout_constraintStart_toEndOf="@+id/attributePathInput" />
app:layout_constraintStart_toEndOf="@+id/attributePathInput"
app:layout_constraintTop_toTopOf="@+id/attributePathInput" />
<EditText
android:id="@+id/filePathInput"

@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E11976D2"
android:background="@color/chatFragmentBackground"
tools:context=".fragments.chatFragment">
<TextView

@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#2EBFB2"
android:background="@color/exploreFragmentBackground"
tools:context=".fragments.exploreFragment">
<TextView

@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#7CB342"
android:background="@color/homeFragmentBackground"
tools:context=".fragments.homeFragment">
<TextView
@ -14,6 +14,7 @@
android:textColor="@android:color/white"
android:textSize="24sp"
android:textStyle="bold"
android:id="@+id/homeFragmentTextIdTest"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"

@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FBC02D"
android:background="@color/mapFragmentBackground"
tools:context=".fragments.mapFragment">
<TextView

@ -4,7 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F57C00"
android:background="@color/settingsFragmentBackground"
tools:context=".fragments.settingsFragment">
<TextView

@ -9,6 +9,7 @@
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_200</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->

@ -7,4 +7,11 @@
<color name="teal_700">#FF018786</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="chatFragmentBackground">#E11976D2</color>
<color name="exploreFragmentBackground">#2EBFB2</color>
<color name="homeFragmentBackground">#7CB342</color>
<color name="mapFragmentBackground">#FBC02D</color>
<color name="settingsFragmentBackground">#F57C00</color>
</resources>

@ -9,6 +9,7 @@
<item name="colorSecondary">#E91E63</item>
<item name="colorSecondaryVariant">#F48FB1</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
@ -24,6 +25,7 @@
<item name="colorSecondary">#E91E63</item>
<item name="colorSecondaryVariant">#F48FB1</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->