Skip to content

Commit 8b3aab8

Browse files
committed
O compatibility
1 parent 0611926 commit 8b3aab8

File tree

7 files changed

+59
-24
lines changed

7 files changed

+59
-24
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ android {
7272

7373
dependencies {
7474

75-
def final SUPPORT_VERSION = '26.0.1'
75+
def final SUPPORT_VERSION = '26.1.0'
7676
def final ARCH_VERSION = '1.0.0-alpha7'
7777

7878
compile fileTree(dir: 'libs', include: ['*.jar'])

app/src/main/kotlin/com/glodanif/bluetoothchat/service/BluetoothConnectionService.kt

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.glodanif.bluetoothchat.service
22

3+
import android.app.NotificationManager
34
import android.app.Service
45
import android.bluetooth.BluetoothAdapter
56
import android.bluetooth.BluetoothDevice
@@ -9,6 +10,7 @@ import android.content.Context
910
import android.content.Intent
1011
import android.content.ServiceConnection
1112
import android.os.Binder
13+
import android.os.Build
1214
import android.os.Handler
1315
import android.os.IBinder
1416
import android.support.v7.app.AppCompatActivity
@@ -116,13 +118,15 @@ class BluetoothConnectionService : Service() {
116118
startForeground(FOREGROUND_SERVICE, notification)
117119
}
118120

119-
@Synchronized fun disconnect() {
121+
@Synchronized
122+
fun disconnect() {
120123
connectedThread?.cancel(true)
121124
connectedThread = null
122125
prepareForAccept()
123126
}
124127

125-
@Synchronized fun prepareForAccept() {
128+
@Synchronized
129+
fun prepareForAccept() {
126130

127131
Log.d(TAG, "start")
128132

@@ -133,7 +137,8 @@ class BluetoothConnectionService : Service() {
133137
showNotification(getString(R.string.notification__ready_to_connect))
134138
}
135139

136-
@Synchronized fun connect(device: BluetoothDevice) {
140+
@Synchronized
141+
fun connect(device: BluetoothDevice) {
137142

138143
log("connect to: ${device.name}")
139144

@@ -155,7 +160,8 @@ class BluetoothConnectionService : Service() {
155160
handler.post { connectionListener?.onConnecting() }
156161
}
157162

158-
@Synchronized fun connected(socket: BluetoothSocket, type: ConnectionType) {
163+
@Synchronized
164+
fun connected(socket: BluetoothSocket, type: ConnectionType) {
159165

160166
cancelConnections()
161167

@@ -174,7 +180,8 @@ class BluetoothConnectionService : Service() {
174180
log("connected")
175181
}
176182

177-
@Synchronized fun stop() {
183+
@Synchronized
184+
fun stop() {
178185

179186
cancelConnections()
180187

@@ -226,8 +233,7 @@ class BluetoothConnectionService : Service() {
226233

227234
val message = Message(messageBody)
228235

229-
val sentMessage: ChatMessage = ChatMessage(
230-
currentSocket!!.remoteDevice.address, Date(), true, message.body)
236+
val sentMessage = ChatMessage(currentSocket!!.remoteDevice.address, Date(), true, message.body)
231237

232238
if (message.type == Message.Type.MESSAGE) {
233239
sentMessage.seenHere = true
@@ -284,8 +290,7 @@ class BluetoothConnectionService : Service() {
284290

285291
val device: BluetoothDevice = currentSocket!!.remoteDevice
286292

287-
val receivedMessage: ChatMessage =
288-
ChatMessage(device.address, Date(), false, message.body)
293+
val receivedMessage = ChatMessage(device.address, Date(), false, message.body)
289294

290295
if (messageListener == null || application.currentChat == null || !application.currentChat.equals(device.address)) {
291296
notificationView.showNewMessageNotification(message.body, currentConversation?.displayName,
@@ -593,6 +598,11 @@ class BluetoothConnectionService : Service() {
593598

594599
fun start(context: Context) {
595600
val intent = Intent(context, BluetoothConnectionService::class.java)
601+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
602+
context.startForegroundService(intent)
603+
} else {
604+
context.startService(intent)
605+
}
596606
context.startService(intent)
597607
}
598608

app/src/main/kotlin/com/glodanif/bluetoothchat/view/NotificationViewImpl.kt

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package com.glodanif.bluetoothchat.view
22

3-
import android.app.Notification
4-
import android.app.NotificationManager
5-
import android.app.PendingIntent
6-
import android.app.Service
3+
import android.app.*
74
import android.content.Context
85
import android.content.Intent
96
import android.graphics.Bitmap
107
import android.graphics.BitmapFactory
118
import android.graphics.Color
129
import android.os.Build
10+
import android.support.v4.app.NotificationCompat
1311
import com.glodanif.bluetoothchat.R
1412
import com.glodanif.bluetoothchat.activity.ChatActivity
1513
import com.glodanif.bluetoothchat.activity.ConversationsActivity
@@ -18,6 +16,10 @@ import com.glodanif.bluetoothchat.util.NotificationSettings
1816

1917
class NotificationViewImpl(private val context: Context) : NotificationView {
2018

19+
private val CHANNEL_FOREGROUND = "channel.foreground"
20+
private val CHANNEL_REQUEST = "channel.request"
21+
private val CHANNEL_MESSAGE = "channel.message"
22+
2123
private val notificationManager =
2224
context.getSystemService(Service.NOTIFICATION_SERVICE) as NotificationManager
2325
private val resources = context.resources
@@ -32,9 +34,13 @@ class NotificationViewImpl(private val context: Context) : NotificationView {
3234
stopIntent.action = BluetoothConnectionService.ACTION_STOP
3335
val stopPendingIntent = PendingIntent.getService(context, 0, stopIntent, 0)
3436

35-
val icon = BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher)
37+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
38+
val channel = NotificationChannel(CHANNEL_FOREGROUND, context.getString(R.string.notification__channel_background), NotificationManager.IMPORTANCE_LOW)
39+
notificationManager.createNotificationChannel(channel)
40+
}
3641

37-
val builder = Notification.Builder(context)
42+
val icon = BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher)
43+
val builder = NotificationCompat.Builder(context, CHANNEL_FOREGROUND)
3844
.setContentTitle(context.getString(R.string.app_name))
3945
.setContentText(message)
4046
.setSmallIcon(R.drawable.ic_notification)
@@ -45,7 +51,7 @@ class NotificationViewImpl(private val context: Context) : NotificationView {
4551
.addAction(0, context.getString(R.string.notification__stop), stopPendingIntent)
4652

4753
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
48-
builder.setColor(resources.getColor(R.color.colorPrimary))
54+
builder.color = resources.getColor(R.color.colorPrimary)
4955
}
5056

5157
return builder.build()
@@ -61,7 +67,12 @@ class NotificationViewImpl(private val context: Context) : NotificationView {
6167
val icon = BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher)
6268
val name = if (displayName.isNullOrEmpty()) deviceName else "$displayName ($deviceName)"
6369

64-
val builder = Notification.Builder(context)
70+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
71+
val channel = NotificationChannel(CHANNEL_MESSAGE, context.getString(R.string.notification__channel_message), NotificationManager.IMPORTANCE_MAX)
72+
notificationManager.createNotificationChannel(channel)
73+
}
74+
75+
val builder = NotificationCompat.Builder(context, CHANNEL_MESSAGE)
6576
.setContentTitle(name)
6677
.setContentText(message)
6778
.setLights(Color.BLUE, 3000, 3000)
@@ -72,7 +83,7 @@ class NotificationViewImpl(private val context: Context) : NotificationView {
7283
.setPriority(Notification.PRIORITY_MAX)
7384

7485
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
75-
builder.setColor(resources.getColor(R.color.colorPrimary))
86+
builder.color = resources.getColor(R.color.colorPrimary)
7687
}
7788

7889
val notification = builder.build()
@@ -94,9 +105,14 @@ class NotificationViewImpl(private val context: Context) : NotificationView {
94105
notificationIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
95106
val pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0)
96107

108+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
109+
val channel = NotificationChannel(CHANNEL_REQUEST, context.getString(R.string.notification__channel_request), NotificationManager.IMPORTANCE_MAX)
110+
notificationManager.createNotificationChannel(channel)
111+
}
112+
97113
val icon = BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher)
98114

99-
val builder = Notification.Builder(context)
115+
val builder = NotificationCompat.Builder(context, CHANNEL_REQUEST)
100116
.setContentTitle(context.getString(R.string.notification__connection_request))
101117
.setContentText(context.getString(R.string.notification__connection_request_body, deviceName))
102118
.setLights(Color.BLUE, 3000, 3000)
@@ -107,7 +123,7 @@ class NotificationViewImpl(private val context: Context) : NotificationView {
107123
.setPriority(Notification.PRIORITY_MAX)
108124

109125
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
110-
builder.setColor(resources.getColor(R.color.colorPrimary))
126+
builder.color = resources.getColor(R.color.colorPrimary)
111127
}
112128

113129
val notification = builder.build()
@@ -125,7 +141,7 @@ class NotificationViewImpl(private val context: Context) : NotificationView {
125141

126142
override fun dismissMessageNotification() {
127143
notificationManager.cancel(
128-
NotificationView.NOTIFICATION_TAG_MESSAGE,NotificationView.NOTIFICATION_ID_MESSAGE)
144+
NotificationView.NOTIFICATION_TAG_MESSAGE, NotificationView.NOTIFICATION_ID_MESSAGE)
129145
}
130146

131147
override fun dismissConnectionNotification() {

app/src/main/res/values-ru/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@
111111
<string name="notification__connection_request">Запрос на соединение</string>
112112
<string name="notification__connection_request_body">%s хочет подключиться к вам</string>
113113
<string name="notification__stop">ОСТАНОВИТЬ</string>
114+
<string name="notification__channel_background">Фоновый сервис</string>
115+
<string name="notification__channel_request">Запрос на соединение</string>
116+
<string name="notification__channel_message">Новое сообщение</string>
114117

115118
<string name="settings__title">Настройки</string>
116119
<string name="settings__notifications">Уведомления</string>

app/src/main/res/values-uk/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@
111111
<string name="notification__connection_request">Запит на з\'єднання</string>
112112
<string name="notification__connection_request_body">%s хоче з\'єднатися з вами</string>
113113
<string name="notification__stop">ЗУПИНИТИ</string>
114+
<string name="notification__channel_background">Фоновий сервіс</string>
115+
<string name="notification__channel_request">Запит на з\'єднання</string>
116+
<string name="notification__channel_message">Нове повідомлення</string>
114117

115118
<string name="settings__title">Налаштування</string>
116119
<string name="settings__notifications">Сповіщення</string>

app/src/main/res/values/strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@
105105
<string name="notification__connection_request">Connection request</string>
106106
<string name="notification__connection_request_body">wants to connect to you %s</string>
107107
<string name="notification__stop">STOP</string>
108+
<string name="notification__channel_background">Background Service</string>
109+
<string name="notification__channel_request">Connection Request</string>
110+
<string name="notification__channel_message">New Message</string>
108111

109112
<string name="settings__title">Settings</string>
110113
<string name="settings__notifications">Notifications</string>

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
buildscript {
22

3-
ext.KOTLIN_VERSION = '1.1.4'
3+
ext.KOTLIN_VERSION = '1.1.4-2'
44

55
repositories {
66
jcenter()
@@ -11,7 +11,7 @@ buildscript {
1111
}
1212
dependencies {
1313
classpath 'com.android.tools.build:gradle:2.3.3'
14-
classpath 'io.fabric.tools:gradle:1.23.0'
14+
classpath 'io.fabric.tools:gradle:1.24.1'
1515
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$KOTLIN_VERSION"
1616
}
1717
}

0 commit comments

Comments
 (0)