connection to FlightGear and design

This commit is contained in:
Omer Schloss 2021-06-20 21:15:51 +03:00
parent d8a1345a4d
commit f2ff9354d3
15 changed files with 354 additions and 173 deletions

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings"> <component name="GradleSettings">
<option name="linkedExternalProjectsSettings"> <option name="linkedExternalProjectsSettings">
<GradleProjectSettings> <GradleProjectSettings>

View File

@ -29,6 +29,10 @@ android {
kotlinOptions { kotlinOptions {
jvmTarget = '1.8' jvmTarget = '1.8'
} }
buildFeatures {
dataBinding true
}
} }
dependencies { dependencies {

View File

@ -1,9 +1,10 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.flightgearcontrollerapp"> package="com.example.flightgearcontrollerapp">
<uses-permission android:name="android.permission.INTERNET"/>
<application <application
android:allowBackup="true" android:allowBackup="true"
android:fullBackupContent="true"
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"

View File

@ -0,0 +1,50 @@
package com.example.flightgearcontrollerapp.model
class FGModel {
private val telnetClient = TelnetClient()
private var isExceptionOccurred = false
//private val joystickView = JoystickView()
private var isTouchingJoystick = false
/*
private val jsCenterX = 0
private val jsCenterY = 0
private val jsOuterRadius = 0
private val jsInnerRadius = 0
*/
fun connect(ipAddress: String, portAddress: String): Boolean {
var res = false
val t = Thread {
try {
res = telnetClient.connect(ipAddress, portAddress.toInt())
} catch (e: Exception) {
e.printStackTrace()
throw RuntimeException(e)
}
}
val h = Thread.UncaughtExceptionHandler { _, _ -> isExceptionOccurred = true;res= false }
t.uncaughtExceptionHandler = h
t.start()
try {
t.join()
if (!isExceptionOccurred) {
this.isTouchingJoystick = false
}
} catch (e: Exception) {
e.printStackTrace()
return false
}
return res
}
fun disconnect() {
telnetClient.disconnect()
}
}

View File

@ -1,4 +0,0 @@
package com.example.flightgearcontrollerapp.model
class FG_model {
}

View File

@ -0,0 +1,28 @@
package com.example.flightgearcontrollerapp.model
import java.io.PrintWriter
import java.net.Socket
class TelnetClient{
private lateinit var client:Socket
private lateinit var output:PrintWriter
fun connect(ipAddress: String, portAddress: Int): Boolean {
return try {
client = Socket(ipAddress, portAddress)
output = PrintWriter(client.getOutputStream(), true)
true
}catch (e: Exception){
e.printStackTrace()
false
}
}
fun disconnect() {
try{
client.close()
}catch (e: Exception){
e.printStackTrace()
}
}
}

View File

@ -1,4 +0,0 @@
package com.example.flightgearcontrollerapp.view_model
class VM_connection {
}

View File

@ -0,0 +1,18 @@
package com.example.flightgearcontrollerapp.view_model
import androidx.lifecycle.ViewModel
import com.example.flightgearcontrollerapp.model.FGModel
class FGViewModel : ViewModel() {
private val model = FGModel()
fun connectToFG(ipAddress: String, portAddress: String): Boolean{
if(model.connect(ipAddress,portAddress)){
return true
}
return false
}
fun disconnectFromFG() {
model.disconnect()
}
}

View File

@ -0,0 +1,4 @@
package com.example.flightgearcontrollerapp.views
class JoystickView{
}

View File

@ -1,12 +1,64 @@
package com.example.flightgearcontrollerapp.views package com.example.flightgearcontrollerapp.views
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import com.example.flightgearcontrollerapp.R import com.example.flightgearcontrollerapp.R
import com.example.flightgearcontrollerapp.R.id.*
import com.example.flightgearcontrollerapp.view_model.FGViewModel
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
//private var vmConnection: FGViewModel = FGViewModel()
private var isConnected = false
private val vmConnection = FGViewModel()
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main) setContentView(R.layout.activity_main)
} }
fun onClickConnect(view: View) {
if (view.id == btn_connect) {
val btnConnection = findViewById<Button>(btn_connect)
if (!isConnected) {
val editTextIP = findViewById<EditText>(ip_address)
val editTextPort = findViewById<EditText>(port_address)
val builder = AlertDialog.Builder(this)
builder.setTitle(getString(R.string.alert_dialog_flightgear_connection))
builder.setMessage(getString(R.string.alert_instruction))
builder.setPositiveButton(getString(R.string.ok_btn)){ dialog, _ ->
if ((editTextIP.text.toString() !="")&&(editTextPort.text.toString()!="")&&(vmConnection.connectToFG(editTextIP.text.toString(),editTextPort.text.toString()))) {
btnConnection.setText(R.string.disconnect)
isConnected = true
} else {
val conFailed = AlertDialog.Builder(this)
conFailed.setTitle(getString(R.string.alert_title))
conFailed.setMessage(getString(R.string.connection_failed))
val alertFail = conFailed.create()
alertFail.show()
}
dialog.cancel()
}
builder.setNegativeButton(getString(android.R.string.cancel)){ dialog, _ ->
dialog.cancel()
}
val alert = builder.create()
alert.show()
} else {
vmConnection.disconnectFromFG()
btnConnection.setText(R.string.btn_connect)
isConnected = false
}
}
}
} }

