Android Open Source - mitlocate A P Editor Activity






From Project

Back to project page mitlocate.

License

The source code is released under:

MIT License

If you think the Android project mitlocate listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package edu.mit.locate;
/*from ww  w .  j  a va 2  s  . co  m*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import edu.mit.locate.R;
import edu.mit.locate.apeditor.APListAdapter;
import edu.mit.locate.apeditor.APScanResult;

public class APEditorActivity extends FragmentActivity implements OnClickListener {

  private WifiManager wifi;
  private SharedPreferences prefs;
  private TextView mScanButton;
  private final String TAG = "APEditorActivity";
  private Context ctx;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    overridePendingTransition(R.anim.fadein, R.anim.fadeout);
    setContentView(R.layout.activity_apeditor);

    wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
    prefs = getSharedPreferences("MITLOCATE", 0);

    mScanButton = (TextView) findViewById(R.id.scanButton);

    ctx = this;

    mScanButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        wifi.startScan();
        mScanButton.setText("Scanning...");
      }
    });
  }

  @Override
  protected void onResume() {
    super.onResume();
    registerReceiver(WiFiScanBR, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
  }

  @Override
  protected void onPause() {
    super.onPause();
    unregisterReceiver(WiFiScanBR);
  }

  private final BroadcastReceiver WiFiScanBR = new BroadcastReceiver() {
    @Override
    public void onReceive(Context c, Intent intent) {
      List<ScanResult> results = wifi.getScanResults();
      JSONObject params = new JSONObject();
      JSONArray aps = new JSONArray();
      try {
        params.put("UUID", prefs.getString("DeviceID",null));
        for(int i = 0;i < results.size();i++){
          final ScanResult result = results.get(i);
          JSONObject ap = new JSONObject();
          ap.put("bssid", result.BSSID.replace(":", ""));
          ap.put("signal", Integer.toString(-result.level));
          ap.put("ssid", result.SSID);
          aps.put(ap);
        }
        params.put("aps", aps);
        if(aps.length() > 0){
          new MITHttpsClient("fetch-aps", APEditorActivity.this, true){
            @Override
            protected void onPostExecute(JSONObject response){
              mScanButton.setText("Scan");
              if(response!=null){
                final ListView lv = (ListView) findViewById(R.id.NearbyAPsList);
                if(response.has("aps")){
                  lv.setAdapter(new APListAdapter(APEditorActivity.this, ProcessAPScanResults(response)));
  
                  lv.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                      final APScanResult fullObject = (APScanResult) lv.getItemAtPosition(position);
                      final View editAPView = LayoutInflater.from(ctx).inflate(R.layout.dialog_edit_ap, null);
                      AlertDialog.Builder adb = new AlertDialog.Builder(ctx).setView(editAPView);
  
                      EditText comment = (EditText) editAPView.findViewById(R.id.comment);
                      EditText building = (EditText) editAPView.findViewById(R.id.building);
                      EditText floor = (EditText) editAPView.findViewById(R.id.floor);
  
                      comment.setText(fullObject.getComment());
                      building.setText(fullObject.getBuilding());
                      floor.setText(fullObject.getFloor());
  
                      adb.setIcon(android.R.drawable.ic_menu_edit).setTitle("Edit Access Point").setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                          // User cancelled
                          // the dialog
                        }
  
                      }).setPositiveButton("Save", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                          JSONObject params = new JSONObject();
                          try {
                            params.put("UUID", prefs.getString("DeviceID", null));
                            final String comment = ((EditText) ((Dialog) dialog).findViewById(R.id.comment)).getText().toString();
                            final String building = ((EditText) ((Dialog) dialog).findViewById(R.id.building)).getText().toString();
                            final String floor = ((EditText) ((Dialog) dialog).findViewById(R.id.floor)).getText().toString();
                            params.put("bssid", fullObject.getBSSID().replaceAll(":", ""));
                            params.put("comment", comment);
                            params.put("building", building);
                            params.put("floor", floor);
                          } catch (JSONException e) {
                            e.printStackTrace();
                          }
  
                          new MITHttpsClient("update-ap", APEditorActivity.this, true){
                            @Override
                            protected void onPostExecute(JSONObject response){
                              mScanButton.setText("Scan");
                              if(response!=null){
                                if(response.optBoolean("success")){
                                  lv.setAdapter(new APListAdapter(APEditorActivity.this, new ArrayList<APScanResult>()));
                                  wifi.startScan();
                                  Toast.makeText(getApplicationContext(), "AP data saved.", Toast.LENGTH_SHORT).show();
                                  Log.d(TAG, response.toString());
                                } else {
                                  Toast.makeText(getApplicationContext(), "Saving AP data failed!", Toast.LENGTH_SHORT).show();
                                  Log.e(TAG, response.toString());
                                }
                              } else {
                                Toast.makeText(getApplicationContext(), "Server error.", Toast.LENGTH_SHORT).show();
                              }
                            }
                          }.execute(params);
                        }
                      }).create().show();
                    }
  
                  });
                }
                //Log.d(TAG, response.toString());
              } else {
                Toast.makeText(APEditorActivity.this, "Network error.", Toast.LENGTH_SHORT).show();
              }
            }
          }.execute(params);
        } else {
          Toast.makeText(APEditorActivity.this, "No WiFi APs in range.", Toast.LENGTH_SHORT).show();
        }
      } catch (JSONException je){
        Log.e(TAG, "JSONException in WiFiScanBR");
      }
    }
  };

  protected ArrayList<APScanResult> ProcessAPScanResults(JSONObject r) {
    JSONArray aps = r.optJSONArray("aps");
    ArrayList<APScanResult> results = new ArrayList<APScanResult>();

    final int len = aps.length();
    for (int i = 0; i < len; i++) {
      final JSONObject ap = aps.optJSONObject(i);
      APScanResult sr = new APScanResult();
      Log.w(TAG, ap.toString());
      sr.setBSSID(ap.optString("bssid"));
      sr.setType(ap.optString("type"));
      sr.setComment(ap.optString("comment"));
      sr.setBuilding(ap.optString("building"));
      sr.setFloor(ap.optString("floor"));
      sr.setSignal(ap.optString("signal"));
      results.add(sr);
    }
    return results;
  }

  @Override
  public void onClick(View arg0) {}
}




Java Source Code List

edu.mit.locate.APEditorActivity.java
edu.mit.locate.BootLocationService.java
edu.mit.locate.LoginActivity.java
edu.mit.locate.MITHttpsClient.java
edu.mit.locate.MITLocationService.java
edu.mit.locate.MainActivity.java
edu.mit.locate.alerts.Alert.java
edu.mit.locate.alerts.AlertsListAdapter.java
edu.mit.locate.alerts.AlertsTab.java
edu.mit.locate.apeditor.APListAdapter.java
edu.mit.locate.apeditor.APScanResult.java
edu.mit.locate.friends.FriendActivity.java
edu.mit.locate.friends.Friend.java
edu.mit.locate.friends.FriendsListAdapter.java
edu.mit.locate.friends.FriendsTab.java
edu.mit.locate.tabs.IconPagerAdapter.java
edu.mit.locate.tabs.IcsLinearLayout.java
edu.mit.locate.tabs.NavigationTab.java
edu.mit.locate.tabs.PageIndicator.java
edu.mit.locate.tabs.SearchTab.java
edu.mit.locate.tabs.SettingsTab.java
edu.mit.locate.tabs.TabPageIndicator.java
edu.mit.locate.tabs.TabPagerAdapter.java