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

add some test for sending and reading info

parent ff465c6b
Loading
Loading
Loading
Loading
+536 B

File added.

No diff preview for this file type.

+1 −1
Changes for app/src/main/AndroidManifest.xml: 1 added line, 1 removed line.
Original line number Original line Diff line number Diff line
@@ -20,7 +20,7 @@
            </intent-filter>
            </intent-filter>
        </activity>
        </activity>


        <receiver android:name=".MainActivity$switchButtonListener" />
        <receiver android:name=".SwitchButtonListener" />


        <service
        <service
            android:name=".services.NotificationService"
            android:name=".services.NotificationService"
+6 −48
Changes for app/src/main/java/it/lavermicocca/giacomo/beautifulnotification/MainActivity.kt: 6 added lines, 48 removed lines.
Original line number Original line Diff line number Diff line
package it.lavermicocca.giacomo.beautifulnotification
package it.lavermicocca.giacomo.beautifulnotification


import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.Intent
import android.os.Bundle
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.View
import android.view.View
import android.widget.Button
import android.widget.Button
import android.widget.Toast
import it.lavermicocca.giacomo.beautifulnotification.services.Helper
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 MainActivity : AppCompatActivity() {



    class switchButtonListener : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            Log.d("Here", "I am here")
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setContentView(R.layout.activity_main)


        Helper.StartForegroundServiceNotification(applicationContext)
        Helper.StartForegroundServiceNotification(applicationContext, "")

        findViewById<Button>(R.id.start_notification).setOnClickListener(View.OnClickListener {
            Helper.StartForegroundServiceNotification(applicationContext)
            SendGet(13)
        })


//        findViewById<Button>(R.id.start_notification).setOnClickListener(View.OnClickListener {
//            Helper.StartForegroundServiceNotification(applicationContext)
//            Sender(applicationContext).SendCheck()
//        })
//
        findViewById<Button>(R.id.start_settings).setOnClickListener(View.OnClickListener {
        findViewById<Button>(R.id.start_settings).setOnClickListener(View.OnClickListener {
            val intent = Intent(this, SettingsActivity::class.java).apply {
            val intent = Intent(this, SettingsActivity::class.java).apply {
                //putExtra(EXTRA_MESSAGE, message)
                //putExtra(EXTRA_MESSAGE, message)
@@ -46,31 +30,5 @@ class MainActivity : AppCompatActivity() {


    //-------------------- HTTP WAY -------------------------
    //-------------------- 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() }
            }
        })
    }
}
}
+118 −0
Changes for app/src/main/java/it/lavermicocca/giacomo/beautifulnotification/Sender.kt: 118 added lines, 0 removed lines.
Original line number Original line Diff line number Diff line
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
+5 −0
Changes for app/src/main/java/it/lavermicocca/giacomo/beautifulnotification/TestStatic.kt: 5 added lines, 0 removed lines.
Original line number Original line Diff line number Diff line
package it.lavermicocca.giacomo.beautifulnotification

object TestStatic {
    fun HelloWorldMethod(unPo: Int) {}
}
Loading