View File

@ -1,181 +1,206 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@mipmap/background"
tools:context=".views.MainActivity">
<LinearLayout <layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="viewModel"
type="com.example.flightgearcontrollerapp.view_model.FGViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context="com.setheightwidthpercentage_android_examples.com.MainActivity" android:background="@mipmap/background"
android:weightSum="100" tools:context=".views.MainActivity">
android:orientation="vertical">
<LinearLayout <LinearLayout
android:layout_width="fill_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:layout_weight="0"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="30sp"
android:paddingBottom="10sp"
android:text="@string/header_text"
android:textColor="@color/white"
android:textSize="41sp"
android:textStyle="bold|italic"
android:textAlignment="center"
android:gravity="center"/>
<TextView
android:id="@+id/text_ip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/ask_ip"
android:textSize="20sp"
android:textColor="@color/white"
android:paddingStart="15sp"
android:paddingEnd="15sp"
android:paddingTop="5sp"
android:paddingBottom="5sp"/>
<EditText
android:id="@+id/ip_address"
android:labelFor="@id/ip_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="0123456789."
android:inputType="number|numberDecimal"
android:textSize="20sp"
android:textDirection="ltr"
android:textColor="@color/white"
android:theme="@android:style/Theme.Holo"
android:paddingLeft="15sp"
android:paddingRight="15sp"
android:paddingTop="5sp"
android:paddingBottom="5sp"
android:singleLine="true"
android:nextFocusForward="@+id/port_address"
android:importantForAutofill="no" />
<TextView
android:id="@+id/text_port"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/ask_port"
android:textSize="20sp"
android:textColor="@color/white"
android:paddingLeft="15sp"
android:paddingRight="15sp"
android:paddingTop="5sp"
android:paddingBottom="5sp"/>
<EditText
android:id="@+id/port_address"
android:labelFor="@id/port_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="0123456789"
android:inputType="number"
android:textSize="20sp"
android:textDirection="ltr"
android:theme="@android:style/Theme.Holo"
android:paddingLeft="15sp"
android:paddingRight="15sp"
android:paddingTop="5sp"
android:paddingBottom="5sp"
android:importantForAutofill="no"
android:singleLine="true" />
<Button
android:id="@+id/btn_connect"
android:textSize="23sp"
android:textAllCaps="false"
android:paddingLeft="20sp"
android:paddingRight="20sp"
android:paddingTop="12sp"
android:paddingBottom="12sp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_connect"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="95"
android:weightSum="100" android:weightSum="100"
android:background="@color/black_60" android:orientation="vertical">
android:orientation="horizontal">
<RelativeLayout <LinearLayout
android:id="@+id/widget_joystick" android:layout_width="fill_parent"
android:layout_width="290sp" android:layout_height="wrap_content"
android:layout_height="290sp" android:layout_weight="0"
android:layout_gravity="center" android:orientation="vertical">
android:layout_weight="0" />
<TextView <TextView
android:id="@+id/text_throttle" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="30sp"
android:paddingBottom="10sp"
android:text="@string/header_text"
android:textColor="@color/white"
android:textSize="41sp"
android:textStyle="bold|italic"
android:textAlignment="center"
android:gravity="center" />
android:layout_width="wrap_content" <TextView
android:layout_height="match_parent" android:id="@+id/text_ip"
android:gravity="center" android:layout_width="match_parent"
android:rotation="-90" android:layout_height="wrap_content"
android:text="@string/txt_throttle" android:text="@string/ask_ip"
android:textColor="@color/black_60" android:textSize="20sp"
android:textSize="15sp" android:textColor="@color/white"
android:textStyle="bold" /> android:paddingStart="15sp"
android:paddingEnd="15sp"
android:paddingTop="5sp"
android:paddingBottom="5sp" />
<com.google.android.material.slider.Slider <EditText
android:layout_width="300sp" android:id="@+id/ip_address"
android:layout_height="match_parent" android:labelFor="@id/ip_address"
android:layout_marginStart="-120sp" android:layout_width="match_parent"
android:layout_marginTop="140sp" android:layout_height="wrap_content"
android:rotation="270" android:digits="0123456789."
android:background="@color/white_60" android:inputType="number|numberDecimal"
android:value="0" android:textSize="20sp"
android:valueFrom="0" android:textDirection="ltr"
android:valueTo="1" /> android:textColor="@color/white"
android:theme="@android:style/Theme.Holo"
android:paddingLeft="15sp"
android:paddingRight="15sp"
android:paddingTop="5sp"
android:paddingBottom="5sp"
android:singleLine="true"
android:nextFocusForward="@+id/port_address"
android:importantForAutofill="no" />
<TextView
android:id="@+id/text_port"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/ask_port"
android:textSize="20sp"
android:textColor="@color/white"
android:paddingLeft="15sp"
android:paddingRight="15sp"
android:paddingTop="5sp"
android:paddingBottom="5sp" />
</LinearLayout> <EditText
android:id="@+id/port_address"
android:labelFor="@id/port_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="0123456789"
android:inputType="number"
android:textSize="20sp"
android:textDirection="ltr"
android:theme="@android:style/Theme.Holo"
android:paddingLeft="15sp"
android:paddingRight="15sp"
android:paddingTop="5sp"
android:paddingBottom="5sp"
<LinearLayout android:importantForAutofill="no"
android:layout_width="fill_parent" android:singleLine="true" />
android:layout_height="wrap_content"
android:layout_weight="5"
android:orientation="vertical"
android:background="@color/white_40"
android:paddingTop="5sp"
android:paddingBottom="5sp">
<Button
android:id="@+id/btn_connect"
android:textSize="23sp"
android:textAllCaps="false"
android:paddingLeft="20sp"
android:paddingRight="20sp"
android:paddingTop="12sp"
android:paddingBottom="12sp"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClickConnect"
android:text="@string/btn_connect" />
<TextView </LinearLayout>
android:id="@+id/text_rudder"
<LinearLayout
android:id="@+id/throttle_slider"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="@string/txt_rudder" android:orientation="horizontal"
android:textSize="18sp" android:layout_weight="80"
android:textColor="@color/black_60" android:background="@color/black_60">
android:gravity="center"
android:textStyle="bold" <RelativeLayout
android:paddingLeft="15sp" android:layout_width="360sp"
android:paddingRight="15sp" android:layout_height="370sp">
<RelativeLayout
android:id="@+id/widget_joystick"
android:layout_width="360sp"
android:layout_height="360sp"
android:layout_gravity="center" />
</RelativeLayout>
<GridLayout
android:layout_width="100sp"
android:layout_height="match_parent"
android:background="@color/white_40">
<TextView
android:id="@+id/text_throttle"
android:layout_width="100sp"
android:layout_height="66sp"
android:layout_row="0"
android:layout_column="1"
android:gravity="center"
android:text="@string/txt_throttle"
android:textColor="@color/black_60"
android:textSize="20sp"
android:textStyle="bold" />
<com.google.android.material.slider.Slider
android:layout_width="320sp"
android:layout_height="wrap_content"
android:layout_row="1"
android:layout_column="1"
android:layout_marginLeft="-110sp"
android:layout_marginTop="120sp"
android:rotation="270"
android:value="0"
android:valueFrom="0"
android:valueTo="1" />
</GridLayout>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/white_40"
android:orientation="vertical"
android:paddingTop="5sp" android:paddingTop="5sp"
android:paddingBottom="0sp"/> android:paddingBottom="5sp">
<com.google.android.material.slider.Slider
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:valueFrom="-1"
android:valueTo="1"
android:value="0" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout> <TextView
android:id="@+id/text_rudder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingLeft="15sp"
android:paddingTop="5sp"
android:paddingRight="15sp"
android:paddingBottom="0sp"
android:text="@string/txt_rudder"
android:textColor="@color/black_60"
android:textSize="20sp"
android:textStyle="bold" />
<com.google.android.material.slider.Slider
android:layout_width="390sp"
android:layout_marginLeft="30sp"
android:layout_height="0sp"
android:layout_weight="0"
android:value="0"
android:valueFrom="-1"
android:valueTo="1" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

View File

@ -6,4 +6,11 @@
<string name="btn_connect">Connect To FlightGear</string> <string name="btn_connect">Connect To FlightGear</string>
<string name="txt_throttle">Throttle</string> <string name="txt_throttle">Throttle</string>
<string name="txt_rudder">Rudder</string> <string name="txt_rudder">Rudder</string>
<string name="disconnect">disconnect</string>
<string name="alert_dialog_flightgear_connection">FlightGear Connection</string>
<string name="alert_instruction">For connecting to FlightGear App,\nFirst open the app.\nThen, copy these lines to Setting-> Additional Setting:\n\n &#45;&#45;generic=socket,in,10,\'ip\',\'port\',tcp,playback_small \n &#45;&#45;fdm=null\n\n and now just press fly!</string>
<string name="ok_btn">continue</string>
<string name="alert_title">FlightGear Connection</string>
<string name="connection_failed">Connection failed!</string>
</resources> </resources>

View File

@ -18,7 +18,6 @@ allprojects {
repositories { repositories {
google() google()
mavenCentral() mavenCentral()
jcenter() // Warning: this repository is going to shut down soon
} }
} }