1 /*
2  * Copyright (C) 2017 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 android.inputmethodservice.cts.receiver;
18 
19 import static android.inputmethodservice.cts.common.DeviceEventConstants.EXTRA_EVENT_TIME;
20 
21 import android.content.BroadcastReceiver;
22 import android.content.ContentValues;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.inputmethodservice.cts.DeviceEvent;
26 import android.inputmethodservice.cts.common.EventProviderConstants.EventTableConstants;
27 import android.net.Uri;
28 import android.os.SystemClock;
29 import android.util.Log;
30 
31 /**
32  * An implementation of {@link BroadcastReceiver} to collect event logs on device.
33  */
34 public final class EventReceiver extends BroadcastReceiver {
35 
36     private static final String TAG = EventReceiver.class.getSimpleName();
37     private static final boolean DEBUG = false;
38 
39     private static final Uri CONTENT_URI = Uri.parse(EventTableConstants.CONTENT_URI);
40 
41     @Override
onReceive(Context context, Intent intent)42     public void onReceive(Context context, Intent intent) {
43         // Since {@code intent} which comes from host has no
44         // {@link DeviceEventConstants#EXTRA_EVENT_TIME EXTRA_EVENT_TIME} extra, here we record the
45         // time.
46         if (!intent.hasExtra(EXTRA_EVENT_TIME)) {
47             intent.putExtra(EXTRA_EVENT_TIME, SystemClock.uptimeMillis());
48         }
49         final DeviceEvent event = DeviceEvent.newEvent(intent);
50         if (DEBUG) {
51             Log.d(TAG, "onReceive: event=" + event);
52         }
53         final ContentValues values = DeviceEvent.buildContentValues(event);
54         context.getContentResolver().insert(CONTENT_URI, values);
55     }
56 }
57