Skip to content

Commit

Permalink
Cleanup (#3)
Browse files Browse the repository at this point in the history
* Delete unused icon

* Cleanup build gradle

* Update all dependencies

* Retro compatibility to lollipop

* Cleanup the main activity
  • Loading branch information
CedrickFlocon authored and balloob committed Oct 25, 2019
1 parent 7b6fbbe commit 5cf20a5
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 102 deletions.
33 changes: 10 additions & 23 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,37 +1,24 @@
apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 28
compileSdkVersion 29

defaultConfig {
applicationId "io.homeassistant.android"
minSdkVersion 24
targetSdkVersion 28
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
versionName "1.0.0"
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.google.firebase:firebase-core:16.0.1'
implementation 'com.google.firebase:firebase-messaging:17.5.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "androidx.appcompat:appcompat:1.1.0"
implementation "com.google.firebase:firebase-core:17.2.0"
implementation "com.google.firebase:firebase-messaging:20.0.0"
}

apply plugin: 'com.google.gms.google-services' // Google Play services Gradle plugin
apply plugin: 'com.google.gms.google-services'
Binary file removed app/src/main/ic_launcher-web.png
Binary file not shown.
63 changes: 26 additions & 37 deletions app/src/main/java/io/homeassistant/android/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package io.homeassistant.android

import android.util.Log
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.tasks.OnCompleteListener
import com.google.firebase.iid.FirebaseInstanceId
import com.google.firebase.messaging.FirebaseMessaging
import android.webkit.WebView
import android.webkit.WebViewClient
import android.support.v4.content.ContextCompat
import android.view.WindowManager



class MainActivity : AppCompatActivity() {
Expand All @@ -20,35 +16,28 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

val window = getWindow()

window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)

window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)

window.setStatusBarColor(ContextCompat.getColor(applicationContext, R.color.colorPrimary))

val myWebView: WebView = findViewById(R.id.webview)
myWebView.settings.javaScriptEnabled = true
myWebView.settings.domStorageEnabled = true
myWebView.webViewClient = WebViewClient()
myWebView.loadUrl("https://demo.home-assistant.io")

FirebaseInstanceId.getInstance().instanceId
.addOnCompleteListener(OnCompleteListener { task ->
if (!task.isSuccessful) {
Log.w(TAG, "getInstanceId failed", task.exception)
return@OnCompleteListener
}

// Get new Instance ID token
val token = task.result?.token

// Log and toast
val msg = getString(R.string.msg_token_fmt, token)
Log.d(TAG, msg)
Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
})
findViewById<WebView>(R.id.webview).apply {
settings.javaScriptEnabled = true
settings.domStorageEnabled = true
webViewClient = WebViewClient()
loadUrl("https://demo.home-assistant.io")
}

FirebaseInstanceId.getInstance()
.instanceId.addOnCompleteListener(OnCompleteListener { task ->
if (!task.isSuccessful) {
Log.w(TAG, "getInstanceId failed", task.exception)
return@OnCompleteListener
}

// Get new Instance ID token
val token = task.result?.token

// Log and toast
val msg = getString(R.string.msg_token_fmt, token)
Log.d(TAG, msg)
Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
})
}

companion object {
Expand Down
34 changes: 16 additions & 18 deletions app/src/main/java/io/homeassistant/android/MessagingService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,45 @@ package io.homeassistant.android
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.Service
import android.content.Context
import android.content.Context.NOTIFICATION_SERVICE
import android.content.Intent
import android.media.RingtoneManager
import android.net.Uri
import android.os.Build
import android.os.IBinder
import android.support.v4.app.NotificationCompat
import android.support.v4.content.ContextCompat.getSystemService
import android.util.Log
import androidx.core.app.NotificationCompat
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import io.homeassistant.android.MainActivity
import io.homeassistant.android.R


class MessagingService : FirebaseMessagingService() {

override fun onMessageReceived(remoteMessage: RemoteMessage?) {
override fun onMessageReceived(remoteMessage: RemoteMessage) {
// Handle data payload of FCM messages.

// TODO(developer): Handle FCM messages here.
// Not getting messages here? See why this may be: https://goo.gl/39bRNJ
Log.d(TAG, "From: ${remoteMessage?.from}")
Log.d(TAG, "From: ${remoteMessage.from}")

// Check if message contains a data payload.
remoteMessage?.data?.isNotEmpty()?.let {
remoteMessage.data.isNotEmpty().let {
Log.d(TAG, "Message data payload: " + remoteMessage.data)
}

// Check if message contains a notification payload.
remoteMessage?.notification?.let {
remoteMessage.notification?.let {
Log.d(TAG, "Message Notification Body: ${it.body}")
}

sendNotification(remoteMessage!!)
sendNotification(remoteMessage)
}

private fun sendNotification(remoteMessage: RemoteMessage) {
val intent = Intent(this, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT)
val pendingIntent = PendingIntent.getActivity(
this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT
)

val channelId = getString(R.string.default_notification_channel_id)
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
Expand All @@ -59,13 +54,16 @@ class MessagingService : FirebaseMessagingService() {
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)

val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(channelId,
val channel = NotificationChannel(
channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT)
NotificationManager.IMPORTANCE_DEFAULT
)
notificationManager.createNotificationChannel(channel)
}

Expand Down
19 changes: 4 additions & 15 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<WebView
android:layout_width="395dp"
android:layout_height="715dp" app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp" app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp" app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp" android:layout_marginEnd="8dp" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5" android:id="@+id/webview"/>
</android.support.constraint.ConstraintLayout>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
12 changes: 4 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.3.21'
ext.kotlin_version = '1.3.50'
repositories {
google()
jcenter()

}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
classpath 'com.android.tools.build:gradle:3.5.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.gms:google-services:4.2.0' // Google Services plugin
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
classpath 'com.google.gms:google-services:4.3.2'
}
}

Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.3-all.zip

0 comments on commit 5cf20a5

Please sign in to comment.