| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.service.ambientcontext; |
| 18 |
|
| 19 |
import android.annotation.BinderThread; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.Nullable; |
| 22 |
import android.annotation.SystemApi; |
| 23 |
import android.app.Service; |
| 24 |
import android.app.ambientcontext.AmbientContextEvent; |
| 25 |
import android.app.ambientcontext.AmbientContextEventRequest; |
| 26 |
import android.content.Intent; |
| 27 |
import android.os.Bundle; |
| 28 |
import android.os.IBinder; |
| 29 |
import android.os.RemoteCallback; |
| 30 |
import android.util.Slog; |
| 31 |
|
| 32 |
import java.util.Objects; |
| 33 |
import java.util.function.Consumer; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
@SystemApi |
| 55 |
public abstract class AmbientContextDetectionService extends Service { |
| 56 |
private static final String TAG = AmbientContextDetectionService.class.getSimpleName(); |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public static final String SERVICE_INTERFACE = |
| 65 |
"android.service.ambientcontext.AmbientContextDetectionService"; |
| 66 |
|
| 67 |
@Nullable |
| 68 |
@Override |
| 69 |
public final IBinder onBind(@NonNull Intent intent) { |
| 70 |
if (SERVICE_INTERFACE.equals(intent.getAction())) { |
| 71 |
return new IAmbientContextDetectionService.Stub() { |
| 72 |
|
| 73 |
@Override |
| 74 |
public void startDetection( |
| 75 |
@NonNull AmbientContextEventRequest request, String packageName, |
| 76 |
RemoteCallback detectionResultCallback, RemoteCallback statusCallback) { |
| 77 |
Objects.requireNonNull(request); |
| 78 |
Objects.requireNonNull(packageName); |
| 79 |
Objects.requireNonNull(detectionResultCallback); |
| 80 |
Objects.requireNonNull(statusCallback); |
| 81 |
Consumer<AmbientContextDetectionResult> detectionResultConsumer = |
| 82 |
result -> { |
| 83 |
Bundle bundle = new Bundle(); |
| 84 |
bundle.putParcelable( |
| 85 |
AmbientContextDetectionResult.RESULT_RESPONSE_BUNDLE_KEY, |
| 86 |
result); |
| 87 |
detectionResultCallback.sendResult(bundle); |
| 88 |
}; |
| 89 |
Consumer<AmbientContextDetectionServiceStatus> statusConsumer = |
| 90 |
status -> { |
| 91 |
Bundle bundle = new Bundle(); |
| 92 |
bundle.putParcelable( |
| 93 |
AmbientContextDetectionServiceStatus |
| 94 |
.STATUS_RESPONSE_BUNDLE_KEY, |
| 95 |
status); |
| 96 |
statusCallback.sendResult(bundle); |
| 97 |
}; |
| 98 |
AmbientContextDetectionService.this.onStartDetection( |
| 99 |
request, packageName, detectionResultConsumer, statusConsumer); |
| 100 |
Slog.d(TAG, "startDetection " + request); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
@Override |
| 105 |
public void stopDetection(String packageName) { |
| 106 |
Objects.requireNonNull(packageName); |
| 107 |
AmbientContextDetectionService.this.onStopDetection(packageName); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
@Override |
| 112 |
public void queryServiceStatus( |
| 113 |
@AmbientContextEvent.EventCode int[] eventTypes, |
| 114 |
String packageName, |
| 115 |
RemoteCallback callback) { |
| 116 |
Objects.requireNonNull(eventTypes); |
| 117 |
Objects.requireNonNull(packageName); |
| 118 |
Objects.requireNonNull(callback); |
| 119 |
Consumer<AmbientContextDetectionServiceStatus> consumer = |
| 120 |
response -> { |
| 121 |
Bundle bundle = new Bundle(); |
| 122 |
bundle.putParcelable( |
| 123 |
AmbientContextDetectionServiceStatus |
| 124 |
.STATUS_RESPONSE_BUNDLE_KEY, |
| 125 |
response); |
| 126 |
callback.sendResult(bundle); |
| 127 |
}; |
| 128 |
AmbientContextDetectionService.this.onQueryServiceStatus( |
| 129 |
eventTypes, packageName, consumer); |
| 130 |
} |
| 131 |
}; |
| 132 |
} |
| 133 |
return null; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
@BinderThread |
| 156 |
public abstract void onStartDetection( |
| 157 |
@NonNull AmbientContextEventRequest request, |
| 158 |
@NonNull String packageName, |
| 159 |
@NonNull Consumer<AmbientContextDetectionResult> detectionResultConsumer, |
| 160 |
@NonNull Consumer<AmbientContextDetectionServiceStatus> statusConsumer); |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public abstract void onStopDetection(@NonNull String packageName); |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
@BinderThread |
| 179 |
public abstract void onQueryServiceStatus( |
| 180 |
@NonNull int[] eventTypes, |
| 181 |
@NonNull String packageName, |
| 182 |
@NonNull Consumer<AmbientContextDetectionServiceStatus> consumer); |
| 183 |
} |