Commit 0ef58f7a authored by Giacomo Lavermicocca's avatar Giacomo Lavermicocca

add some test for sending and reading info

parent ff465c6b
......@@ -20,7 +20,7 @@
</intent-filter>
</activity>
<receiver android:name=".MainActivity$switchButtonListener" />
<receiver android:name=".SwitchButtonListener" />
<service
android:name=".services.NotificationService"
......
package it.lavermicocca.giacomo.beautifulnotification
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.Toast
import it.lavermicocca.giacomo.beautifulnotification.services.Helper
import okhttp3.Call
import okhttp3.Callback
import okhttp3.OkHttpClient
import okhttp3.Response
import java.io.IOException
class MainActivity : AppCompatActivity() {
class switchButtonListener : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
Log.d("Here", "I am here")
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Helper.StartForegroundServiceNotification(applicationContext)
findViewById<Button>(R.id.start_notification).setOnClickListener(View.OnClickListener {
Helper.StartForegroundServiceNotification(applicationContext)
SendGet(13)
})
Helper.StartForegroundServiceNotification(applicationContext, "")
// findViewById<Button>(R.id.start_notification).setOnClickListener(View.OnClickListener {
// Helper.StartForegroundServiceNotification(applicationContext)
// Sender(applicationContext).SendCheck()
// })
//
findViewById<Button>(R.id.start_settings).setOnClickListener(View.OnClickListener {
val intent = Intent(this, SettingsActivity::class.java).apply {
//putExtra(EXTRA_MESSAGE, message)
......@@ -46,31 +30,5 @@ class MainActivity : AppCompatActivity() {
//-------------------- HTTP WAY -------------------------
private fun SendGet(pin: Int) {
val prf = getApplicationContext().getSharedPreferences("myPref", Context.MODE_PRIVATE)
val ipServer = prf.getString("ip_server", "192.168.5.6")
val url = "http://" + ipServer + "/pin/check" // /pin$pin"
val client = OkHttpClient()
val request = okhttp3.Request.Builder()
.url(url)
.get()
//.addHeader("Content-Type", "application/json")
.build()
client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
e.printStackTrace()
}
@Throws(IOException::class)
override fun onResponse(call: Call, response: Response) {
val networkResp = response.body()!!.string()
this@MainActivity.runOnUiThread { Toast.makeText(this@MainActivity, networkResp, Toast.LENGTH_LONG).show() }
}
})
}
}
package it.lavermicocca.giacomo.beautifulnotification
import android.content.Context
import android.os.Handler
import android.os.Looper
import android.support.v7.app.AppCompatActivity
import android.widget.Toast
import it.lavermicocca.giacomo.beautifulnotification.notifications.GenericNotification
import it.lavermicocca.giacomo.beautifulnotification.services.Helper
import okhttp3.*
import java.io.IOException
class Sender(val context: Context) {
fun SendCheck() {
val prf = context.getSharedPreferences("myPref", Context.MODE_PRIVATE)
val ipServer = prf.getString("ip_server", "192.168.43.176")
val url = "http://" + ipServer + "/check" // /pin$pin"
val request = okhttp3.Request.Builder()
.url(url)
.get()
//.addHeader("Content-Type", "application/json")
.build()
val client = OkHttpClient()
client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
e.printStackTrace()
}
@Throws(IOException::class)
override fun onResponse(call: Call, response: Response) {
val networkResp = response.body()!!.string()
var looper = Looper.getMainLooper()
Handler(looper).post(object :Runnable{
override fun run() {
//TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
Toast.makeText(context, networkResp, Toast.LENGTH_LONG).show()
Helper.StartForegroundServiceNotification(context, networkResp)
}
})
}
})
}
fun SendRead(pin: Int) {
val prf = context.getSharedPreferences("myPref", Context.MODE_PRIVATE)
val ipServer = prf.getString("ip_server", "192.168.43.176")
val url = "http://$ipServer/read/$pin" // /pin$pin"
val request = okhttp3.Request.Builder()
.url(url)
.get()
//.addHeader("Content-Type", "application/json")
.build()
val client = OkHttpClient()
client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
e.printStackTrace()
}
@Throws(IOException::class)
override fun onResponse(call: Call, response: Response) {
val networkResp = response.body()!!.string()
var looper = Looper.getMainLooper()
Handler(looper).post(object :Runnable{
override fun run() {
//TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
Toast.makeText(context, networkResp, Toast.LENGTH_LONG).show()
Helper.StartForegroundServiceNotification(context, networkResp)
}
})
}
})
}
fun SendPin(pin: Int) {
val prf = context.getSharedPreferences("myPref", Context.MODE_PRIVATE)
val ipServer = prf.getString("ip_server", "192.168.43.176")
val url = "http://$ipServer/pin/$pin" // /pin$pin"
val request = okhttp3.Request.Builder()
.url(url)
.get()
//.addHeader("Content-Type", "application/json")
.build()
val client = OkHttpClient()
client.newCall(request).enqueue(object : Callback {
override fun onFailure(call: Call, e: IOException) {
e.printStackTrace()
}
@Throws(IOException::class)
override fun onResponse(call: Call, response: Response) {
val networkResp = response.body()!!.string()
var looper = Looper.getMainLooper()
Handler(looper).post(object :Runnable{
override fun run() {
//TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
Toast.makeText(context, networkResp, Toast.LENGTH_LONG).show()
}
})
}
})
}
}
\ No newline at end of file
package it.lavermicocca.giacomo.beautifulnotification
object TestStatic {
fun HelloWorldMethod(unPo: Int) {}
}
package it.lavermicocca.giacomo.beautifulnotification
import android.content.Context
import android.widget.Toast
class myRunnable constructor(val context:Context, val text: String):Runnable {
override fun run() {
//TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
Toast.makeText(context, text, Toast.LENGTH_LONG).show()
}
}
\ No newline at end of file
package it.lavermicocca.giacomo.beautifulnotification.notifications;
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.os.Build;
import android.support.v4.app.NotificationCompat;
import android.widget.RemoteViews;
import it.lavermicocca.giacomo.beautifulnotification.MainActivity;
import it.lavermicocca.giacomo.beautifulnotification.R;
/**
* Created by giacomo on 12/05/18.
*/
public class GenericNotification {
private static final String CHANNEL_ID = "media_playback_channel";
public static void startNotification(Service service){
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager notificationManager = (NotificationManager) service.getSystemService(ns);
NotificationCompat.Builder builder = new NotificationCompat.Builder(service.getApplicationContext(), CHANNEL_ID)
.setContentTitle("TITLE")
.setContentText("BODY")
.setSmallIcon(R.drawable.ic_stat_notification)
.setAutoCancel(true)
.setTicker("Ticker TEXT");
Notification notification = builder.build();
RemoteViews notificationView = new RemoteViews(service.getPackageName(),
R.layout.mynotification);
//the intent that is started when the notification is clicked (works)
Intent notificationIntent = new Intent(service, MainActivity.class);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(service, 0,
notificationIntent, 0);
notification.contentView = notificationView;
notification.contentIntent = pendingNotificationIntent;
notification.flags |= Notification.FLAG_NO_CLEAR;
//this is the intent that is supposed to be called when the
//button is clicked
Intent switchIntent = new Intent(service, MainActivity.switchButtonListener.class);
PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(service, 0, switchIntent, 0);
// Intent intent = new Intent(getApplicationContext(), MainActivity.class);
// PendingIntent pendingSwitchIntent = PendingIntent.getActivity(this, 0, intent, 0);
notificationView.setOnClickPendingIntent(R.id.closeOnFlash, pendingSwitchIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "Channel human readable title", NotificationManager.IMPORTANCE_HIGH);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
notificationManager.createNotificationChannel(channel);
}
service.startForeground(1, notification);
}
}
package it.lavermicocca.giacomo.beautifulnotification.notifications
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.os.Build
import android.support.v4.app.NotificationCompat
import android.widget.RemoteViews
import it.lavermicocca.giacomo.beautifulnotification.MainActivity
import it.lavermicocca.giacomo.beautifulnotification.R
import it.lavermicocca.giacomo.beautifulnotification.SwitchButtonListener
/**
* Created by giacomo on 12/05/18.
*/
object GenericNotification {
private val CHANNEL_ID = "media_playback_channel"
val COMMAND = "COMMAND"
enum class STATUS {none, garage, cancello, all, portone, lastCommand}
fun startNotification(service: Service, text : String) {
val ns = Context.NOTIFICATION_SERVICE
val notificationManager = service.getSystemService(ns) as NotificationManager
val builder = NotificationCompat.Builder(service.applicationContext, CHANNEL_ID)
.setContentTitle("TITLE")
.setContentText("BODY")
.setSmallIcon(R.drawable.ic_stat_notification)
.setAutoCancel(true)
.setTicker("Ticker TEXT")
val notification = builder.build()
val notificationView = RemoteViews(service.packageName, R.layout.mynotification)
notificationView.setTextViewText(R.id.text_response, text)
//the intent that is started when the notification is clicked (works)
val notificationIntent = Intent(service, MainActivity::class.java)
val pendingNotificationIntent = PendingIntent.getActivity(service, 0, notificationIntent, 0)
notification.contentView = notificationView
notification.contentIntent = pendingNotificationIntent
notification.flags = notification.flags or Notification.FLAG_NO_CLEAR
//this is the intent that is supposed to be called when the
//button is clicked
val switchIntent = Intent(service, SwitchButtonListener::class.java)
prepareData(switchIntent, STATUS.lastCommand)
val lastCommand = PendingIntent.getBroadcast(service, 0, switchIntent, PendingIntent.FLAG_UPDATE_CURRENT)
prepareData(switchIntent, STATUS.garage)
val openCloseGarage = PendingIntent.getBroadcast(service, STATUS.garage.ordinal, switchIntent, PendingIntent.FLAG_UPDATE_CURRENT)
prepareData(switchIntent, STATUS.cancello)
val openCloseCancello = PendingIntent.getBroadcast(service, STATUS.cancello.ordinal, switchIntent, PendingIntent.FLAG_UPDATE_CURRENT)
prepareData(switchIntent, STATUS.all)
val all = PendingIntent.getBroadcast(service, STATUS.all.ordinal, switchIntent, PendingIntent.FLAG_UPDATE_CURRENT)
prepareData(switchIntent, STATUS.portone)
val openClosePortone = PendingIntent.getBroadcast(service, STATUS.portone.ordinal, switchIntent, PendingIntent.FLAG_UPDATE_CURRENT)
notificationView.setOnClickPendingIntent(R.id.last_command, lastCommand)
notificationView.setOnClickPendingIntent(R.id.open_close_garage, openCloseGarage)
notificationView.setOnClickPendingIntent(R.id.open_close_cancello, openCloseCancello)
notificationView.setOnClickPendingIntent(R.id.open_close_all, all)
notificationView.setOnClickPendingIntent(R.id.open_close_portone, openClosePortone)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(CHANNEL_ID, "Channel human readable title", NotificationManager.IMPORTANCE_HIGH)
channel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
notificationManager.createNotificationChannel(channel)
}
service.startForeground(1, notification)
}
fun prepareData(i:Intent, status: STATUS){
i.putExtra(COMMAND, status.ordinal)
}
}
package it.lavermicocca.giacomo.beautifulnotification
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import it.lavermicocca.giacomo.beautifulnotification.notifications.GenericNotification
import it.lavermicocca.giacomo.beautifulnotification.notifications.GenericNotification.COMMAND
class SwitchButtonListener : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
var sender = Sender(context)
Log.d("Here", "I am here")
val requestCode = intent;
var status:GenericNotification.STATUS;
status = GenericNotification.STATUS.values()[
requestCode.getIntExtra(COMMAND, GenericNotification.STATUS.none.ordinal
)]
when(status){
GenericNotification.STATUS.lastCommand -> sender.SendCheck()
GenericNotification.STATUS.cancello -> sender.SendPin(17)
GenericNotification.STATUS.garage -> sender.SendPin(21)
GenericNotification.STATUS.portone -> sender.SendRead(37)
}
}
}
package it.lavermicocca.giacomo.beautifulnotification.receivers;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.Log;
import it.lavermicocca.giacomo.beautifulnotification.services.Helper;
/**
* Created by giacomo on 12/05/18.
*/
public class BootCompletedReceiver extends BroadcastReceiver {
private static final String TAG = "BootCompletedReceiver";
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onReceive(Context context, Intent arg1) {
// TODO Auto-generated method stub
Log.w("boot_broadcast_poc", "starting service...");
Helper.StartForegroundServiceNotification(context);
}
}
\ No newline at end of file
package it.lavermicocca.giacomo.beautifulnotification.receivers
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Build
import android.support.annotation.RequiresApi
import android.util.Log
import it.lavermicocca.giacomo.beautifulnotification.services.Helper
/**
* Created by giacomo on 12/05/18.
*/
class BootCompletedReceiver : BroadcastReceiver() {
@RequiresApi(api = Build.VERSION_CODES.O)
override fun onReceive(context: Context, arg1: Intent) {
// TODO Auto-generated method stub
Log.w(TAG, "starting service...")
Helper.StartForegroundServiceNotification(context, "")
}
companion object {
private val TAG = "BootCompletedReceiver"
}
}
\ No newline at end of file
package it.lavermicocca.giacomo.beautifulnotification.services;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
/**
* Created by giacomo on 12/05/18.
*/
public class Helper {
public static void StartForegroundServiceNotification(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(new Intent(context, NotificationService.class));
} else {
context.startService(new Intent(context, NotificationService.class));
}
}
}
package it.lavermicocca.giacomo.beautifulnotification.services
import android.content.Context
import android.content.Intent
import android.os.Build
/**
* Created by giacomo on 12/05/18.
*/
object Helper {
val LAST_COMMAND = "LAST_COMMAND";
fun StartForegroundServiceNotification(context: Context, text: String) {
var intent = Intent(context, NotificationService::class.java)
intent.putExtra(LAST_COMMAND, text)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent)
} else {
context.startService(intent)
}
}
}
package it.lavermicocca.giacomo.beautifulnotification.services;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import it.lavermicocca.giacomo.beautifulnotification.notifications.GenericNotification;
/**
* Created by giacomo on 12/05/18.
*/
public class NotificationService extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
GenericNotification.startNotification(this);
return START_STICKY;
}
}
package it.lavermicocca.giacomo.beautifulnotification.services
import android.app.Service
import android.content.Intent
import android.os.IBinder
import it.lavermicocca.giacomo.beautifulnotification.notifications.GenericNotification
import it.lavermicocca.giacomo.beautifulnotification.services.Helper.LAST_COMMAND
/**
* Created by giacomo on 12/05/18.
*/
class NotificationService : Service() {
override fun onBind(intent: Intent): IBinder? {
// TODO: Return the communication channel to the service.
throw UnsupportedOperationException("Not yet implemented")
}
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
var text = intent.getStringExtra(LAST_COMMAND);
GenericNotification.startNotification(this, text)
return Service.START_STICKY
}
}
......@@ -6,27 +6,52 @@
android:orientation="horizontal"
android:weightSum="100" >
<ImageView
android:id="@+id/notifiation_image"
android:layout_width="0dp"
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/last_command"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="30"
android:contentDescription="description"
android:src="@android:drawable/arrow_down_float" />
android:text="Last C." />
<TextView
android:id="@+id/appName"
android:id="@+id/text_response"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:layout_below="@+id/last_command"
android:layout_alignEnd="@+id/last_command"
android:layout_alignRight="@+id/last_command"
android:gravity="center"
android:text="flash light on"
android:text="some text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
<Button
android:id="@+id/open_close_portone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:text="POR." />
<Button
android:id="@+id/open_close_garage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GAR." />
<Button
android:id="@+id/open_close_cancello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CAN." />
<Button
android:id="@+id/closeOnFlash"
android:id="@+id/open_close_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="20"
android:text="close" />
android:text="ALL" />
</LinearLayout>
\ No newline at end of file
......@@ -8,7 +8,7 @@ buildscript {
jcenter()
}
dependencies {