Removes ContextHubBroadcastReceiver

Dynamically registered BroadcastReceivers are only valid through
the lifecycle of a process, so clients should use static
BroadcastReceivers.

Bug: 117612105
Test: None
Change-Id: Id1a13b0f54aa138685bf8cb57ffe9f176d55e6e0
This commit is contained in:
Arthur Ishiguro
2018-11-02 09:25:31 -07:00
parent ee72c438b7
commit 41f4e64280
2 changed files with 3 additions and 107 deletions

View File

@@ -1,102 +0,0 @@
/*
* Copyright 2018 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 android.hardware.location;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
/**
* A BroadcastReceiver that can be used with the Context Hub Service notifications.
*
* @hide
*/
public class ContextHubBroadcastReceiver extends BroadcastReceiver {
// The context at which this receiver operates in
private Context mContext;
// The handler to post callbacks to when receiving Context Hub Service intents
private Handler mHandler;
// The callback to be invoked when receiving Context Hub Service intents
private ContextHubClientCallback mCallback;
// The string to use as the broadcast action for this receiver
private String mAction;
// True when this receiver is registered to receive Intents, false otherwise
private boolean mRegistered = false;
public ContextHubBroadcastReceiver(Context context, Handler handler,
ContextHubClientCallback callback, String tag) {
mContext = context;
mHandler = handler;
mCallback = callback;
mAction = tag;
}
/**
* Registers this receiver to receive Intents from the Context Hub Service. This method must
* only be invoked when the receiver is not registered.
*
* @throws IllegalStateException if the receiver is already registered
*/
public void register() throws IllegalStateException {
if (mRegistered) {
throw new IllegalStateException(
"Cannot register ContextHubBroadcastReceiver multiple times");
}
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(mAction);
mContext.registerReceiver(this, intentFilter, null /* broadcastPermission */, mHandler);
mRegistered = true;
}
/**
* Unregisters this receiver. This method must only be invoked if {@link #register()} is
* previously invoked.
*
* @throws IllegalStateException if the receiver is not yet registered
*/
public void unregister() throws IllegalStateException {
if (!mRegistered) {
throw new IllegalStateException(
"Cannot unregister ContextHubBroadcastReceiver when not registered");
}
mContext.unregisterReceiver(this);
mRegistered = false;
}
/**
* Creates a new PendingIntent associated with this receiver.
*
* @param flags the flags {@link PendingIntent.Flags} to use for the PendingIntent
*
* @return a PendingIntent to receive notifications for this receiver
*/
public PendingIntent getPendingIntent(@PendingIntent.Flags int flags) {
return PendingIntent.getBroadcast(
mContext, 0 /* requestCode */, new Intent(mAction), flags);
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO: Implement this
}
}

View File

@@ -19,6 +19,7 @@ import android.annotation.NonNull;
import android.annotation.RequiresPermission;
import android.annotation.SystemApi;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.RemoteException;
import com.android.internal.util.Preconditions;
@@ -113,7 +114,8 @@ public class ContextHubClient implements Closeable {
* describes the Context Hub the intent event was for. The intent will also have an extra
* {@link ContextHubManager.EXTRA_EVENT_TYPE} of type {@link ContextHubManager.Event}, which
* will contain the type of the event. See {@link ContextHubManager.Event} for description of
* each event type, along with event-specific extra fields.
* each event type, along with event-specific extra fields. A client can use
* {@link ContextHubIntentEvent.fromIntent(Intent)} to parse the Intent generated by the event.
*
* When the intent is received, this client can be recreated through
* {@link ContextHubManager.createClient(PendingIntent, ContextHubInfo,
@@ -126,10 +128,6 @@ public class ContextHubClient implements Closeable {
* continued to be maintained at the Context Hub Service until
* {@link #unregisterIntent(PendingIntent)} is called for registered intents.
*
* See {@link ContextHubBroadcastReceiver} for a helper class to generate the
* {@link PendingIntent} through a {@link BroadcastReceiver}, and maps an {@link Intent} to a
* {@link ContextHubClientCallback}.
*
* @param pendingIntent the PendingIntent to register for this client
* @param nanoAppId the unique ID of the nanoapp to receive events for
* @return true on success, false otherwise