Commit c75ed000 authored by Corentin Mors's avatar Corentin Mors
Browse files

Clean code and add comments

parent 73bda7bf
......@@ -4,12 +4,10 @@ package fr.pixelswap.onmap.onmap;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.support.constraint.Placeholder;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
......@@ -26,10 +24,8 @@ import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.List;
......@@ -176,7 +172,7 @@ public class ChatFragment extends Fragment {
}
});
ImageButton sendButton = (ImageButton) view.findViewById(R.id.send_button);
ImageButton sendButton = view.findViewById(R.id.send_button);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
......
......@@ -37,7 +37,6 @@ import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
import android.util.Base64;
import android.util.Log;
public class CryptLib {
......
package fr.pixelswap.onmap.onmap;
import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
......@@ -13,17 +12,12 @@ import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.Pair;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
......@@ -34,11 +28,7 @@ import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.Marker;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import io.socket.client.Socket;
......@@ -59,24 +49,11 @@ public class MainActivity extends AppCompatActivity {
MapView map = null;
GeoPoint currentLocation;
// Create a criteria to find best provider (faster first)
public static Criteria createCoarseCriteria() {
Criteria c = new Criteria();
c.setAccuracy(Criteria.ACCURACY_COARSE);
c.setAltitudeRequired(false);
c.setBearingRequired(false);
c.setSpeedRequired(false);
c.setCostAllowed(true);
c.setPowerRequirement(Criteria.POWER_HIGH);
return c;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Retrieve settings and redirect to activity if not set
final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
final String mRoom = settings.getString("mRoom", null);
......@@ -106,19 +83,20 @@ public class MainActivity extends AppCompatActivity {
}
}
//load/initialize the osmdroid configuration, this can be done
// Load / Initialize the osmdroid configuration, this can be done
Context ctx = getApplicationContext();
Configuration.getInstance().load(ctx, PreferenceManager.getDefaultSharedPreferences(ctx));
//inflate and create the map
// Inflate and create the map
setContentView(R.layout.activity_main);
map = findViewById(R.id.map);
map.setTileSource(TileSourceFactory.MAPNIK);
map.setMultiTouchControls(true);
// Define map parameters and center it to nearly Paris
IMapController mapController = map.getController();
mapController.setZoom(9.0);
mapController.setZoom(8.0);
GeoPoint startPoint = new GeoPoint(48.8583, 2.2944);
mapController.setCenter(startPoint);
......@@ -130,14 +108,16 @@ public class MainActivity extends AppCompatActivity {
map.getOverlays().add(startMarker);
*/
// Retrieve all users colors from resources
mUsernameColors = MainActivity.this.getResources().getIntArray(R.array.username_colors);
// Run chat application to access socket
ChatApplication app = (ChatApplication) getApplication();
mSocket = app.getSocket();
mSocket.on("geoloc", onGeoloc);
mSocket.on("user left", onUserLeft);
// Handle setting button to access parameters view
Button settingsButton = findViewById(R.id.accessSettings);
settingsButton.setOnClickListener(new View.OnClickListener() {
@Override
......@@ -159,7 +139,21 @@ public class MainActivity extends AppCompatActivity {
mSocket.off("geoloc", onGeoloc);
mSocket.off("user left", onUserLeft);
mLocManager.removeUpdates(mLocListener);
mLocManager.removeUpdates(mLocListener); // Stop listener when finishing activity to avoid crash
}
// Create a criteria to find best location provider (faster first)
public static Criteria createCoarseCriteria() {
Criteria c = new Criteria();
c.setAccuracy(Criteria.ACCURACY_COARSE);
c.setAltitudeRequired(false);
c.setBearingRequired(false);
c.setSpeedRequired(false);
c.setCostAllowed(true);
c.setPowerRequirement(Criteria.POWER_HIGH);
return c;
}
private void displayMyCurrentLocationOverlay() {
......@@ -168,27 +162,25 @@ public class MainActivity extends AppCompatActivity {
locationMarker.setPosition(currentLocation);
locationMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
Drawable icon = getResources().getDrawable(R.drawable.ic_person_pin_circle_black_24dp);
icon.mutate(); // Make an separate instance of the drawable
icon.mutate(); // Make a separate instance of the drawable
icon.setTint(getUsernameColor(mUsername));
locationMarker.setIcon(icon);
locationMarker.setTitle("Your location");
if(userMarkers.containsKey(mUsername)){
userMarkers.remove(mUsername);
}
userMarkers.remove(mUsername);
userMarkers.put(mUsername, locationMarker);
// Clear map and reload all users
map.getOverlays().clear();
for (Marker value : userMarkers.values()) {
map.getOverlays().add(value);
}
map.invalidate();
map.invalidate(); // We need to invalidate map to force reload
}
}
// Instantiate list of users as a pair of name / position marker
private Map<String, Marker> userMarkers = new HashMap<String, Marker>();
public void moveParticipant(String username, String coordinates){
......@@ -204,10 +196,7 @@ public class MainActivity extends AppCompatActivity {
uLocation.setLongitude(Double.parseDouble(split[0]));
GeoPoint uGeoPoint = new GeoPoint(uLocation);
//uGeoPoint.setCoords(uLocation.getLatitude(), uLocation.getLongitude());
if(userMarkers.containsKey(username)){
userMarkers.remove(username);
}
userMarkers.remove(username);
Marker locationMarker = new Marker(map);
locationMarker.setPosition(uGeoPoint);
......@@ -219,6 +208,7 @@ public class MainActivity extends AppCompatActivity {
locationMarker.setTitle(username);
userMarkers.put(username, locationMarker);
// Clear map and reload all users
map.getOverlays().clear();
for (Marker value : userMarkers.values()) {
map.getOverlays().add(value);
......@@ -229,6 +219,7 @@ public class MainActivity extends AppCompatActivity {
public void removeParticipant(String username){
userMarkers.remove(username);
// Clear map and reload all users
map.getOverlays().clear();
for (Marker value : userMarkers.values()) {
map.getOverlays().add(value);
......@@ -254,6 +245,8 @@ public class MainActivity extends AppCompatActivity {
public void run() {
JSONObject data = (JSONObject) args[0];
String username;
// Remove the user on the map when he leaves
try {
username = data.getString("username");
} catch (JSONException e) {
......@@ -284,6 +277,7 @@ public class MainActivity extends AppCompatActivity {
return;
}
// Move the received participant location (decrypt it first)
try {
moveParticipant(username, CryptoFunc.sdecrypt(mPassword, coordinates));
} catch (Exception e) {
......@@ -313,27 +307,24 @@ public class MainActivity extends AppCompatActivity {
map.onPause(); //needed for compass, my location overlays, v6.0.0 and up
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// Override this method in the activity that hosts the Fragment and call super
// in order to receive the result inside onActivityResult from the fragment.
Log.e("OM_LOG", "result main");
super.onActivityResult(requestCode, resultCode, data);
}
public class MyLocationListener implements LocationListener{
public void onLocationChanged(Location loc) {
String message = String.format(
/*String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
loc.getLongitude(), loc.getLatitude()
);
// Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
// Log.e("OM_LOG", message);
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
Log.e("OM_LOG", message);*/
// Catch current location
currentLocation = new GeoPoint(loc);
currentLocation.setCoords(loc.getLatitude(), loc.getLongitude());
// Display the location on map
displayMyCurrentLocationOverlay();
// Send the location to other users
try {
mSocket.emit("geoloc", CryptoFunc.sencrypt(mPassword, "["+loc.getLongitude()+","+loc.getLatitude()+"]"));
} catch (Exception e) {
......
......@@ -14,15 +14,15 @@ public class Message {
public int getType() {
return mType;
};
}
public String getMessage() {
return mMessage;
};
}
public String getUsername() {
return mUsername;
};
}
public static class Builder {
......
......@@ -64,8 +64,8 @@ public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.ViewHold
public ViewHolder(View itemView) {
super(itemView);
mUsernameView = (TextView) itemView.findViewById(R.id.username);
mMessageView = (TextView) itemView.findViewById(R.id.message);
mUsernameView = itemView.findViewById(R.id.username);
mMessageView = itemView.findViewById(R.id.message);
}
public void setUsername(String username) {
......
......@@ -55,6 +55,8 @@ public class SettingsActivity extends AppCompatActivity {
usernameInput.setText(settings.getString("mUsername", "John"));
passwordInput.setText(settings.getString("mPassword", "azerty"));
// Save settings to preferences manager on button click
Button settingsButton = findViewById(R.id.buttonSettings);
settingsButton.setOnClickListener(new View.OnClickListener() {
@Override
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment