• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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  *      http://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 
17 package com.android.imsserviceentitlement.fcm;
18 
19 import static java.util.concurrent.TimeUnit.SECONDS;
20 
21 import android.content.Context;
22 import android.content.SharedPreferences;
23 import android.util.Log;
24 
25 import androidx.annotation.WorkerThread;
26 
27 import java.util.concurrent.CountDownLatch;
28 
29 /** Convenience methods for FCM. */
30 public final class FcmUtils {
31     public static final String LOG_TAG = "IMSSE-FcmUtils";
32 
33     private static final long TOKEN_UPDATE_WAITING_SECONDS = 25L;
34 
FcmUtils()35     private FcmUtils() {}
36 
37     /** Fetches FCM token, if it's not available via {@link FcmTokenStore#getToken}. */
38     @WorkerThread
fetchFcmToken(Context context, int subId)39     public static void fetchFcmToken(Context context, int subId) {
40         if (FcmTokenStore.hasToken(context, subId)) {
41             Log.d(LOG_TAG, "FCM token available.");
42             return;
43         }
44 
45         Log.d(LOG_TAG, "FCM token unavailable. Try to update...");
46         final CountDownLatch tokenUpdated = new CountDownLatch(1);
47         final SharedPreferences.OnSharedPreferenceChangeListener listener =
48                 (s, k) -> {
49                     Log.d(LOG_TAG, "FCM preference changed.");
50                     if (FcmTokenStore.hasToken(context, subId)) {
51                         tokenUpdated.countDown();
52                     }
53                 };
54         FcmTokenStore.registerTokenUpdateListener(context, listener);
55 
56         // Starts a JobIntentService to update FCM token by calling FCM API on a worker thread.
57         FcmRegistrationService.enqueueJob(context);
58 
59         try {
60             // Wait for 25s. If FCM token update failed/timeout, we will let user see
61             // the error message returned by server. Then user can manually retry.
62             if (tokenUpdated.await(TOKEN_UPDATE_WAITING_SECONDS, SECONDS)) {
63                 Log.d(LOG_TAG, "FCM token updated.");
64             } else {
65                 Log.d(LOG_TAG, "FCM token update failed.");
66             }
67         } catch (InterruptedException e) {
68             // Do nothing
69             Log.d(LOG_TAG, "FCM token update interrupted.");
70         }
71         FcmTokenStore.unregisterTokenUpdateListener(context, listener);
72     }
73 }
74