2 *******************************************************************
\r
4 * Copyright 2015 Intel Corporation.
\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.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
37 public class MainActivity extends AppCompatActivity {
\r
39 private static final int PERMISSIONS_REQUEST_READ_PHONE_STATE = 0;
\r
40 private final String TAG = "NS_JNI_MAIN_ACTIVITY";
\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
53 private static int notiId = 0;
\r
55 private boolean isStarted = false;
\r
56 private String LastMessageId = null;
\r
57 private String consumerId;
\r
59 private NotiListener mNotiListener = null;
\r
60 private ProviderProxy mProviderProxy = null;
\r
63 protected void onCreate(Bundle savedInstanceState) {
\r
64 super.onCreate(savedInstanceState);
\r
65 setContentView(R.layout.activity_main);
\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
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
76 editTextTitle = (EditText) findViewById(R.id.EditTextTitle);
\r
77 editTextBody = (EditText) findViewById(R.id.EditTextBody);
\r
79 TvLog = (TextView) findViewById(R.id.TvLog);
\r
81 btnTitle.setEnabled(false);
\r
82 btnBody.setEnabled(false);
\r
84 btnSend.setOnClickListener(mClickListener);
\r
86 btnStart.setOnClickListener(mClickListener);
\r
88 btnAccept.setOnClickListener(mClickListener);
\r
89 btnAccept.setVisibility(View.INVISIBLE);
\r
91 btnSync.setOnClickListener(mClickListener);
\r
92 btnSync.setVisibility(View.INVISIBLE);
\r
94 btnStop.setOnClickListener(mClickListener);
\r
96 mProviderProxy = new ProviderProxy(getApplicationContext());
\r
97 mNotiListener = new NotiListener(this);
\r
99 Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
\r
100 startActivity(intent);
\r
103 public ProviderProxy getProviderExample() {
\r
104 return mProviderProxy;
\r
107 Button.OnClickListener mClickListener = new View.OnClickListener() {
\r
108 public void onClick(View v) {
\r
109 switch (v.getId()) {
\r
111 case R.id.BtnStart: {
\r
112 if (isStarted == false) {
\r
113 Log.i(TAG, "Start NS Provider Service");
\r
115 boolean access = true; // ptovider controls the acceptance of consumers
\r
116 mProviderProxy.startNotificationServer(access);
\r
119 Log.e(TAG, "NS Provider Service had already started");
\r
124 case R.id.BtnAccept: {
\r
125 if(isStarted == false)
\r
127 Log.e(TAG, "Fail to request Accept");
\r
130 mProviderProxy.accept("#consumerid", true);
\r
134 case R.id.BtnCreateNoti: {
\r
136 Random r = new Random();
\r
137 int notiId = r.nextInt(901) + 100; //[100, 1000]
\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
143 if(isStarted == false)
\r
145 Log.e(TAG, "Fail to send NSMessage");
\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
157 LastMessageId= id; // for test to send sync
\r
159 Log.i(TAG, "#" + notiId + " notified ..");
\r
164 case R.id.BtnSync: {
\r
165 if(isStarted == false)
\r
167 Log.e(TAG, "Fail to send sync");
\r
170 mProviderProxy.readCheck(LastMessageId);
\r
174 case R.id.BtnStop: {
\r
175 if(isStarted == false)
\r
177 Log.e(TAG, "Fail to stop service");
\r
181 mProviderProxy.stopNotificationServer();
\r