Imported Upstream version 1.0.0
[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.GroupManager;
30 import org.iotivity.service.tm.GroupManager.*;
31
32 import android.app.Activity;
33 import android.app.AlertDialog;
34 import android.content.DialogInterface;
35 import android.content.DialogInterface.OnClickListener;
36 import android.content.Intent;
37 import android.net.ConnectivityManager;
38 import android.net.NetworkInfo;
39 import android.os.Bundle;
40 import android.util.Log;
41 import android.view.View;
42 import android.widget.AdapterView;
43 import android.widget.AdapterView.OnItemClickListener;
44 import android.widget.ArrayAdapter;
45 import android.widget.ListView;
46
47 /**
48  * Starting Activity of the application responsible for configuring the
49  * OcPlatform and redirecting to other activity according to user's selection on
50  * UI.
51  */
52 public class MainActivity extends Activity {
53
54     private static MainActivity  activityObj;
55     private ArrayAdapter<String> apis;
56     private ArrayList<String>    apisList;
57     private ListView             list;
58     private final String         LOG_TAG         = "[TMSample] "
59                                                          + this.getClass()
60                                                                  .getSimpleName();
61     public GroupManager          groupManagerObj = new GroupManager();
62
63     @Override
64     protected void onCreate(Bundle savedInstanceState) {
65         super.onCreate(savedInstanceState);
66         setContentView(R.layout.activity_main);
67         activityObj = this;
68
69         list = (ListView) findViewById(R.id.list);
70         apisList = new ArrayList<String>();
71
72         // adding the item to list that will be displayed on the UI.
73         apisList.add("GROUP APIS");
74         apisList.add("CONFIGURATION APIS");
75         apis = new ArrayAdapter<String>(activityObj,
76                 android.R.layout.simple_list_item_1, apisList);
77         list.setAdapter(apis);
78
79         // handling user's selection on the UI
80         list.setOnItemClickListener(new OnItemClickListener() {
81             @Override
82             public void onItemClick(AdapterView<?> parent, View view,
83                     int position, long id) {
84
85                 if (position == 0) {
86                     Intent intent = new Intent(activityObj,
87                             GroupApiActivity.class);
88                     startActivity(intent);
89                 } else if (position == 1) {
90                     Intent intent = new Intent(activityObj,
91                             ConfigurationApiActivity.class);
92                     startActivity(intent);
93                 }
94             }
95         });
96
97         // calling the method to check the Wi-fi connectivity and configuring
98         // the OcPlatform
99         configurePlatform();
100     }
101
102     @Override
103     public void onBackPressed() {
104         apisList.clear();
105         super.onBackPressed();
106     }
107
108     private void configurePlatform() {
109         // local Variables
110         ConnectivityManager connManager;
111         NetworkInfo wifi;
112         AlertDialog dialog;
113         PlatformConfig platformConfigObj;
114
115         // Check the wifi connectivity
116         connManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
117         wifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
118         if (false == wifi.isConnected()) {
119             // WiFi is not connected close the application
120             AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
121             dialogBuilder.setTitle("Error");
122             dialogBuilder
123                     .setMessage("WiFi is not enabled/connected! Please connect the WiFi and start application again...");
124             dialogBuilder.setCancelable(false);
125             dialogBuilder.setPositiveButton("OK", new OnClickListener() {
126                 @Override
127                 public void onClick(DialogInterface dialog, int which) {
128                     // Closing the application
129                     activityObj.finish();
130                 }
131             });
132
133             dialog = dialogBuilder.create();
134             dialog.show();
135             Log.i(LOG_TAG,
136                     "WiFi is not enabled/connected! Please connect the WiFi and start application again...");
137             return;
138         }
139         // If wifi is connected calling the configure method for configuring the
140         // OcPlatform
141         platformConfigObj = new PlatformConfig(getApplicationContext(),
142                 ServiceType.IN_PROC, ModeType.CLIENT_SERVER, "0.0.0.0", 0,
143                 QualityOfService.LOW);
144
145         Log.i(LOG_TAG, "Before Calling Configure of ocPlatform");
146         OcPlatform.Configure(platformConfigObj);
147         Log.i(LOG_TAG, "Configuration done Successfully");
148     }
149 }