Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / things-manager / sampleapp / android / Sample / src / com / tm / sample / MainActivity.java
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20 package com.tm.sample;
21
22 import java.util.ArrayList;
23
24 import org.iotivity.base.ModeType;
25 import org.iotivity.base.OcPlatform;
26 import org.iotivity.base.PlatformConfig;
27 import org.iotivity.base.QualityOfService;
28 import org.iotivity.base.ServiceType;
29 import org.iotivity.service.tm.ThingsManager;
30
31 import android.app.Activity;
32 import android.app.AlertDialog;
33 import android.content.DialogInterface;
34 import android.content.DialogInterface.OnClickListener;
35 import android.content.Intent;
36 import android.net.ConnectivityManager;
37 import android.net.NetworkInfo;
38 import android.os.Bundle;
39 import android.util.Log;
40 import android.view.View;
41 import android.widget.AdapterView;
42 import android.widget.AdapterView.OnItemClickListener;
43 import android.widget.ArrayAdapter;
44 import android.widget.ListView;
45
46 /**
47  * Starting Activity of the application responsible for configuring the
48  * OcPlatform and redirecting to other activity according to user's selection on
49  * UI.
50  */
51 public class MainActivity extends Activity {
52
53     private static MainActivity  activityObj;
54     private ArrayAdapter<String> apis;
55     private ArrayList<String>    apisList;
56     private ListView             list;
57     private final String         LOG_TAG          = "[TMSample] " + this.getClass()
58                                                                            .getSimpleName();
59     public ThingsManager         thingsManagerObj = new ThingsManager();
60
61     @Override
62     protected void onCreate(Bundle savedInstanceState) {
63         super.onCreate(savedInstanceState);
64         setContentView(R.layout.activity_main);
65         activityObj = this;
66
67         list = (ListView) findViewById(R.id.list);
68         apisList = new ArrayList<String>();
69
70         // adding the item to list that will be displayed on the UI.
71         apisList.add("GROUP APIS");
72         apisList.add("CONFIGURATION APIS");
73         apis = new ArrayAdapter<String>(activityObj,
74                 android.R.layout.simple_list_item_1, apisList);
75         list.setAdapter(apis);
76
77         // handling user's selection on the UI
78         list.setOnItemClickListener(new OnItemClickListener() {
79             @Override
80             public void onItemClick(AdapterView<?> parent, View view,
81                     int position, long id) {
82
83                 if (position == 0) {
84                     Intent intent = new Intent(activityObj,
85                             GroupApiActivity.class);
86                     startActivity(intent);
87                 } else if (position == 1) {
88                     Intent intent = new Intent(activityObj,
89                             ConfigurationApiActivity.class);
90                     startActivity(intent);
91                 }
92             }
93         });
94
95         // calling the method to check the Wi-fi connectivity and configuring
96         // the OcPlatform
97         configurePlatform();
98     }
99
100     @Override
101     public void onBackPressed() {
102         apisList.clear();
103         super.onBackPressed();
104     }
105
106     private void configurePlatform() {
107         // local Variables
108         ConnectivityManager connManager;
109         NetworkInfo wifi;
110         AlertDialog dialog;
111         PlatformConfig platformConfigObj;
112
113         // Check the wifi connectivity
114         connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
115         wifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
116         if (false == wifi.isConnected()) {
117             // WiFi is not connected close the application
118             AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
119             dialogBuilder.setTitle("Error");
120             dialogBuilder
121                     .setMessage("WiFi is not enabled/connected! Please connect the WiFi and start application again...");
122             dialogBuilder.setCancelable(false);
123             dialogBuilder.setPositiveButton("OK", new OnClickListener() {
124                 @Override
125                 public void onClick(DialogInterface dialog, int which) {
126                     // Closing the application
127                     activityObj.finish();
128                 }
129             });
130
131             dialog = dialogBuilder.create();
132             dialog.show();
133             Log.i(LOG_TAG, "WiFi is not enabled/connected! Please connect the WiFi and start application again...");
134             return;
135         }
136         // If wifi is connected calling the configure method for configuring the
137         // OcPlatform
138         platformConfigObj = new PlatformConfig(getApplicationContext(),ServiceType.IN_PROC,
139                 ModeType.CLIENT_SERVER, "0.0.0.0", 0, QualityOfService.LOW);
140
141         Log.i(LOG_TAG, "Before Calling Configure of ocPlatform");
142         OcPlatform.Configure(platformConfigObj);
143         Log.i(LOG_TAG, "Configuration done Successfully");
144     }
145 }