Adding config file for sharing.
[iot2.git] / benchmarks / other / PhoneInterface / SpeakerLocator / app / src / main / java / com / example / xub3 / speakerlocator / MainActivity.java
1 package com.example.xub3.speakerlocator;
2
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;
20
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;
24
25 import java.io.File;
26 import java.util.ArrayList;
27 import java.util.List;
28
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;
35
36
37 public class MainActivity extends AppCompatActivity implements PositionListener {
38
39         static Helper helper = new Helper();
40         private TextView tv;
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
51         };
52
53         /**
54          * ATTENTION: This was auto-generated to implement the App Indexing API.
55          * See https://g.co/AppIndexing/AndroidStudio for more information.
56          */
57         private GoogleApiClient client;
58
59         @Override
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();
72                 }
73                 fab.setOnClickListener(new View.OnClickListener() {
74                         @Override
75                         public void onClick(View view) {
76                                 Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
77                                                 .setAction("Action", null).show();
78                         }
79                 });
80
81
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();
85         }
86
87         @Override
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);
91                 return true;
92         }
93
94         @Override
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();
100
101                 //noinspection SimplifiableIfStatement
102                 if (id == R.id.action_settings) {
103                         return true;
104                 }
105
106                 return super.onOptionsItemSelected(item);
107         }
108
109         private void initializePositioning() {
110
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(
115                                         this,
116                                         PERMISSIONS_STORAGE,
117                                         REQUEST_EXTERNAL_STORAGE
118                         );
119                 }
120                 File file = new File(Environment.getExternalStorageDirectory(), "positioningPersistence.xml");
121                 try {
122                         positionManager = new PositionManager(file);
123                         Log.d("positionManager", "initialized");
124                         //System.out.println("PositionManager: Initialized successfully!");
125                 } catch (PositioningPersistenceException e) {
126                         e.printStackTrace();
127                 }
128
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());
136
137                 Technology wifiTechnology = new WifiTechnology(this, "WIFI", keyWhiteList);
138                 CompassTechnology compassTechnology = new CompassTechnology(this, "compass", 80
139                 );
140                 try {
141                         //positionManager.addTechnology(compassTechnology);
142                         positionManager.addTechnology(wifiTechnology);
143                 } catch (PositioningException e) {
144                         e.printStackTrace();
145                 }
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);
151
152                 mapBtn.setOnClickListener(new View.OnClickListener() {
153                         @Override
154                         public void onClick(View v) {
155                                 positionManager.map(mapName.getText().toString());
156                         }
157                 });
158                 startBtn.setOnClickListener(new View.OnClickListener() {
159                         @Override
160                         public void onClick(View v) {
161                                 positionManager.startPositioning(2000);
162                         }
163                 });
164                 stopBtn.setOnClickListener(new View.OnClickListener() {
165                         @Override
166                         public void onClick(View v) {
167                                 positionManager.stopPositioning();
168                         }
169                 });
170         }
171
172         @Override
173         public void positionReceived(final PositionInformation positionInformation) {
174
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;
183                         } else {
184                                 room_id = positionInformation.getName();
185                                 roomIDbuffer = room_id;
186                         }
187                         new Thread() {
188                                 public void run() {
189                                         String driver_IP = getApplicationContext().getResources().getString(R.string.gateway_ip);
190                                         helper.makeRequest(driver_IP, Integer.parseInt(room_id), "setRoomID");
191                                         curr_Loc = room_id;
192                                         System.out.println("room changed to " + room_id);
193                                 }
194                         }.start();
195                         curr_Loc = positioningText;
196                 }
197
198
199                 // Do nothing
200         }
201
202         @Override
203         public void positionReceived(final List<PositionInformation> positionInformation) {
204
205                 System.out.println("This is called! List!");
206                 String positioningText = "";
207                 int count = 0;
208                 for (int i = 0; i < positionInformation.size(); i++) {
209                         if (!positionInformation.get(i).getName().equals(curr_Loc)) {
210                                 count += 1;
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;
217                                         } else {
218                                                 room_id = positionInformation.get(i).getName();
219                                                 roomIDbuffer = room_id;
220                                         }
221                                         new Thread() {
222                                                 public void run() {
223                                                         String driver_IP = getApplicationContext().getResources().getString(R.string.gateway_ip);
224                                                         helper.makeRequest(driver_IP, Integer.parseInt(room_id), "setRoomID");
225                                                         curr_Loc = room_id;
226                                                         System.out.println("room changed to " + room_id);
227                                                 }
228                                         }.start();
229                                         positioningText = curr_Loc;
230                                         break;
231                                 }
232                         }
233                 }
234
235
236         }
237
238         @Override
239         public void onStart() {
240                 super.onStart();
241
242                 // ATTENTION: This was auto-generated to implement the App Indexing API.
243                 // See https://g.co/AppIndexing/AndroidStudio for more information.
244                 client.connect();
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")
254                 );
255                 AppIndex.AppIndexApi.start(client, viewAction);
256         }
257
258         @Override
259         public void onStop() {
260                 super.onStop();
261
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")
273                 );
274                 AppIndex.AppIndexApi.end(client, viewAction);
275                 client.disconnect();
276         }
277 }
278