Imported Upstream version 1.1.1
[platform/upstream/iotivity.git] / android / examples / devicediscoveryserver / src / main / java / org / iotivity / base / examples / DeviceDiscoveryServer.java
1 /*
2  * //******************************************************************
3  * //
4  * // Copyright 2015 Intel Corporation.
5  * //
6  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7  * //
8  * // Licensed under the Apache License, Version 2.0 (the "License");
9  * // you may not use this file except in compliance with the License.
10  * // You may obtain a copy of the License at
11  * //
12  * //      http://www.apache.org/licenses/LICENSE-2.0
13  * //
14  * // Unless required by applicable law or agreed to in writing, software
15  * // distributed under the License is distributed on an "AS IS" BASIS,
16  * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * // See the License for the specific language governing permissions and
18  * // limitations under the License.
19  * //
20  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21  */
22 package org.iotivity.base.examples;
23
24 import android.app.Activity;
25 import android.content.Context;
26 import android.os.Bundle;
27 import android.text.method.ScrollingMovementMethod;
28 import android.util.Log;
29 import android.view.View;
30 import android.widget.Button;
31 import android.widget.ScrollView;
32 import android.widget.TextView;
33
34 import org.iotivity.base.ModeType;
35 import org.iotivity.base.OcDeviceInfo;
36 import org.iotivity.base.OcException;
37 import org.iotivity.base.OcPlatform;
38 import org.iotivity.base.OcPlatformInfo;
39 import org.iotivity.base.PlatformConfig;
40 import org.iotivity.base.QualityOfService;
41 import org.iotivity.base.ServiceType;
42
43 import java.util.Arrays;
44
45 /**
46  * This sample demonstrates platform and device discovery feature.
47  * The server sets the platform and device related info. which can be later retrieved by a client.
48  */
49 public class DeviceDiscoveryServer extends Activity {
50
51     private void startDeviceDiscoveryServer() {
52         Context context = this;
53
54         PlatformConfig platformConfig = new PlatformConfig(
55                 context,
56                 ServiceType.IN_PROC,
57                 ModeType.SERVER,
58                 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
59                 0,         // Uses randomly available port
60                 QualityOfService.LOW
61         );
62
63         msg("Configuring platform.");
64         OcPlatform.Configure(platformConfig);
65
66         OcDeviceInfo deviceInfo = new OcDeviceInfo(
67                 "myDeviceName",
68                 Arrays.asList(new String[]{"oic.d.phone"})
69         );
70
71         try {
72             msg("Registering device info");
73             OcPlatform.registerDeviceInfo(deviceInfo);
74         } catch (OcException e) {
75             Log.e(TAG, e.toString());
76             msg("Failed to register device info.");
77         }
78
79         OcPlatformInfo platformInfo = new OcPlatformInfo(
80                 "myPlatformId",             //Platform ID
81                 "myManufactName",           //Manufacturer Name
82                 "www.myurl.com",            //Manufacturer URL
83                 "myModelNumber",            //Model Number
84                 "myDateOfManufacture",      //Date of Manufacture
85                 "myPlatformVersion",        //Platform Version
86                 "Manufacturer OS version",  //Operating System Version
87                 "myHardwareVersion",        //Hardware Version
88                 "myFirmwareVersion",        //Firmware Version
89                 "www.mysupporturl.com",     //Support URL
90                 String.valueOf(System.currentTimeMillis()) // System Time
91         );
92         try {
93             msg("Registering platform info");
94             OcPlatform.registerPlatformInfo(platformInfo);
95         } catch (OcException e) {
96             Log.e(TAG, e.toString());
97             msg("Failed to register platform info.");
98         }
99
100         msg("Waiting for the requests...");
101         printLine();
102     }
103
104     //******************************************************************************
105     // End of the OIC specific code
106     //******************************************************************************
107
108     private final static String TAG = DeviceDiscoveryServer.class.getSimpleName();
109     private TextView mConsoleTextView;
110     private ScrollView mScrollView;
111
112     @Override
113     protected void onCreate(Bundle savedInstanceState) {
114         super.onCreate(savedInstanceState);
115         setContentView(R.layout.activity_device_discovery_server);
116
117         mConsoleTextView = (TextView) findViewById(R.id.consoleTextView);
118         mConsoleTextView.setMovementMethod(new ScrollingMovementMethod());
119         mScrollView = (ScrollView) findViewById(R.id.scrollView);
120         mScrollView.fullScroll(View.FOCUS_DOWN);
121         final Button button = (Button) findViewById(R.id.button);
122
123         if (null == savedInstanceState) {
124             button.setOnClickListener(new View.OnClickListener() {
125                 @Override
126                 public void onClick(View v) {
127                     button.setEnabled(false);
128                     new Thread(new Runnable() {
129                         public void run() {
130                             startDeviceDiscoveryServer();
131                         }
132                     }).start();
133                 }
134             });
135         } else {
136             String consoleOutput = savedInstanceState.getString("consoleOutputString");
137             mConsoleTextView.setText(consoleOutput);
138         }
139     }
140
141     @Override
142     protected void onSaveInstanceState(Bundle outState) {
143         super.onSaveInstanceState(outState);
144         outState.putString("consoleOutputString", mConsoleTextView.getText().toString());
145     }
146
147     @Override
148     protected void onRestoreInstanceState(Bundle savedInstanceState) {
149         super.onRestoreInstanceState(savedInstanceState);
150
151         String consoleOutput = savedInstanceState.getString("consoleOutputString");
152         mConsoleTextView.setText(consoleOutput);
153     }
154
155     private void msg(final String text) {
156         runOnUiThread(new Runnable() {
157             public void run() {
158                 mConsoleTextView.append("\n");
159                 mConsoleTextView.append(text);
160                 mScrollView.fullScroll(View.FOCUS_DOWN);
161             }
162         });
163         Log.i(TAG, text);
164     }
165
166     private void printLine() {
167         msg("------------------------------------------------------------------------");
168     }
169 }