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

Make sending geoloc from device working

parent c7adbfc6
......@@ -9,6 +9,7 @@ android {
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
......@@ -23,6 +24,7 @@ dependencies {
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:support-vector-drawable:28.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
......
......@@ -10,22 +10,21 @@
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:name=".ChatApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:networkSecurityConfig="@xml/network_security_config"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:name="fr.pixelswap.onmap.onmap.ChatApplication"
android:networkSecurityConfig="@xml/network_security_config"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".LoginActivity"></activity>
<activity android:name=".LoginActivity" />
</application>
</manifest>
\ No newline at end of file
package fr.pixelswap.onmap.onmap;
import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.widget.Toolbar;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A {@link android.preference.PreferenceActivity} which implements and proxies the necessary calls
* to be used with AppCompat.
*/
public abstract class AppCompatPreferenceActivity extends PreferenceActivity {
private AppCompatDelegate mDelegate;
@Override
protected void onCreate(Bundle savedInstanceState) {
getDelegate().installViewFactory();
getDelegate().onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
getDelegate().onPostCreate(savedInstanceState);
}
public ActionBar getSupportActionBar() {
return getDelegate().getSupportActionBar();
}
public void setSupportActionBar(@Nullable Toolbar toolbar) {
getDelegate().setSupportActionBar(toolbar);
}
@Override
public MenuInflater getMenuInflater() {
return getDelegate().getMenuInflater();
}
@Override
public void setContentView(@LayoutRes int layoutResID) {
getDelegate().setContentView(layoutResID);
}
@Override
public void setContentView(View view) {
getDelegate().setContentView(view);
}
@Override
public void setContentView(View view, ViewGroup.LayoutParams params) {
getDelegate().setContentView(view, params);
}
@Override
public void addContentView(View view, ViewGroup.LayoutParams params) {
getDelegate().addContentView(view, params);
}
@Override
protected void onPostResume() {
super.onPostResume();
getDelegate().onPostResume();
}
@Override
protected void onTitleChanged(CharSequence title, int color) {
super.onTitleChanged(title, color);
getDelegate().setTitle(title);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
getDelegate().onConfigurationChanged(newConfig);
}
@Override
protected void onStop() {
super.onStop();
getDelegate().onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
getDelegate().onDestroy();
}
public void invalidateOptionsMenu() {
getDelegate().invalidateOptionsMenu();
}
private AppCompatDelegate getDelegate() {
if (mDelegate == null) {
mDelegate = AppCompatDelegate.create(this, null);
}
return mDelegate;
}
}
......@@ -258,7 +258,7 @@ public class ChatFragment extends Fragment {
addMessage(mUsername, message);
// perform the sending message attempt.
mSocket.emit("new message", sencrypt(mPassword, message));
mSocket.emit("new message", CryptoFunc.sencrypt(mPassword, message));
} catch (Exception e) {
e.printStackTrace();
}
......@@ -283,43 +283,6 @@ public class ChatFragment extends Fragment {
mMessagesView.scrollToPosition(mAdapter.getItemCount() - 1);
}
private static String encrypt(String raw, String clear) throws Exception {
byte[] salt = new String("12345678").getBytes("Utf8");
int iterationCount = 2048;
int keyStrength = 256;
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec spec = new PBEKeySpec(raw.toCharArray(), salt, iterationCount, keyStrength);
SecretKey tmp = factory.generateSecret(spec);
//SecretKeySpec skeySpec = new SecretKeySpec(raw.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, tmp);
byte[] encrypted = cipher.doFinal(clear.getBytes());
return encrypted.toString();
}
private static String sencrypt(String password, String data) throws Exception {
CryptLib cryptLib = new CryptLib();
String iv = "1234123412341234";
return cryptLib.encryptPlainText(data, password, iv);
}
private static String sdecrypt(String password, String data) throws Exception {
CryptLib cryptLib = new CryptLib();
String iv = "1234123412341234";
return cryptLib.decryptCipherText(data, password, iv);
}
private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] decrypted = cipher.doFinal(encrypted);
return decrypted;
}
private Emitter.Listener onConnect = new Emitter.Listener() {
@Override
......@@ -387,7 +350,7 @@ public class ChatFragment extends Fragment {
removeTyping(username);
try {
addMessage(username, sdecrypt(mPassword, message));
addMessage(username, CryptoFunc.sdecrypt(mPassword, message));
} catch (Exception e) {
e.printStackTrace();
}
......
package fr.pixelswap.onmap.onmap;
public class CryptoFunc {
public static String sencrypt(String password, String data) throws Exception {
CryptLib cryptLib = new CryptLib();
String iv = "1234123412341234";
return cryptLib.encryptPlainText(data, password, iv);
}
public static String sdecrypt(String password, String data) throws Exception {
CryptLib cryptLib = new CryptLib();
String iv = "1234123412341234";
return cryptLib.decryptCipherText(data, password, iv);
}
}
......@@ -28,8 +28,12 @@ import org.osmdroid.views.overlay.Marker;
import java.io.File;
import io.socket.client.Socket;
public class MainActivity extends AppCompatActivity {
private Socket mSocket;
private static final int REQUEST_ACCESS_FINE_LOCATION = 0;
MapView map = null;
......@@ -116,6 +120,9 @@ public class MainActivity extends AppCompatActivity {
startMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
map.getOverlays().add(startMarker);
ChatApplication app = (ChatApplication) getApplication();
mSocket = app.getSocket();
}
private void displayMyCurrentLocationOverlay() {
......@@ -170,6 +177,11 @@ public class MainActivity extends AppCompatActivity {
currentLocation = new GeoPoint(loc);
currentLocation.setCoords(loc.getLatitude(), loc.getLongitude());
displayMyCurrentLocationOverlay();
try {
mSocket.emit("geoloc", CryptoFunc.sencrypt("azerty", "["+loc.getLongitude()+","+loc.getLatitude()+"]"));
} catch (Exception e) {
e.printStackTrace();
}
}
public void onProviderDisabled(String arg0) {
......
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zm1,15h-2v-6h2v6zm0,-8h-2V7h2v2z" />
</vector>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M11.5,22c1.1,0 2,-0.9 2,-2h-4c0,1.1 0.9,2 2,2zm6.5,-6v-5.5c0,-3.07 -2.13,-5.64 -5,-6.32V3.5c0,-0.83 -0.67,-1.5 -1.5,-1.5S10,2.67 10,3.5v0.68c-2.87,0.68 -5,3.25 -5,6.32V16l-2,2v1h17v-1l-2,-2z" />
</vector>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FF000000"
android:pathData="M12 4V1L8 5l4 4V6c3.31 0 6 2.69 6 6 0 1.01,-.25 1.97,-.7 2.8l1.46 1.46C19.54 15.03 20 13.57 20 12c0,-4.42,-3.58,-8,-8,-8zm0 14c-3.31 0,-6,-2.69,-6,-6 0,-1.01.25,-1.97.7,-2.8L5.24 7.74C4.46 8.97 4 10.43 4 12c0 4.42 3.58 8 8 8v3l4,-4,-4,-4v3z" />
</vector>
\ No newline at end of file
......@@ -18,5 +18,78 @@
<string name="message_user_joined">%s joined</string>
<string name="message_user_left">%s left</string>
<string name="user_action_typing">is typing</string>
<string name="title_activity_settings">Settings</string>
<!-- Strings related to Settings -->
<!-- Example General settings -->
<string name="pref_header_general">General</string>
<string name="pref_title_social_recommendations">Enable social recommendations</string>
<string name="pref_description_social_recommendations">Recommendations for people to contact
based on your message history
</string>
<string name="pref_title_display_name">Display name</string>
<string name="pref_default_display_name">John Smith</string>
<string name="pref_title_add_friends_to_messages">Add friends to messages</string>
<string-array name="pref_example_list_titles">
<item>Always</item>
<item>When possible</item>
<item>Never</item>
</string-array>
<string-array name="pref_example_list_values">
<item>1</item>
<item>0</item>
<item>-1</item>
</string-array>
<!-- Example settings for Data & Sync -->
<string name="pref_header_data_sync">Data &amp; sync</string>
<string name="pref_title_sync_frequency">Sync frequency</string>
<string-array name="pref_sync_frequency_titles">
<item>15 minutes</item>
<item>30 minutes</item>
<item>1 hour</item>
<item>3 hours</item>
<item>6 hours</item>
<item>Never</item>
</string-array>
<string-array name="pref_sync_frequency_values">
<item>15</item>
<item>30</item>
<item>60</item>
<item>180</item>
<item>360</item>
<item>-1</item>
</string-array>
<string-array name="list_preference_entries">
<item>Entry 1</item>
<item>Entry 2</item>
<item>Entry 3</item>
</string-array>
<string-array name="list_preference_entry_values">
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
<string-array name="multi_select_list_preference_default_value" />
<string name="pref_title_system_sync_settings">System sync settings</string>
<!-- Example settings for Notifications -->
<string name="pref_header_notifications">Notifications</string>
<string name="pref_title_new_message_notifications">New message notifications</string>
<string name="pref_title_ringtone">Ringtone</string>
<string name="pref_ringtone_silent">Silent</string>
<string name="pref_title_vibrate">Vibrate</string>
</resources>
\ No newline at end of file
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<!-- NOTE: Hide buttons to simplify the UI. Users can touch outside the dialog to
dismiss it. -->
<!-- NOTE: ListPreference's summary should be set to its value by the activity code. -->
<ListPreference
android:defaultValue="180"
android:entries="@array/pref_sync_frequency_titles"
android:entryValues="@array/pref_sync_frequency_values"
android:key="sync_frequency"
android:negativeButtonText="@null"
android:positiveButtonText="@null"
android:title="@string/pref_title_sync_frequency" />
<!-- This preference simply launches an intent when selected. Use this UI sparingly, per
design guidelines. -->
<Preference android:title="@string/pref_title_system_sync_settings">
<intent android:action="android.settings.SYNC_SETTINGS" />
</Preference>
</PreferenceScreen>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<SwitchPreference
android:defaultValue="true"
android:key="example_switch"
android:summary="@string/pref_description_social_recommendations"
android:title="@string/pref_title_social_recommendations" />
<!-- NOTE: EditTextPreference accepts EditText attributes. -->
<!-- NOTE: EditTextPreference's summary should be set to its value by the activity code. -->
<EditTextPreference
android:capitalize="words"
android:defaultValue="@string/pref_default_display_name"
android:inputType="textCapWords"
android:key="example_text"
android:maxLines="1"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="@string/pref_title_display_name" />
<!-- NOTE: Hide buttons to simplify the UI. Users can touch outside the dialog to
dismiss it. -->
<!-- NOTE: ListPreference's summary should be set to its value by the activity code. -->
<ListPreference
android:defaultValue="-1"
android:entries="@array/pref_example_list_titles"
android:entryValues="@array/pref_example_list_values"
android:key="example_list"
android:negativeButtonText="@null"
android:positiveButtonText="@null"
android:title="@string/pref_title_add_friends_to_messages" />
</PreferenceScreen>
<preference-headers xmlns:android="http://schemas.android.com/apk/res/android">
<!-- These settings headers are only used on tablets. -->
<header
android:fragment="fr.pixelswap.onmap.onmap.SettingsActivity$GeneralPreferenceFragment"
android:icon="@drawable/ic_info_black_24dp"
android:title="@string/pref_header_general" />
<header
android:fragment="fr.pixelswap.onmap.onmap.SettingsActivity$NotificationPreferenceFragment"
android:icon="@drawable/ic_notifications_black_24dp"
android:title="@string/pref_header_notifications" />
<header
android:fragment="fr.pixelswap.onmap.onmap.SettingsActivity$DataSyncPreferenceFragment"
android:icon="@drawable/ic_sync_black_24dp"
android:title="@string/pref_header_data_sync" />
</preference-headers>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<!-- A 'parent' preference, which enables/disables child preferences (below)
when checked/unchecked. -->
<SwitchPreference
android:defaultValue="true"
android:key="notifications_new_message"
android:title="@string/pref_title_new_message_notifications" />
<!-- Allows the user to choose a ringtone in the 'notification' category. -->
<!-- NOTE: This preference will be enabled only when the checkbox above is checked. -->
<!-- NOTE: RingtonePreference's summary should be set to its value by the activity code. -->
<RingtonePreference
android:defaultValue="content://settings/system/notification_sound"
android:dependency="notifications_new_message"
android:key="notifications_new_message_ringtone"
android:ringtoneType="notification"
android:title="@string/pref_title_ringtone" />
<!-- NOTE: This preference will be enabled only when the checkbox above is checked. -->
<SwitchPreference
android:defaultValue="true"
android:dependency="notifications_new_message"
android:key="notifications_new_message_vibrate"
android:title="@string/pref_title_vibrate" />
</PreferenceScreen>
......@@ -2659,7 +2659,6 @@ $(function() {
}
function decrypt(data, password){
console.log(data);
try {
shaKey = cryptoLib.getHashSha256(password, 32);
return cryptoLib.decrypt(data, shaKey, iv);
......@@ -2668,18 +2667,20 @@ $(function() {
}
}
 
function oencrypt(object, password){
function oencrypt(data, password){
try {
return CryptoJS.AES.encrypt(JSON.stringify(object), password).toString();
shaKey = cryptoLib.getHashSha256(password, 32);
return cryptoLib.encrypt(JSON.stringify(data), shaKey, iv);
} catch (exception) {
throw new Error(exception.message);
}
}
 
function odecrypt(ocipher, password){
function odecrypt(data, password){
try {
var bytes = CryptoJS.AES.decrypt(ocipher, password);
return JSON.parse(bytes.toString(CryptoJS.enc.Utf8));
shaKey = cryptoLib.getHashSha256(password, 32);
var ostring = cryptoLib.decrypt(data, shaKey, iv);
return JSON.parse(ostring);
} catch (exception) {
throw new Error(exception.message);
}
......@@ -2777,7 +2778,9 @@ $(function() {
positionFeature.setGeometry(coordinates ?
new Point(coordinates) : null);
console.log(coordinates);
socket.emit('geoloc', oencrypt(coordinates, password));
var newCoord = ol.proj.transform(coordinates, 'EPSG:3857', 'EPSG:4326');
console.log(newCoord);
socket.emit('geoloc', oencrypt(newCoord, password));
});
 
var mylayer = new VectorLayer({
......@@ -2809,7 +2812,8 @@ $(function() {
mylayer.getSource().addFeature(userPositions[data.username]);
}
var ucoord = odecrypt(data.coordinates, password);
var coord = odecrypt(data.coordinates, password);
var ucoord = ol.proj.transform(coord, 'EPSG:4326', 'EPSG:3857');
userPositions[data.username].setGeometry(ucoord ?
new Point(ucoord) : null);
 
......
......@@ -285,7 +285,6 @@ $(function() {
}
function decrypt(data, password){
console.log(data);
try {
shaKey = cryptoLib.getHashSha256(password, 32);
return cryptoLib.decrypt(data, shaKey, iv);
......@@ -294,18 +293,20 @@ $(function() {
}
}
function oencrypt(object, password){
function oencrypt(data, password){
try {
return CryptoJS.AES.encrypt(JSON.stringify(object), password).toString();
shaKey = cryptoLib.getHashSha256(password, 32);
return cryptoLib.encrypt(JSON.stringify(data), shaKey, iv);
} catch (exception) {
throw new Error(exception.message);
}
}
function odecrypt(ocipher, password){
function odecrypt(data, password){
try {
var bytes = CryptoJS.AES.decrypt(ocipher, password);
return JSON.parse(bytes.toString(CryptoJS.enc.Utf8));
shaKey = cryptoLib.getHashSha256(password, 32);
var ostring = cryptoLib.decrypt(data, shaKey, iv);
return JSON.parse(ostring);
} catch (exception) {
throw new Error(exception.message);
}
......@@ -403,7 +404,9 @@ $(function() {
positionFeature.setGeometry(coordinates ?
new Point(coordinates) : null);
console.log(coordinates);
socket.emit('geoloc', oencrypt(coordinates, password));
var newCoord = ol.proj.transform(coordinates, 'EPSG:3857', 'EPSG:4326');
console.log(newCoord);
socket.emit('geoloc', oencrypt(newCoord, password));
});
var mylayer = new VectorLayer({
......@@ -435,7 +438,8 @@ $(function() {
mylayer.getSource().addFeature(userPositions[data.username]);
}