1 /* 2 * Copyright (C) 2020 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 17 package com.android.eventlib; 18 19 import android.app.Service; 20 import android.content.Intent; 21 import android.os.Bundle; 22 import android.os.IBinder; 23 24 import java.time.Duration; 25 import java.time.Instant; 26 27 /** 28 * Implementation of {@link IQueryService}. 29 */ 30 public final class QueryService extends Service { 31 32 public static final String EARLIEST_LOG_TIME_KEY = "EARLIEST_LOG_TIME"; 33 public static final String QUERIER_KEY = "QUERIER"; 34 public static final String EVENT_KEY = "EVENT"; 35 public static final String TIMEOUT_KEY = "TIMEOUT"; 36 37 private final IQueryService.Stub binder = new IQueryService.Stub() { 38 @Override 39 public Bundle poll(Bundle data, int skip) { 40 EventLogsQuery<?, ?> query = (EventLogsQuery<?, ?>) data.getSerializable(QUERIER_KEY); 41 LocalEventQuerier<?, ?> querier = 42 new LocalEventQuerier<>(getApplicationContext(), query); 43 Instant earliestLogtime = (Instant) data.getSerializable(EARLIEST_LOG_TIME_KEY); 44 Duration timeoutDuration = (Duration) data.getSerializable(TIMEOUT_KEY); 45 Event e = querier.poll(earliestLogtime, timeoutDuration, skip); 46 return prepareReturnBundle(e); 47 } 48 49 private Bundle prepareReturnBundle(Event event) { 50 Bundle responseBundle = new Bundle(); 51 if (event != null) { 52 responseBundle.putSerializable(EVENT_KEY, event); 53 } 54 55 return responseBundle; 56 } 57 }; 58 59 @Override onBind(Intent intent)60 public IBinder onBind(Intent intent) { 61 return binder; 62 } 63 } 64