Imported Upstream version 1.1.0
[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 /**
44  * This sample demonstrates platform and device discovery feature.
45  * The server sets the platform and device related info. which can be later retrieved by a client.
46  */
47 public class DeviceDiscoveryServer extends Activity {
48
49     private void startDeviceDiscoveryServer() {
50         Context context = this;
51
52         PlatformConfig platformConfig = new PlatformConfig(
53                 context,
54                 ServiceType.IN_PROC,
55                 ModeType.SERVER,
56                 "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces
57                 0,         // Uses randomly available port
58                 QualityOfService.LOW
59         );
60
61         msg("Configuring platform.");
62         OcPlatform.Configure(platformConfig);
63
64         OcDeviceInfo deviceInfo = new OcDeviceInfo("myDeviceName");
65         try {
66             msg("Registering device info");
67             OcPlatform.registerDeviceInfo(deviceInfo);
68         } catch (OcException e) {
69             Log.e(TAG, e.toString());
70             msg("Failed to register device info.");
71         }
72
73         OcPlatformInfo platformInfo = new OcPlatformInfo(
74                 "myPlatformId",             //Platform ID
75                 "myManufactName",           //Manufacturer Name
76                 "www.myurl.com",            //Manufacturer URL
77                 "myModelNumber",            //Model Number
78                 "myDateOfManufacture",      //Date of Manufacture
79                 "myPlatformVersion",        //Platform Version
80                 "Manufacturer OS version",  //Operating System Version
81                 "myHardwareVersion",        //Hardware Version
82                 "myFirmwareVersion",        //Firmware Version
83                 "www.mysupporturl.com",     //Support URL
84                 String.valueOf(System.currentTimeMillis()) // System Time
85         );
86         try {
87             msg("Registering platform info");
88             OcPlatform.registerPlatformInfo(platformInfo);
89         } catch (OcException e) {
90             Log.e(TAG, e.toString());
91             msg("Failed to register platform info.");
92         }
93
94         msg("Waiting for the requests...");
95         printLine();
96     }
97
98     //******************************************************************************
99     // End of the OIC specific code
100     //******************************************************************************
101
102     private final static String TAG = DeviceDiscoveryServer.class.getSimpleName();
103     private TextView mConsoleTextView;
104     private ScrollView mScrollView;
105
106     @Override
107     protected void onCreate(Bundle savedInstanceState) {
108         super.onCreate(savedInstanceState);
109         setContentView(R.layout.activity_device_discovery_server);
110
111         mConsoleTextView = (TextView) findViewById(R.id.consoleTextView);
112         mConsoleTextView.setMovementMethod(new ScrollingMovementMethod());
113         mScrollView = (ScrollView) findViewById(R.id.scrollView);
114         mScrollView.fullScroll(View.FOCUS_DOWN);
115         final Button button = (Button) findViewById(R.id.button);
116
117         if (null == savedInstanceState) {
118             button.setOnClickListener(new View.OnClickListener() {
119                 @Override
120                 public void onClick(View v) {
121                     button.setEnabled(false);
122                     new Thread(new Runnable() {
123                         public void run() {
124                             startDeviceDiscoveryServer();
125                         }
126                     }).start();
127                 }
128             });
129         } else {
130             String consoleOutput = savedInstanceState.getString("consoleOutputString");
131             mConsoleTextView.setText(consoleOutput);
132         }
133     }
134
135     @Override
136     protected void onSaveInstanceState(Bundle outState) {
137         super.onSaveInstanceState(outState);
138         outState.putString("consoleOutputString", mConsoleTextView.getText().toString());
139     }
140
141     @Override
142     protected void onRestoreInstanceState(Bundle savedInstanceState) {
143         super.onRestoreInstanceState(savedInstanceState);
144
145         String consoleOutput = savedInstanceState.getString("consoleOutputString");
146         mConsoleTextView.setText(consoleOutput);
147     }
148
149     private void msg(final String text) {
150         runOnUiThread(new Runnable() {
151             public void run() {
152                 mConsoleTextView.append("\n");
153                 mConsoleTextView.append(text);
154                 mScrollView.fullScroll(View.FOCUS_DOWN);
155             }
156         });
157         Log.i(TAG, text);
158     }
159
160     private void printLine() {
161         msg("------------------------------------------------------------------------");
162     }
163 }