Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / protocol-plugin / plugins / Android / plugin.gear.noti / src / oic / plugin / gear / noti / 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.gear.noti;
24
25 import java.util.EnumSet;
26 import java.util.UUID;
27
28 import org.iotivity.base.ModeType;
29 import org.iotivity.base.ObserveType;
30 import org.iotivity.base.OcPlatform;
31 import org.iotivity.base.OcResource;
32 import org.iotivity.base.OcResourceHandle;
33 import org.iotivity.base.PlatformConfig;
34 import org.iotivity.base.QualityOfService;
35 import org.iotivity.base.ResourceProperty;
36 import org.iotivity.base.ServiceType;
37 import org.osgi.framework.BundleActivator;
38 import org.osgi.framework.BundleContext;
39 import org.osgi.framework.ServiceReference;
40
41 import android.app.Activity;
42 import android.content.Context;
43 import android.util.Log;
44
45 import com.samsung.android.sdk.SsdkUnsupportedException;
46 import com.samsung.android.sdk.richnotification.Srn;
47 import com.samsung.android.sdk.richnotification.SrnRichNotification;
48 import com.samsung.android.sdk.richnotification.SrnRichNotificationManager;
49 import com.samsung.android.sdk.richnotification.SrnRichNotificationManager.ErrorType;
50 import com.samsung.android.sdk.richnotification.SrnRichNotificationManager.EventListener;
51
52 public class Activator extends Activity implements BundleActivator,
53         EventListener {
54
55     final private static String TAG = "GearNoti";
56
57     public enum TemplateTypes {
58         SMALL_HEADER;
59     }
60
61     private static BundleContext             context;
62     public static Context                    AppContext;
63     public static SrnRichNotificationManager mRichNotificationManager;
64
65     class Notify {
66         public String m_power;
67         public String m_name;
68
69         public Notify() {
70             m_power = "";
71             m_name = "device.notify";
72         }
73     }
74
75     static int                     OBSERVE_TYPE_TO_USE = ObserveType.OBSERVE
76                                                                .getValue();
77
78     public static Notify           myNotify;
79     public static OcResource       curResource;
80     public static OcResourceHandle resourceHandle;
81     static int                     oc                  = 0;
82     static Activity                mActivity;
83
84     static BundleContext getContext() {
85         return context;
86     }
87
88     public void start(BundleContext bundleContext) throws Exception {
89         Activator.context = bundleContext;
90
91         ServiceReference<Context> ref = Activator.context
92                 .getServiceReference(Context.class);
93         AppContext = Activator.context.getService(ref);
94
95         myNotify = new Notify();
96
97         Srn srn = new Srn();
98         try {
99             // Initializes an instance of Srn.
100             srn.initialize(AppContext);
101             Log.w(TAG, "SRN initialized");
102         } catch (SsdkUnsupportedException e) {
103             Log.w(TAG, "SsdkUnsupportedException");
104         }
105
106         mRichNotificationManager = new SrnRichNotificationManager(AppContext);
107
108         if (mRichNotificationManager.isConnected()) {
109             // Starts the manager.
110             mRichNotificationManager.start();
111             Log.w(TAG, "RichNoti Manager Started");
112
113             SrnRichNotification myRichNotification = new SrnRichNotification(
114                     AppContext);
115             Log.w(TAG, "RichNoti instance created");
116
117             PlatformConfig cfg = new PlatformConfig(this, ServiceType.IN_PROC,
118                     ModeType.CLIENT_SERVER, "0.0.0.0", 0, QualityOfService.LOW);
119
120             OcPlatform.Configure(cfg);
121
122             EntityHandlerNoti entitycb = new EntityHandlerNoti();
123
124             resourceHandle = OcPlatform.registerResource("/a/galaxy/gear",
125                     "device.notify", OcPlatform.DEFAULT_INTERFACE, entitycb,
126                     EnumSet.of(ResourceProperty.DISCOVERABLE));
127         }
128         else{
129             Log.w(TAG, "Something Happened");
130         }
131     }
132
133     @Override
134     protected void onResume() {
135         super.onResume();
136         mRichNotificationManager.registerRichNotificationListener(this);
137     }
138
139     @Override
140     protected void onPause() {
141         super.onPause();
142         mRichNotificationManager.unregisterRichNotificationListener(this);
143     }
144
145     @Override
146     public void onError(UUID arg0, ErrorType arg1) {
147         // TODO Auto-generated method stub
148
149     }
150
151     @Override
152     public void onRead(UUID arg0) {
153         // TODO Auto-generated method stub
154
155     }
156
157     @Override
158     public void onRemoved(UUID arg0) {
159         // TODO Auto-generated method stub
160
161     }
162
163     @Override
164     public void stop(BundleContext arg0) throws Exception {
165         // TODO Auto-generated method stub
166         OcPlatform.unregisterResource(resourceHandle);
167         context = null;
168         mRichNotificationManager = null;
169         myNotify = null;
170
171     }
172
173 }