2 //******************************************************************
\r
4 // Copyright 2016 Samsung Electronics All Rights Reserved.
\r
6 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
\r
8 // Licensed under the Apache License, Version 2.0 (the "License");
\r
9 // you may not use this file except in compliance with the License.
\r
10 // You may obtain a copy of the License at
\r
12 // http://www.apache.org/licenses/LICENSE-2.0
\r
14 // Unless required by applicable law or agreed to in writing, software
\r
15 // distributed under the License is distributed on an "AS IS" BASIS,
\r
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
17 // See the License for the specific language governing permissions and
\r
18 // limitations under the License.
\r
20 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
\r
23 package com.sec.notiproviderexample;
\r
25 import android.app.Notification;
\r
26 import android.app.NotificationManager;
\r
27 import android.content.Intent;
\r
28 import android.os.Bundle;
\r
29 import android.os.Handler;
\r
30 import android.os.Message;
\r
31 import android.util.Log;
\r
32 import android.view.View;
\r
33 import android.widget.Button;
\r
34 import android.widget.EditText;
\r
35 import android.widget.RadioButton;
\r
36 import android.widget.TextView;
\r
37 import android.widget.Toast;
\r
38 import android.app.Activity;
\r
40 import org.iotivity.service.ns.common.MediaContents;
\r
42 import java.text.DateFormat;
\r
43 import java.util.Date;
\r
45 public class MainActivity extends Activity {
\r
47 private final String TAG = "NS_MAIN_ACTIVITY";
\r
48 private static final int CONSUMER_SUBSCRIBED = 1;
\r
49 private static final int MESSAGE_SYNC = 2;
\r
50 private static final int MESSAGE_NOTIFICATION = 3;
\r
52 private Button btnTitle;
\r
53 private Button btnBody;
\r
54 private Button btnTopic;
\r
55 private Button btnSend;
\r
56 private Button btnStart;
\r
57 private Button btnRegister;
\r
58 private Button btnSet;
\r
59 private Button btnStop;
\r
60 private Button btnLog;
\r
61 private EditText editTextTitle;
\r
62 private EditText editTextBody;
\r
63 private EditText editTextTopic;
\r
64 private RadioButton radioProvider;
\r
65 private RadioButton radioConsumer;
\r
66 private static TextView TvLog;
\r
68 private static int notiId = 100;
\r
69 private static int subCnt = 0;
\r
70 private boolean isStarted = false;
\r
71 private String consumerId;
\r
72 private boolean gAcceptor = true;
\r
74 private NotiListener mNotiListener = null;
\r
75 private ProviderSample mProviderSample = null;
\r
77 public static Handler mHandler = new Handler() {
\r
79 public void handleMessage(Message msg) {
\r
81 case CONSUMER_SUBSCRIBED:
\r
82 String ConsumerId = (String) msg.obj;
\r
83 if(ConsumerId != null)
\r
84 TvLog.append("Consumer Subscibed: " + ConsumerId + "\n");
\r
88 String sync = (String) msg.obj;
\r
90 TvLog.append("SyncInfo Received :" + sync + "\n");
\r
99 public void showToast(final String toast)
\r
101 runOnUiThread(new Runnable() {
\r
103 public void run() {
\r
104 Toast.makeText(getApplicationContext(), toast, Toast.LENGTH_SHORT).show();
\r
110 protected void onCreate(Bundle savedInstanceState) {
\r
111 super.onCreate(savedInstanceState);
\r
112 setContentView(R.layout.activity_main);
\r
114 btnTitle = (Button) findViewById(R.id.BtnTitle);
\r
115 btnBody = (Button) findViewById(R.id.BtnBody);
\r
116 btnTopic = (Button) findViewById(R.id.BtnTopic);
\r
117 btnSend = (Button) findViewById(R.id.BtnCreateNoti);
\r
119 btnStart = (Button) findViewById(R.id.BtnStart);
\r
120 btnRegister = (Button) findViewById(R.id.BtnRegister);
\r
121 btnSet = (Button) findViewById(R.id.BtnSet);
\r
122 btnLog = (Button) findViewById(R.id.BtnLog);
\r
123 btnStop = (Button) findViewById(R.id.BtnStop);
\r
125 editTextTitle = (EditText) findViewById(R.id.EditTextTitle);
\r
126 editTextBody = (EditText) findViewById(R.id.EditTextBody);
\r
127 editTextTopic = (EditText) findViewById(R.id.EditTextTopic);
\r
129 radioProvider = (RadioButton) findViewById(R.id.RadioProvider);
\r
130 radioConsumer = (RadioButton) findViewById(R.id.RadioConsumer);
\r
132 TvLog = (TextView) findViewById(R.id.TvLog);
\r
134 btnTitle.setEnabled(false);
\r
135 btnBody.setEnabled(false);
\r
136 btnTopic.setEnabled(false);
\r
138 btnSend.setOnClickListener(mClickListener);
\r
139 btnStart.setOnClickListener(mClickListener);
\r
140 btnRegister.setOnClickListener(mClickListener);
\r
141 btnSet.setOnClickListener(mClickListener);
\r
142 btnLog.setOnClickListener(mClickListener);
\r
143 btnStop.setOnClickListener(mClickListener);
\r
144 radioProvider.setOnClickListener(mClickListener);
\r
145 radioConsumer.setOnClickListener(mClickListener);
\r
147 mProviderSample = new ProviderSample(getApplicationContext());
\r
148 mProviderSample.setHandler(mHandler);
\r
150 mNotiListener = new NotiListener(this);
\r
152 Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
\r
153 startActivity(intent);
\r
157 protected void onDestroy() {
\r
161 public ProviderSample getProviderSample()
\r
163 return mProviderSample;
\r
166 Button.OnClickListener mClickListener = new View.OnClickListener() {
\r
167 public void onClick(View v) {
\r
168 switch (v.getId()) {
\r
169 case R.id.RadioProvider: {
\r
170 if (isStarted == false) {
\r
172 showToast("Provider as acceptor is " + gAcceptor);
\r
175 showToast("Start ProviderService again to change acceptor as provider");
\r
179 case R.id.RadioConsumer: {
\r
180 if (isStarted == false) {
\r
182 showToast("Provider as acceptor is "+ gAcceptor);
\r
185 showToast("Start ProviderService again to change acceptor as consumer");
\r
189 case R.id.BtnStart: {
\r
190 if (isStarted == false) {
\r
191 Log.i(TAG, "Start Provider Service");
\r
192 TvLog.setText("Start Provider Service\n");
\r
193 mProviderSample.Start(gAcceptor);
\r
195 radioProvider.setEnabled(false);
\r
196 radioConsumer.setEnabled(false);
\r
198 Log.e(TAG, " Provider Service had already started");
\r
199 showToast(" Provider Service had already started");
\r
204 case R.id.BtnRegister: {
\r
205 if (isStarted == false) {
\r
206 Log.i(TAG, "Start Provider Service");
\r
207 TvLog.append("Register Topic : OCF_TOPIC1\n");
\r
208 TvLog.append("Register Topic : OCF_TOPIC2\n");
\r
209 TvLog.append("Register Topic : OCF_TOPIC3\n");
\r
210 TvLog.append("Register Topic : OCF_TOPIC4\n");
\r
211 showToast("Start Provider Service First");
\r
214 mProviderSample.RegisterTopic();
\r
219 case R.id.BtnSet: {
\r
220 if (isStarted == false) {
\r
221 Log.i(TAG, "Start Provider Service");
\r
222 TvLog.append("Set Topic : OCF_TOPIC1\n");
\r
223 TvLog.append("Set Topic : OCF_TOPIC2\n");
\r
224 TvLog.append("Set Topic : OCF_TOPIC3\n");
\r
225 TvLog.append("Set Topic : OCF_TOPIC4\n");
\r
226 showToast("Start Provider Service First");
\r
229 if(gAcceptor == false){
\r
230 showToast("Operation Not Permitted: \nStart Provider Service with provider as acceptor");
\r
233 mProviderSample.SetTopic();
\r
238 case R.id.BtnCreateNoti: {
\r
240 String id = Integer.toString(notiId); // generate notificaion ID
\r
241 String title = editTextTitle.getText().toString();
\r
242 String body = editTextBody.getText().toString();
\r
243 String topic = editTextTopic.getText().toString();
\r
245 if(isStarted == false)
\r
247 Log.e(TAG, "Fail to send NSMessage");
\r
248 showToast("Start ProviderService First");
\r
252 // Build android noti object and send it to Notification service receiver
\r
253 Notification.Builder notiBuilder = new Notification.Builder(getApplicationContext());
\r
254 notiBuilder.setContentTitle(title);
\r
255 notiBuilder.setContentText(body);
\r
256 notiBuilder.setPriority(Notification.PRIORITY_MAX);
\r
257 notiBuilder.setDefaults(Notification.DEFAULT_ALL);
\r
258 notiBuilder.setSmallIcon(R.mipmap.ic_launcher);
\r
259 NotificationManager notiMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
\r
260 notiMgr.notify(notiId, notiBuilder.build());
\r
262 Log.i(TAG, "#" + notiId + " notified ..");
\r
263 TvLog.append("Send Notitication(Msg ID: " + notiId + ")\n");
\r
268 case R.id.BtnStop: {
\r
269 if(isStarted == false) {
\r
270 Log.e(TAG, "Fail to stop service");
\r
271 showToast("Already Stopped");
\r
275 mProviderSample.Stop();
\r
277 radioProvider.setEnabled(true);
\r
278 radioConsumer.setEnabled(true);
\r
279 showToast("Stopped ProviderService"+ isStarted);
\r
280 TvLog.append("Stop Provider Service\n");
\r
284 case R.id.BtnLog: {
\r