• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 package com.android.cts.verifier.vr;
17 
18 import android.content.ComponentName;
19 import android.content.Intent;
20 import android.os.IBinder;
21 import android.service.vr.VrListenerService;
22 import android.util.Log;
23 
24 import java.util.concurrent.ArrayBlockingQueue;
25 import java.util.concurrent.atomic.AtomicInteger;
26 
27 public class MockVrListenerService extends VrListenerService {
28     private static final String TAG = "MockVrListener";
29     private static final AtomicInteger sNumBound = new AtomicInteger();
30 
31     private static final ArrayBlockingQueue<Event> sEventQueue = new ArrayBlockingQueue<>(4096);
32 
getPendingEvents()33     public static ArrayBlockingQueue<Event> getPendingEvents() {
34         return sEventQueue;
35     }
36 
getNumBoundMockVrListeners()37     public static int getNumBoundMockVrListeners() {
38         return sNumBound.get();
39     }
40 
41     public enum EventType{
42         ONBIND,
43         ONREBIND,
44         ONUNBIND,
45         ONCREATE,
46         ONDESTROY,
47         ONCURRENTVRMODEACTIVITYCHANGED
48     }
49 
50     public static class Event {
51         public final VrListenerService instance;
52         public final EventType type;
53         public final Object arg1;
54 
Event(VrListenerService i, EventType t, Object o)55         private Event(VrListenerService i, EventType t, Object o) {
56             instance = i;
57             type = t;
58             arg1 = o;
59         }
60 
build(VrListenerService instance, EventType type, Object argument1)61         public static Event build(VrListenerService instance, EventType type, Object argument1) {
62             return new Event(instance, type, argument1);
63         }
64 
build(VrListenerService instance, EventType type)65         public static Event build(VrListenerService instance, EventType type) {
66             return new Event(instance, type, null);
67         }
68     }
69 
70     @Override
onBind(Intent intent)71     public IBinder onBind(Intent intent) {
72         Log.i(TAG, "onBind called");
73         sNumBound.getAndIncrement();
74         try {
75             sEventQueue.put(Event.build(this, EventType.ONBIND, intent));
76         } catch (InterruptedException e) {
77             Log.e(TAG, "Service thread interrupted: " + e);
78         }
79         return super.onBind(intent);
80     }
81 
82     @Override
onRebind(Intent intent)83     public void onRebind(Intent intent) {
84         Log.i(TAG, "onRebind called");
85         try {
86             sEventQueue.put(Event.build(this, EventType.ONREBIND, intent));
87         } catch (InterruptedException e) {
88             Log.e(TAG, "Service thread interrupted: " + e);
89         }
90         super.onRebind(intent);
91     }
92 
93     @Override
onUnbind(Intent intent)94     public boolean onUnbind(Intent intent) {
95         Log.i(TAG, "onUnbind called");
96         sNumBound.getAndDecrement();
97         try {
98             sEventQueue.put(Event.build(this, EventType.ONUNBIND, intent));
99         } catch (InterruptedException e) {
100             Log.e(TAG, "Service thread interrupted: " + e);
101         }
102         return super.onUnbind(intent);
103     }
104 
105     @Override
onCreate()106     public void onCreate() {
107         Log.i(TAG, "onCreate called");
108         try {
109             sEventQueue.put(Event.build(this, EventType.ONCREATE));
110         } catch (InterruptedException e) {
111             Log.e(TAG, "Service thread interrupted: " + e);
112         }
113         super.onCreate();
114     }
115 
116     @Override
onDestroy()117     public void onDestroy() {
118         Log.i(TAG, "onDestroy called");
119         try {
120             sEventQueue.put(Event.build(this, EventType.ONDESTROY));
121         } catch (InterruptedException e) {
122             Log.e(TAG, "Service thread interrupted: " + e);
123         }
124         super.onDestroy();
125     }
126 
127     @Override
onCurrentVrActivityChanged(ComponentName component)128     public void onCurrentVrActivityChanged(ComponentName component) {
129         Log.i(TAG, "onCurrentVrActivityChanged called with: " + component);
130         try {
131             sEventQueue.put(Event.build(this, EventType.ONCURRENTVRMODEACTIVITYCHANGED, component));
132         } catch (InterruptedException e) {
133             Log.e(TAG, "Service thread interrupted: " + e);
134         }
135     }
136 
137 }
138