@@ -9,13 +9,8 @@ import android.annotation.SuppressLint
9
9
import android.app.PendingIntent
10
10
import android.content.Intent
11
11
import android.graphics.Bitmap
12
- import android.graphics.Canvas
13
12
import android.graphics.drawable.Icon
14
- import android.os.Build
15
13
import android.util.Log
16
- import android.util.TypedValue
17
- import android.view.View
18
- import android.view.View.MeasureSpec
19
14
import androidx.wear.watchface.complications.data.ComplicationData
20
15
import androidx.wear.watchface.complications.data.ComplicationText
21
16
import androidx.wear.watchface.complications.data.ComplicationType
@@ -30,13 +25,11 @@ import androidx.wear.watchface.complications.datasource.SuspendingComplicationDa
30
25
import com.example.util.simpletimetracker.R
31
26
import com.example.util.simpletimetracker.data.WearDataRepo
32
27
import com.example.util.simpletimetracker.data.WearIconMapper
33
- import com.example.util.simpletimetracker.data.WearMessenger
34
28
import com.example.util.simpletimetracker.domain.WearActivityIcon
35
29
import com.example.util.simpletimetracker.presentation.MainActivity
36
30
import dagger.hilt.android.AndroidEntryPoint
37
31
import java.time.Instant
38
32
import javax.inject.Inject
39
- import kotlin.math.roundToInt
40
33
41
34
@AndroidEntryPoint
42
35
class WearComplicationService : SuspendingComplicationDataSourceService () {
@@ -47,9 +40,9 @@ class WearComplicationService : SuspendingComplicationDataSourceService() {
47
40
@Inject
48
41
lateinit var iconMapper: WearIconMapper
49
42
50
- private val tag: String = WearMessenger ::class .java.name
43
+ private val tag: String = WearComplicationService ::class .java.name
51
44
private val appIcon = R .drawable.app_ic_launcher_monochrome
52
- private val iconSize by lazy { 20 .dpToPx() }
45
+ private val iconSizeDp = 20
53
46
private val defaultText = " ×"
54
47
private val previewText = " Tracking"
55
48
@@ -144,41 +137,14 @@ class WearComplicationService : SuspendingComplicationDataSourceService() {
144
137
}
145
138
146
139
private fun getBitmap (icon : WearActivityIcon ): Bitmap {
147
- return IconView (this )
140
+ return WearIconView (this )
148
141
.apply {
149
142
itemIcon = icon
150
- measureExactly(iconSize )
143
+ measureExactly(iconSizeDp.dpToPx( this .context) )
151
144
}
152
145
.getBitmapFromView()
153
146
}
154
147
155
- private fun View.measureExactly (width : Int , height : Int = width) {
156
- val specWidth = MeasureSpec .makeMeasureSpec(width, MeasureSpec .EXACTLY )
157
- val specHeight = MeasureSpec .makeMeasureSpec(height, MeasureSpec .EXACTLY )
158
- measure(specWidth, specHeight)
159
- layout(0 , 0 , measuredWidth, measuredHeight)
160
- }
161
-
162
- private fun View.getBitmapFromView (): Bitmap {
163
- fun Int.checkValue (): Int = this .takeUnless { it <= 0 } ? : iconSize
164
-
165
- return Bitmap .createBitmap(
166
- measuredWidth.checkValue(),
167
- measuredHeight.checkValue(),
168
- Bitmap .Config .ARGB_8888 ,
169
- ).also {
170
- draw(Canvas (it))
171
- }
172
- }
173
-
174
- private fun Int.dpToPx (): Int {
175
- return TypedValue .applyDimension(
176
- TypedValue .COMPLEX_UNIT_DIP ,
177
- this .toFloat(),
178
- this @WearComplicationService.resources.displayMetrics,
179
- ).roundToInt()
180
- }
181
-
182
148
@SuppressLint(" WearRecents" )
183
149
private fun getMainStartIntent (): PendingIntent {
184
150
val startIntent = Intent (this , MainActivity ::class .java).apply {
@@ -189,15 +155,7 @@ class WearComplicationService : SuspendingComplicationDataSourceService() {
189
155
this ,
190
156
0 ,
191
157
startIntent,
192
- getFlags (),
158
+ getPendingIntentFlags (),
193
159
)
194
160
}
195
-
196
- private fun getFlags (): Int {
197
- return if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .S ) {
198
- PendingIntent .FLAG_UPDATE_CURRENT or PendingIntent .FLAG_MUTABLE
199
- } else {
200
- PendingIntent .FLAG_UPDATE_CURRENT
201
- }
202
- }
203
161
}
0 commit comments