diff --git a/build.gradle b/build.gradle index 519d300f5d61453634bbd8163b11055b745b4d9c..d9beda7673e1e3e41609b31276bd30052b129342 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,6 @@ buildscript { ext.kotlin_version = "1.5.21" + ext.hilt_version = "2.37" repositories { google() mavenCentral() @@ -7,6 +8,7 @@ buildscript { dependencies { classpath 'com.android.tools.build:gradle:4.2.2' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version" } } diff --git a/mailbox/build.gradle b/mailbox/build.gradle index f66ffd4215b4c4d5fcf783bfdfa4128128b7390e..c990be55fe1ee9900418f417611ce007530501bb 100644 --- a/mailbox/build.gradle +++ b/mailbox/build.gradle @@ -2,6 +2,7 @@ plugins { id 'com.android.application' id 'kotlin-android' id 'kotlin-kapt' + id 'dagger.hilt.android.plugin' } android { @@ -35,11 +36,22 @@ android { dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" - implementation 'com.google.dagger:dagger:2.37' - kapt 'com.google.dagger:dagger-compiler:2.37' - implementation 'androidx.appcompat:appcompat:1.0.0' - implementation 'androidx.constraintlayout:constraintlayout:1.1.3' - testImplementation 'junit:junit:4.13.1' - androidTestImplementation 'androidx.test.ext:junit:1.1.1' - androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0' + implementation 'androidx.appcompat:appcompat:1.3.0' + implementation "androidx.activity:activity-ktx:1.2.3" + implementation "androidx.fragment:fragment-ktx:1.3.5" + + def lifecycle_version = "2.3.1" + implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version" + implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version" + implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version" + implementation "androidx.lifecycle:lifecycle-viewmodel-savedstate:$lifecycle_version" + + implementation 'androidx.constraintlayout:constraintlayout:2.0.4' + implementation "com.google.dagger:hilt-android:$hilt_version" + kapt "com.google.dagger:hilt-compiler:$hilt_version" + + testImplementation 'junit:junit:4.13.2' + + androidTestImplementation 'androidx.test.ext:junit:1.1.3' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' } diff --git a/mailbox/src/main/java/org/briarproject/mailbox/MailboxApplication.kt b/mailbox/src/main/java/org/briarproject/mailbox/MailboxApplication.kt index 1f3d599bcb1062ae477eff16b3dbd011875e8fe8..cc46087350518193bc455a354cc73ba0ae9a5e4a 100644 --- a/mailbox/src/main/java/org/briarproject/mailbox/MailboxApplication.kt +++ b/mailbox/src/main/java/org/briarproject/mailbox/MailboxApplication.kt @@ -1,14 +1,14 @@ package org.briarproject.mailbox import android.app.Application +import dagger.hilt.android.HiltAndroidApp +@HiltAndroidApp class MailboxApplication : Application() { - val appComponent = DaggerApplicationComponent.create() - override fun onCreate() { super.onCreate() - MailboxService.startService(this, "Waiting for messages") + MailboxService.startService(this) } -} \ No newline at end of file +} diff --git a/mailbox/src/main/java/org/briarproject/mailbox/MailboxNotificationManager.kt b/mailbox/src/main/java/org/briarproject/mailbox/MailboxNotificationManager.kt new file mode 100644 index 0000000000000000000000000000000000000000..7d9b7a4a9c5ca013f9160ffde86818db14c2598e --- /dev/null +++ b/mailbox/src/main/java/org/briarproject/mailbox/MailboxNotificationManager.kt @@ -0,0 +1,61 @@ +package org.briarproject.mailbox + +import android.app.Notification +import android.app.NotificationChannel +import android.app.NotificationManager +import android.app.NotificationManager.IMPORTANCE_LOW +import android.app.PendingIntent +import android.content.Context +import android.content.Intent +import android.os.Build +import androidx.annotation.RequiresApi +import androidx.core.app.NotificationCompat +import androidx.core.app.NotificationCompat.PRIORITY_MIN +import androidx.core.content.ContextCompat.getSystemService +import dagger.hilt.android.qualifiers.ApplicationContext +import javax.inject.Inject + +class MailboxNotificationManager @Inject constructor( + @ApplicationContext private val ctx: Context, +) { + + companion object { + private const val CHANNEL_ID = "Briar Mailbox Service" + + const val NOTIFICATION_MAIN_ID = 1 + } + + private val nm = getSystemService(ctx, NotificationManager::class.java)!! + + init { + if (Build.VERSION.SDK_INT >= 26) createNotificationChannels() + } + + @RequiresApi(26) + private fun createNotificationChannels() { + val channels = listOf( + NotificationChannel( + CHANNEL_ID, + ctx.getString(R.string.notification_channel_name), + IMPORTANCE_LOW, + ) + ) + nm.createNotificationChannels(channels) + } + + val serviceNotification: Notification + get() { + val notificationIntent = Intent(ctx, MainActivity::class.java) + val pendingIntent = PendingIntent.getActivity( + ctx, 0, notificationIntent, 0 + ) + return NotificationCompat.Builder(ctx, CHANNEL_ID) + .setContentTitle(ctx.getString(R.string.notification_mailbox_running)) + .setContentText("Waiting for messages...") + .setSmallIcon(R.drawable.ic_launcher_foreground) + .setContentIntent(pendingIntent) + .setPriority(PRIORITY_MIN) + .build() + } + +} diff --git a/mailbox/src/main/java/org/briarproject/mailbox/MailboxService.kt b/mailbox/src/main/java/org/briarproject/mailbox/MailboxService.kt index c3f50e5f9452714f52916ca0ed0b639d65012ddb..71e14e59152d4a6fc9d2485013c6ffc49439db3b 100644 --- a/mailbox/src/main/java/org/briarproject/mailbox/MailboxService.kt +++ b/mailbox/src/main/java/org/briarproject/mailbox/MailboxService.kt @@ -1,31 +1,20 @@ package org.briarproject.mailbox -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.VERSION -import android.os.Build.VERSION.SDK_INT -import android.os.Build.VERSION_CODES import android.os.IBinder -import androidx.core.app.NotificationCompat import androidx.core.content.ContextCompat +import dagger.hilt.android.AndroidEntryPoint +import org.briarproject.mailbox.MailboxNotificationManager.Companion.NOTIFICATION_MAIN_ID +import javax.inject.Inject +@AndroidEntryPoint class MailboxService : Service() { - companion object { - - private const val CHANNEL_ID = "Briar Mailbox Service" - private const val ARG_MESSAGE = "message" - - private const val NOTIFICATION_MAIN_ID = 1 - - fun startService(context: Context, message: String) { + fun startService(context: Context) { val startIntent = Intent(context, MailboxService::class.java) - startIntent.putExtra(ARG_MESSAGE, message) ContextCompat.startForegroundService(context, startIntent) } @@ -35,22 +24,11 @@ class MailboxService : Service() { } } - override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { - val input = intent?.getStringExtra(ARG_MESSAGE) - createNotificationChannel() - val notificationIntent = Intent(this, MainActivity::class.java) - val pendingIntent = PendingIntent.getActivity( - this, 0, notificationIntent, 0 - ) - - val notification = NotificationCompat.Builder(this, CHANNEL_ID) - .setContentTitle(getString(R.string.notification_mailbox_running)) - .setContentText(input) - .setSmallIcon(R.drawable.ic_launcher_foreground) - .setContentIntent(pendingIntent) - .build() + @Inject + lateinit var notificationManager: MailboxNotificationManager - startForeground(NOTIFICATION_MAIN_ID, notification) + override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { + startForeground(NOTIFICATION_MAIN_ID, notificationManager.serviceNotification) return START_NOT_STICKY } @@ -58,15 +36,4 @@ class MailboxService : Service() { return null } - private fun createNotificationChannel() { - if (SDK_INT >= 26) { - val serviceChannel = NotificationChannel( - CHANNEL_ID, getString(R.string.notification_channel_name), - NotificationManager.IMPORTANCE_DEFAULT - ) - - val manager = getSystemService(NotificationManager::class.java) - manager!!.createNotificationChannel(serviceChannel) - } - } -} \ No newline at end of file +} diff --git a/mailbox/src/main/java/org/briarproject/mailbox/MailboxViewModel.kt b/mailbox/src/main/java/org/briarproject/mailbox/MailboxViewModel.kt index e10d86d4fe022dd51f44f37d784cd283af4f653f..2784d8038c3df078fdce47fad9663d70fbf3cf52 100644 --- a/mailbox/src/main/java/org/briarproject/mailbox/MailboxViewModel.kt +++ b/mailbox/src/main/java/org/briarproject/mailbox/MailboxViewModel.kt @@ -1,9 +1,23 @@ package org.briarproject.mailbox +import android.app.Application +import androidx.lifecycle.AndroidViewModel +import androidx.lifecycle.LiveData +import androidx.lifecycle.SavedStateHandle +import dagger.hilt.android.lifecycle.HiltViewModel import javax.inject.Inject -class MailboxViewModel @Inject constructor() { +@HiltViewModel +class MailboxViewModel @Inject constructor( + app: Application, + handle: SavedStateHandle, +) : AndroidViewModel(app) { - val text = "Hello Mailbox" + private val _text = handle.getLiveData("text", "Hello Mailbox") + val text: LiveData<String> = _text -} \ No newline at end of file + fun updateText(str: String) { + _text.value = str + } + +} diff --git a/mailbox/src/main/java/org/briarproject/mailbox/MainActivity.kt b/mailbox/src/main/java/org/briarproject/mailbox/MainActivity.kt index 634d1e8813593c3ffbc188e16de23aa1b4f40c9d..78ec30908d8fa8e260b3db3736bcf876b5206bbc 100644 --- a/mailbox/src/main/java/org/briarproject/mailbox/MainActivity.kt +++ b/mailbox/src/main/java/org/briarproject/mailbox/MainActivity.kt @@ -1,23 +1,30 @@ package org.briarproject.mailbox import android.os.Bundle +import android.widget.Button import android.widget.TextView +import androidx.activity.viewModels import androidx.appcompat.app.AppCompatActivity -import javax.inject.Inject +import dagger.hilt.android.AndroidEntryPoint +@AndroidEntryPoint class MainActivity : AppCompatActivity() { - lateinit var applicationComponent: ApplicationComponent - - @Inject - lateinit var mailboxViewModel: MailboxViewModel + private val viewModel: MailboxViewModel by viewModels() override fun onCreate(savedInstanceState: Bundle?) { - (applicationContext as MailboxApplication).appComponent.inject(this) super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val textView = findViewById<TextView>(R.id.text) - textView.text = mailboxViewModel.text + val button = findViewById<Button>(R.id.button) + + button.setOnClickListener { + viewModel.updateText("Tested") + } + + viewModel.text.observe(this, { text -> + textView.text = text + }) } -} \ No newline at end of file +} diff --git a/mailbox/src/main/res/layout/activity_main.xml b/mailbox/src/main/res/layout/activity_main.xml index 18428a363919373000fca735f0b1cbe008c63051..d9571a8f01db7004980983960455c29a45575d8e 100644 --- a/mailbox/src/main/res/layout/activity_main.xml +++ b/mailbox/src/main/res/layout/activity_main.xml @@ -16,4 +16,14 @@ app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> + <Button + android:id="@+id/button" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_marginTop="16dp" + android:text="Test" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toBottomOf="@+id/text" /> + </androidx.constraintlayout.widget.ConstraintLayout> \ No newline at end of file