1 /******************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 ******************************************************************/
20 package com.tm.sample;
22 import java.util.ArrayList;
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;
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;
47 * Starting Activity of the application responsible for configuring the
48 * OcPlatform and redirecting to other activity according to user's selection on
51 public class MainActivity extends Activity {
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()
59 public ThingsManager thingsManagerObj = new ThingsManager();
62 protected void onCreate(Bundle savedInstanceState) {
63 super.onCreate(savedInstanceState);
64 setContentView(R.layout.activity_main);
67 list = (ListView) findViewById(R.id.list);
68 apisList = new ArrayList<String>();
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);
77 // handling user's selection on the UI
78 list.setOnItemClickListener(new OnItemClickListener() {
80 public void onItemClick(AdapterView<?> parent, View view,
81 int position, long id) {
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);
95 // calling the method to check the Wi-fi connectivity and configuring
101 public void onBackPressed() {
103 super.onBackPressed();
106 private void configurePlatform() {
108 ConnectivityManager connManager;
111 PlatformConfig platformConfigObj;
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");
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() {
125 public void onClick(DialogInterface dialog, int which) {
126 // Closing the application
127 activityObj.finish();
131 dialog = dialogBuilder.create();
133 Log.i(LOG_TAG, "WiFi is not enabled/connected! Please connect the WiFi and start application again...");
136 // If wifi is connected calling the configure method for configuring the
138 platformConfigObj = new PlatformConfig(getApplicationContext(),ServiceType.IN_PROC,
139 ModeType.CLIENT_SERVER, "0.0.0.0", 0, QualityOfService.LOW);
141 Log.i(LOG_TAG, "Before Calling Configure of ocPlatform");
142 OcPlatform.Configure(platformConfigObj);
143 Log.i(LOG_TAG, "Configuration done Successfully");