Skip to content

Commit 37c369f

Browse files
author
Rachel Prince
committed
Rename functions for clarity
1 parent 32a9fd9 commit 37c369f

File tree

4 files changed

+28
-23
lines changed

4 files changed

+28
-23
lines changed

firebase-app-distribution/src/main/java/com/google/firebase/appdistribution/CheckForNewReleaseClient.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
package com.google.firebase.appdistribution;
1616

17-
import static com.google.firebase.appdistribution.internal.ReleaseIdentificationUtils.calculateApkInternalCodeHash;
17+
import static com.google.firebase.appdistribution.internal.ReleaseIdentificationUtils.calculateApkHash;
1818

1919
import android.content.Context;
2020
import android.content.pm.PackageInfo;
@@ -43,7 +43,7 @@ class CheckForNewReleaseClient {
4343
private final FirebaseApp firebaseApp;
4444
private final FirebaseAppDistributionTesterApiClient firebaseAppDistributionTesterApiClient;
4545
private final FirebaseInstallationsApi firebaseInstallationsApi;
46-
private static final ConcurrentMap<String, String> cachedCodeHashes = new ConcurrentHashMap<>();
46+
private static final ConcurrentMap<String, String> cachedApkHashes = new ConcurrentHashMap<>();
4747
private final ReleaseIdentifierStorage releaseIdentifierStorage;
4848

4949
Task<AppDistributionReleaseInternal> cachedCheckForNewRelease = null;
@@ -127,7 +127,8 @@ AppDistributionReleaseInternal getNewReleaseFromClient(
127127
firebaseAppDistributionTesterApiClient.fetchNewRelease(
128128
fid, appId, apiKey, authToken, firebaseApp.getApplicationContext());
129129

130-
if (isNewerBuildVersion(retrievedNewRelease) || !isInstalledRelease(retrievedNewRelease)) {
130+
if (isNewerBuildVersion(retrievedNewRelease)
131+
|| !isSameAsInstalledRelease(retrievedNewRelease)) {
131132
return retrievedNewRelease;
132133
} else {
133134
// Return null if retrieved new release is older or currently installed
@@ -149,9 +150,9 @@ private boolean isNewerBuildVersion(AppDistributionReleaseInternal newRelease)
149150
}
150151

151152
@VisibleForTesting
152-
boolean isInstalledRelease(AppDistributionReleaseInternal newRelease) {
153+
boolean isSameAsInstalledRelease(AppDistributionReleaseInternal newRelease) {
153154
if (newRelease.getBinaryType().equals(BinaryType.APK)) {
154-
return hasSameApkHashAsInstalledRelease(newRelease);
155+
return hasSameHashAsInstalledRelease(newRelease);
155156
}
156157

157158
if (newRelease.getIasArtifactId() == null) {
@@ -186,13 +187,13 @@ String extractApkHash(PackageInfo packageInfo) {
186187
String key =
187188
String.format(
188189
Locale.ENGLISH, "%s.%d", sourceFile.getAbsolutePath(), sourceFile.lastModified());
189-
if (!cachedCodeHashes.containsKey(key)) {
190-
cachedCodeHashes.put(key, calculateApkInternalCodeHash(sourceFile));
190+
if (!cachedApkHashes.containsKey(key)) {
191+
cachedApkHashes.put(key, calculateApkHash(sourceFile));
191192
}
192-
return cachedCodeHashes.get(key);
193+
return cachedApkHashes.get(key);
193194
}
194195

195-
private boolean hasSameApkHashAsInstalledRelease(AppDistributionReleaseInternal newRelease) {
196+
private boolean hasSameHashAsInstalledRelease(AppDistributionReleaseInternal newRelease) {
196197
try {
197198
Context context = firebaseApp.getApplicationContext();
198199
PackageInfo metadataPackageInfo =

firebase-app-distribution/src/main/java/com/google/firebase/appdistribution/UpdateApkClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import static com.google.firebase.appdistribution.FirebaseAppDistributionException.Status;
1818
import static com.google.firebase.appdistribution.FirebaseAppDistributionException.Status.NETWORK_FAILURE;
19-
import static com.google.firebase.appdistribution.internal.ReleaseIdentificationUtils.calculateApkInternalCodeHash;
19+
import static com.google.firebase.appdistribution.internal.ReleaseIdentificationUtils.calculateApkHash;
2020

2121
import android.app.Activity;
2222
import android.content.Context;
@@ -237,7 +237,7 @@ private void downloadToDisk(
237237

238238
File downloadedFile = new File(firebaseApp.getApplicationContext().getFilesDir(), fileName);
239239

240-
String internalCodeHash = calculateApkInternalCodeHash(downloadedFile);
240+
String internalCodeHash = calculateApkHash(downloadedFile);
241241

242242
if (internalCodeHash != null) {
243243
releaseIdentifierStorage.setCodeHashMap(internalCodeHash, newRelease);

firebase-app-distribution/src/main/java/com/google/firebase/appdistribution/internal/ReleaseIdentificationUtils.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public static String extractInternalAppSharingArtifactId(@NonNull Context appCon
5252
}
5353

5454
@Nullable
55-
public static String calculateApkInternalCodeHash(@NonNull File file) {
55+
public static String calculateApkHash(@NonNull File file) {
5656
Log.v(TAG, String.format("Calculating release id for %s", file.getPath()));
5757
Log.v(TAG, String.format("File size: %d", file.length()));
5858

@@ -64,8 +64,10 @@ public static String calculateApkInternalCodeHash(@NonNull File file) {
6464
ArrayList<Byte> checksums = new ArrayList<>();
6565

6666
// Since calculating the codeHash returned from the release backend is computationally
67-
// expensive, using existing checksum data from the ZipFile we can quickly calculate
68-
// an intermediate hash that then gets mapped to the backend's returned release codehash
67+
// expensive, we has the existing checksum data from the ZipFile and compare it to
68+
// (1) the apk hash returned by the backend, or (2) look up a mapping from the apk zip hash to
69+
// the
70+
// full codehash, and compare that to the codehash to the backend
6971
ZipFile zis = new ZipFile(file);
7072
try {
7173
Enumeration<? extends ZipEntry> zipEntries = zis.entries();

firebase-app-distribution/src/test/java/com/google/firebase/appdistribution/CheckForNewReleaseClientTest.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -290,44 +290,46 @@ public void handleNewReleaseFromClient_whenNewReleaseIsSameAsInstalledAab_return
290290
}
291291

292292
@Test
293-
public void isInstalledRelease_whenApkHashesEqual_returnsTrue() {
293+
public void iisSameAsInstalledRelease_whenApkHashesEqual_returnsTrue() {
294294
doReturn(CURRENT_APK_HASH).when(checkForNewReleaseClient).extractApkHash(any());
295-
assertTrue(checkForNewReleaseClient.isInstalledRelease(getTestInstalledRelease().build()));
295+
assertTrue(
296+
checkForNewReleaseClient.isSameAsInstalledRelease(getTestInstalledRelease().build()));
296297
}
297298

298299
@Test
299-
public void isInstalledRelease_whenApkHashesNotEqual_returnsFalse() {
300+
public void isSameAsInstalledRelease_whenApkHashesNotEqual_returnsFalse() {
300301
doReturn(CURRENT_APK_HASH).when(checkForNewReleaseClient).extractApkHash(any());
301-
assertFalse(checkForNewReleaseClient.isInstalledRelease(getTestNewRelease().build()));
302+
assertFalse(checkForNewReleaseClient.isSameAsInstalledRelease(getTestNewRelease().build()));
302303
}
303304

304305
@Test
305-
public void isInstalledRelease_ifApkHashNotPresent_fallsBackToExternalCodeHash() {
306+
public void isSameAsInstalledRelease_ifApkHashNotPresent_fallsBackToExternalCodeHash() {
306307
doReturn(CURRENT_APK_HASH).when(checkForNewReleaseClient).extractApkHash(any());
307308
when(mockReleaseIdentifierStorage.getExternalCodeHash(any())).thenReturn(CURRENT_CODEHASH);
308309

309310
assertFalse(
310-
checkForNewReleaseClient.isInstalledRelease(getTestNewRelease().setApkHash("").build()));
311+
checkForNewReleaseClient.isSameAsInstalledRelease(
312+
getTestNewRelease().setApkHash("").build()));
311313
verify(mockReleaseIdentifierStorage).getExternalCodeHash(CURRENT_APK_HASH);
312314
}
313315

314316
@Test
315-
public void extractApkCodeHash_ifKeyInCachedCodeHashes_doesNotRecalculateZipHash() {
317+
public void extractApkHash_ifKeyInCachedApkHashes_doesNotRecalculateZipHash() {
316318

317319
try (MockedStatic mockedReleaseIdentificationUtils =
318320
mockStatic(ReleaseIdentificationUtils.class)) {
319321
PackageInfo packageInfo =
320322
shadowPackageManager.getInternalMutablePackageInfo(
321323
ApplicationProvider.getApplicationContext().getPackageName());
322324
mockedReleaseIdentificationUtils
323-
.when(() -> ReleaseIdentificationUtils.calculateApkInternalCodeHash(any()))
325+
.when(() -> ReleaseIdentificationUtils.calculateApkHash(any()))
324326
.thenReturn(NEW_CODEHASH);
325327

326328
checkForNewReleaseClient.extractApkHash(packageInfo);
327329
checkForNewReleaseClient.extractApkHash(packageInfo);
328330
// check that calculateApkInternalCodeHash is only called once
329331
mockedReleaseIdentificationUtils.verify(
330-
() -> ReleaseIdentificationUtils.calculateApkInternalCodeHash(any()));
332+
() -> ReleaseIdentificationUtils.calculateApkHash(any()));
331333
}
332334
}
333335

0 commit comments

Comments
 (0)