Skip to content

Commit

Permalink
Adds LocationListActivity
Browse files Browse the repository at this point in the history
Closes #29
  • Loading branch information
Gisson committed May 5, 2017
1 parent 2efdd0f commit 6bf50d0
Show file tree
Hide file tree
Showing 5 changed files with 250 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.util.Log;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
Expand All @@ -13,14 +14,23 @@
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.Toast;

import pt.ulisboa.tecnico.ist.cmu.locmess.adapters.MessageListAdapter;
import java.util.ArrayList;
import java.util.List;

import pt.ulisboa.tecnico.ist.cmu.locmess.adapters.LocationsListAdapter;
import pt.ulisboa.tecnico.ist.cmu.locmess.commands.ListLocationsCommand;
import pt.ulisboa.tecnico.ist.cmu.locmess.dto.LocationDto;
import pt.ulisboa.tecnico.ist.cmu.locmess.exception.LocMessHttpException;

public class LocationsMenuActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {

private MessageListAdapter _messages;
private LocationsListAdapter messagesAdapter;
private static final String TAG = "LocationsMeny";
private ListLocationsCommand command;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -29,16 +39,8 @@ protected void onCreate(Bundle savedInstanceState) {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
Intent i = new Intent(LocationsMenuActivity.this, AddLocationActivity.class);
startActivity(i);
}
});



DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
Expand All @@ -48,6 +50,53 @@ public void onClick(View view) {

NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);


command = new ListLocationsCommand(LocMessManager.getInstance().getToken());

//Run in Manager and catch errors
LocMessManager.getInstance().executeAsync(command, new LocMessManager.CompleteCallback() {
@Override
public void OnComplete(boolean result, String message) {
if(result){
Toast.makeText(LocationsMenuActivity.this, "Success", Toast.LENGTH_SHORT).show();
populateData();
} else {
Toast.makeText(LocationsMenuActivity.this, message, Toast.LENGTH_SHORT).show();
}
}
});
}

private void populateData(){
try {
//Parse received messages
ArrayList<LocationDto> locations = new ArrayList<>();
List<LocationDto> bla = command.getResults();
for (LocationDto m : command.getResults()) {
locations.add(m);
}
messagesAdapter = new LocationsListAdapter(locations, this);
ListView messagesList = (ListView) findViewById(R.id.locations_list);
messagesList.setAdapter(messagesAdapter);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.add_location);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
Intent i = new Intent(LocationsMenuActivity.this, AddLocationActivity.class);
startActivity(i);
}
});
((FloatingActionButton) findViewById(R.id.add_location)).setEnabled(true);
}catch( LocMessHttpException e) {
Toast.makeText(LocationsMenuActivity.this,e.getReason(),Toast.LENGTH_SHORT);
}catch (Exception e){
//Still not receiving the messages? I need to fix this...
Log.e("MESSAGES", "Request success but we do not receive any locations");
e.printStackTrace();
}
}

@Override
Expand Down Expand Up @@ -107,6 +156,8 @@ public boolean onNavigationItemSelected(MenuItem item) {
return true;
}



public void addLocation(View v){
Intent i = new Intent(this, AddLocationActivity.class);
startActivity(i);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package pt.ulisboa.tecnico.ist.cmu.locmess.adapters;

import android.app.Activity;
import android.app.AlertDialog;
import android.location.Location;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import pt.ulisboa.tecnico.ist.cmu.locmess.R;
import pt.ulisboa.tecnico.ist.cmu.locmess.dto.LocationDto;

/**
* Created by jorge on 05/05/17.
*/

public class LocationsListAdapter extends BaseAdapter {

private List<LocationDto> _locations;
private Activity _activity;

public LocationsListAdapter(List<LocationDto> locations,Activity parentActivity){
_locations=locations;
_activity=parentActivity;
}

@Deprecated
public LocationsListAdapter(Activity parentActivity){
_locations=new ArrayList<>();
_activity=parentActivity;
}

@Override
public int getCount() {
return _locations.size();
}

@Override
public LocationDto getItem(int i) {
return _locations.get(i);
}

@Override
public long getItemId(int i) {
return i;
}

@Override
public View getView(final int index, View reusableView, ViewGroup viewGroup) {
final ViewHolder holder;
if (reusableView == null) {
// create a new view
holder = new ViewHolder();
LayoutInflater inflater = _activity.getLayoutInflater();
reusableView = inflater.inflate(android.R.layout.simple_list_item_1, null);
holder.text = (TextView) reusableView.findViewById(android.R.id.text1);
reusableView.setTag(holder);
} else {
// an old view can be reused!
holder = (ViewHolder) reusableView.getTag();
}
holder.index = index;

holder.text.setText(_locations.get(index).getName());
reusableView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final ViewHolder holder = (ViewHolder) view.getTag();
showEditTopicDialog(holder.index, getItem(holder.index).getName());
}
});
return reusableView;
}

