commit 20e7c911cb9c06c05b08853101d6fe80b9e948ea Author: Jan Kunze Date: Mon Jun 14 16:19:39 2021 +0200 Init push diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/build.gradle b/build.gradle new file mode 100644 index 0000000..17de039 --- /dev/null +++ b/build.gradle @@ -0,0 +1,50 @@ +plugins { + id 'com.android.application' +} + +android { + + + compileSdkVersion 30 + buildToolsVersion "30.0.3" + + defaultConfig { + applicationId "com.example.erdbebensender" + minSdkVersion 29 + targetSdkVersion 30 + 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 { + + compileOnly "org.projectlombok:lombok:1.18.20" + + implementation 'androidx.appcompat:appcompat:1.3.0' + implementation 'com.google.android.material:material:1.3.0' + implementation 'androidx.constraintlayout:constraintlayout:2.0.4' + implementation 'androidx.navigation:navigation-fragment:2.3.5' + implementation 'androidx.navigation:navigation-ui:2.3.5' + testImplementation 'junit:junit:4.+' + androidTestImplementation 'androidx.test.ext:junit:1.1.2' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' + + implementation 'com.android.volley:volley:1.2.0' + +} diff --git a/proguard-rules.pro b/proguard-rules.pro new file mode 100644 index 0000000..481bb43 --- /dev/null +++ b/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/src/androidTest/java/com/example/erdbebensender/ExampleInstrumentedTest.java b/src/androidTest/java/com/example/erdbebensender/ExampleInstrumentedTest.java new file mode 100644 index 0000000..e47b708 --- /dev/null +++ b/src/androidTest/java/com/example/erdbebensender/ExampleInstrumentedTest.java @@ -0,0 +1,25 @@ +package com.example.erdbebensender; + +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.erdbebensender", appContext.getPackageName()); + } +} \ No newline at end of file diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml new file mode 100644 index 0000000..d07cf44 --- /dev/null +++ b/src/main/AndroidManifest.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/java/com/example/erdbebensender/ErdbebenParameter.java b/src/main/java/com/example/erdbebensender/ErdbebenParameter.java new file mode 100644 index 0000000..c080901 --- /dev/null +++ b/src/main/java/com/example/erdbebensender/ErdbebenParameter.java @@ -0,0 +1,46 @@ +package com.example.erdbebensender; + + + + +public class ErdbebenParameter { + + public String sender; + public String x; + public String y; + public String v; + + + + public String getSender() { + return sender; + } + + public void setSender(String sender) { + this.sender = sender; + } + + public String getX() { + return x; + } + + public void setX(String x) { + this.x = x; + } + + public String getY() { + return y; + } + + public void setY(String y) { + this.y = y; + } + + public String getV() { + return v; + } + + public void setV(String v) { + this.v = v; + } +} diff --git a/src/main/java/com/example/erdbebensender/FirstFragment.java b/src/main/java/com/example/erdbebensender/FirstFragment.java new file mode 100644 index 0000000..32cbb9c --- /dev/null +++ b/src/main/java/com/example/erdbebensender/FirstFragment.java @@ -0,0 +1,52 @@ +package com.example.erdbebensender; + +import android.os.Bundle; +import android.util.Log; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import androidx.annotation.NonNull; +import androidx.fragment.app.Fragment; +import com.android.volley.RequestQueue; +import com.android.volley.toolbox.Volley; +import lombok.SneakyThrows; + +public class FirstFragment extends Fragment { + + private static final String TAG = "MessageSender"; + MessageSender messageSender = new MessageSender(); + + @Override + public View onCreateView( + LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState + ) { + // Inflate the layout for this fragment + return inflater.inflate(R.layout.fragment_first, container, false); + } + + public void onViewCreated(@NonNull View view, Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + + view.findViewById(R.id.button_first).setOnClickListener(new View.OnClickListener() { + + @Override + public void onClick(View view) { + try { + ErdbebenParameter erdbebebenParam = new ErdbebenParameter(); + erdbebebenParam.setX("200"); + erdbebebenParam.setY("200"); + erdbebebenParam.setV("10"); + erdbebebenParam.setSender("TOKEN"); + //messageSender.sendMessage(erdbebebenParam); + } + catch (Exception e) + { + Log.i("ErdbebenMessage", "Fehler aufgetretten"); + } + + } + }); + } +} \ No newline at end of file diff --git a/src/main/java/com/example/erdbebensender/MainActivity.java b/src/main/java/com/example/erdbebensender/MainActivity.java new file mode 100644 index 0000000..62a15a5 --- /dev/null +++ b/src/main/java/com/example/erdbebensender/MainActivity.java @@ -0,0 +1,63 @@ +package com.example.erdbebensender; + +import android.os.Bundle; +import com.android.volley.RequestQueue; +import com.android.volley.toolbox.Volley; +import com.google.android.material.floatingactionbutton.FloatingActionButton; +import com.google.android.material.snackbar.Snackbar; +import androidx.appcompat.app.AppCompatActivity; +import androidx.appcompat.widget.Toolbar; +import android.view.View; + +import android.view.Menu; +import android.view.MenuItem; + +import java.io.IOException; + +public class MainActivity extends AppCompatActivity { + + + public MainActivity() throws IOException { + + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + Toolbar toolbar = findViewById(R.id.toolbar); + setSupportActionBar(toolbar); + + FloatingActionButton fab = findViewById(R.id.fab); + fab.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + + Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) + .setAction("Action", null).show(); + } + }); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + // Inflate the menu; this adds items to the action bar if it is present. + getMenuInflater().inflate(R.menu.menu_main, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + // Handle action bar item clicks here. The action bar will + // automatically handle clicks on the Home/Up button, so long + // as you specify a parent activity in AndroidManifest.xml. + int id = item.getItemId(); + + //noinspection SimplifiableIfStatement + if (id == R.id.action_settings) { + return true; + } + + return super.onOptionsItemSelected(item); + } +} \ No newline at end of file diff --git a/src/main/java/com/example/erdbebensender/MessageSender.java b/src/main/java/com/example/erdbebensender/MessageSender.java new file mode 100644 index 0000000..d97efde --- /dev/null +++ b/src/main/java/com/example/erdbebensender/MessageSender.java @@ -0,0 +1,83 @@ +package com.example.erdbebensender; + +import android.content.Context; +import android.nfc.Tag; +import android.util.Log; +import com.android.volley.*; +import com.android.volley.toolbox.*; +import org.json.JSONException; +import org.json.JSONObject; + +import java.io.*; +import java.net.HttpURLConnection; +import java.net.URL; +import java.nio.charset.StandardCharsets; + + +public class MessageSender { + + private static final String FCM_API = "https://fcm.googleapis.com/fcm/send"; + private static final String TOPIC = "erdbeben"; + private static final String SERVER_KEY = "AAAA2pVhMk4:APA91bHeiZGBd0BZi-RqnlCqnMkD7ICFW7SKzft-j1RIm16LX4M0Jez8AUQOoGgKYhHsVDD1kDxfKnul9Xbcjwo6QDAnPNLqOnk3XViVI5HjxgL8mpyzDLZXFharMIaqPANqBiMs5Gm2"; + + private static final String TAG = "ErdbebenMessage"; + + + public void sendMessage(ErdbebenParameter erdbebenParameter) throws IOException, JSONException { + URL url = new URL(FCM_API); + HttpURLConnection con = (HttpURLConnection) url.openConnection(); + try { + Log.i(TAG, "Init HTTP Request"); + con.setRequestMethod("POST"); + con.setRequestProperty("Content-Type", "application/json"); + //con.setRequestProperty("Accept", "application/json"); + con.setRequestProperty("Authorization", "key=" + SERVER_KEY); + con.setDoOutput(true); + //con.setChunkedStreamingMode(0); + Log.i(TAG, "---Fehlerstelle kommt----"); + OutputStream out = new BufferedOutputStream(con.getOutputStream()); + Log.i(TAG, "---Fehler behoben---"); + out.write("Test".getBytes(),0, "Test".getBytes().length); + out.flush(); + out.close(); + + Log.i(TAG, "Request Erfolgreich gesendet"); + + try (BufferedReader br = new BufferedReader( + new InputStreamReader(con.getInputStream(), "utf-8"))) { + StringBuilder response = new StringBuilder(); + String responseLine = null; + while ((responseLine = br.readLine()) != null) { + response.append(responseLine.trim()); + } + Log.i(TAG, "Response erhalten: "); + } + + } catch (Exception e) { + throw e; + } + finally { + con.disconnect(); + } + } + + private JSONObject generateJson(ErdbebenParameter erdbebenParameter) throws JSONException { + Log.i(TAG, "Erstelle JSON"); + JSONObject json = new JSONObject(); + JSONObject data = new JSONObject(); + JSONObject notification = new JSONObject(); + + notification.put("title", "Erdbeben"); + notification.put("body", "Neues Erdbebeben aufgetretten"); + data.put("sender", erdbebenParameter.getSender()); + data.put("x", erdbebenParameter.getX()); + data.put("y", erdbebenParameter.getY()); + data.put("v", erdbebenParameter.getV()); + + json.put("to", "/topics/" + TOPIC); + json.put("notification", notification); + json.put("data", data); + + return json; + } +} diff --git a/src/main/java/com/example/erdbebensender/SecondFragment.java b/src/main/java/com/example/erdbebensender/SecondFragment.java new file mode 100644 index 0000000..3f58305 --- /dev/null +++ b/src/main/java/com/example/erdbebensender/SecondFragment.java @@ -0,0 +1,34 @@ +package com.example.erdbebensender; + +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import androidx.annotation.NonNull; +import androidx.fragment.app.Fragment; +import androidx.navigation.fragment.NavHostFragment; + +public class SecondFragment extends Fragment { + + @Override + public View onCreateView( + LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState + ) { + // Inflate the layout for this fragment + return inflater.inflate(R.layout.fragment_second, container, false); + } + + public void onViewCreated(@NonNull View view, Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + + view.findViewById(R.id.button_second).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + NavHostFragment.findNavController(SecondFragment.this) + .navigate(R.id.action_SecondFragment_to_FirstFragment); + } + }); + } +} \ No newline at end of file diff --git a/src/main/res/drawable-v24/ic_launcher_foreground.xml b/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000..1ee1493 --- /dev/null +++ b/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/res/drawable/ic_launcher_background.xml b/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..956b344 --- /dev/null +++ b/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,74 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/res/layout/activity_main.xml b/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..3b1619e --- /dev/null +++ b/src/main/res/layout/activity_main.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/res/layout/content_main.xml b/src/main/res/layout/content_main.xml new file mode 100644 index 0000000..0b96b99 --- /dev/null +++ b/src/main/res/layout/content_main.xml @@ -0,0 +1,19 @@ + + + + \ No newline at end of file diff --git a/src/main/res/layout/fragment_first.xml b/src/main/res/layout/fragment_first.xml new file mode 100644 index 0000000..eea7f45 --- /dev/null +++ b/src/main/res/layout/fragment_first.xml @@ -0,0 +1,31 @@ + + + + + +