36
36
import java .util .concurrent .Executor ;
37
37
import java .util .concurrent .Executors ;
38
38
39
- class CheckForUpdateClient {
40
- private static final int UPDATE_THREAD_POOL_SIZE = 4 ;
39
+ class CheckForNewReleaseClient {
40
+ private static final int NEW_RELEASE_THREAD_POOL_SIZE = 4 ;
41
+ private static final String TAG = "CheckForNewReleaseClient:" ;
41
42
42
43
private final FirebaseApp firebaseApp ;
43
44
private final FirebaseAppDistributionTesterApiClient firebaseAppDistributionTesterApiClient ;
44
45
private final FirebaseInstallationsApi firebaseInstallationsApi ;
45
46
private static final ConcurrentMap <String , String > cachedCodeHashes = new ConcurrentHashMap <>();
46
47
private final ReleaseIdentifierStorage releaseIdentifierStorage ;
47
48
48
- Task <AppDistributionReleaseInternal > cachedCheckForUpdate = null ;
49
- private final Executor checkForUpdateExecutor ;
49
+ Task <AppDistributionReleaseInternal > cachedCheckForNewRelease = null ;
50
+ private final Executor checkForNewReleaseExecutor ;
50
51
51
- CheckForUpdateClient (
52
+ CheckForNewReleaseClient (
52
53
@ NonNull FirebaseApp firebaseApp ,
53
54
@ NonNull FirebaseAppDistributionTesterApiClient firebaseAppDistributionTesterApiClient ,
54
55
@ NonNull FirebaseInstallationsApi firebaseInstallationsApi ) {
55
56
this .firebaseApp = firebaseApp ;
56
57
this .firebaseAppDistributionTesterApiClient = firebaseAppDistributionTesterApiClient ;
57
58
this .firebaseInstallationsApi = firebaseInstallationsApi ;
58
59
// TODO: verify if this is best way to use executorservice here
59
- this .checkForUpdateExecutor = Executors .newFixedThreadPool (UPDATE_THREAD_POOL_SIZE );
60
+ this .checkForNewReleaseExecutor = Executors .newFixedThreadPool (NEW_RELEASE_THREAD_POOL_SIZE );
60
61
this .releaseIdentifierStorage =
61
62
new ReleaseIdentifierStorage (firebaseApp .getApplicationContext ());
62
63
}
63
64
64
- CheckForUpdateClient (
65
+ CheckForNewReleaseClient (
65
66
@ NonNull FirebaseApp firebaseApp ,
66
67
@ NonNull FirebaseAppDistributionTesterApiClient firebaseAppDistributionTesterApiClient ,
67
68
@ NonNull FirebaseInstallationsApi firebaseInstallationsApi ,
@@ -70,94 +71,94 @@ class CheckForUpdateClient {
70
71
this .firebaseAppDistributionTesterApiClient = firebaseAppDistributionTesterApiClient ;
71
72
this .firebaseInstallationsApi = firebaseInstallationsApi ;
72
73
// TODO: verify if this is best way to use executorservice here
73
- this .checkForUpdateExecutor = executor ;
74
+ this .checkForNewReleaseExecutor = executor ;
74
75
this .releaseIdentifierStorage =
75
76
new ReleaseIdentifierStorage (firebaseApp .getApplicationContext ());
76
77
}
77
78
78
79
@ NonNull
79
- public synchronized Task <AppDistributionReleaseInternal > checkForUpdate () {
80
+ public synchronized Task <AppDistributionReleaseInternal > checkForNewRelease () {
80
81
81
- if (cachedCheckForUpdate != null && !cachedCheckForUpdate .isComplete ()) {
82
- return cachedCheckForUpdate ;
82
+ if (cachedCheckForNewRelease != null && !cachedCheckForNewRelease .isComplete ()) {
83
+ return cachedCheckForNewRelease ;
83
84
}
84
85
85
86
Task <String > installationIdTask = firebaseInstallationsApi .getId ();
86
87
// forceRefresh is false to get locally cached token if available
87
88
Task <InstallationTokenResult > installationAuthTokenTask =
88
89
firebaseInstallationsApi .getToken (false );
89
90
90
- this .cachedCheckForUpdate =
91
+ this .cachedCheckForNewRelease =
91
92
Tasks .whenAllSuccess (installationIdTask , installationAuthTokenTask )
92
93
.onSuccessTask (
93
- checkForUpdateExecutor ,
94
+ checkForNewReleaseExecutor ,
94
95
tasks -> {
95
96
String fid = installationIdTask .getResult ();
96
97
InstallationTokenResult installationTokenResult =
97
98
installationAuthTokenTask .getResult ();
98
99
try {
99
- AppDistributionReleaseInternal latestRelease =
100
- getLatestReleaseFromClient (
100
+ AppDistributionReleaseInternal newRelease =
101
+ getNewReleaseFromClient (
101
102
fid ,
102
103
firebaseApp .getOptions ().getApplicationId (),
103
104
firebaseApp .getOptions ().getApiKey (),
104
105
installationTokenResult .getToken ());
105
- return Tasks .forResult (latestRelease );
106
+ return Tasks .forResult (newRelease );
106
107
} catch (FirebaseAppDistributionException ex ) {
107
108
return Tasks .forException (ex );
108
109
}
109
110
})
110
111
.continueWithTask (
111
- checkForUpdateExecutor ,
112
+ checkForNewReleaseExecutor ,
112
113
task ->
113
114
TaskUtils .handleTaskFailure (
114
115
task ,
115
116
Constants .ErrorMessages .NETWORK_ERROR ,
116
117
FirebaseAppDistributionException .Status .NETWORK_FAILURE ));
117
118
118
- return cachedCheckForUpdate ;
119
+ return cachedCheckForNewRelease ;
119
120
}
120
121
121
122
@ VisibleForTesting
122
- AppDistributionReleaseInternal getLatestReleaseFromClient (
123
+ AppDistributionReleaseInternal getNewReleaseFromClient (
123
124
String fid , String appId , String apiKey , String authToken )
124
125
throws FirebaseAppDistributionException {
125
126
try {
126
- AppDistributionReleaseInternal retrievedLatestRelease =
127
- firebaseAppDistributionTesterApiClient .fetchLatestRelease (fid , appId , apiKey , authToken );
127
+ AppDistributionReleaseInternal retrievedNewRelease =
128
+ firebaseAppDistributionTesterApiClient .fetchNewRelease (fid , appId , apiKey , authToken );
128
129
129
- if (isNewerBuildVersion (retrievedLatestRelease )
130
- || !isInstalledRelease (retrievedLatestRelease )) {
131
- return retrievedLatestRelease ;
130
+ if (isNewerBuildVersion (retrievedNewRelease ) || !isInstalledRelease (retrievedNewRelease )) {
131
+ return retrievedNewRelease ;
132
132
} else {
133
- // Return null if retrieved latest release is older or currently installed
133
+ // Return null if retrieved new release is older or currently installed
134
134
return null ;
135
135
}
136
136
} catch (NumberFormatException e ) {
137
+ LogWrapper .getInstance ().e (TAG + "Error parsing buildVersion." , e );
137
138
throw new FirebaseAppDistributionException (
138
139
Constants .ErrorMessages .NETWORK_ERROR ,
139
140
FirebaseAppDistributionException .Status .NETWORK_FAILURE ,
140
141
e );
141
142
}
142
143
}
143
144
144
- private boolean isNewerBuildVersion (AppDistributionReleaseInternal latestRelease )
145
+ private boolean isNewerBuildVersion (AppDistributionReleaseInternal newRelease )
145
146
throws FirebaseAppDistributionException {
146
- return Long .parseLong (latestRelease .getBuildVersion ())
147
+ return Long .parseLong (newRelease .getBuildVersion ())
147
148
> getInstalledAppVersionCode (firebaseApp .getApplicationContext ());
148
149
}
149
150
150
151
@ VisibleForTesting
151
- boolean isInstalledRelease (AppDistributionReleaseInternal latestRelease ) {
152
- if (latestRelease .getBinaryType ().equals (BinaryType .APK )) {
153
- return hasSameCodeHashAsInstallledRelease (latestRelease );
152
+ boolean isInstalledRelease (AppDistributionReleaseInternal newRelease ) {
153
+ if (newRelease .getBinaryType ().equals (BinaryType .APK )) {
154
+ return hasSameCodeHashAsInstallledRelease (newRelease );
154
155
}
155
156
156
- if (latestRelease .getIasArtifactId () == null ) {
157
+ if (newRelease .getIasArtifactId () == null ) {
157
158
return false ;
158
159
}
159
160
// AAB BinaryType
160
- return latestRelease
161
+ return newRelease
161
162
.getIasArtifactId ()
162
163
.equals (
163
164
ReleaseIdentificationUtils .extractInternalAppSharingArtifactId (
@@ -169,6 +170,7 @@ private long getInstalledAppVersionCode(Context context) throws FirebaseAppDistr
169
170
try {
170
171
pInfo = context .getPackageManager ().getPackageInfo (context .getPackageName (), 0 );
171
172
} catch (PackageManager .NameNotFoundException e ) {
173
+ LogWrapper .getInstance ().e (TAG + "Unable to locate Firebase App." , e );
172
174
throw new FirebaseAppDistributionException (
173
175
Constants .ErrorMessages .UNKNOWN_ERROR ,
174
176
FirebaseAppDistributionException .Status .UNKNOWN ,
@@ -190,7 +192,7 @@ String extractApkCodeHash(PackageInfo packageInfo) {
190
192
return releaseIdentifierStorage .getExternalCodeHash (cachedCodeHashes .get (key ));
191
193
}
192
194
193
- private boolean hasSameCodeHashAsInstallledRelease (AppDistributionReleaseInternal latestRelease ) {
195
+ private boolean hasSameCodeHashAsInstallledRelease (AppDistributionReleaseInternal newRelease ) {
194
196
try {
195
197
Context context = firebaseApp .getApplicationContext ();
196
198
PackageInfo metadataPackageInfo =
@@ -204,10 +206,11 @@ private boolean hasSameCodeHashAsInstallledRelease(AppDistributionReleaseInterna
204
206
return false ;
205
207
}
206
208
207
- // If the codeHash for the retrieved latestRelease is equal to the stored codeHash
209
+ // If the codeHash for the retrieved newRelease is equal to the stored codeHash
208
210
// of the installed release, then they are the same release.
209
- return externalCodeHash .equals (latestRelease .getCodeHash ());
211
+ return externalCodeHash .equals (newRelease .getCodeHash ());
210
212
} catch (PackageManager .NameNotFoundException e ) {
213
+ LogWrapper .getInstance ().e (TAG + "Unable to locate App." , e );
211
214
return false ;
212
215
}
213
216
}
0 commit comments