private class ViewHolder {
TextView text;
int index;
}

public void showEditTopicDialog(final int index, final String title) {
final AlertDialog.Builder builder = new AlertDialog.Builder(_activity);
builder.setTitle(title);

final LayoutInflater inflater = _activity.getLayoutInflater();
final View view1 = inflater.inflate(R.layout.dialog_location_info, null);
final TextView dialogText = (TextView) view1.findViewById(R.id.textView);
dialogText.setText("Latitude: "+_locations.get(index).getLat()+"\nLongitude: "+_locations.get(index).getLongitude()
+"\nRadius: "+_locations.get(index).getRadius()+"\nSSids: "+
_locations.get(index).getWifiIdsAsString());

builder.setView(view1);
builder.show();

}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package pt.ulisboa.tecnico.ist.cmu.locmess.commands;

import android.location.Location;

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

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import pt.ulisboa.tecnico.ist.cmu.locmess.dto.LocationDto;
import pt.ulisboa.tecnico.ist.cmu.locmess.exception.DuplicateExecutionException;
import pt.ulisboa.tecnico.ist.cmu.locmess.exception.CommandNotExecutedException;

Expand All @@ -19,7 +24,7 @@ public class ListLocationsCommand extends AbstractCommand {

private static final String _endpoint="listLocations";

private HashMap<String,HashMap<String,String>> _results=null;
private List<LocationDto> _results=null;

public ListLocationsCommand(String token) {
super(_endpoint,"token="+token);
Expand All @@ -31,19 +36,16 @@ public void execute() throws IOException, DuplicateExecutionException {
}

//DONT GET SCARED!!! READ THE COMMENTS
public Map<String,HashMap<String,String>> getResults() throws IOException, DuplicateExecutionException, JSONException, CommandNotExecutedException {
public List<LocationDto> getResults() throws IOException, DuplicateExecutionException, JSONException, CommandNotExecutedException {
if(_results==null){
JSONObject obj=new JSONObject(getResponse());
JSONArray arr=obj.getJSONArray("locations");
HashMap<String,String> references;
_results=new ArrayList<>();
for(int i=0; i<arr.length(); i++){
references=new HashMap<String,String>();
references.put("latitude",arr.getJSONObject(i).getJSONObject("references").getString("latitude")); //This basically puts everything that came from the json into a map
references.put("longitude",arr.getJSONObject(i).getJSONObject("references").getString("longitude")); //Yes it could be a dictionary but idc
references.put("radius",arr.getJSONObject(i).getJSONObject("references").getString("radius"));
references.put("bssids",arr.getJSONObject(i).getJSONObject("references").getString("bssids"));
references.put("ssids",arr.getJSONObject(i).getJSONObject("references").getString("ssids"));
_results.put(arr.getJSONObject(i).getString("name"),references);
_results.add(new LocationDto(arr.getJSONObject(i).getJSONObject("references").getString("name"),
arr.getJSONObject(i).getJSONObject("references").getString("latitude"),
arr.getJSONObject(i).getJSONObject("references").getString("longitude"),
arr.getJSONObject(i).getJSONObject("references").getString("radius")));
}
}
return _results;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package pt.ulisboa.tecnico.ist.cmu.locmess.dto;

import java.util.ArrayList;
import java.util.List;

/**
* Created by jorge on 05/05/17.
*/

public class LocationDto implements LocMessDto {

private String _name;
private String _lat="",_longitude="",_radius="";
private List<String> _wifiIds=null;

@Deprecated
private String wifiids;

public LocationDto(String name, String lat, String longitude, String radius){
_name=name;
_lat=lat;
_longitude=longitude;
_radius=radius;
}

public LocationDto(String name, List<String> wifiIds){
_name=name;
_wifiIds=wifiIds;
}

@Deprecated
public LocationDto(String name, String wifi){
_name=name;
_wifiIds=new ArrayList<>();
wifiids=wifi;
}


public String getName() {
return _name;
}

public String getLat() {
return _lat;
}

public String getLongitude() {
return _longitude;
}

public String getRadius() {
return _radius;
}

public List<String> getWifiIds() {
return _wifiIds;
}

public String getWifiIdsAsString(){
if(_wifiIds!=null){
String ids="";
for(String s: _wifiIds){
ids+=s;
}
return ids;
}
return "None";
}

@Deprecated
public String getWifiids() {
return wifiids;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<include layout="@layout/content_locations_menu" />

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:id="@+id/add_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
Expand Down

0 comments on commit 6bf50d0

Please sign in to comment.