1 /* 2 * Copyright (C) 2019 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 package com.android.launcher3.util; 17 18 import android.content.BroadcastReceiver; 19 import android.content.Context; 20 import android.content.Intent; 21 import android.content.IntentFilter; 22 import android.os.PatternMatcher; 23 import android.text.TextUtils; 24 25 import androidx.annotation.Nullable; 26 27 import java.util.function.Consumer; 28 29 public class SimpleBroadcastReceiver extends BroadcastReceiver { 30 31 private final Consumer<Intent> mIntentConsumer; 32 SimpleBroadcastReceiver(Consumer<Intent> intentConsumer)33 public SimpleBroadcastReceiver(Consumer<Intent> intentConsumer) { 34 mIntentConsumer = intentConsumer; 35 } 36 37 @Override onReceive(Context context, Intent intent)38 public void onReceive(Context context, Intent intent) { 39 mIntentConsumer.accept(intent); 40 } 41 42 /** 43 * Helper method to register multiple actions 44 */ register(Context context, String... actions)45 public void register(Context context, String... actions) { 46 context.registerReceiver(this, getFilter(actions)); 47 } 48 49 /** 50 * Helper method to register multiple actions associated with a paction 51 */ registerPkgActions(Context context, @Nullable String pkg, String... actions)52 public void registerPkgActions(Context context, @Nullable String pkg, String... actions) { 53 context.registerReceiver(this, getPackageFilter(pkg, actions)); 54 } 55 56 /** 57 * Creates an intent filter to listen for actions with a specific package in the data field. 58 */ getPackageFilter(String pkg, String... actions)59 public static IntentFilter getPackageFilter(String pkg, String... actions) { 60 IntentFilter filter = getFilter(actions); 61 filter.addDataScheme("package"); 62 if (!TextUtils.isEmpty(pkg)) { 63 filter.addDataSchemeSpecificPart(pkg, PatternMatcher.PATTERN_LITERAL); 64 } 65 return filter; 66 } 67 getFilter(String... actions)68 private static IntentFilter getFilter(String... actions) { 69 IntentFilter filter = new IntentFilter(); 70 for (String action : actions) { 71 filter.addAction(action); 72 } 73 return filter; 74 } 75 76 /** 77 * Unregisters the receiver ignoring any errors 78 */ unregisterReceiverSafely(Context context)79 public void unregisterReceiverSafely(Context context) { 80 try { 81 context.unregisterReceiver(this); 82 } catch (IllegalArgumentException e) { 83 // It was probably never registered or already unregistered. Ignore. 84 } 85 } 86 } 87