Android project added

- Android can now send messages over RabbitMQ to the backend.
This commit is contained in:
2020-10-19 22:19:28 +02:00
parent 48140f557b
commit f722ee9595
56 changed files with 1522 additions and 12 deletions

View File

@@ -0,0 +1,31 @@
package de.nicolasklier.livebeat
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.navigation.fragment.findNavController
/**
* A simple [Fragment] subclass as the default destination in the navigation.
*/
class FirstFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_first, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.findViewById<Button>(R.id.button_first).setOnClickListener {
findNavController().navigate(R.id.action_FirstFragment_to_SecondFragment)
}
}
}

View File

@@ -0,0 +1,40 @@
package de.nicolasklier.livebeat
import android.content.Intent
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.floatingactionbutton.FloatingActionButton
import com.google.android.material.snackbar.Snackbar
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(findViewById(R.id.toolbar))
startService(Intent(this, TrackerService::class.java))
findViewById<FloatingActionButton>(R.id.fab).setOnClickListener { view ->
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.menu_main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// 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.
return when (item.itemId) {
R.id.action_settings -> true
else -> super.onOptionsItemSelected(item)
}
}
}

View File

@@ -0,0 +1,31 @@
package de.nicolasklier.livebeat
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.navigation.fragment.findNavController
/**
* A simple [Fragment] subclass as the second destination in the navigation.
*/
class SecondFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_second, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
view.findViewById<Button>(R.id.button_second).setOnClickListener {
findNavController().navigate(R.id.action_SecondFragment_to_FirstFragment)
}
}
}

View File

@@ -0,0 +1,96 @@
package de.nicolasklier.livebeat;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Color;
import android.os.IBinder;
import android.util.Log;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class TrackerService extends Service {
private static final int NOTIF_ID = 1;
private static final String NOTIF_CHANNEL_ID = "LiveTracker";
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
final Connection[] conn = new Connection[1];
// This thread only connects to RabbitMQ
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
ConnectionFactory factory = new ConnectionFactory();
factory.setUsername("lineage");
factory.setPassword("ZSo$X97GQ547JXL7nGq");
factory.setVirtualHost("/");
factory.setHost("192.168.178.26");
factory.setPort(5672);
try {
conn[0] = factory.newConnection();
Channel channel = conn[0].createChannel();
channel.queueDeclare("Tracker", false, false, false, null);
channel.basicPublish("", "Tracker", null, "Test message!!!!!!!!!!!!!!!!!".getBytes());
Log.i("RabbitMQ", "run: Published test message");
} catch (IOException | TimeoutException e) {
e.printStackTrace();
}
}
});
thread.start();
startForeground();
return super.onStartCommand(intent, flags, startId);
}
private void startForeground() {
Intent noticicationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, noticicationIntent, 0);
NotificationChannel chan = new NotificationChannel(
NOTIF_CHANNEL_ID,
"Tracker",
NotificationManager.IMPORTANCE_LOW);
chan.setLightColor(Color.BLACK);
chan.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
manager.createNotificationChannel(chan);
startForeground(NOTIF_ID, new NotificationCompat.Builder(this, NOTIF_CHANNEL_ID)
.setOngoing(true)
.setContentTitle("Livebeat")
.setContentText("Tracker is running")
.setContentIntent(pendingIntent)
.setCategory(Notification.CATEGORY_SERVICE)
.setPriority(NotificationManager.IMPORTANCE_LOW)
.setChannelId(NOTIF_CHANNEL_ID)
.setColor(Color.BLACK)
.build());
}
}