|
| 1 | +// Copyright 2020 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +package com.google.firebase.messaging.directboot; |
| 15 | + |
| 16 | +import android.content.Context; |
| 17 | +import android.content.Intent; |
| 18 | +import android.util.Log; |
| 19 | +import androidx.legacy.content.WakefulBroadcastReceiver; |
| 20 | +import com.google.android.gms.common.util.concurrent.NamedThreadFactory; |
| 21 | +import com.google.firebase.iid.FcmBroadcastProcessor; |
| 22 | +import com.google.firebase.iid.ServiceStarter; |
| 23 | +import com.google.firebase.messaging.directboot.threads.PoolableExecutors; |
| 24 | +import com.google.firebase.messaging.directboot.threads.ThreadPriority; |
| 25 | +import java.util.concurrent.ExecutorService; |
| 26 | + |
| 27 | +/** |
| 28 | + * WakefulBroadcastReceiver that receives FirebaseMessaging events and delivers them to the |
| 29 | + * application-specific {@link com.google.firebase.iid.FirebaseInstanceIdService} subclass in direct |
| 30 | + * boot mode. |
| 31 | + * |
| 32 | + * <p>This receiver is automatically added to your application's manifest file via manifest merge. |
| 33 | + * If necessary it can be manually declared via: |
| 34 | + * |
| 35 | + * <pre> |
| 36 | + * {@literal |
| 37 | + * <receiver |
| 38 | + * android:name="com.google.firebase.messaging.directboot.FirebaseMessagingDirectBootReceiver" |
| 39 | + * android:directBootAware="true" |
| 40 | + * android:exported="true" |
| 41 | + * android:permission="com.google.android.c2dm.permission.SEND" > |
| 42 | + * <intent-filter> |
| 43 | + * <action android:name="com.google.firebase.messaging.RECEIVE_DIRECT_BOOT" /> |
| 44 | + * </intent-filter> |
| 45 | + * </receiver>}</pre> |
| 46 | + * |
| 47 | + * <p>The {@code com.google.android.c2dm.permission.SEND} permission is held by Google Play |
| 48 | + * services. This prevents other apps from invoking the broadcast receiver. |
| 49 | + * |
| 50 | + * @hide |
| 51 | + */ |
| 52 | +public final class FirebaseMessagingDirectBootReceiver extends WakefulBroadcastReceiver { |
| 53 | + |
| 54 | + /** TAG for log statements coming from FCM */ |
| 55 | + static final String TAG = "FCM"; |
| 56 | + |
| 57 | + /** Action for FCM direct boot message intents */ |
| 58 | + private static final String ACTION_DIRECT_BOOT_REMOTE_INTENT = |
| 59 | + "com.google.firebase.messaging.RECEIVE_DIRECT_BOOT"; |
| 60 | + |
| 61 | + /** All broadcasts get processed on this executor. */ |
| 62 | + private final ExecutorService processorExecutor = |
| 63 | + PoolableExecutors.factory() |
| 64 | + .newSingleThreadExecutor( |
| 65 | + new NamedThreadFactory("fcm-db-intent-handle"), ThreadPriority.LOW_POWER); |
| 66 | + |
| 67 | + @Override |
| 68 | + public void onReceive(Context context, Intent intent) { |
| 69 | + if (intent == null) { |
| 70 | + return; |
| 71 | + } |
| 72 | + if (!ACTION_DIRECT_BOOT_REMOTE_INTENT.equals(intent.getAction())) { |
| 73 | + Log.d(TAG, "Unexpected intent: " + intent.getAction()); |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + // Just pass the intent to the service mostly unchanged. |
| 78 | + // Clear the component and ensure package name is set so that the standard dispatching |
| 79 | + // mechanism will find the right service in the app. |
| 80 | + intent.setComponent(null); |
| 81 | + intent.setPackage(context.getPackageName()); |
| 82 | + |
| 83 | + // We don't actually want to process this broadcast on the main thread, so we're going to use |
| 84 | + // goAsync to deal with this in the background. Unfortunately, we need to check whether the |
| 85 | + // broadcast was ordered (and thus needs a result) before calling goAsync, because once we've |
| 86 | + // called goAsync then isOrderedBroadcast will always return false. |
| 87 | + boolean needsResult = isOrderedBroadcast(); |
| 88 | + PendingResult pendingBroadcastResult = goAsync(); |
| 89 | + |
| 90 | + new FcmBroadcastProcessor(context, processorExecutor) |
| 91 | + .process(intent) |
| 92 | + .addOnCompleteListener( |
| 93 | + processorExecutor, |
| 94 | + resultCodeTask -> { |
| 95 | + // If we call setResultCode on a non-ordered broadcast it'll throw, so only set the |
| 96 | + // result if the broadcast was ordered |
| 97 | + if (needsResult) { |
| 98 | + pendingBroadcastResult.setResultCode( |
| 99 | + resultCodeTask.isSuccessful() |
| 100 | + ? resultCodeTask.getResult() |
| 101 | + : ServiceStarter.ERROR_UNKNOWN); |
| 102 | + } |
| 103 | + pendingBroadcastResult.finish(); |
| 104 | + }); |
| 105 | + } |
| 106 | +} |
0 commit comments