Skip to content

Commit 402a5c7

Browse files
committed
Addresses comments
1 parent da5f82c commit 402a5c7

File tree

5 files changed

+21
-24
lines changed

5 files changed

+21
-24
lines changed

gradle/libs.versions.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# build
33
appVersionCode = "10"
44
appVersionName = "1.3.0"
5+
appVersionWearOffset = "60000000"
56
agp = "8.11.1"
67
bcpkixJdk18on = "1.81"
78
compileSdk = "36"

wear/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ android {
3535
applicationId = "com.android.developers.androidify"
3636
targetSdk = 36
3737
// Ensure Wear OS app has its own version space
38-
versionCode = 60_000_000 + libs.versions.appVersionCode.get().toInt()
38+
versionCode = libs.versions.appVersionWearOffset.get().toInt() + libs.versions.appVersionCode.get().toInt()
3939
versionName = libs.versions.appVersionName.get()
4040
}
4141

wear/src/main/java/com/android/developers/androidify/updater/UpdateReceiver.kt

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,7 @@ import androidx.work.WorkManager
2929
* onReceive.
3030
*/
3131
class UpdateReceiver : BroadcastReceiver() {
32-
override fun onReceive(context: Context?, intent: Intent?) {
33-
if (context == null || intent == null) {
34-
return
35-
}
36-
32+
override fun onReceive(context: Context, intent: Intent) {
3733
if (Intent.ACTION_MY_PACKAGE_REPLACED == intent.action) {
3834
val updateRequest = OneTimeWorkRequestBuilder<UpdateWorker>().build()
3935
val workManager = WorkManager.getInstance(context)

wear/src/main/java/com/android/developers/androidify/updater/UpdateWorker.kt

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

wear/watchface/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ android {
2727
minSdk = 36
2828
targetSdk = 36
2929
// The default watch face version is kept in lock step with the Wear OS app.
30-
versionCode = 60_000_000 + libs.versions.appVersionCode.get().toInt()
30+
versionCode = libs.versions.appVersionWearOffset.get().toInt() + libs.versions.appVersionCode.get().toInt()
3131
versionName = libs.versions.appVersionName.get()
3232
}
3333

0 commit comments

Comments
 (0)