Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / service / resource-container / examples / android / AndroidBundle / app / src / main / java / org / iotivity / service / sample / androidbundle / resources / GyroscopeResource.java
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 package org.iotivity.service.sample.androidbundle.resources;
22
23 import android.content.Context;
24 import android.hardware.Sensor;
25 import android.hardware.SensorEvent;
26 import android.hardware.SensorEventListener;
27 import android.hardware.SensorManager;
28 import android.util.Log;
29
30 import org.iotivity.service.resourcecontainer.BundleResource;
31 import org.iotivity.service.resourcecontainer.BundleResource;
32 import org.iotivity.service.resourcecontainer.RcsResourceAttributes;
33 import org.iotivity.service.resourcecontainer.RcsValue;
34
35 public class GyroscopeResource extends BundleResource implements SensorEventListener {
36     private static final String LOG_TAG = GyroscopeResource.class.getSimpleName();
37     private final SensorManager mSensorManager;
38     private final Sensor humiditySensor;
39
40     public GyroscopeResource(Context context){
41         super(context);
42         this.setResourceType("oic.r.gyroscope");
43         this.setName("gyroscopeSensor");
44         mSensorManager = (SensorManager) context.getApplicationContext().
45                 getSystemService(Context.SENSOR_SERVICE);
46         humiditySensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
47         mSensorManager.registerListener(this, humiditySensor, SensorManager.SENSOR_DELAY_NORMAL);
48         Log.d(LOG_TAG, "Created new gyroscope instance");
49     }
50
51     @Override
52     protected void initAttributes() {
53         this.m_attributes.put("x", 0d);
54         this.m_attributes.put("y", 0d);
55         this.m_attributes.put("z", 0d);
56     }
57
58     @Override
59     public void handleSetAttributesRequest(RcsResourceAttributes attrs) {
60         Log.i(LOG_TAG, "Set Attributes called with ");
61         for(String key: attrs.keySet()){
62             Log.i(LOG_TAG, " " + key + ": " + attrs.get(key));
63         }
64     }
65
66     @Override
67     public RcsResourceAttributes handleGetAttributesRequest() {
68         Log.i(LOG_TAG, "Get Attributes called");
69         Log.i(LOG_TAG, "Returning: ");
70         for(String key:m_attributes.keySet()){
71             Log.i(LOG_TAG, " " + key + ": " + m_attributes.get(key));
72         }
73         return this.m_attributes;
74     }
75
76     @Override
77     public void onSensorChanged(SensorEvent sensorEvent) {
78         Log.i(LOG_TAG, "Sensor event " + sensorEvent.values[0] + ", " + sensorEvent.values[1]
79             + ", " + sensorEvent.values[2]);
80         setAttribute("x", new RcsValue( (int) (sensorEvent.values[0])), true);
81         setAttribute("y", new RcsValue( (int) (sensorEvent.values[1]) ) , true);
82         setAttribute("z", new RcsValue( (int) (sensorEvent.values[2]) ) , true);
83     }
84
85     @Override
86     public void onAccuracyChanged(Sensor sensor, int i) {
87
88     }
89 }