LeOS-GSI/patches/leos/platform_packages_apps_ImsS.../0001-remove_fcm.patch

529 lines
20 KiB
Diff

From eda6d034d7013b17c6c33a3ddea25a8b1435e6b7 Mon Sep 17 00:00:00 2001
From: harvey186 <harvey186@hotmail.com>
Date: Mon, 11 Dec 2023 18:03:06 +0100
Subject: [PATCH] remove_fcm
Change-Id: I71f795a263a5f846ba4c4fe07745e61219381e26
---
.../fcm/FcmRegistrationReceiver.java | 32 ----
.../fcm/FcmRegistrationService.java | 153 ------------------
.../imsserviceentitlement/fcm/FcmService.java | 135 ----------------
.../fcm/FcmTokenStore.java | 83 ----------
.../imsserviceentitlement/fcm/FcmUtils.java | 73 ---------
5 files changed, 476 deletions(-)
delete mode 100644 src/com/android/imsserviceentitlement/fcm/FcmRegistrationReceiver.java
delete mode 100644 src/com/android/imsserviceentitlement/fcm/FcmRegistrationService.java
delete mode 100644 src/com/android/imsserviceentitlement/fcm/FcmService.java
delete mode 100644 src/com/android/imsserviceentitlement/fcm/FcmTokenStore.java
delete mode 100644 src/com/android/imsserviceentitlement/fcm/FcmUtils.java
diff --git a/src/com/android/imsserviceentitlement/fcm/FcmRegistrationReceiver.java b/src/com/android/imsserviceentitlement/fcm/FcmRegistrationReceiver.java
deleted file mode 100644
index 9c72f39..0000000
--- a/src/com/android/imsserviceentitlement/fcm/FcmRegistrationReceiver.java
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- * Copyright (C) 2021 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.imsserviceentitlement.fcm;
-
-import android.content.BroadcastReceiver;
-import android.content.Context;
-import android.content.Intent;
-
-/** A {@link BroadcastReceiver} that triggers FCM registration jobs. */
-public class FcmRegistrationReceiver extends BroadcastReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
- FcmRegistrationService.enqueueJob(context);
- }
- }
-}
diff --git a/src/com/android/imsserviceentitlement/fcm/FcmRegistrationService.java b/src/com/android/imsserviceentitlement/fcm/FcmRegistrationService.java
deleted file mode 100644
index 184740d..0000000
--- a/src/com/android/imsserviceentitlement/fcm/FcmRegistrationService.java
+++ /dev/null
@@ -1,153 +0,0 @@
-/*
- * Copyright (C) 2021 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.imsserviceentitlement.fcm;
-
-import android.app.job.JobParameters;
-import android.app.job.JobService;
-import android.content.ComponentName;
-import android.content.Context;
-import android.os.AsyncTask;
-import android.telephony.SubscriptionManager;
-import android.util.Log;
-
-import androidx.annotation.VisibleForTesting;
-
-import com.android.imsserviceentitlement.R;
-import com.android.imsserviceentitlement.job.JobManager;
-import com.android.imsserviceentitlement.utils.TelephonyUtils;
-
-import com.google.firebase.FirebaseApp;
-import com.google.firebase.FirebaseOptions;
-import com.google.firebase.iid.FirebaseInstanceId;
-import com.google.firebase.messaging.FirebaseMessaging;
-
-import java.io.IOException;
-
-/** A {@link JobService} that gets a FCM tokens for all active SIMs. */
-public class FcmRegistrationService extends JobService {
- private static final String TAG = "IMSSE-FcmRegistrationService";
-
- private FirebaseInstanceId mFakeInstanceID = null;
- private FirebaseApp mApp = null;
-
- @VisibleForTesting AsyncTask<JobParameters, Void, Void> mOngoingTask;
-
- /** Enqueues a job for FCM registration. */
- public static void enqueueJob(Context context) {
- ComponentName componentName = new ComponentName(context, FcmRegistrationService.class);
- // No subscription id associated job, use {@link
- // SubscriptionManager#INVALID_SUBSCRIPTION_ID}.
- JobManager jobManager =
- JobManager.getInstance(
- context, componentName, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
- jobManager.registerFcmOnceNetworkReady();
- }
-
- @VisibleForTesting
- void setFakeInstanceID(FirebaseInstanceId instanceID) {
- mFakeInstanceID = instanceID;
- }
-
- @Override
- @VisibleForTesting
- protected void attachBaseContext(Context base) {
- super.attachBaseContext(base);
- }
-
- /** Returns a {@link FirebaseApp} instance, lazily initialized. */
- private FirebaseApp getFirebaseApp() {
- if (mApp == null) {
- try {
- mApp = FirebaseApp.getInstance();
- } catch (IllegalStateException e) {
- Log.d(TAG, "initialize FirebaseApp");
- mApp = FirebaseApp.initializeApp(
- this,
- new FirebaseOptions.Builder()
- .setApplicationId(getResources().getString(R.string.fcm_app_id))
- .setProjectId(getResources().getString(R.string.fcm_project_id))
- .setApiKey(getResources().getString(R.string.fcm_api_key))
- .build());
- }
- }
- return mApp;
- }
-
- @Override
- public boolean onStartJob(JobParameters params) {
- mOngoingTask = new AsyncTask<JobParameters, Void, Void>() {
- @Override
- protected Void doInBackground(JobParameters... params) {
- onHandleWork(params[0]);
- return null;
- }
- };
- mOngoingTask.execute(params);
- return true;
- }
-
- @Override
- public boolean onStopJob(JobParameters params) {
- return true; // Always re-run if job stopped.
- }
-
- /**
- * Registers to receive FCM messages published to subscribe topics under the retrieved token.
- * The token changes when the InstanceID becomes invalid (e.g. app data is deleted).
- */
- protected void onHandleWork(JobParameters params) {
- boolean wantsReschedule = false;
- for (int subId : TelephonyUtils.getSubIdsWithFcmSupported(this)) {
- if (!updateFcmToken(getFirebaseInstanceId(), subId)) {
- wantsReschedule = true;
- }
- }
-
- jobFinished(params, wantsReschedule);
- }
-
- /** Returns {@code false} if failed to get token. */
- private boolean updateFcmToken(FirebaseInstanceId instanceID, int subId) {
- Log.d(TAG, "FcmRegistrationService.updateFcmToken: subId=" + subId);
- String token = getTokenForSubId(instanceID, subId);
- if (token == null) {
- Log.d(TAG, "getToken null");
- return false;
- }
- Log.d(TAG, "FCM token: " + token + " subId: " + subId);
- FcmTokenStore.setToken(this, subId, token);
- return true;
- }
-
- private FirebaseInstanceId getFirebaseInstanceId() {
- return (mFakeInstanceID != null)
- ? mFakeInstanceID
- : FirebaseInstanceId.getInstance(getFirebaseApp());
- }
-
- private String getTokenForSubId(FirebaseInstanceId instanceID, int subId) {
- String token = null;
- try {
- token = instanceID.getToken(
- TelephonyUtils.getFcmSenderId(this, subId),
- FirebaseMessaging.INSTANCE_ID_SCOPE);
- } catch (IOException e) {
- Log.e(TAG, "Failed to get a new FCM token: " + e);
- }
- return token;
- }
-}
diff --git a/src/com/android/imsserviceentitlement/fcm/FcmService.java b/src/com/android/imsserviceentitlement/fcm/FcmService.java
deleted file mode 100644
index 9ab33fc..0000000
--- a/src/com/android/imsserviceentitlement/fcm/FcmService.java
+++ /dev/null
@@ -1,135 +0,0 @@
-/*
- * Copyright (C) 2021 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.imsserviceentitlement.fcm;
-
-import android.content.ComponentName;
-import android.content.Context;
-import android.util.Log;
-
-import com.android.imsserviceentitlement.ImsEntitlementPollingService;
-import com.android.imsserviceentitlement.job.JobManager;
-import com.android.imsserviceentitlement.utils.TelephonyUtils;
-import com.android.libraries.entitlement.ServiceEntitlement;
-
-import com.google.common.annotations.VisibleForTesting;
-import com.google.firebase.messaging.FirebaseMessagingService;
-import com.google.firebase.messaging.RemoteMessage;
-
-import java.util.Map;
-
-/** Service for handling Firebase Cloud Messaging.*/
-public class FcmService extends FirebaseMessagingService {
- private static final String TAG = "IMSSE-FcmService";
-
- private static final String DATA_APP_KEY = "app";
- private static final String DATA_TIMESTAMP_KEY = "timestamp";
-
- private JobManager mJobManager;
-
- @Override
- @VisibleForTesting
- protected void attachBaseContext(Context base) {
- super.attachBaseContext(base);
- }
-
- /**
- * Called when a new token for the default Firebase project is generated.
- *
- * @param token the token used for sending messages to this application instance.
- */
- @Override
- public void onNewToken(String token) {
- Log.d(TAG, "New token: " + token);
-
- // TODO(b/182560867): check if we need to update the new token to server.
-
- // Note we cannot directly save the new token, as we don't know which subId
- // it's associated with.
- FcmRegistrationService.enqueueJob(this);
- }
-
- /**
- * Handles FCM message for entitlement.
- *
- * @param message holds the message received from Firebase Cloud Messaging.
- */
- @Override
- public void onMessageReceived(RemoteMessage message) {
- // Not testable.
- onMessageReceived(message.getSenderId(), message.getData());
- }
-
- @VisibleForTesting
- void onMessageReceived(String fcmSenderId, Map<String, String> fcmData) {
- Log.d(TAG, "onMessageReceived, SenderId:" + fcmSenderId);
- if (!isTs43EntitlementsChangeEvent(fcmData)) {
- Log.i(TAG, "Ignore message not related to entitlements change.");
- return;
- }
- // A corner case: a FCM received after SIM is removed, and SIM inserted back later.
- // We missed the FCM in this case.
- scheduleEntitlementStatusCheckForSubIdAssociatedWithSenderId(fcmSenderId);
- }
-
- private static boolean isTs43EntitlementsChangeEvent(Map<String, String> dataMap) {
- if (dataMap == null) {
- return false;
- }
- Log.v(TAG, "The payload data: " + dataMap);
-
- // Based on GSMA TS.43 2.4.2 Messaging Infrastructure-Based Notifications, the notification
- // payload for multiple applications follows:
- // "data":
- // {
- // "app": ["ap2003", "ap2004", "ap2005"],
- // "timestamp": "2019-01-29T13:15:31-08:00"
- // }
- if (!dataMap.containsKey(DATA_APP_KEY) || !dataMap.containsKey(DATA_TIMESTAMP_KEY)) {
- Log.d(TAG, "data format error");
- return false;
- }
- // Check if APP_VOWIFI i.e. "ap2004" is in notification data.
- if (dataMap.get(DATA_APP_KEY).contains(ServiceEntitlement.APP_VOWIFI)) {
- return true;
- }
- return false;
- }
-
- @VisibleForTesting
- void setMockJobManager(JobManager jobManager) {
- mJobManager = jobManager;
- }
-
- private JobManager getJobManager(int subId) {
- return (mJobManager != null)
- ? mJobManager
- : JobManager.getInstance(
- this,
- ImsEntitlementPollingService.COMPONENT_NAME,
- subId);
- }
-
- private void scheduleEntitlementStatusCheckForSubIdAssociatedWithSenderId(String msgSenderId) {
- for (int subId : TelephonyUtils.getSubIdsWithFcmSupported(this)) {
- String configSenderId = TelephonyUtils.getFcmSenderId(this, subId);
- if (msgSenderId.equals(configSenderId)) {
- Log.d(TAG, "check entitlement status for subscription id(" + subId + ")");
- getJobManager(subId).queryEntitlementStatusOnceNetworkReady();
- }
- }
- }
-}
diff --git a/src/com/android/imsserviceentitlement/fcm/FcmTokenStore.java b/src/com/android/imsserviceentitlement/fcm/FcmTokenStore.java
deleted file mode 100644
index a972fb7..0000000
--- a/src/com/android/imsserviceentitlement/fcm/FcmTokenStore.java
+++ /dev/null
@@ -1,83 +0,0 @@
-/*
- * Copyright (C) 2021 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.imsserviceentitlement.fcm;
-
-import android.content.Context;
-import android.content.SharedPreferences;
-import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
-import android.text.TextUtils;
-import android.util.Log;
-
-import androidx.annotation.WorkerThread;
-
-/** Stores FCM token. */
-public final class FcmTokenStore {
- private static final String TAG = "IMSSE-FcmTokenStore";
-
- private static final String FCM_TOKEN_FILE = "FCM_TOKEN";
- private static final String FCM_TOKEN_KEY = "FCM_TOKEN_SUB_";
-
- private FcmTokenStore() {}
-
- /** Returns FCM token or empty string if not available. */
- public static String getToken(Context context, int subId) {
- return getFcmTokenFile(context).getString(FCM_TOKEN_KEY + subId, "");
- }
-
- /** Returns {@code true} if FCM token available. */
- public static boolean hasToken(Context context, int subId) {
- return !TextUtils.isEmpty(getToken(context, subId));
- }
-
- /** Saves the FCM token into data store. */
- @WorkerThread
- public static boolean setToken(Context context, int subId, String token) {
- if (!TextUtils.isEmpty(token)) {
- return getFcmTokenFile(context)
- .edit()
- .putString(FCM_TOKEN_KEY + subId, token)
- .commit();
- } else {
- return getFcmTokenFile(context)
- .edit()
- .remove(FCM_TOKEN_KEY + subId)
- .commit();
- }
- }
-
- /** Registers a listener for FCM token update. */
- public static void registerTokenUpdateListener(
- Context context, OnSharedPreferenceChangeListener listener) {
- Log.d(TAG, "registerTokenUpdateListener");
- // Since FCM_TOKEN_FILE only contains one item FCM_TOKEN_KEY, a change to FCM_TOKEN_FILE
- // means a change to FCM_TOKEN_KEY. The listener can ignore its arguments.
- getFcmTokenFile(context).registerOnSharedPreferenceChangeListener(listener);
- }
-
- /** Unregisters a listener for FCM token update. */
- public static void unregisterTokenUpdateListener(
- Context context, OnSharedPreferenceChangeListener listener) {
- Log.d(TAG, "unregisterTokenUpdateListener");
- // Since FCM_TOKEN_FILE only contains one item FCM_TOKEN_KEY, a change to FCM_TOKEN_FILE
- // means a change to FCM_TOKEN_KEY. The listener can ignore its arguments.
- getFcmTokenFile(context).unregisterOnSharedPreferenceChangeListener(listener);
- }
-
- private static SharedPreferences getFcmTokenFile(Context context) {
- return context.getSharedPreferences(FCM_TOKEN_FILE, Context.MODE_PRIVATE);
- }
-}
diff --git a/src/com/android/imsserviceentitlement/fcm/FcmUtils.java b/src/com/android/imsserviceentitlement/fcm/FcmUtils.java
deleted file mode 100644
index 70ec276..0000000
--- a/src/com/android/imsserviceentitlement/fcm/FcmUtils.java
+++ /dev/null
@@ -1,73 +0,0 @@
-/*
- * Copyright (C) 2021 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.imsserviceentitlement.fcm;
-
-import static java.util.concurrent.TimeUnit.SECONDS;
-
-import android.content.Context;
-import android.content.SharedPreferences;
-import android.util.Log;
-
-import androidx.annotation.WorkerThread;
-
-import java.util.concurrent.CountDownLatch;
-
-/** Convenience methods for FCM. */
-public final class FcmUtils {
- public static final String LOG_TAG = "IMSSE-FcmUtils";
-
- private static final long TOKEN_UPDATE_WAITING_SECONDS = 25L;
-
- private FcmUtils() {}
-
- /** Fetches FCM token, if it's not available via {@link FcmTokenStore#getToken}. */
- @WorkerThread
- public static void fetchFcmToken(Context context, int subId) {
- if (FcmTokenStore.hasToken(context, subId)) {
- Log.d(LOG_TAG, "FCM token available.");
- return;
- }
-
- Log.d(LOG_TAG, "FCM token unavailable. Try to update...");
- final CountDownLatch tokenUpdated = new CountDownLatch(1);
- final SharedPreferences.OnSharedPreferenceChangeListener listener =
- (s, k) -> {
- Log.d(LOG_TAG, "FCM preference changed.");
- if (FcmTokenStore.hasToken(context, subId)) {
- tokenUpdated.countDown();
- }
- };
- FcmTokenStore.registerTokenUpdateListener(context, listener);
-
- // Starts a JobIntentService to update FCM token by calling FCM API on a worker thread.
- FcmRegistrationService.enqueueJob(context);
-
- try {
- // Wait for 25s. If FCM token update failed/timeout, we will let user see
- // the error message returned by server. Then user can manually retry.
- if (tokenUpdated.await(TOKEN_UPDATE_WAITING_SECONDS, SECONDS)) {
- Log.d(LOG_TAG, "FCM token updated.");
- } else {
- Log.d(LOG_TAG, "FCM token update failed.");
- }
- } catch (InterruptedException e) {
- // Do nothing
- Log.d(LOG_TAG, "FCM token update interrupted.");
- }
- FcmTokenStore.unregisterTokenUpdateListener(context, listener);
- }
-}
--
2.34.1