@@ -39,55 +39,55 @@ private const val TAG = "UpdateWorker"
3939 * and if so, updates the default watch face, taking also the new watch face validation token from
4040 * the manifest file.
4141 */
42- class UpdateWorker (val appContext : Context , workerParams : WorkerParameters ) :
42+ class UpdateWorker (appContext : Context , workerParams : WorkerParameters ) :
4343 CoroutineWorker (appContext, workerParams) {
4444
4545 override suspend fun doWork (): Result {
46- val watchFacePushManager = WatchFacePushManagerFactory .createWatchFacePushManager(appContext )
46+ val watchFacePushManager = WatchFacePushManagerFactory .createWatchFacePushManager(applicationContext )
4747
4848 val watchFaces = watchFacePushManager.listWatchFaces().installedWatchFaceDetails
4949 .associateBy { it.packageName }
5050
51- val copiedFile = File .createTempFile(" tmp" , " .apk" , appContext .cacheDir)
51+ val copiedFile = File .createTempFile(" tmp" , " .apk" , applicationContext .cacheDir)
5252 try {
53- copiedFile.deleteOnExit()
54- appContext.assets.open(defaultWatchFaceName).use { inputStream ->
53+ applicationContext.assets.open(defaultWatchFaceName).use { inputStream ->
5554 FileOutputStream (copiedFile).use { outputStream -> inputStream.copyTo(outputStream) }
5655 }
5756 val packageInfo =
58- appContext .packageManager.getPackageArchiveInfo(copiedFile.absolutePath, 0 )
57+ applicationContext .packageManager.getPackageArchiveInfo(copiedFile.absolutePath, 0 )
5958
6059 packageInfo?.let { newPkg ->
6160 // Check if the default watch face is currently installed and should therefore be
6261 // updated if the one in the assets folder has a higher version code.
6362 watchFaces[newPkg.packageName]?.let { curPkg ->
6463 if (newPkg.longVersionCode > curPkg.versionCode) {
65- val pfd = ParcelFileDescriptor .open(
64+ ParcelFileDescriptor .open(
6665 copiedFile,
6766 ParcelFileDescriptor .MODE_READ_ONLY ,
68- )
69- val token = getDefaultWatchFaceToken()
70- if (token != null ) {
71- watchFacePushManager.updateWatchFace(curPkg.slotId, pfd, token)
72- Log .d(TAG , " Watch face updated from ${curPkg.versionCode} to ${newPkg.longVersionCode} " )
73- } else {
74- Log .w(TAG , " Watch face not updated, no token found" )
67+ ).use { pfd ->
68+ val token = getDefaultWatchFaceToken()
69+ if (token != null ) {
70+ watchFacePushManager.updateWatchFace(curPkg.slotId, pfd, token)
71+ Log .d(TAG , " Watch face updated from ${curPkg.versionCode} to ${newPkg.longVersionCode} " )
72+ } else {
73+ Log .w(TAG , " Watch face not updated, no token found" )
74+ }
7575 }
76- pfd.close()
7776 }
7877 }
7978 }
8079 } catch (e: IOException ) {
8180 Log .w(TAG , " Watch face not updated" , e)
81+ return Result .failure()
8282 } finally {
8383 copiedFile.delete()
8484 }
8585 return Result .success()
8686 }
8787
8888 private fun getDefaultWatchFaceToken (): String? {
89- val appInfo = appContext .packageManager.getApplicationInfo(
90- appContext .packageName,
89+ val appInfo = applicationContext .packageManager.getApplicationInfo(
90+ applicationContext .packageName,
9191 PackageManager .GET_META_DATA ,
9292 )
9393 return appInfo.metaData?.getString(manifestTokenKey)
0 commit comments