connection to FlightGear and design
This commit is contained in:
parent
d8a1345a4d
commit
f2ff9354d3
@ -1,5 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="GradleMigrationSettings" migrationVersion="1" />
|
||||
<component name="GradleSettings">
|
||||
<option name="linkedExternalProjectsSettings">
|
||||
<GradleProjectSettings>
|
||||
|
@ -29,6 +29,10 @@ android {
|
||||
kotlinOptions {
|
||||
jvmTarget = '1.8'
|
||||
}
|
||||
|
||||
buildFeatures {
|
||||
dataBinding true
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
@ -1,9 +1,10 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.example.flightgearcontrollerapp">
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET"/>
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:fullBackupContent="true"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
android:roundIcon="@mipmap/ic_launcher_round"
|
||||
|
@ -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()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -1,4 +0,0 @@
|
||||
package com.example.flightgearcontrollerapp.model
|
||||
|
||||
class FG_model {
|
||||
}
|
@ -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()
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +0,0 @@
|
||||
package com.example.flightgearcontrollerapp.view_model
|
||||
|
||||
class VM_connection {
|
||||
}
|
@ -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()
|
||||
}
|
||||
}
|
@ -0,0 +1,4 @@
|
||||
package com.example.flightgearcontrollerapp.views
|
||||
|
||||
class JoystickView{
|
||||
}
|
@ -1,12 +1,64 @@
|
||||
package com.example.flightgearcontrollerapp.views
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
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.id.*
|
||||
import com.example.flightgearcontrollerapp.view_model.FGViewModel
|
||||
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
|
||||
//private var vmConnection: FGViewModel = FGViewModel()
|
||||
private var isConnected = false
|
||||
private val vmConnection = FGViewModel()
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -1,17 +1,24 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
|
||||
<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"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="fill_parent"
|
||||
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_height="match_parent"
|
||||
android:background="@mipmap/background"
|
||||
tools:context=".views.MainActivity">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context="com.setheightwidthpercentage_android_examples.com.MainActivity"
|
||||
android:weightSum="100"
|
||||
android:orientation="vertical">
|
||||
|
||||
@ -19,7 +26,7 @@
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="0"
|
||||
android:orientation="vertical" >
|
||||
android:orientation="vertical">
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
@ -31,7 +38,8 @@
|
||||
android:textSize="41sp"
|
||||
android:textStyle="bold|italic"
|
||||
android:textAlignment="center"
|
||||
android:gravity="center"/>
|
||||
android:gravity="center" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/text_ip"
|
||||
android:layout_width="match_parent"
|
||||
@ -42,7 +50,8 @@
|
||||
android:paddingStart="15sp"
|
||||
android:paddingEnd="15sp"
|
||||
android:paddingTop="5sp"
|
||||
android:paddingBottom="5sp"/>
|
||||
android:paddingBottom="5sp" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/ip_address"
|
||||
android:labelFor="@id/ip_address"
|
||||
@ -61,6 +70,7 @@
|
||||
android:singleLine="true"
|
||||
android:nextFocusForward="@+id/port_address"
|
||||
android:importantForAutofill="no" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/text_port"
|
||||
android:layout_width="match_parent"
|
||||
@ -71,7 +81,8 @@
|
||||
android:paddingLeft="15sp"
|
||||
android:paddingRight="15sp"
|
||||
android:paddingTop="5sp"
|
||||
android:paddingBottom="5sp"/>
|
||||
android:paddingBottom="5sp" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/port_address"
|
||||
android:labelFor="@id/port_address"
|
||||
@ -101,81 +112,95 @@
|
||||
android:layout_gravity="center"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/btn_connect"/>
|
||||
android:onClick="onClickConnect"
|
||||
android:text="@string/btn_connect" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="fill_parent"
|
||||
android:id="@+id/throttle_slider"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="95"
|
||||
android:weightSum="100"
|
||||
android:background="@color/black_60"
|
||||
android:orientation="horizontal">
|
||||
android:orientation="horizontal"
|
||||
android:layout_weight="80"
|
||||
android:background="@color/black_60">
|
||||
|
||||
<RelativeLayout
|
||||
android:layout_width="360sp"
|
||||
android:layout_height="370sp">
|
||||
|
||||
<RelativeLayout
|
||||
android:id="@+id/widget_joystick"
|
||||
android:layout_width="290sp"
|
||||
android:layout_height="290sp"
|
||||
android:layout_gravity="center"
|
||||
android:layout_weight="0" />
|
||||
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="wrap_content"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_width="100sp"
|
||||
android:layout_height="66sp"
|
||||
android:layout_row="0"
|
||||
android:layout_column="1"
|
||||
android:gravity="center"
|
||||
android:rotation="-90"
|
||||
android:text="@string/txt_throttle"
|
||||
android:textColor="@color/black_60"
|
||||
android:textSize="15sp"
|
||||
android:textSize="20sp"
|
||||
android:textStyle="bold" />
|
||||
|
||||
<com.google.android.material.slider.Slider
|
||||
android:layout_width="300sp"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_marginStart="-120sp"
|
||||
android:layout_marginTop="140sp"
|
||||
|
||||
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:background="@color/white_60"
|
||||
android:value="0"
|
||||
android:valueFrom="0"
|
||||
android:valueTo="1" />
|
||||
|
||||
</GridLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="5"
|
||||
android:orientation="vertical"
|
||||
android:background="@color/white_40"
|
||||
android:orientation="vertical"
|
||||
android:paddingTop="5sp"
|
||||
android:paddingBottom="5sp">
|
||||
|
||||
|
||||
<TextView
|
||||
android:id="@+id/text_rudder"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/txt_rudder"
|
||||
android:textSize="18sp"
|
||||
android:textColor="@color/black_60"
|
||||
android:gravity="center"
|
||||
android:textStyle="bold"
|
||||
android:paddingLeft="15sp"
|
||||
android:paddingRight="15sp"
|
||||
android:paddingTop="5sp"
|
||||
android:paddingBottom="0sp"/>
|
||||
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_height="wrap_content"
|
||||
android:layout_width="match_parent"
|
||||
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"
|
||||
android:value="0" />
|
||||
android:valueTo="1" />
|
||||
</LinearLayout>
|
||||
</LinearLayout>
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
</layout>
|
||||
|
@ -6,4 +6,11 @@
|
||||
<string name="btn_connect">Connect To FlightGear</string>
|
||||
<string name="txt_throttle">Throttle</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 --generic=socket,in,10,\'ip\',\'port\',tcp,playback_small \n --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>
|
@ -18,7 +18,6 @@ allprojects {
|
||||
repositories {
|
||||
google()
|
||||
mavenCentral()
|
||||
jcenter() // Warning: this repository is going to shut down soon
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user