ddab18b07eee78641ba36e17869c8aeeee25fce1
[platform/upstream/iotivity.git] / service / notification / examples / android / NotiProviderExample / app / src / main / java / com / sec / notiproviderexample / MainActivity.java
1 /*\r
2  *******************************************************************\r
3  *\r
4  * Copyright 2015 Intel Corporation.\r
5  *\r
6  *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r
7  *\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
11  *\r
12  *      http://www.apache.org/licenses/LICENSE-2.0\r
13  *\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
19  *\r
20  *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\r
21  */\r
22 \r
23 package com.sec.notiproviderexample;\r
24 \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.support.v7.app.AppCompatActivity;\r
30 import android.util.Log;\r
31 import android.view.View;\r
32 import android.widget.Button;\r
33 import android.widget.EditText;\r
34 import android.widget.TextView;\r
35 import java.util.Random;\r
36 \r
37 public class MainActivity extends AppCompatActivity {\r
38 \r
39     private static final int  PERMISSIONS_REQUEST_READ_PHONE_STATE = 0;\r
40     private final String TAG = "NS_JNI_MAIN_ACTIVITY";\r
41 \r
42     private Button btnTitle;\r
43     private Button btnBody;\r
44     private Button btnSend;\r
45     private Button btnStart;\r
46     private Button btnStop;\r
47     private Button btnAccept;\r
48     private Button btnSync;\r
49     private EditText editTextTitle;\r
50     private EditText editTextBody;\r
51     private TextView TvLog;\r
52 \r
53     private static int notiId = 0;\r
54 \r
55     private boolean isStarted = false;\r
56     private String LastMessageId = null;\r
57     private String consumerId;\r
58 \r
59     private NotiListener mNotiListener = null;\r
60     private ProviderProxy mProviderProxy = null;\r
61 \r
62     @Override\r
63     protected void onCreate(Bundle savedInstanceState) {\r
64         super.onCreate(savedInstanceState);\r
65         setContentView(R.layout.activity_main);\r
66 \r
67         btnTitle = (Button) findViewById(R.id.BtnTitle);\r
68         btnBody = (Button) findViewById(R.id.BtnBody);\r
69         btnSend = (Button) findViewById(R.id.BtnCreateNoti);\r
70 \r
71         btnStart = (Button) findViewById(R.id.BtnStart);\r
72         btnAccept = (Button) findViewById(R.id.BtnAccept);\r
73         btnSync = (Button) findViewById(R.id.BtnSync);\r
74         btnStop = (Button) findViewById(R.id.BtnStop);\r
75 \r
76         editTextTitle = (EditText) findViewById(R.id.EditTextTitle);\r
77         editTextBody = (EditText) findViewById(R.id.EditTextBody);\r
78 \r
79         TvLog = (TextView) findViewById(R.id.TvLog);\r
80 \r
81         btnTitle.setEnabled(false);\r
82         btnBody.setEnabled(false);\r
83 \r
84         btnSend.setOnClickListener(mClickListener);\r
85 \r
86         btnStart.setOnClickListener(mClickListener);\r
87 \r
88         btnAccept.setOnClickListener(mClickListener);\r
89         btnAccept.setVisibility(View.INVISIBLE);\r
90 \r
91         btnSync.setOnClickListener(mClickListener);\r
92         btnSync.setVisibility(View.INVISIBLE);\r
93 \r
94         btnStop.setOnClickListener(mClickListener);\r
95 \r
96         mProviderProxy = new ProviderProxy(getApplicationContext());\r
97         mNotiListener = new NotiListener(this);\r
98 \r
99         Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");\r
100         startActivity(intent);\r
101     }\r
102 \r
103     public ProviderProxy getProviderExample() {\r
104         return mProviderProxy;\r
105     }\r
106 \r
107    Button.OnClickListener mClickListener = new View.OnClickListener() {\r
108         public void onClick(View v) {\r
109             switch (v.getId()) {\r
110 \r
111                 case R.id.BtnStart: {\r
112                     if (isStarted == false) {\r
113                         Log.i(TAG, "Start NS Provider Service");\r
114 \r
115                         boolean access = true; // ptovider controls the acceptance of consumers\r
116                         mProviderProxy.startNotificationServer(access);\r
117                         isStarted = true;\r
118                     } else {\r
119                         Log.e(TAG, "NS Provider Service had already started");\r
120                     }\r
121                 }\r
122                 break;\r
123 \r
124                 case R.id.BtnAccept: {\r
125                     if(isStarted == false)\r
126                     {\r
127                         Log.e(TAG, "Fail to request Accept");\r
128                         break;\r
129                     }\r
130                     mProviderProxy.accept("#consumerid", true);\r
131                 }\r
132                 break;\r
133 \r
134                 case R.id.BtnCreateNoti: {\r
135 \r
136                     Random r = new Random();\r
137                     int notiId = r.nextInt(901) + 100; //[100, 1000]\r
138 \r
139                     String id = Integer.toString(notiId); // generate notificaion ID\r
140                     String title = editTextTitle.getText().toString();\r
141                     String body = editTextBody.getText().toString();\r
142 \r
143                     if(isStarted == false)\r
144                     {\r
145                         Log.e(TAG, "Fail to send NSMessage");\r
146                         break;\r
147                     }\r
148 \r
149                     // Build android noti object and send it to Notification service receiver\r
150                     Notification.Builder notiBuilder = new Notification.Builder(getApplicationContext());\r
151                     notiBuilder.setContentTitle(title);\r
152                     notiBuilder.setContentText(body);\r
153                     notiBuilder.setSmallIcon(R.mipmap.ic_launcher);\r
154                     NotificationManager notiMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);\r
155                     notiMgr.notify(notiId, notiBuilder.build());\r
156 \r
157                     LastMessageId= id; // for test to send sync\r
158 \r
159                     Log.i(TAG, "#" + notiId + " notified ..");\r
160 \r
161                 }\r
162                 break;\r
163 \r
164                 case R.id.BtnSync: {\r
165                     if(isStarted == false)\r
166                     {\r
167                         Log.e(TAG, "Fail to send sync");\r
168                         break;\r
169                     }\r
170                     mProviderProxy.readCheck(LastMessageId);\r
171                 }\r
172                 break;\r
173 \r
174                 case R.id.BtnStop: {\r
175                     if(isStarted == false)\r
176                     {\r
177                         Log.e(TAG, "Fail to stop service");\r
178                         break;\r
179                     }\r
180 \r
181                     mProviderProxy.stopNotificationServer();\r
182                     isStarted = false;\r
183                 }\r
184                 break;\r
185             }\r
186         }\r
187     };\r
188 }\r
189 \r