Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / service / protocol-plugin / plugins / Android / plugin.wemo / src / oic / plugin / wemo / Activator.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 /// @file Activator.java
22
23 package oic.plugin.wemo;
24
25 import java.util.ArrayList;
26 import java.util.EnumSet;
27
28 import org.iotivity.base.ModeType;
29 import org.iotivity.base.ObserveType;
30 import org.iotivity.base.OcException;
31 import org.iotivity.base.OcPlatform;
32 import org.iotivity.base.OcResource;
33 import org.iotivity.base.OcResourceHandle;
34 import org.iotivity.base.PlatformConfig;
35 import org.iotivity.base.QualityOfService;
36 import org.iotivity.base.ResourceProperty;
37 import org.iotivity.base.ServiceType;
38 import org.osgi.framework.BundleActivator;
39 import org.osgi.framework.BundleContext;
40 import org.osgi.framework.ServiceReference;
41
42 import android.app.Activity;
43 import android.content.Context;
44 import android.os.Bundle;
45 import android.util.Log;
46
47 import com.belkin.wemo.localsdk.WeMoDevice;
48 import com.belkin.wemo.localsdk.WeMoSDKContext;
49
50 public class Activator extends Activity implements BundleActivator,
51         WeMoSDKContext.NotificationListener {
52     final private static String  TAG             = "SimpleClient";
53
54     private static BundleContext context;
55     private Context              AppContext;
56     public static WeMoSDKContext mWeMoSDKContext = null;
57     WeMoDevice                   wemoDevice;
58
59     static class SmartPlug {
60         public String m_power;
61         public String m_name;
62
63         public SmartPlug() {
64             m_power = "";
65             m_name = "";
66         }
67     }
68
69     static int                     OBSERVE_TYPE_TO_USE = ObserveType.OBSERVE
70                                                                .getValue();
71
72     public static SmartPlug        mySmartPlug;
73     public static OcResource       curResource;
74     public static OcResourceHandle resourceHandle;
75     static int                     oc                  = 0;
76     static Activity                mActivity;
77
78     static BundleContext getContext() {
79
80         return context;
81     }
82
83     @Override
84     protected void onCreate(Bundle savedInstanceState) {
85         super.onCreate(savedInstanceState);
86         mActivity = this;
87     }
88
89     public void start(BundleContext bundleContext) throws Exception {
90         /*
91          * OSGi bundle context
92          */
93         context = bundleContext;
94
95         /*
96          * Android Application Context
97          */
98         ServiceReference<Context> ref = context
99                 .getServiceReference(Context.class);
100         this.AppContext = ((Context) context.getService(ref));
101
102         /*
103          * WeMo Context
104          */
105         this.mWeMoSDKContext = new WeMoSDKContext(this.AppContext);
106         this.mWeMoSDKContext.addNotificationListener(this);
107         refresh();
108
109         mySmartPlug = new SmartPlug();
110     }
111
112     public void stop(BundleContext bundleContext) throws Exception {
113         OcPlatform.unregisterResource(resourceHandle);
114         context = null;
115         mWeMoSDKContext = null;
116         mySmartPlug = null;
117
118     }
119
120     public void onNotify(final String event, final String udn) {
121         runOnUiThread(new Runnable() {
122             public void run() {
123                 Activator.this.wemoDevice = Activator.this.mWeMoSDKContext
124                         .getWeMoDeviceByUDN(udn);
125                 if (event.equals("refresh")) {
126                     ArrayList<String> udns = Activator.this.mWeMoSDKContext
127                             .getListOfWeMoDevicesOnLAN();
128                     ArrayList<WeMoDevice> wemoDevices = new ArrayList();
129                     for (String udn : udns) {
130                         WeMoDevice listDevice = Activator.this.mWeMoSDKContext
131                                 .getWeMoDeviceByUDN(udn);
132                         if ((listDevice != null) && (listDevice.isAvailable())) {
133                             wemoDevices.add(listDevice);
134                             Log.i("WeMoPlugin", "WeMo Decive Name: "
135                                     + listDevice.getFriendlyName());
136                             Log.i("WeMoPlugin", "WeMo Decive UDN: "
137                                     + listDevice.getUDN());
138                             Log.i("WeMoPlugin", "WeMo Decive Type: "
139                                     + listDevice.getType());
140                             Log.i("WeMoPlugin", "WeMo Decive Serial #: "
141                                     + listDevice.getSerialNumber());
142                         }
143                     }
144                 } else if (event.equals(WeMoSDKContext.ADD_DEVICE)) {
145                     ArrayList<String> udns = Activator.this.mWeMoSDKContext
146                             .getListOfWeMoDevicesOnLAN();
147                     ArrayList<WeMoDevice> wemoDevices = new ArrayList();
148                     for (String udn : udns) {
149                         WeMoDevice listDevice = Activator.this.mWeMoSDKContext
150                                 .getWeMoDeviceByUDN(udn);
151                         if ((listDevice != null) && (listDevice.isAvailable())) {
152                             PlatformConfig cfg = new PlatformConfig(
153                                     this,
154                                     ServiceType.IN_PROC,
155                                     ModeType.CLIENT_SERVER, "0.0.0.0", 0,
156                                     QualityOfService.LOW);
157
158                             OcPlatform.Configure(cfg);
159
160                             EntityHandlerWemo entitycb = new EntityHandlerWemo();
161                             try {
162                                 resourceHandle = OcPlatform
163                                         .registerResource(
164                                                 "/a/wemo",
165                                                 "device.smartplug",
166                                                 "oc.mi.def",
167                                                 entitycb,
168                                                 EnumSet.of(ResourceProperty.DISCOVERABLE));
169                             } catch (OcException e) {
170                                 e.printStackTrace();
171                             }
172                         }
173                     }
174                 }
175             }
176         });
177     }
178
179     private void refresh() {
180         this.mWeMoSDKContext.refreshListOfWeMoDevicesOnLAN();
181     }
182 }