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.GroupManager;
30 import org.iotivity.service.tm.GroupManager.*;
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;
48 * Starting Activity of the application responsible for configuring the
49 * OcPlatform and redirecting to other activity according to user's selection on
52 public class MainActivity extends Activity {
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] "
61 public GroupManager groupManagerObj = new GroupManager();
64 protected void onCreate(Bundle savedInstanceState) {
65 super.onCreate(savedInstanceState);
66 setContentView(R.layout.activity_main);
69 list = (ListView) findViewById(R.id.list);
70 apisList = new ArrayList<String>();
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);
79 // handling user's selection on the UI
80 list.setOnItemClickListener(new OnItemClickListener() {
82 public void onItemClick(AdapterView<?> parent, View view,
83 int position, long id) {
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);
97 // calling the method to check the Wi-fi connectivity and configuring
103 public void onBackPressed() {
105 super.onBackPressed();
108 private void configurePlatform() {
110 ConnectivityManager connManager;
113 PlatformConfig platformConfigObj;
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");
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() {
127 public void onClick(DialogInterface dialog, int which) {
128 // Closing the application
129 activityObj.finish();
133 dialog = dialogBuilder.create();
136 "WiFi is not enabled/connected! Please connect the WiFi and start application again...");
139 // If wifi is connected calling the configure method for configuring the
141 platformConfigObj = new PlatformConfig(getApplicationContext(),
142 ServiceType.IN_PROC, ModeType.CLIENT_SERVER, "0.0.0.0", 0,
143 QualityOfService.LOW);
145 Log.i(LOG_TAG, "Before Calling Configure of ocPlatform");
146 OcPlatform.Configure(platformConfigObj);
147 Log.i(LOG_TAG, "Configuration done Successfully");