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.util.ArrayList;
27 import java.util.List;
29 import de.hadizadeh.positioning.controller.PositionListener;
30 import de.hadizadeh.positioning.controller.PositionManager;
31 import de.hadizadeh.positioning.controller.Technology;
32 import de.hadizadeh.positioning.exceptions.PositioningException;
33 import de.hadizadeh.positioning.exceptions.PositioningPersistenceException;
34 import de.hadizadeh.positioning.model.PositionInformation;
37 public class MainActivity extends AppCompatActivity implements PositionListener {
39 static Helper helper = new Helper();
41 private Button button;
42 private PositionManager positionManager;
43 private String curr_Loc = "";
44 private static String roomIDbuffer = "0";
45 protected static Context context;
46 // Storage Permissions
47 private static final int REQUEST_EXTERNAL_STORAGE = 1;
48 private static String[] PERMISSIONS_STORAGE = {
49 android.Manifest.permission.READ_EXTERNAL_STORAGE,
50 android.Manifest.permission.WRITE_EXTERNAL_STORAGE
54 * ATTENTION: This was auto-generated to implement the App Indexing API.
55 * See https://g.co/AppIndexing/AndroidStudio for more information.
57 private GoogleApiClient client;
60 protected void onCreate(Bundle savedInstanceState) {
61 super.onCreate(savedInstanceState);
62 context = getApplicationContext();
63 setContentView(R.layout.activity_main);
64 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
65 setSupportActionBar(toolbar);
66 //System.out.println("Status: " + Environment.getExternalStorageState());
67 initializePositioning();
68 FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
69 tv = (TextView) findViewById(R.id.hellotext);
70 // if (helper.httpclient == null) {
71 // helper.setConnection(getApplicationContext().getResources().getString(R.string.gateway_ip));
73 fab.setOnClickListener(new View.OnClickListener() {
75 public void onClick(View view) {
76 Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
77 .setAction("Action", null).show();
82 // ATTENTION: This was auto-generated to implement the App Indexing API.
83 // See https://g.co/AppIndexing/AndroidStudio for more information.
84 client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
88 public boolean onCreateOptionsMenu(Menu menu) {
89 // Inflate the menu; this adds items to the action bar if it is present.
90 getMenuInflater().inflate(R.menu.menu_main, menu);
95 public boolean onOptionsItemSelected(MenuItem item) {
96 // Handle action bar item clicks here. The action bar will
97 // automatically handle clicks on the Home/Up button, so long
98 // as you specify a parent activity in AndroidManifest.xml.
99 int id = item.getItemId();
101 //noinspection SimplifiableIfStatement
102 if (id == R.id.action_settings) {
106 return super.onOptionsItemSelected(item);
109 private void initializePositioning() {
111 int permission = ActivityCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
112 if (permission != PackageManager.PERMISSION_GRANTED) {
113 // We don't have permission so prompt the user
114 ActivityCompat.requestPermissions(
117 REQUEST_EXTERNAL_STORAGE
120 File file = new File(Environment.getExternalStorageDirectory(), "positioningPersistence.xml");
122 positionManager = new PositionManager(file);
123 //Log.d("positionManager", "initialized");
124 //System.out.println("PositionManager: Initialized successfully!");
125 } catch (PositioningPersistenceException e) {
129 List<String> keyWhiteList = new ArrayList<String>();
130 keyWhiteList.add(getApplicationContext().getResources().getString(R.string.mac_1).toLowerCase());
131 keyWhiteList.add(getApplicationContext().getResources().getString(R.string.mac_2).toLowerCase());
132 keyWhiteList.add(getApplicationContext().getResources().getString(R.string.mac_3).toLowerCase());
133 keyWhiteList.add(getApplicationContext().getResources().getString(R.string.mac_4).toLowerCase());
134 keyWhiteList.add(getApplicationContext().getResources().getString(R.string.mac_5).toLowerCase());
135 keyWhiteList.add(getApplicationContext().getResources().getString(R.string.mac_6).toLowerCase());
137 Technology wifiTechnology = new WifiTechnology(this, "WIFI", keyWhiteList);
138 CompassTechnology compassTechnology = new CompassTechnology(this, "compass", 80
141 //positionManager.addTechnology(compassTechnology);
142 positionManager.addTechnology(wifiTechnology);
143 } catch (PositioningException e) {
146 positionManager.registerPositionListener(this);
147 final EditText mapName = (EditText) findViewById(R.id.mapname_et);
148 Button mapBtn = (Button) findViewById(R.id.map_btn);
149 Button startBtn = (Button) findViewById(R.id.start_btn);
150 Button stopBtn = (Button) findViewById(R.id.stop_btn);
152 mapBtn.setOnClickListener(new View.OnClickListener() {
154 public void onClick(View v) {
155 positionManager.map(mapName.getText().toString());
158 startBtn.setOnClickListener(new View.OnClickListener() {
160 public void onClick(View v) {
161 positionManager.startPositioning(2000);
164 stopBtn.setOnClickListener(new View.OnClickListener() {
166 public void onClick(View v) {
167 positionManager.stopPositioning();
173 public void positionReceived(final PositionInformation positionInformation) {
175 System.out.println("This is called! Single!");
176 String positioningText = positionInformation.getName();
177 if (!positioningText.equals(curr_Loc)) {
178 final String room_id;
179 // we need to handle a situation where
180 // positionInformation.getName() returns ""
181 if (positionInformation.getName().equals("")) {
182 room_id = roomIDbuffer;
184 room_id = positionInformation.getName();
185 roomIDbuffer = room_id;
189 String driver_IP = getApplicationContext().getResources().getString(R.string.gateway_ip);
190 helper.makeRequest(driver_IP, Integer.parseInt(room_id), "setRoomID");
192 System.out.println("room changed to " + room_id);
195 curr_Loc = positioningText;
203 public void positionReceived(final List<PositionInformation> positionInformation) {
205 System.out.println("This is called! List!");
206 String positioningText = "";
208 for (int i = 0; i < positionInformation.size(); i++) {
209 if (!positionInformation.get(i).getName().equals(curr_Loc)) {
211 if (count > positionInformation.size() / 2) {
212 final String room_id;
213 // we need to handle a situation where
214 // positionInformation.getName() returns ""
215 if (positionInformation.get(i).getName().equals("")) {
216 room_id = roomIDbuffer;
218 room_id = positionInformation.get(i).getName();
219 roomIDbuffer = room_id;
223 String driver_IP = getApplicationContext().getResources().getString(R.string.gateway_ip);
224 helper.makeRequest(driver_IP, Integer.parseInt(room_id), "setRoomID");
226 System.out.println("room changed to " + room_id);
229 positioningText = curr_Loc;
239 public void onStart() {
242 // ATTENTION: This was auto-generated to implement the App Indexing API.
243 // See https://g.co/AppIndexing/AndroidStudio for more information.
245 Action viewAction = Action.newAction(
246 Action.TYPE_VIEW, // TODO: choose an action type.
247 "Main Page", // TODO: Define a title for the content shown.
248 // TODO: If you have web page content that matches this app activity's content,
249 // make sure this auto-generated web page URL is correct.
250 // Otherwise, set the URL to null.
251 Uri.parse("http://host/path"),
252 // TODO: Make sure this auto-generated app URL is correct.
253 Uri.parse("android-app://com.example.xub3.speakerlocator/http/host/path")
255 AppIndex.AppIndexApi.start(client, viewAction);
259 public void onStop() {
262 // ATTENTION: This was auto-generated to implement the App Indexing API.
263 // See https://g.co/AppIndexing/AndroidStudio for more information.
264 Action viewAction = Action.newAction(
265 Action.TYPE_VIEW, // TODO: choose an action type.
266 "Main Page", // TODO: Define a title for the content shown.
267 // TODO: If you have web page content that matches this app activity's content,
268 // make sure this auto-generated web page URL is correct.
269 // Otherwise, set the URL to null.
270 Uri.parse("http://host/path"),
271 // TODO: Make sure this auto-generated app URL is correct.
272 Uri.parse("android-app://com.example.xub3.speakerlocator/http/host/path")
274 AppIndex.AppIndexApi.end(client, viewAction);