Skip to content

Commit

Permalink
feat: add solved
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryx076 committed Nov 12, 2024
1 parent 934a362 commit c8b25a7
Show file tree
Hide file tree
Showing 39 changed files with 1,123 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
48 changes: 48 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
}

android {
namespace = "ro.pub.cs.systems.eim.PracticalTest01"
compileSdk = 35

defaultConfig {
applicationId = "ro.pub.cs.systems.eim.PracticalTest01"
minSdk = 24
targetSdk = 34
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}
kotlinOptions {
jvmTarget = "11"
}
}

dependencies {

implementation(libs.androidx.core.ktx)
implementation(libs.androidx.appcompat)
implementation(libs.material)
implementation(libs.androidx.activity)
implementation(libs.androidx.constraintlayout)
testImplementation(libs.junit)
androidTestImplementation(libs.androidx.junit)
androidTestImplementation(libs.androidx.espresso.core)
}
21 changes: 21 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ro.pub.cs.systems.eim.PracticalTest01

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("ro.pub.cs.systems.eim.PracticalTest01", appContext.packageName)
}
}
34 changes: 34 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.PracticalTest01"
tools:targetApi="31">
<service
android:name=".Colocviu1_1Service"
android:enabled="true"
android:exported="true"></service>

<activity
android:name=".Colocviu1_1SecondaryActivity"
android:exported="false" />
<activity
android:name=".Colocviu1_1MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package ro.pub.cs.systems.eim.PracticalTest01

import android.app.Activity
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class Colocviu1_1MainActivity : AppCompatActivity() {

private lateinit var input1: EditText
private lateinit var input2: EditText

private var leftNumber = 0
private var rightNumber = 0

private val intentFilter = IntentFilter()

private val messageBroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
intent?.let {
Log.d("RECEIVER", it.action.toString())
Log.d("RECEIVER", it.getStringExtra("message").toString())
}
}
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_colocviul1_1_main)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}

input1 = findViewById(R.id.input1)
input2 = findViewById(R.id.input2)
input1.setText("0")
input2.setText("0")
val pressMeButton = findViewById<Button>(R.id.press_me_button)
pressMeButton.setOnClickListener {
leftNumber++;
input1.setText(leftNumber.toString())
if (leftNumber + rightNumber == 4) {
val intent = Intent(applicationContext, Colocviu1_1Service::class.java).apply {
putExtra("total_clicks", rightNumber+leftNumber)
}
applicationContext.startService(intent)
}
}

val pressMeToo = findViewById<Button>(R.id.press_me_too_button)
pressMeToo.setOnClickListener {

rightNumber++;
input2.setText(rightNumber.toString())

if (leftNumber + rightNumber == 4) {
val intent = Intent(applicationContext, Colocviu1_1Service::class.java).apply {
putExtra("total_clicks", rightNumber+leftNumber)
}
applicationContext.startService(intent)
}
}

val activityResultsLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
Toast.makeText(this, "The activity returned with result OK", Toast.LENGTH_LONG).show()
}
else if (result.resultCode == Activity.RESULT_CANCELED) {
Toast.makeText(this, "The activity returned with result CANCELED", Toast.LENGTH_LONG).show()
leftNumber = 0;
rightNumber = 0;
input1.setText("0")
input2.setText("0")
}
}

val navigateToSecondaryActivityButton = findViewById<Button>(R.id.navigate_to_second_activity)
navigateToSecondaryActivityButton.setOnClickListener {
val intent = Intent(this, Colocviu1_1SecondaryActivity::class.java)
intent.putExtra("leftNumber", leftNumber)
intent.putExtra("rightNumber", rightNumber)
activityResultsLauncher.launch(intent)
}

intentFilter.addAction("com.eim.BROADCAST_MESSAGE");
}

@RequiresApi(Build.VERSION_CODES.O)
override fun onResume() {
super.onResume()
//registerReceiver(messageBroadcastReceiver, intentFilter, RECEIVER_NOT_EXPORTED)
registerReceiver(messageBroadcastReceiver, intentFilter, RECEIVER_EXPORTED)
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putInt("leftNumber", leftNumber)
outState.putInt("rightNumber", rightNumber)
}

override fun onRestoreInstanceState(savedInstanceState: Bundle) {
super.onRestoreInstanceState(savedInstanceState)
if (savedInstanceState.containsKey("leftNumber") && savedInstanceState.containsKey("rightNumber")) {
input1.setText(savedInstanceState.getInt("leftNumber").toString())
input2.setText(savedInstanceState.getInt("rightNumber").toString())
leftNumber = savedInstanceState.getInt("leftNumber")
rightNumber = savedInstanceState.getInt("rightNumber")
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ro.pub.cs.systems.eim.PracticalTest01

import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class Colocviu1_1SecondaryActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_colocviu1_1_secondary)

val sumaDisplay = findViewById<TextView>(R.id.sumaClickuri)
val input1 = intent.getIntExtra("leftNumber", 0)
val input2 = intent.getIntExtra("rightNumber", 0)
val suma = input1 + input2
sumaDisplay.text = suma.toString()


val okButton = findViewById<Button>(R.id.register_button)
okButton.setOnClickListener {
setResult(RESULT_OK)
finish()
}

val cancelButton = findViewById<Button>(R.id.cancel_button)
cancelButton.setOnClickListener {
setResult(RESULT_CANCELED)
finish()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package ro.pub.cs.systems.eim.PracticalTest01

import android.app.Service
import android.content.Intent
import android.os.Handler
import android.os.IBinder
import android.util.Log
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale



class Colocviu1_1Service : Service() {

private var isTaskScheduled = false
private var handler = Handler()

val INSTRUCTION_EXTRA: String = "total_clicks"

val BROADCAST_ACTION: String = "com.eim.BROADCAST_MESSAGE"

val BROADCAST_EXTRA_MESSAGE: String = "message"


override fun onCreate() {
super.onCreate()
}

override fun onBind(intent: Intent?): IBinder? {
return null
}

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Log.d("PracticalTest01Service", "onStartCommand() callback method was invoked")

if (intent != null && !isTaskScheduled) {
val instruction = intent.getIntExtra(INSTRUCTION_EXTRA, 0);
scheduleDelayedBroadcast(instruction.toString())
isTaskScheduled = true
}
return START_NOT_STICKY
}

private fun scheduleDelayedBroadcast(instruction: String) {
handler.postDelayed(Runnable {
sendInstructionBroadcast(instruction)
stopSelf()
}, 5000)
}

private fun sendInstructionBroadcast(instruction: String) {
val sdf: SimpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault())
val currentDateTime: String = sdf.format(Date())

val message = String.format("DateTime: %s, Instruction: %s", currentDateTime, instruction)

val broadcastIntent: Intent = Intent(BROADCAST_ACTION)
broadcastIntent.putExtra(BROADCAST_EXTRA_MESSAGE, message)

sendBroadcast(broadcastIntent)
}

override fun onDestroy() {
super.onDestroy()

}
}
Loading

0 comments on commit c8b25a7

Please sign in to comment.