Advertisement
Guest User

Untitled

a guest
May 28th, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.89 KB | None | 0 0
  1. package pl.wroc.pwr.indoorlocalizationtieto.localization;
  2.  
  3. import android.app.Activity;
  4. import android.app.AlertDialog;
  5. import android.content.BroadcastReceiver;
  6. import android.content.Context;
  7. import android.content.DialogInterface;
  8. import android.content.Intent;
  9. import android.net.wifi.ScanResult;
  10. import android.net.wifi.WifiManager;
  11. import android.widget.TextView;
  12. import java.util.List;
  13.  
  14. import pl.wroc.pwr.indoorlocalizationtieto.R;
  15.  
  16. /**
  17. * Created by Mateusz on 2015-05-09.
  18. * Scan receiver for capturing data about access points and signal level
  19. */
  20. public class WifiScanReceiver extends BroadcastReceiver {
  21. private WifiManager wifiManager;
  22. private LocalizationData localizationData;
  23. private Context context;
  24. private Localization localization;
  25.  
  26. private List<ScanResult> scanResults;
  27.  
  28. private boolean changingLevel;
  29. private boolean alertBuilt;
  30.  
  31. /*
  32. testowe text fieldy
  33. */
  34. // private TextView textViewLat;
  35. // private TextView textViewLon;
  36. // private TextView textViewCounter;
  37. // private TextView textViewLevelChanging;
  38. // private TextView textViewTicks;
  39.  
  40. private boolean tick; //helper line
  41.  
  42. /**
  43. * Constructor for WifiScanReceiver
  44. * @param wifiManager - WifiManager object
  45. * @param context - application context
  46. * @param localization - instance of Localization class
  47. */
  48. protected WifiScanReceiver(WifiManager wifiManager, Context context, Localization localization) {
  49. this.wifiManager = wifiManager;
  50. this.context = context;
  51. this.localization = localization;
  52.  
  53. LocalizationFetcher localizationFetcher = new LocalizationFetcher(this.context);
  54. this.localizationData = localizationFetcher.fetchData();
  55.  
  56. this.alertBuilt = false;
  57. this.changingLevel = false;
  58. // this.tick = false;
  59.  
  60. // this.textViewLat = (TextView) ((Activity) context).findViewById(R.id.textView);
  61. // this.textViewLon = (TextView) ((Activity) context).findViewById(R.id.textView2);
  62. // this.textViewCounter = (TextView) ((Activity) context).findViewById(R.id.textView3);
  63. // this.textViewLevelChanging = (TextView) ((Activity) context).findViewById(R.id.textView4);
  64. // this.textViewTicks = (TextView) ((Activity) context).findViewById(R.id.textView5);
  65. }
  66.  
  67. @Override
  68. public void onReceive(Context context, Intent intent) {
  69. if (wifiManager.isWifiEnabled()) {
  70. wifiManager.startScan();
  71. if (changingLevel && !alertBuilt) {
  72. alertBuilt = true;
  73. changingLevel = false;
  74.  
  75. //run alert dialog asking if changing floor
  76. AlertDialog.Builder builderYesNo = new AlertDialog.Builder(context);
  77.  
  78. builderYesNo.setTitle("Change floor");
  79. builderYesNo.setMessage("Do you want to change floor? Current = " + localization.getPosLevel());
  80.  
  81. builderYesNo.setPositiveButton("Yes, +1", new DialogInterface.OnClickListener() {
  82.  
  83. public void onClick(DialogInterface dialog, int which) {
  84. changeLevel(localization.getPosLevel() + 1);
  85. //changingLevel = false;
  86. dialog.dismiss();
  87. runThreadSleep();
  88. }
  89.  
  90. });
  91.  
  92. builderYesNo.setNeutralButton("NO", new DialogInterface.OnClickListener() {
  93. public void onClick(DialogInterface dialog, int which) {
  94. //changingLevel = false;
  95. dialog.dismiss();
  96. runThreadSleep();
  97. }
  98. });
  99.  
  100. builderYesNo.setNegativeButton("Yes, -1", new DialogInterface.OnClickListener() {
  101.  
  102. @Override
  103. public void onClick(DialogInterface dialog, int which) {
  104. changeLevel(localization.getPosLevel() - 1);
  105. //changingLevel = false;
  106. dialog.dismiss();
  107. runThreadSleep();
  108. }
  109. });
  110.  
  111. AlertDialog alert = builderYesNo.create();
  112. alert.show();
  113. } else {
  114. scanResults = wifiManager.getScanResults();
  115. Thread thread = new Thread(new LocalizeByRefPointsRunnable(this, localizationData, scanResults));
  116. thread.start();
  117. wifiManager.startScan();
  118. tick = !tick;
  119. }
  120. // textViewLat.setText("Lat: " + localization.getPosLat());
  121. // textViewLon.setText("Lon: " + localization.getPosLon());
  122. // textViewCounter.setText("level: " + localization.getPosLevel());
  123. // textViewLevelChanging.setText("Changing: " + isChangingLevel());
  124. // textViewTicks.setText("Tick: " + !tick);
  125. // wifiManager.startScan();
  126. }
  127. }
  128.  
  129. protected List<ScanResult> getScanResults() {
  130. return scanResults;
  131. }
  132.  
  133. synchronized protected void changeLevel(int level) {
  134. setChangingLevel(false);
  135. localization.setPosLevel(level);
  136. }
  137.  
  138. protected Localization getLocalization() {
  139. return localization;
  140. }
  141.  
  142. protected boolean isChangingLevel() {
  143. return changingLevel;
  144. }
  145.  
  146. protected synchronized void setChangingLevel(boolean changingLevel) {
  147. this.changingLevel = changingLevel;
  148. }
  149.  
  150. protected Context getContext() {
  151. return context;
  152. }
  153.  
  154. private void runThreadSleep() {
  155. new Thread(new Runnable() {
  156. public void run() {
  157. try {
  158. Thread.sleep(10000, 0);
  159. alertBuilt = false;
  160. } catch (Exception e) {
  161. e.printStackTrace();
  162. }
  163. }
  164. }).start();
  165. }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement