|
| 1 | +/* |
| 2 | + * Copyright 2025 The Android Open Source Project |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.android.developers.androidify.updater |
| 17 | + |
| 18 | +import android.content.Context |
| 19 | +import android.content.pm.PackageManager |
| 20 | +import android.os.ParcelFileDescriptor |
| 21 | +import android.util.Log |
| 22 | +import androidx.wear.watchfacepush.WatchFacePushManagerFactory |
| 23 | +import androidx.work.CoroutineWorker |
| 24 | +import androidx.work.WorkerParameters |
| 25 | +import java.io.File |
| 26 | +import java.io.FileOutputStream |
| 27 | +import java.io.IOException |
| 28 | + |
| 29 | +private const val defaultWatchFaceName = "default_watchface.apk" |
| 30 | +private const val manifestTokenKey = "com.google.android.wearable.marketplace.DEFAULT_WATCHFACE_VALIDATION_TOKEN" |
| 31 | + |
| 32 | +private const val TAG = "UpdateWorker" |
| 33 | + |
| 34 | +/** |
| 35 | + * WorkManager worker that tries to update the default watch face, if installed. |
| 36 | + * |
| 37 | + * Checks which watch faces the package already has installed, and if there is a default watch face |
| 38 | + * in the assets bundle. Compares the versions of these to determine whether an update is necessary |
| 39 | + * and if so, updates the default watch face, taking also the new watch face validation token from |
| 40 | + * the manifest file. |
| 41 | + */ |
| 42 | +class UpdateWorker(appContext: Context, workerParams: WorkerParameters) : |
| 43 | + CoroutineWorker(appContext, workerParams) { |
| 44 | + |
| 45 | + override suspend fun doWork(): Result { |
| 46 | + val watchFacePushManager = WatchFacePushManagerFactory.createWatchFacePushManager(applicationContext) |
| 47 | + |
| 48 | + val watchFaces = watchFacePushManager.listWatchFaces().installedWatchFaceDetails |
| 49 | + .associateBy { it.packageName } |
| 50 | + |
| 51 | + val copiedFile = File.createTempFile("tmp", ".apk", applicationContext.cacheDir) |
| 52 | + try { |
| 53 | + applicationContext.assets.open(defaultWatchFaceName).use { inputStream -> |
| 54 | + FileOutputStream(copiedFile).use { outputStream -> inputStream.copyTo(outputStream) } |
| 55 | + } |
| 56 | + val packageInfo = |
| 57 | + applicationContext.packageManager.getPackageArchiveInfo(copiedFile.absolutePath, 0) |
| 58 | + |
| 59 | + packageInfo?.let { newPkg -> |
| 60 | + // Check if the default watch face is currently installed and should therefore be |
| 61 | + // updated if the one in the assets folder has a higher version code. |
| 62 | + watchFaces[newPkg.packageName]?.let { curPkg -> |
| 63 | + if (newPkg.longVersionCode > curPkg.versionCode) { |
| 64 | + ParcelFileDescriptor.open( |
| 65 | + copiedFile, |
| 66 | + ParcelFileDescriptor.MODE_READ_ONLY, |
| 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 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } catch (e: IOException) { |
| 80 | + Log.w(TAG, "Watch face not updated", e) |
| 81 | + return Result.failure() |
| 82 | + } finally { |
| 83 | + copiedFile.delete() |
| 84 | + } |
| 85 | + return Result.success() |
| 86 | + } |
| 87 | + |
| 88 | + private fun getDefaultWatchFaceToken(): String? { |
| 89 | + val appInfo = applicationContext.packageManager.getApplicationInfo( |
| 90 | + applicationContext.packageName, |
| 91 | + PackageManager.GET_META_DATA, |
| 92 | + ) |
| 93 | + return appInfo.metaData?.getString(manifestTokenKey) |
| 94 | + } |
| 95 | +} |
0 commit comments