24
24
import com .google .android .gms .tasks .Tasks ;
25
25
import com .google .firebase .FirebaseApp ;
26
26
import com .google .firebase .installations .local .PersistedFid ;
27
+ import com .google .firebase .installations .local .PersistedFid .RegistrationStatus ;
27
28
import com .google .firebase .installations .local .PersistedFidEntry ;
28
29
import com .google .firebase .installations .remote .FirebaseInstallationServiceClient ;
29
30
import com .google .firebase .installations .remote .FirebaseInstallationServiceException ;
@@ -50,12 +51,13 @@ public class FirebaseInstallations implements FirebaseInstallationsApi {
50
51
private final FirebaseInstallationServiceClient serviceClient ;
51
52
private final PersistedFid persistedFid ;
52
53
private final Executor executor ;
53
- /*package*/ static Clock clock = DefaultClock . getInstance () ;
54
+ private final Clock clock ;
54
55
private final Utils utils ;
55
56
56
57
/** package private constructor. */
57
58
FirebaseInstallations (FirebaseApp firebaseApp ) {
58
59
this (
60
+ DefaultClock .getInstance (),
59
61
new ThreadPoolExecutor (0 , 1 , 30L , TimeUnit .SECONDS , new SynchronousQueue <>()),
60
62
firebaseApp ,
61
63
new FirebaseInstallationServiceClient (),
@@ -64,11 +66,13 @@ public class FirebaseInstallations implements FirebaseInstallationsApi {
64
66
}
65
67
66
68
FirebaseInstallations (
69
+ Clock clock ,
67
70
Executor executor ,
68
71
FirebaseApp firebaseApp ,
69
72
FirebaseInstallationServiceClient serviceClient ,
70
73
PersistedFid persistedFid ,
71
74
Utils utils ) {
75
+ this .clock = clock ;
72
76
this .firebaseApp = firebaseApp ;
73
77
this .serviceClient = serviceClient ;
74
78
this .executor = executor ;
@@ -107,7 +111,7 @@ public static FirebaseInstallations getInstance(@NonNull FirebaseApp app) {
107
111
@ Override
108
112
public Task <String > getId () {
109
113
return Tasks .call (executor , this ::getPersistedFid )
110
- .continueWith (orElse (new NewFidSupplier () ))
114
+ .continueWith (orElse (this :: createAndPersistNewFid ))
111
115
.onSuccessTask (this ::registerFidIfNecessary );
112
116
}
113
117
@@ -152,8 +156,7 @@ private PersistedFidEntry getPersistedFid() throws FirebaseInstallationsExceptio
152
156
153
157
private static boolean persistedFidMissingOrInErrorState (PersistedFidEntry persistedFidEntry ) {
154
158
return persistedFidEntry == null
155
- || persistedFidEntry .getRegistrationStatus ()
156
- == PersistedFid .RegistrationStatus .REGISTER_ERROR ;
159
+ || persistedFidEntry .getRegistrationStatus () == RegistrationStatus .REGISTER_ERROR ;
157
160
}
158
161
159
162
@ NonNull
@@ -178,7 +181,7 @@ private void persistFid(String fid) throws FirebaseInstallationsException {
178
181
persistedFid .insertOrUpdatePersistedFidEntry (
179
182
PersistedFidEntry .builder ()
180
183
.setFirebaseInstallationId (fid )
181
- .setRegistrationStatus (PersistedFid . RegistrationStatus .UNREGISTERED )
184
+ .setRegistrationStatus (RegistrationStatus .UNREGISTERED )
182
185
.build ());
183
186
184
187
if (!firstUpdateCacheResult ) {
@@ -192,7 +195,7 @@ private Task<String> registerFidIfNecessary(PersistedFidEntry persistedFidEntry)
192
195
String fid = persistedFidEntry .getFirebaseInstallationId ();
193
196
194
197
// Check if the fid is unregistered
195
- if (persistedFidEntry .getRegistrationStatus () == PersistedFid . RegistrationStatus .UNREGISTERED ) {
198
+ if (persistedFidEntry .getRegistrationStatus () == RegistrationStatus .UNREGISTERED ) {
196
199
updatePersistedFidWithPendingStatus (fid );
197
200
Tasks .call (executor , () -> registerAndSaveFid (persistedFidEntry ));
198
201
}
@@ -203,15 +206,15 @@ private void updatePersistedFidWithPendingStatus(String fid) {
203
206
persistedFid .insertOrUpdatePersistedFidEntry (
204
207
PersistedFidEntry .builder ()
205
208
.setFirebaseInstallationId (fid )
206
- .setRegistrationStatus (PersistedFid . RegistrationStatus .PENDING )
209
+ .setRegistrationStatus (RegistrationStatus .PENDING )
207
210
.build ());
208
211
}
209
212
210
213
/** Registers the created Fid with FIS servers and update the shared prefs. */
211
214
private Void registerAndSaveFid (PersistedFidEntry persistedFidEntry )
212
215
throws FirebaseInstallationsException {
213
216
try {
214
- long creationTime = clock .currentTimeMillis () / 1000 ;
217
+ long creationTime = TimeUnit . MILLISECONDS . toSeconds ( clock .currentTimeMillis ()) ;
215
218
216
219
InstallationResponse installationResponse =
217
220
serviceClient .createFirebaseInstallation (
@@ -222,7 +225,7 @@ private Void registerAndSaveFid(PersistedFidEntry persistedFidEntry)
222
225
persistedFid .insertOrUpdatePersistedFidEntry (
223
226
PersistedFidEntry .builder ()
224
227
.setFirebaseInstallationId (persistedFidEntry .getFirebaseInstallationId ())
225
- .setRegistrationStatus (PersistedFid . RegistrationStatus .REGISTERED )
228
+ .setRegistrationStatus (RegistrationStatus .REGISTERED )
226
229
.setAuthToken (installationResponse .getAuthToken ().getToken ())
227
230
.setRefreshToken (installationResponse .getRefreshToken ())
228
231
.setExpiresInSecs (
@@ -234,21 +237,13 @@ private Void registerAndSaveFid(PersistedFidEntry persistedFidEntry)
234
237
persistedFid .insertOrUpdatePersistedFidEntry (
235
238
PersistedFidEntry .builder ()
236
239
.setFirebaseInstallationId (persistedFidEntry .getFirebaseInstallationId ())
237
- .setRegistrationStatus (PersistedFid . RegistrationStatus .REGISTER_ERROR )
240
+ .setRegistrationStatus (RegistrationStatus .REGISTER_ERROR )
238
241
.build ());
239
242
throw new FirebaseInstallationsException (
240
243
exception .getMessage (), FirebaseInstallationsException .Status .SDK_INTERNAL_ERROR );
241
244
}
242
245
return null ;
243
246
}
244
-
245
- class NewFidSupplier implements Supplier <PersistedFidEntry > {
246
-
247
- @ Override
248
- public PersistedFidEntry get () throws FirebaseInstallationsException {
249
- return createAndPersistNewFid ();
250
- }
251
- }
252
247
}
253
248
254
249
interface Supplier <T > {
0 commit comments