Hold a wake lock while dispatching network activity events.
Also add new API for determining whether the current data network is active, and thus better scheduling network operations. This API is designed to not be tied to a mobile network -- regardless of the network, apps can use it to determine whether they should initiate activity or wait. On non-mobile networks, it simply always reports as the network being active. This changed involved reworking how the idle timers are done so that we only register an idle timer with the current default network. This way, we can know whether we currently expect to get callbacks about the network being active, or should just always report that it is active. (Ultimately we need to be getting this radio active data from the radio itself.) Change-Id: Iaf6cc91a960d7542a70b72f87a7db26d12c4ea8e
This commit is contained in:
@@ -20,5 +20,6 @@ package com.android.server;
|
||||
interface INativeDaemonConnectorCallbacks {
|
||||
|
||||
void onDaemonConnected();
|
||||
boolean onCheckHoldWakeLock(int code);
|
||||
boolean onEvent(int code, String raw, String[] cooked);
|
||||
}
|
||||
|
||||
@@ -21,6 +21,7 @@ import android.net.LocalSocketAddress;
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.os.Message;
|
||||
import android.os.PowerManager;
|
||||
import android.os.SystemClock;
|
||||
import android.util.LocalLog;
|
||||
import android.util.Slog;
|
||||
@@ -56,6 +57,8 @@ final class NativeDaemonConnector implements Runnable, Handler.Callback, Watchdo
|
||||
|
||||
private final ResponseQueue mResponseQueue;
|
||||
|
||||
private final PowerManager.WakeLock mWakeLock;
|
||||
|
||||
private INativeDaemonConnectorCallbacks mCallbacks;
|
||||
private Handler mCallbackHandler;
|
||||
|
||||
@@ -70,10 +73,14 @@ final class NativeDaemonConnector implements Runnable, Handler.Callback, Watchdo
|
||||
private final int BUFFER_SIZE = 4096;
|
||||
|
||||
NativeDaemonConnector(INativeDaemonConnectorCallbacks callbacks, String socket,
|
||||
int responseQueueSize, String logTag, int maxLogSize) {
|
||||
int responseQueueSize, String logTag, int maxLogSize, PowerManager.WakeLock wl) {
|
||||
mCallbacks = callbacks;
|
||||
mSocket = socket;
|
||||
mResponseQueue = new ResponseQueue(responseQueueSize);
|
||||
mWakeLock = wl;
|
||||
if (mWakeLock != null) {
|
||||
mWakeLock.setReferenceCounted(true);
|
||||
}
|
||||
mSequenceNumber = new AtomicInteger(0);
|
||||
TAG = logTag != null ? logTag : "NativeDaemonConnector";
|
||||
mLocalLog = new LocalLog(maxLogSize);
|
||||
@@ -102,6 +109,10 @@ final class NativeDaemonConnector implements Runnable, Handler.Callback, Watchdo
|
||||
}
|
||||
} catch (Exception e) {
|
||||
loge("Error handling '" + event + "': " + e);
|
||||
} finally {
|
||||
if (mCallbacks.onCheckHoldWakeLock(msg.what)) {
|
||||
mWakeLock.release();
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -154,18 +165,29 @@ final class NativeDaemonConnector implements Runnable, Handler.Callback, Watchdo
|
||||
buffer, start, i - start, StandardCharsets.UTF_8);
|
||||
log("RCV <- {" + rawEvent + "}");
|
||||
|
||||
boolean releaseWl = false;
|
||||
try {
|
||||
final NativeDaemonEvent event = NativeDaemonEvent.parseRawEvent(
|
||||
rawEvent);
|
||||
if (event.isClassUnsolicited()) {
|
||||
// TODO: migrate to sending NativeDaemonEvent instances
|
||||
mCallbackHandler.sendMessage(mCallbackHandler.obtainMessage(
|
||||
event.getCode(), event.getRawEvent()));
|
||||
if (mCallbacks.onCheckHoldWakeLock(event.getCode())) {
|
||||
mWakeLock.acquire();
|
||||
releaseWl = true;
|
||||
}
|
||||
if (mCallbackHandler.sendMessage(mCallbackHandler.obtainMessage(
|
||||
event.getCode(), event.getRawEvent()))) {
|
||||
releaseWl = false;
|
||||
}
|
||||
} else {
|
||||
mResponseQueue.add(event.getCmdNumber(), event);
|
||||
}
|
||||
} catch (IllegalArgumentException e) {
|
||||
log("Problem parsing message: " + rawEvent + " - " + e);
|
||||
} finally {
|
||||
if (releaseWl) {
|
||||
mWakeLock.acquire();
|
||||
}
|
||||
}
|
||||
|
||||
start = i + 1;
|
||||
|
||||
@@ -534,7 +534,7 @@ public class NsdService extends INsdManager.Stub {
|
||||
mContentResolver = context.getContentResolver();
|
||||
|
||||
mNativeConnector = new NativeDaemonConnector(new NativeCallbackReceiver(), "mdns", 10,
|
||||
MDNS_TAG, 25);
|
||||
MDNS_TAG, 25, null);
|
||||
|
||||
mNsdStateMachine = new NsdStateMachine(TAG);
|
||||
mNsdStateMachine.start();
|
||||
@@ -622,6 +622,10 @@ public class NsdService extends INsdManager.Stub {
|
||||
mNativeDaemonConnected.countDown();
|
||||
}
|
||||
|
||||
public boolean onCheckHoldWakeLock(int code) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean onEvent(int code, String raw, String[] cooked) {
|
||||
// TODO: NDC translates a message to a callback, we could enhance NDC to
|
||||
// directly interact with a state machine through messages
|
||||
|
||||
Reference in New Issue
Block a user