diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aa724b7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,15 @@ +*.iml +.gradle +/local.properties +/.idea/caches +/.idea/libraries +/.idea/modules.xml +/.idea/workspace.xml +/.idea/navEditor.xml +/.idea/assetWizardSettings.xml +.DS_Store +/build +/captures +.externalNativeBuild +.cxx +local.properties diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..fb7f4a8 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml new file mode 100644 index 0000000..a2d7c21 --- /dev/null +++ b/.idea/gradle.xml @@ -0,0 +1,19 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..575a6e6 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..288b36b --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/app/build.gradle b/app/build.gradle new file mode 100644 index 0000000..f458f8d --- /dev/null +++ b/app/build.gradle @@ -0,0 +1,48 @@ +plugins { + id 'com.android.application' +} + +android { + compileSdk 32 + + defaultConfig { + applicationId "com.example.miniandroidapp13_websocket02" + minSdk 30 + targetSdk 32 + versionCode 1 + versionName "1.0" + + testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" + } + + buildTypes { + release { + minifyEnabled false + proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + } + } + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } +} + +dependencies { + + implementation 'androidx.appcompat:appcompat:1.5.1' + implementation 'com.google.android.material:material:1.7.0' + implementation 'androidx.constraintlayout:constraintlayout:2.1.4' + testImplementation 'junit:junit:4.13.2' + androidTestImplementation 'androidx.test.ext:junit:1.1.4' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0' + + // 追加 + implementation 'com.squareup.okhttp3:okhttp:4.9.1' + implementation 'javax.websocket:javax.websocket-api:1.1' + + // JSONを使うjackson + implementation group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.13.0' + implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.13.0' + implementation group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.13.0' + +} \ No newline at end of file diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..481bb43 --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/app/src/androidTest/java/com/example/miniandroidapp13_websocket02/ExampleInstrumentedTest.java b/app/src/androidTest/java/com/example/miniandroidapp13_websocket02/ExampleInstrumentedTest.java new file mode 100644 index 0000000..e613d63 --- /dev/null +++ b/app/src/androidTest/java/com/example/miniandroidapp13_websocket02/ExampleInstrumentedTest.java @@ -0,0 +1,26 @@ +package com.example.miniandroidapp13_websocket02; + +import android.content.Context; + +import androidx.test.platform.app.InstrumentationRegistry; +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumented test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); + assertEquals("com.example.miniandroidapp13_websocket02", appContext.getPackageName()); + } +} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..f9b818c --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/example/miniandroidapp13_websocket02/MainActivity.java b/app/src/main/java/com/example/miniandroidapp13_websocket02/MainActivity.java new file mode 100644 index 0000000..7c6ec63 --- /dev/null +++ b/app/src/main/java/com/example/miniandroidapp13_websocket02/MainActivity.java @@ -0,0 +1,50 @@ +package com.example.miniandroidapp13_websocket02; + +import androidx.appcompat.app.AppCompatActivity; +import androidx.lifecycle.ViewModelProvider; + +import android.os.Bundle; +import android.view.View; +import android.widget.Button; +import android.widget.TextView; + +import com.example.miniandroidapp13_websocket02.viewmodels.MainViewModel; + +public class MainActivity extends AppCompatActivity { + + private MainViewModel mainViewModel; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + TextView textView = (TextView) findViewById(R.id.textView); + Button button_left = (Button) findViewById(R.id.button_left); + Button button_center = (Button) findViewById(R.id.button_center); + Button button_right = (Button) findViewById(R.id.button_right); + button_left.setOnClickListener(this::onClick); + button_center.setOnClickListener(this::onClick); + button_right.setOnClickListener(this::onClick); + textView.setText("未接続"); + + mainViewModel = new ViewModelProvider(this).get(MainViewModel.class); + mainViewModel.getTextLiveData().observe ( + this, + textObserver -> { + textView.setText(textObserver); + } + ); + } + + public void onClick(View v) { + if (v.getId() == R.id.button_left) { + mainViewModel.openWebSocket(); + } else if (v.getId() == R.id.button_center) { + mainViewModel.closeWebSocket(); + } else if (v.getId() == R.id.button_right) { + mainViewModel.sendMessageWebSocket("Hello from Android"); + } + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/example/miniandroidapp13_websocket02/viewmodels/MainViewModel.java b/app/src/main/java/com/example/miniandroidapp13_websocket02/viewmodels/MainViewModel.java new file mode 100644 index 0000000..8091f17 --- /dev/null +++ b/app/src/main/java/com/example/miniandroidapp13_websocket02/viewmodels/MainViewModel.java @@ -0,0 +1,134 @@ +package com.example.miniandroidapp13_websocket02.viewmodels; + +import android.os.Handler; + +import androidx.lifecycle.LiveData; +import androidx.lifecycle.MutableLiveData; +import androidx.lifecycle.ViewModel; + +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; +import okhttp3.WebSocket; +import okhttp3.WebSocketListener; +import okio.ByteString; + +public class MainViewModel extends ViewModel { + + private WebSocketClient webSocketClient; + + //ライブデータ + private MutableLiveData textLiveData; + + // コンストラクタ + public MainViewModel() { + this.textLiveData = new MutableLiveData<>(); + this.textLiveData.setValue("テキスト"); + webSocketClient = new WebSocketClient(); + } + + //----------------------------------------------------------------------------- + // getter + public LiveData getTextLiveData() { + return this.textLiveData; + } + + //setter + public void sendMessageWebSocket(String message) { + webSocketClient.sendMessage(message); + } + + public void closeWebSocket() { + //第1引数(code)には、1000、または3000〜4000の整数を指定 + //第2引数(reason)には、切断理由を表す文字列を指定 + webSocketClient.webSocket.close(1000,"切断のボタンを押した"); + } + + public void openWebSocket() { + webSocketClient = new WebSocketClient(); + } + //----------------------------------------------------------------------------- + + + /* + WebSocketに関わる内部クラス + */ + public class WebSocketClient extends WebSocketListener { + + private WebSocket webSocket; + private Handler mHandler = new Handler(); + + // 外部クラスのLiveDateにアクセスする + private void setTextLiveData(String message) { + // UIスレッドにしないと動かない... + mHandler.post(new Runnable() { + public void run() { + // UIの操作 + textLiveData.setValue(message); + } + }); + } + + public WebSocketClient() { + OkHttpClient client = new OkHttpClient(); + + Request.Builder request = new Request.Builder(); + + // 接続先のURLを指定。 + // 今回は3つ種類のサーバーURLをサンプルで用意しているので、どれか1つをコメントアウト外して使う。 + // エミュレータから実行するために、ここではlocalhostとか127.0.0.1ではないことに注意 + + // ● サンプル1シンプルなWebSocketのサンプル + request.url("ws://10.0.2.2:8080/demo/WebSocketDemo"); + + // ● パスパラメータを付与できるサンプル + // request.url("ws://10.0.2.2:8080/demo/WebSocketDemoPathParam/kokowa-nanidemo-ok"); + + // ● サーバーでエンコードしたJSONが返ってくるサンプル + //request.url("ws://10.0.2.2:8080/demo/WebSocketDemoJSON"); + + webSocket = client.newWebSocket(request.build(), this); + } + + //----------------------------------------------------------------------------- + //setter + public void sendMessage(String message) { + System.out.println("kota: メッセージを送信:"+message); + webSocket.send(message); + } + //----------------------------------------------------------------------------- + + + @Override + public void onOpen(WebSocket webSocket, Response response) { + this.setTextLiveData("接続完了"); + System.out.println("kota: WebSocketの接続成功"); + } + + @Override + public void onMessage(WebSocket webSocket, String text) { + this.setTextLiveData("接続中"+text); + System.out.println("kota: メッセージを受け取りました:" + text); + } + + @Override + public void onMessage(WebSocket webSocket, ByteString bytes) { + this.setTextLiveData("接続中"+bytes.hex()); + System.out.println("kota: メッセージを受け取りました:" + bytes.hex()); + } + + @Override + public void onClosing(WebSocket webSocket, int code, String reason) { + webSocket.close(1000, null); + this.setTextLiveData("切断しました"); + System.out.println("kota: WebSocketが切断されました:" + code + reason); + } + + @Override + public void onFailure(WebSocket webSocket, Throwable t, Response response) { + this.setTextLiveData("接続失敗"); + System.out.println("kota: WebSocketの接続が失敗しました:"+ t.getLocalizedMessage()); + } + } + +} diff --git a/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000..2b068d1 --- /dev/null +++ b/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_launcher_background.xml b/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..07d5da9 --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..1fd1631 --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,57 @@ + + + + + +