Commit a7e495a5 authored by Giacomo Lavermicocca's avatar Giacomo Lavermicocca

fix flow concept add Notification class with generic constractor

parent 4bb3a76b
......@@ -21,12 +21,20 @@ android {
}
}
ext {
supportLibVersion = '27.0.0'
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation "com.android.support:appcompat-v7:$supportLibVersion"
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "com.android.support:support-emoji:$supportLibVersion"
implementation "com.android.support:support-emoji-appcompat:$supportLibVersion"
implementation "com.android.support:support-emoji-bundled:$supportLibVersion"
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.android.support:support-vector-drawable:27.1.1'
......
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="it.lavermicocca.giacomo.beautifulnotification">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".EmojiCompatApplication"
tools:ignore="GoogleAppIndexingWarning"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
......
......@@ -14,7 +14,7 @@
* limitations under the License.
*/
package com.example.android.emojicompat
package it.lavermicocca.giacomo.beautifulnotification
import android.app.Application
import android.support.text.emoji.EmojiCompat
......
package it.lavermicocca.giacomo.beautifulnotification
import android.util.Log
class HelperMessages{
enum class STATUS { UNKNOWN, TIMEOUT, GARAGE_OPEN, GARAGE_CLOSE }
fun Do(messaggio: String) : STATUS{
when(messaggio)
{
"L.C. : read : 37 1"-> {
Log.e("TTAG", "UNO")
return STATUS.GARAGE_OPEN
}
"L.C. : read : 37 0"-> {
Log.e("TTAG", "ZERO")
return STATUS.GARAGE_CLOSE
}
"Timeout command" -> {
Log.e("TTAG", "Timeout")
return STATUS.TIMEOUT
}
}
return STATUS.UNKNOWN
}
}
\ No newline at end of file
package it.lavermicocca.giacomo.beautifulnotification
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.provider.FontRequest
import android.support.annotation.RequiresApi
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.Button
import it.lavermicocca.giacomo.beautifulnotification.services.Helper
import kotlinx.android.synthetic.main.activity_main.*
import android.support.text.emoji.EmojiCompat
import android.support.text.emoji.FontRequestEmojiCompatConfig
import android.support.text.emoji.bundled.BundledEmojiCompatConfig
import android.util.Log
import android.widget.TextView
class MainActivity : AppCompatActivity() {
companion object {
internal val GEAR = "⚙️"
internal val DOOR = "\uD83D\uDEAA"
internal val CAR = "\uD83D\uDE98"
internal val SIGNAL = "\uD83D\uDE8F"
}
//@RequiresApi(Build.VERSION_CODES.O)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Helper.StartForegroundServiceNotification(applicationContext, "")
// TextView variant provided by EmojiCompat library
//val emojiTextView = findViewById<TextView>(R.id.emoji_text_view)
start_notification.text = "$SIGNAL"
start_settings.text = "$GEAR"
//Helper.StartForegroundServiceNotification(applicationContext, "")
start_notification.setOnClickListener(View.OnClickListener {
Helper.StartForegroundServiceNotification(applicationContext, "a\nb\nc\nd\ne\nf\ng", Helper.FULL_ON)
})
// 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)
......@@ -31,4 +59,4 @@ class MainActivity : AppCompatActivity() {
//-------------------- HTTP WAY -------------------------
}
}
\ No newline at end of file
package it.lavermicocca.giacomo.beautifulnotification
import android.content.Context
import android.os.CountDownTimer
import android.os.Handler
import android.os.Looper
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.widget.Toast
import it.lavermicocca.giacomo.beautifulnotification.notifications.GenericNotification
import it.lavermicocca.giacomo.beautifulnotification.mappers.Arguments
import it.lavermicocca.giacomo.beautifulnotification.services.Helper
import okhttp3.*
import java.io.IOException
class Sender(val context: Context) {
fun SendCheck() {
companion object {
val LAST_COMMAND_TIMEOUT = "LAST_COMMAND_TIMEOUT"
val LAST_COMMAND = "LAST_COMMAND"
val TIMEOUT = "TIMEOUT"
}
//NULL
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 + "/check" // /pin$pin"
val url = "http://$ipServer/read/$pin" // /pin$pin"
val request = okhttp3.Request.Builder()
.url(url)
......@@ -39,7 +48,8 @@ class Sender(val context: Context) {
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)
//Helper.StartForegroundServiceNotification(context, networkResp, Helper.FULL_OFF)
SendCheck(true)
}
})
......@@ -47,10 +57,12 @@ class Sender(val context: Context) {
})
}
fun SendRead(pin: Int) {
//UNO
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/read/$pin" // /pin$pin"
val url = "http://$ipServer/pin/$pin"
val request = okhttp3.Request.Builder()
.url(url)
......@@ -74,7 +86,11 @@ class Sender(val context: Context) {
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)
if(networkResp.contains(Arguments.PIN, true))
{
Log.e("TTAG", "RICHIESTA PIN")
SendCheck(false)
}
}
})
......@@ -82,10 +98,13 @@ class Sender(val context: Context) {
})
}
fun SendPin(pin: Int) {
//DUE
fun SendCheck(oneShot:Boolean) {
Thread.sleep(1000);
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 url = "http://" + ipServer + "/check" // /pin$pin"
val request = okhttp3.Request.Builder()
.url(url)
......@@ -103,16 +122,48 @@ class Sender(val context: Context) {
@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()
}
})
var status:HelperMessages.STATUS = HelperMessages().Do(networkResp)
if(status == HelperMessages.STATUS.TIMEOUT) {
}
else if(status == HelperMessages.STATUS.GARAGE_CLOSE) {
Helper.StartForegroundServiceNotification(context, "GARAGE OPEN", Helper.FULL_ON)
}
else if(status == HelperMessages.STATUS.GARAGE_OPEN) {
Helper.StartForegroundServiceNotification(context, "GARAGE CLOSE", Helper.FULL_ON)
}
if(!oneShot)
{
var looper = Looper.getMainLooper()
Handler(looper).post(object :Runnable{
override fun run() {
CountD(networkResp)
}
})
}
}
})
}
//------------------------------- EXTRA ----------------------------------
private fun CountD(testo: String) {
val prf = context.getSharedPreferences("myPref", Context.MODE_PRIVATE)
val timeout:Long = prf.getString("timeout", "10000").toLong()
object : CountDownTimer(timeout, 1000) {
override fun onTick(millisUntilFinished: Long) {
var t = "${testo} ${millisUntilFinished/1000}"
Helper.StartForegroundServiceNotification(context, t, Helper.FULL_OFF)
}
override fun onFinish() {
Helper.StartForegroundServiceNotification(context, "Fine", Helper.FULL_ON);
}
}.start()
}
}
\ No newline at end of file
......@@ -63,8 +63,6 @@ class SettingsActivity : AppCompatPreferenceActivity() {
override fun isValidFragment(fragmentName: String): Boolean {
return PreferenceFragment::class.java.name == fragmentName
|| GeneralPreferenceFragment::class.java.name == fragmentName
|| DataSyncPreferenceFragment::class.java.name == fragmentName
|| NotificationPreferenceFragment::class.java.name == fragmentName
}
/**
......@@ -84,65 +82,7 @@ class SettingsActivity : AppCompatPreferenceActivity() {
// updated to reflect the new value, per the Android Design
// guidelines.
bindPreferenceSummaryToValue(findPreference("ip_server"))
bindPreferenceSummaryToValue(findPreference("example_list"))
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val id = item.itemId
if (id == android.R.id.home) {
startActivity(Intent(activity, SettingsActivity::class.java))
return true
}
return super.onOptionsItemSelected(item)
}
}
/**
* This fragment shows notification preferences only. It is used when the
* activity is showing a two-pane settings UI.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
class NotificationPreferenceFragment : PreferenceFragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
getPreferenceManager().setSharedPreferencesName("myPref");
addPreferencesFromResource(R.xml.pref_notification)
setHasOptionsMenu(true)
// Bind the summaries of EditText/List/Dialog/Ringtone preferences
// to their values. When their values change, their summaries are
// updated to reflect the new value, per the Android Design
// guidelines.
bindPreferenceSummaryToValue(findPreference("notifications_new_message_ringtone"))
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val id = item.itemId
if (id == android.R.id.home) {
startActivity(Intent(activity, SettingsActivity::class.java))
return true
}
return super.onOptionsItemSelected(item)
}
}
/**
* This fragment shows data and sync preferences only. It is used when the
* activity is showing a two-pane settings UI.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
class DataSyncPreferenceFragment : PreferenceFragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
getPreferenceManager().setSharedPreferencesName("myPref");
addPreferencesFromResource(R.xml.pref_data_sync)
setHasOptionsMenu(true)
// Bind the summaries of EditText/List/Dialog/Ringtone preferences
// to their values. When their values change, their summaries are
// updated to reflect the new value, per the Android Design
// guidelines.
bindPreferenceSummaryToValue(findPreference("sync_frequency"))
bindPreferenceSummaryToValue(findPreference("timeout"))
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
......
package it.lavermicocca.giacomo.beautifulnotification
object TestStatic {
fun HelloWorldMethod(unPo: Int) {}
}
package it.lavermicocca.giacomo.beautifulnotification.mappers
class Arguments{
companion object {
var READ = "read"
var PIN = "pin"
var CHECK = "check"
}
}
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.app.*
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
import it.lavermicocca.giacomo.beautifulnotification.services.Helper
/**
* Created by giacomo on 12/05/18.
*/
object GenericNotification {
class GenericNotificationClass(val service: Service, val text : String, val status:Short) {
private val builder:NotificationCompat.Builder
private val notificationManager:NotificationManager
private val notification:Notification
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) {
init {
val ns = Context.NOTIFICATION_SERVICE
val notificationManager = service.getSystemService(ns) as NotificationManager
notificationManager = service.getSystemService(ns) as NotificationManager
val builder = NotificationCompat.Builder(service.applicationContext, CHANNEL_ID)
builder = NotificationCompat.Builder(service.applicationContext, CHANNEL_ID)
.setContentTitle("TITLE")
.setContentText("BODY")
.setSmallIcon(R.drawable.ic_stat_notification)
.setAutoCancel(true)
.setTicker("Ticker TEXT")
.setOnlyAlertOnce(true)
notification = builder.build()
}
fun startNotification() {
when (status){
Helper.FULL_ON -> fullOn()
Helper.FULL_OFF -> fullOff()
}
}
private fun fullOff() {
val notificationView = RemoteViews(service.packageName, R.layout.mynotification_off)
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)
notificationView.setOnClickPendingIntent(R.id.last_command, lastCommand)
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 fullOn(){
val notification = builder.build()
val notificationView = RemoteViews(service.packageName, R.layout.mynotification)
notificationView.setTextViewText(R.id.text_response, text)
......@@ -86,7 +124,11 @@ object GenericNotification {
service.startForeground(1, notification)
}
fun prepareData(i:Intent, status: STATUS){
companion object {
val COMMAND = "COMMAND"
}
fun prepareData(i: Intent, status: STATUS){
i.putExtra(COMMAND, status.ordinal)
}
}
}
\ No newline at end of file
......@@ -4,8 +4,7 @@ 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
import it.lavermicocca.giacomo.beautifulnotification.notifications.GenericNotificationClass
class SwitchButtonListener : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
......@@ -15,16 +14,16 @@ class SwitchButtonListener : BroadcastReceiver() {
Log.d("Here", "I am here")
val requestCode = intent;
var status:GenericNotification.STATUS;
status = GenericNotification.STATUS.values()[
requestCode.getIntExtra(COMMAND, GenericNotification.STATUS.none.ordinal
var status = GenericNotificationClass.STATUS.values()[
requestCode.getIntExtra(GenericNotificationClass.COMMAND, GenericNotificationClass.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)
GenericNotificationClass.STATUS.lastCommand -> sender.SendCheck(true)
GenericNotificationClass.STATUS.portone -> sender.SendRead(37)
GenericNotificationClass.STATUS.cancello -> sender.SendPin(17)
GenericNotificationClass.STATUS.garage -> sender.SendPin(21)
}
}
}
......@@ -19,7 +19,7 @@ class BootCompletedReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, arg1: Intent) {
// TODO Auto-generated method stub
Log.w(TAG, "starting service...")
Helper.StartForegroundServiceNotification(context, "")
Helper.StartForegroundServiceNotification(context, "", Helper.FULL_ON)
}
companion object {
......
......@@ -3,6 +3,7 @@ package it.lavermicocca.giacomo.beautifulnotification.services
import android.content.Context
import android.content.Intent
import android.os.Build
import java.util.logging.SocketHandler
/**
* Created by giacomo on 12/05/18.
......@@ -11,11 +12,16 @@ import android.os.Build
object Helper {
val LAST_COMMAND = "LAST_COMMAND";
val BUTTON_STATUS = "BUTTON_STATUS";
fun StartForegroundServiceNotification(context: Context, text: String) {
val FULL_OFF:Short = 0;
val FULL_ON:Short = 255;
fun StartForegroundServiceNotification(context: Context, text: String, valore: Short) {
var intent = Intent(context, NotificationService::class.java)
intent.putExtra(LAST_COMMAND, text)
intent.putExtra(BUTTON_STATUS, valore)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent)
......
......@@ -4,8 +4,9 @@ import android.app.Service
import android.content.Intent
import android.os.IBinder
import it.lavermicocca.giacomo.beautifulnotification.notifications.GenericNotification
import it.lavermicocca.giacomo.beautifulnotification.notifications.GenericNotificationClass
import it.lavermicocca.giacomo.beautifulnotification.services.Helper.LAST_COMMAND
import it.lavermicocca.giacomo.beautifulnotification.services.Helper.BUTTON_STATUS
/**
* Created by giacomo on 12/05/18.
......@@ -21,8 +22,9 @@ class NotificationService : Service() {
override fun onStartCommand(intent: Intent, flags: Int, startId: Int): Int {
var text = intent.getStringExtra(LAST_COMMAND);
var buttonStatus = intent.getShortExtra(BUTTON_STATUS, 0)
GenericNotification.startNotification(this, text)
GenericNotificationClass(this, text, buttonStatus).startNotification()
return Service.START_STICKY
}
......
......@@ -6,7 +6,7 @@
android:layout_height="match_parent"
tools:context="it.lavermicocca.giacomo.beautifulnotification.MainActivity">
<Button
<android.support.text.emoji.widget.EmojiAppCompatButton
android:id="@+id/start_notification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
......@@ -17,9 +17,10 @@
android:text="Start notification"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/start_settings" />
app:layout_constraintTop_toBottomOf="@+id/start_settings"
android:textSize="80sp" />
<Button
<android.support.text.emoji.widget.EmojiAppCompatButton
android:id="@+id/start_settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
......@@ -31,6 +32,7 @@
android:text="Start settings"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toTopOf="parent"
android:textSize="80sp" />
</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_height="200dp"
android:background="@color/colorPrimary"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="100" >
android:weightSum="100">
<RelativeLayout
android:layout_width="wrap_content"
......
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="100" >
<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:text="Last C." />
<TextView
android:id="@+id/text_response"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/last_command"
android:layout_alignEnd="@+id/last_command"
android:layout_alignRight="@+id/last_command"
android:gravity="center"
android:text="some text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
</LinearLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimary">#3f51b5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
</resources>
<resources>
<string name="app_name">BeautifulNotification</string>
<string name="title_activity_settings">Settings</string>
<!-- Strings related to Settings -->
<!-- Example General settings -->
......
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<SwitchPreference