1 package com.example.xub3.speakerlocator;
3 import android.content.Context;
4 import android.content.pm.PackageManager;
5 import android.net.Uri;
6 import android.os.Bundle;
7 import android.os.Environment;
8 import android.support.design.widget.FloatingActionButton;
9 import android.support.design.widget.Snackbar;
10 import android.support.v4.app.ActivityCompat;
11 import android.support.v7.app.AppCompatActivity;
12 import android.support.v7.widget.Toolbar;
13 import android.util.Log;
14 import android.view.View;
15 import android.widget.EditText;
16 import android.view.Menu;
17 import android.view.MenuItem;
18 import android.widget.Button;
19 import android.widget.TextView;
21 import com.google.android.gms.appindexing.Action;
22 import com.google.android.gms.appindexing.AppIndex;
23 import com.google.android.gms.common.api.GoogleApiClient;
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.List;
30 import de.hadizadeh.positioning.controller.PositionListener;
31 import de.hadizadeh.positioning.controller.PositionManager;
32 import de.hadizadeh.positioning.controller.Technology;
33 import de.hadizadeh.positioning.exceptions.PositioningException;
34 import de.hadizadeh.positioning.exceptions.PositioningPersistenceException;
35 import de.hadizadeh.positioning.model.PositionInformation;
38 public class MainActivity extends AppCompatActivity implements PositionListener {
40 static Helper helper = new Helper();
42 private Button button;
43 private PositionManager positionManager;
44 private String curr_Loc = "";
45 private static String roomIDbuffer = "0";
46 protected static Context context;
47 // Storage Permissions
48 private static final int REQUEST_EXTERNAL_STORAGE = 1;
49 private static String[] PERMISSIONS_STORAGE = {
50 android.Manifest.permission.READ_EXTERNAL_STORAGE,
51 android.Manifest.permission.WRITE_EXTERNAL_STORAGE
55 * ATTENTION: This was auto-generated to implement the App Indexing API.
56 * See https://g.co/AppIndexing/AndroidStudio for more information.
58 private GoogleApiClient client;
61 protected void onCreate(Bundle savedInstanceState) {
62 super.onCreate(savedInstanceState);
63 context = getApplicationContext();
64 setContentView(R.layout.activity_main);
65 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
66 setSupportActionBar(toolbar);
67 //System.out.println("Status: " + Environment.getExternalStorageState());
68 initializePositioning();
69 FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
70 tv = (TextView) findViewById(R.id.hellotext);
71 if (helper.httpclient == null) {
72 helper.setConnection(getApplicationContext().getResources().getString(R.string.gateway_ip));
74 fab.setOnClickListener(new View.OnClickListener() {
76 public void onClick(View view) {
77 Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
78 .setAction("Action", null).show();
83 // ATTENTION: This was auto-generated to implement the App Indexing API.
84 // See https://g.co/AppIndexing/AndroidStudio for more information.
85 client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
89 public boolean onCreateOptionsMenu(Menu menu) {
90 // Inflate the menu; this adds items to the action bar if it is present.
91 getMenuInflater().inflate(R.menu.menu_main, menu);
96 public boolean onOptionsItemSelected(MenuItem item) {
97 // Handle action bar item clicks here. The action bar will
98 // automatically handle clicks on the Home/Up button, so long
99 // as you specify a parent activity in AndroidManifest.xml.
100 int id = item.getItemId();
102 //noinspection SimplifiableIfStatement
103 if (id == R.id.action_settings) {
107 return super.onOptionsItemSelected(item);
110 private void initializePositioning() {
112 int permission = ActivityCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
113 if (permission != PackageManager.PERMISSION_GRANTED) {
114 // We don't have permission so prompt the user
115 ActivityCompat.requestPermissions(
118 REQUEST_EXTERNAL_STORAGE
121 File file = new File(Environment.getExternalStorageDirectory(), "positioningPersistence.xml");
123 positionManager = new PositionManager(file);
124 Log.d("positionManager", "initialized");
125 System.out.println("PositionManager: Initialized successfully!");
126 } catch (PositioningPersistenceException e) {
130 List<String> keyWhiteList = new ArrayList<String>();
131 keyWhiteList.add(getApplicationContext().getResources().getString(R.string.mac_1).toLowerCase());
132 keyWhiteList.add(getApplicationContext().getResources().getString(R.string.mac_2).toLowerCase());
133 keyWhiteList.add(getApplicationContext().getResources().getString(R.string.mac_3).toLowerCase());
134 keyWhiteList.add(getApplicationContext().getResources().getString(R.string.mac_4).toLowerCase());
135 keyWhiteList.add(getApplicationContext().getResources().getString(R.string.mac_5).toLowerCase());
136 keyWhiteList.add(getApplicationContext().getResources().getString(R.string.mac_6).toLowerCase());
138 Technology wifiTechnology = new WifiTechnology(this, "WIFI", keyWhiteList);
139 CompassTechnology compassTechnology = new CompassTechnology(this, "compass", 80
142 //positionManager.addTechnology(compassTechnology);
143 positionManager.addTechnology(wifiTechnology);
144 } catch (PositioningException e) {
147 positionManager.registerPositionListener(this);
148 final EditText mapName = (EditText) findViewById(R.id.mapname_et);
149 Button mapBtn = (Button) findViewById(R.id.map_btn);
150 Button startBtn = (Button) findViewById(R.id.start_btn);
151 Button stopBtn = (Button) findViewById(R.id.stop_btn);
153 mapBtn.setOnClickListener(new View.OnClickListener() {
155 public void onClick(View v) {
156 positionManager.map(mapName.getText().toString());
159 startBtn.setOnClickListener(new View.OnClickListener() {
161 public void onClick(View v) {
162 positionManager.startPositioning(2000);
165 stopBtn.setOnClickListener(new View.OnClickListener() {
167 public void onClick(View v) {
168 positionManager.stopPositioning();
174 public void positionReceived(final PositionInformation positionInformation) {
176 System.out.println("This is called! Single!");
177 String positioningText = positionInformation.getName();
178 if (!positioningText.equals(curr_Loc)) {
179 final String room_id;
180 // we need to handle a situation where
181 // positionInformation.getName() returns ""
182 if (positionInformation.getName().equals("")) {
183 room_id = roomIDbuffer;
185 room_id = positionInformation.getName();
186 roomIDbuffer = room_id;
190 String driver_IP = getApplicationContext().getResources().getString(R.string.gateway_ip);
191 helper.makeRequest(driver_IP, Integer.parseInt(room_id), "setRoomID");
193 System.out.println("room changed to " + room_id);
196 curr_Loc = positioningText;
204 public void positionReceived(final List<PositionInformation> positionInformation) {
206 System.out.println("This is called! List!");
207 String positioningText = "";
209 for (int i = 0; i < positionInformation.size(); i++) {
210 if (!positionInformation.get(i).getName().equals(curr_Loc)) {
212 if (count > positionInformation.size() / 2) {
213 final String room_id;
214 // we need to handle a situation where
215 // positionInformation.getName() returns ""
216 if (positionInformation.get(i).getName().equals("")) {
217 room_id = roomIDbuffer;
219 room_id = positionInformation.get(i).getName();
220 roomIDbuffer = room_id;
224 String driver_IP = getApplicationContext().getResources().getString(R.string.gateway_ip);
225 helper.makeRequest(driver_IP, Integer.parseInt(room_id), "setRoomID");
227 System.out.println("room changed to " + room_id);
230 positioningText = curr_Loc;
240 public void onStart() {
243 // ATTENTION: This was auto-generated to implement the App Indexing API.
244 // See https://g.co/AppIndexing/AndroidStudio for more information.
246 Action viewAction = Action.newAction(
247 Action.TYPE_VIEW, // TODO: choose an action type.
248 "Main Page", // TODO: Define a title for the content shown.
249 // TODO: If you have web page content that matches this app activity's content,
250 // make sure this auto-generated web page URL is correct.
251 // Otherwise, set the URL to null.
252 Uri.parse("http://host/path"),
253 // TODO: Make sure this auto-generated app URL is correct.
254 Uri.parse("android-app://com.example.xub3.speakerlocator/http/host/path")
256 AppIndex.AppIndexApi.start(client, viewAction);
260 public void onStop() {
263 // ATTENTION: This was auto-generated to implement the App Indexing API.
264 // See https://g.co/AppIndexing/AndroidStudio for more information.
265 Action viewAction = Action.newAction(
266 Action.TYPE_VIEW, // TODO: choose an action type.
267 "Main Page", // TODO: Define a title for the content shown.
268 // TODO: If you have web page content that matches this app activity's content,
269 // make sure this auto-generated web page URL is correct.
270 // Otherwise, set the URL to null.
271 Uri.parse("http://host/path"),
272 // TODO: Make sure this auto-generated app URL is correct.
273 Uri.parse("android-app://com.example.xub3.speakerlocator/http/host/path")
275 AppIndex.AppIndexApi.end(client, viewAction);