From 74a047180006ab2098c482ecb01e85be3dbb029c Mon Sep 17 00:00:00 2001 From: Tiago Rosado Date: Thu, 18 May 2017 12:01:57 +0100 Subject: [PATCH 1/4] Changed login text --- .../cmu/tg14/locmessclient/Utils/Network/ServerActions.java | 2 +- LocMessClient/app/src/main/res/values/strings.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/Utils/Network/ServerActions.java b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/Utils/Network/ServerActions.java index 02c0cae..bfdc06b 100644 --- a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/Utils/Network/ServerActions.java +++ b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/Utils/Network/ServerActions.java @@ -43,7 +43,7 @@ * Created by trosado on 31/03/17. */ public class ServerActions { - private final static String addr = "194.210.220.63"; + private final static String addr = "194.210.221.241"; private final static String port = "8443"; private final static String endpoint = "https://"+addr+":"+port+"/api"; private static RequestQueue queue; diff --git a/LocMessClient/app/src/main/res/values/strings.xml b/LocMessClient/app/src/main/res/values/strings.xml index 1b98e29..a01917d 100644 --- a/LocMessClient/app/src/main/res/values/strings.xml +++ b/LocMessClient/app/src/main/res/values/strings.xml @@ -6,7 +6,7 @@ Do you really want to logout? - Email + Username Password Sign in Sign in From 0e3387afafce2945a9360faef01f4bbcd5224472 Mon Sep 17 00:00:00 2001 From: Tiago Rosado Date: Thu, 18 May 2017 15:29:44 +0100 Subject: [PATCH 2/4] Solved logout issues for db clean and credentials clear --- .../locmessclient/AddLocationActivity.java | 2 +- .../cmu/tg14/locmessclient/AddProfileKey.java | 2 +- .../cmu/tg14/locmessclient/ListLocations.java | 2 +- .../cmu/tg14/locmessclient/ListMessages.java | 4 +- .../cmu/tg14/locmessclient/LoginActivity.java | 15 +------- .../cmu/tg14/locmessclient/MainActivity.java | 28 +++++++++++++- .../MessageLocationActivity.java | 2 +- .../locmessclient/MessagePolicyActivity.java | 2 +- .../cmu/tg14/locmessclient/MessageView.java | 2 +- .../locmessclient/MyMessagesFragment.java | 4 +- .../tg14/locmessclient/ProfileFragment.java | 4 +- .../locmessclient/Services/DBService.java | 23 ++++++++--- .../locmessclient/Services/MasterService.java | 2 +- .../Utils/FeedReaderDbHelper.java | 38 ++++++++++++++----- .../Utils/Network/ServerActions.java | 26 ++++++------- 15 files changed, 98 insertions(+), 58 deletions(-) diff --git a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/AddLocationActivity.java b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/AddLocationActivity.java index 82851eb..5f0fec8 100644 --- a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/AddLocationActivity.java +++ b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/AddLocationActivity.java @@ -168,7 +168,7 @@ public void onClick(View v){ addGPS(); } - FeedReaderDbHelper dbHelper = new FeedReaderDbHelper(getApplicationContext()); + FeedReaderDbHelper dbHelper = FeedReaderDbHelper.getInstance(getApplicationContext()); dbHelper.insertLocation(mLocation.getName(),mLocation.getSsid(),mLocation.getBle(),mLocation.getLatitude(),mLocation.getLongitude(),mLocation.getRadius(),"false"); finish(); diff --git a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/AddProfileKey.java b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/AddProfileKey.java index 3a69cce..edb1831 100644 --- a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/AddProfileKey.java +++ b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/AddProfileKey.java @@ -94,7 +94,7 @@ public void onClick(View v) { @Override public void onClick(View view) { if(!keyValueMap.isEmpty()){ - FeedReaderDbHelper dbHelper = new FeedReaderDbHelper(activity); + FeedReaderDbHelper dbHelper = FeedReaderDbHelper.getInstance(activity); dbHelper.insertAllProfiles(keyValueMap); diff --git a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/ListLocations.java b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/ListLocations.java index 29a79f3..8a5022a 100644 --- a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/ListLocations.java +++ b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/ListLocations.java @@ -194,7 +194,7 @@ protected void onPostExecute(Void aVoid) { @Override protected Void doInBackground(Void... params) { - FeedReaderDbHelper dbHelper = new FeedReaderDbHelper(getActivity()); + FeedReaderDbHelper dbHelper = FeedReaderDbHelper.getInstance(getActivity()); dbHelper.deleteLocation(locationName); return null; diff --git a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/ListMessages.java b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/ListMessages.java index b44b9b9..3992b5f 100644 --- a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/ListMessages.java +++ b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/ListMessages.java @@ -114,7 +114,7 @@ private void fillDatabase(Activity activity) { } private void createDatabase(Context context, boolean deleteDatabase){ - FeedReaderDbHelper dbHelper = new FeedReaderDbHelper(context); + FeedReaderDbHelper dbHelper = FeedReaderDbHelper.getInstance(context); if (doesDatabaseExist(context, dbHelper.getDatabaseName()) && deleteDatabase){ //context.deleteDatabase(FeedReaderDbHelper.DATABASE_NAME); //dbHelper.onDrop(dbHelper.getWritableDatabase()); @@ -266,7 +266,7 @@ protected void onPostExecute(Void aVoid) { @Override protected Void doInBackground(Void... params) { - FeedReaderDbHelper dbHelper = new FeedReaderDbHelper(getActivity()); + FeedReaderDbHelper dbHelper = FeedReaderDbHelper.getInstance(getActivity()); List dbMessages = dbHelper.getAllNearbyMessages(); for(Message m: dbMessages){ diff --git a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/LoginActivity.java b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/LoginActivity.java index 8b8b8cd..6755aad 100644 --- a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/LoginActivity.java +++ b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/LoginActivity.java @@ -21,6 +21,7 @@ import android.os.Bundle; import android.provider.ContactsContract; import android.text.TextUtils; +import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; @@ -327,7 +328,6 @@ public class UserLoginTask extends AsyncTask { @Override protected Boolean doInBackground(Void... params) { - // TODO: attempt authentication against a network service. try { @@ -341,6 +341,7 @@ public void onHTTPResponse(Boolean response) { ServicesDataHolder servicesDataHolder = ServicesDataHolder.getInstance(); servicesDataHolder.setUsername(mUsername); servicesDataHolder.setPassword(mPassword); + Log.d(TAG, "onHTTPResponse: Username:"+servicesDataHolder.getUsername()+" P: "+servicesDataHolder.getPassword()); Intent intent = new Intent(mActivity,MainActivity.class); startActivity(intent); @@ -357,18 +358,6 @@ public void onHTTPResponse(Boolean response) { return false; } - for (String credential : DUMMY_CREDENTIALS) { - String[] pieces = credential.split(":"); - if (pieces[0].equals(mUsername)) { - // Account exists, return true if the password matches. - if(pieces[1].equals(mPassword)){ - ServicesDataHolder dataHolder = ServicesDataHolder.getInstance(); - dataHolder.setUsername(mUsername); - } - - return true; - } - } // TODO: register the new account here. return true; diff --git a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/MainActivity.java b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/MainActivity.java index 83ba1b8..5cc8af6 100644 --- a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/MainActivity.java +++ b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/MainActivity.java @@ -25,6 +25,11 @@ import android.widget.TextView; import pt.ulisboa.tecnico.cmu.tg14.locmessclient.DataObjects.ServicesDataHolder; +import pt.ulisboa.tecnico.cmu.tg14.locmessclient.Services.BluetoothService; +import pt.ulisboa.tecnico.cmu.tg14.locmessclient.Services.DBService; +import pt.ulisboa.tecnico.cmu.tg14.locmessclient.Services.GPSService; +import pt.ulisboa.tecnico.cmu.tg14.locmessclient.Services.MasterService; +import pt.ulisboa.tecnico.cmu.tg14.locmessclient.Services.WifiService; import pt.ulisboa.tecnico.cmu.tg14.locmessclient.Utils.FeedReaderDbHelper; import pt.ulisboa.tecnico.cmu.tg14.locmessclient.Utils.ServiceManager; @@ -207,6 +212,16 @@ public void onClick(DialogInterface dialog,int id) { return true; } + @Override + protected void onStop() { + super.onStop(); + stopService(new Intent(this,GPSService.class)); + stopService(new Intent(this,WifiService.class)); + stopService(new Intent(this,BluetoothService.class)); + stopService(new Intent(this,DBService.class)); + stopService(new Intent(this, MasterService.class)); + } + @Override public void onFragmentInteraction(Uri uri) { //Log.d("URI",uri.toString()); @@ -215,8 +230,17 @@ public void onFragmentInteraction(Uri uri) { private void logout() { // delete all sensitive data from database // TODO : do I need to stop services? - FeedReaderDbHelper dbHelper = new FeedReaderDbHelper(getApplicationContext()); - dbHelper.dropDatabase(activity); + + FeedReaderDbHelper dbHelper = FeedReaderDbHelper.getInstance(getApplicationContext()); + // dbHelper.dropDatabase(activity); + dbHelper.deleteAll(); + ServiceManager serviceManager = ServiceManager.getInstance(); + serviceManager.stopServices(); + ServicesDataHolder dataHolder = ServicesDataHolder.getInstance(); + dataHolder.setUsername(""); + dataHolder.setPassword(""); + + } } diff --git a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/MessageLocationActivity.java b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/MessageLocationActivity.java index c4187d1..049ea5a 100644 --- a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/MessageLocationActivity.java +++ b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/MessageLocationActivity.java @@ -121,7 +121,7 @@ protected void onPostExecute(Void aVoid) { @Override protected Void doInBackground(Void... params) { - FeedReaderDbHelper dbHelper = new FeedReaderDbHelper(activity); + FeedReaderDbHelper dbHelper = FeedReaderDbHelper.getInstance(activity); List dbLocationNames = dbHelper.getAllLocationsNames(); for(String s: dbLocationNames){ diff --git a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/MessagePolicyActivity.java b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/MessagePolicyActivity.java index e3c0241..420e803 100644 --- a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/MessagePolicyActivity.java +++ b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/MessagePolicyActivity.java @@ -74,7 +74,7 @@ protected void onCreate(Bundle savedInstanceState) { activity = this; - dbHelper = new FeedReaderDbHelper(activity); + dbHelper = FeedReaderDbHelper.getInstance(activity); mKey = (EditText) findViewById(R.id.message_profile_key); diff --git a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/MessageView.java b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/MessageView.java index ae9590c..9a60131 100644 --- a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/MessageView.java +++ b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/MessageView.java @@ -67,7 +67,7 @@ protected void onPostExecute(Void aVoid) { @Override protected Void doInBackground(Void... params) { - FeedReaderDbHelper dbHelper = new FeedReaderDbHelper(getApplicationContext()); + FeedReaderDbHelper dbHelper = FeedReaderDbHelper.getInstance(getApplicationContext()); Message dbMessage = null; try { dbMessage = dbHelper.getMessage(messageID.toString()); diff --git a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/MyMessagesFragment.java b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/MyMessagesFragment.java index aa6cb28..460dc1c 100644 --- a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/MyMessagesFragment.java +++ b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/MyMessagesFragment.java @@ -133,7 +133,7 @@ public void onItemClick(AdapterView adapterView, final View view, final int p listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView adapterView, final View view, final int position, long l) { - final FeedReaderDbHelper dbHelper = new FeedReaderDbHelper(getActivity()); + final FeedReaderDbHelper dbHelper = FeedReaderDbHelper.getInstance(getActivity()); final String messageContent = messagesList.get(position); AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder( @@ -260,7 +260,7 @@ protected void onPostExecute(Void aVoid) { @Override protected Void doInBackground(Void... params) { - FeedReaderDbHelper dbHelper = new FeedReaderDbHelper(getActivity()); + FeedReaderDbHelper dbHelper = FeedReaderDbHelper.getInstance(getActivity()); String username = ServicesDataHolder.getInstance().getUsername(); diff --git a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/ProfileFragment.java b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/ProfileFragment.java index 228ee9e..eec0e50 100644 --- a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/ProfileFragment.java +++ b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/ProfileFragment.java @@ -208,7 +208,7 @@ protected void onPostExecute(Void aVoid) { @Override protected Void doInBackground(Void... params) { - FeedReaderDbHelper dbHelper = new FeedReaderDbHelper(getActivity()); + FeedReaderDbHelper dbHelper = FeedReaderDbHelper.getInstance(getActivity()); List profilesList = dbHelper.getMyProfiles(); for(Profile p: profilesList){ @@ -244,7 +244,7 @@ protected void onPostExecute(Void aVoid) { @Override protected Void doInBackground(Void... params) { - FeedReaderDbHelper dbHelper = new FeedReaderDbHelper(getActivity()); + FeedReaderDbHelper dbHelper = FeedReaderDbHelper.getInstance(getActivity()); try { dbHelper.deleteProfileInTheFuture(k); } catch (ProfileNotFoundException e) { diff --git a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/Services/DBService.java b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/Services/DBService.java index 9b22d9d..a387050 100644 --- a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/Services/DBService.java +++ b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/Services/DBService.java @@ -17,6 +17,8 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; import pt.ulisboa.tecnico.cmu.tg14.locmessclient.DTO.LocationQuery; import pt.ulisboa.tecnico.cmu.tg14.locmessclient.DTO.MessageServer; @@ -50,19 +52,28 @@ public class DBService extends Service implements OnResponseListener { private boolean updateProfiles = true; private boolean updateOldProfileKeys = true; private boolean updateOldMessages = true; - + private boolean stop = false; + private Future runnableManager; @Nullable @Override public IBinder onBind(Intent intent) { return null; } + @Override + public void onDestroy() { + super.onDestroy(); + this.stop = true; + Log.d(TAG, "onDestroy: GOT DESTROiED"); + runnableManager.cancel(true); + } + @Override public void onCreate() { //Log.d("DBUpdater","DB Updater Started"); serverActions = new ServerActions(this); - dbHelper = new FeedReaderDbHelper(getApplicationContext()); + dbHelper = FeedReaderDbHelper.getInstance(getApplicationContext()); //Do once if(dataHolder.isCentralizedMode()) { @@ -75,8 +86,7 @@ public void onCreate() { runnable = new Runnable() { public void run() { - - if(dataHolder.isCentralizedMode()){ + if(dataHolder.isCentralizedMode()||!stop){ //Log.d("DBService", "entered DBService"); @@ -160,6 +170,8 @@ public void run() { } }; + runnableManager = Executors.newSingleThreadExecutor().submit(runnable); + handler.postDelayed(runnable, 2000); } @@ -429,8 +441,7 @@ private ArrayList checkOfflineAddedLocations() { private void updateNearLocations(){ LocationQuery query = new LocationQuery(dataHolder.getLatitude(),dataHolder.getLongitude(), dataHolder.getSsidNames(),dataHolder.getBleNames()); - Gson gson = new Gson(); - //Log.d(TAG,"DB service query "+gson.toJson(query)); + serverActions.getNearLocations(query, new OnResponseListener>() { @Override public void onHTTPResponse(List response) { diff --git a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/Services/MasterService.java b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/Services/MasterService.java index 563c61f..970b030 100644 --- a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/Services/MasterService.java +++ b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/Services/MasterService.java @@ -86,7 +86,7 @@ public void clearBluetoothList() { public void onDestroy() { super.onDestroy(); //Log.d("MasterService: ", "onDestroy -> unregistering receivers"); - serviceManager.unRegisterReceivers(); + //serviceManager.unRegisterReceivers(); //Log.d("MasterService: ", "onDestroy -> stopping services"); serviceManager.stopServices(); } diff --git a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/Utils/FeedReaderDbHelper.java b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/Utils/FeedReaderDbHelper.java index 8cdfa3d..452a3d8 100644 --- a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/Utils/FeedReaderDbHelper.java +++ b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/Utils/FeedReaderDbHelper.java @@ -134,10 +134,19 @@ public class FeedReaderDbHelper extends SQLiteOpenHelper { private static final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS "; + private static FeedReaderDbHelper mInstance = null; + public FeedReaderDbHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } + public static FeedReaderDbHelper getInstance(Context context){ + if (mInstance == null) { + mInstance = new FeedReaderDbHelper(context.getApplicationContext()); + } + return mInstance; + } + public void onCreate(SQLiteDatabase db) { /*db.execSQL(SQL_CREATE_LOCATION); db.execSQL(SQL_CREATE_MESSAGE); @@ -190,8 +199,8 @@ public void createLocationTable(SQLiteDatabase db) { public void dropLocation() { SQLiteDatabase db = this.getWritableDatabase(); - - db.execSQL(SQL_DELETE_ENTRIES + FeedEntry.LOCATION_TABLE_NAME); + if(db.isReadOnly()) + db.execSQL(SQL_DELETE_ENTRIES + FeedEntry.LOCATION_TABLE_NAME); } public void deleteAllLocations() { @@ -209,7 +218,8 @@ public void insertLocation (String name, String ssid, String ble, float lat, flo contentValues.put(FeedEntry.LOCATION_COLUMN_LON, lon); contentValues.put(FeedEntry.LOCATION_COLUMN_RAD, radius); contentValues.put(FeedEntry.LOCATION_COLUMN_CENTRALIZED, centralized); - db.insert(FeedEntry.LOCATION_TABLE_NAME, null, contentValues); + if(!db.isReadOnly()) + db.insert(FeedEntry.LOCATION_TABLE_NAME, null, contentValues); //Log.d("insertAllLocations: ","added to DB location " + name + ssid + ble + lat + lon + radius); } @@ -760,9 +770,8 @@ public void insertMessageFromServer (String uuid, long creationTime, long startT contentValues.put(FeedEntry.MESSAGE_COLUMN_ADDEDDECENTRALIZED, "false"); contentValues.put(FeedEntry.MESSAGE_COLUMN_DELETEDDECENTRALIZED, "false"); contentValues.put(FeedEntry.MESSAGE_COLUMN_NEARBY, "true"); - - - db.insert(FeedEntry.MESSAGE_TABLE_NAME, null, contentValues); + if(!db.isReadOnly()) + db.insert(FeedEntry.MESSAGE_TABLE_NAME, null, contentValues); } public void updateMessageInsertedToServer(String uuid) throws MultipleRowsAfectedException, MessageNotFoundException { @@ -985,7 +994,9 @@ public boolean deleteAllNearbyMessages() { String whereClause = FeedEntry.MESSAGE_COLUMN_NEARBY + " = ?"; String[] whereArgs = new String[] { "true" }; - int removed = db.delete(table, whereClause, whereArgs); + int removed = 0; + if(!db.isReadOnly()) + removed = db.delete(table, whereClause, whereArgs); //Log.d("DBService", "deleteAllNearbyMessages -> " + removed); @@ -1465,8 +1476,8 @@ public List getListAllServerProfiles() { public void deleteAllServerProfiles() { SQLiteDatabase db = this.getWritableDatabase(); - - db.execSQL("delete from "+ FeedEntry.SERVER_PROFILES_TABLE_NAME); + if(!db.isReadOnly()) + db.execSQL("delete from "+ FeedEntry.SERVER_PROFILES_TABLE_NAME); } private Profile associateServerProfiles(Cursor cursor) { @@ -1596,4 +1607,13 @@ public void deleteAllMuleProfiles() { db.execSQL("delete from "+ FeedEntry.MULE_PROFILE_TABLE_NAME); } + public void deleteAll() { + this.deleteAllMessages(); + this.deleteAllLocations(); + this.deleteAllMessageMules(); + this.deleteAllProfiles(); + this.deleteAllMuleProfiles(); + this.deleteAllServerProfiles(); + this.deleteAllNearbyMessages(); + } } diff --git a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/Utils/Network/ServerActions.java b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/Utils/Network/ServerActions.java index bfdc06b..d36056d 100644 --- a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/Utils/Network/ServerActions.java +++ b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/Utils/Network/ServerActions.java @@ -43,30 +43,26 @@ * Created by trosado on 31/03/17. */ public class ServerActions { - private final static String addr = "194.210.221.241"; + private final static String addr = "193.136.167.169"; private final static String port = "8443"; private final static String endpoint = "https://"+addr+":"+port+"/api"; private static RequestQueue queue; private Context mContext; - private static String username = ""; - private static String password = ""; private static String sessionID = ""; private static String sessionIdURL = ""; - + private static ServicesDataHolder dataHolder; public ServerActions(Context context) { queue = Volley.newRequestQueue(context); mContext = context; - ServicesDataHolder dataHolder = ServicesDataHolder.getInstance(); - username = dataHolder.getUsername(); - password = dataHolder.getPassword(); + dataHolder = ServicesDataHolder.getInstance(); } private void makeAuthenticatedRequest(int method, String url, JSONObject jsonObject, final OnResponseListener listener){ new HttpsTrustManager(mContext).allowServerCertificate(); - JsonObjectAuthenticatedRequest request = new JsonObjectAuthenticatedRequest(method,url,username,password,jsonObject,new Response.Listener() { @Override + JsonObjectAuthenticatedRequest request = new JsonObjectAuthenticatedRequest(method,url,dataHolder.getUsername(),dataHolder.getPassword(),jsonObject,new Response.Listener() { @Override public void onResponse(JSONObject response) { Gson gson = new Gson(); //Log.d(TAG, "onResponse: " + response.toString()); @@ -118,7 +114,7 @@ public List getMyProfileKeys(final OnResponseListener listener) { String url = generateURL("/profile/myList"); final List profiles = new ArrayList<>(); - JsonArrayAuthenticatedRequest stringRequest = new JsonArrayAuthenticatedRequest(url,username,password, new Response.Listener() { + JsonArrayAuthenticatedRequest stringRequest = new JsonArrayAuthenticatedRequest(url,dataHolder.getUsername(),dataHolder.getPassword(), new Response.Listener() { @Override public void onResponse(JSONArray response) { for(int i = 0;i getProfileKeys(final OnResponseListener listener) { final List profiles = new ArrayList<>(); new HttpsTrustManager(mContext).allowServerCertificate(); - JsonArrayAuthenticatedRequest stringRequest = new JsonArrayAuthenticatedRequest(url,username,password, new Response.Listener() { + JsonArrayAuthenticatedRequest stringRequest = new JsonArrayAuthenticatedRequest(url,dataHolder.getUsername(),dataHolder.getPassword(), new Response.Listener() { @Override public void onResponse(JSONArray response) { for(int i = 0;i messages = new ArrayList<>(); final Gson gson = new Gson(); - JsonArrayAuthenticatedRequest request = new JsonArrayAuthenticatedRequest(url, username, password + JsonArrayAuthenticatedRequest request = new JsonArrayAuthenticatedRequest(url, dataHolder.getUsername(), dataHolder.getPassword() , new Response.Listener() { @Override public void onResponse(JSONArray response) { @@ -300,7 +296,7 @@ public static List getMessagesFromLocation(Location location, final OnR try { JSONObject jsonObject = new JSONObject(gson.toJson(location)); HttpsTrustManager.allowServerCertificate(); - JsonArrayFromJsonObjectAuthenticatedRequest request = new JsonArrayFromJsonObjectAuthenticatedRequest(Request.Method.POST,url,username,password,jsonObject,null, new Response.Listener() { + JsonArrayFromJsonObjectAuthenticatedRequest request = new JsonArrayFromJsonObjectAuthenticatedRequest(Request.Method.POST,url,dataHolder.getUsername(),dataHolder.getPassword(),jsonObject,null, new Response.Listener() { @Override public void onResponse(JSONArray response) { for(int i = 0;i getAllLocations(final OnResponseListener listener) { final List locations = new ArrayList<>(); new HttpsTrustManager(mContext).allowServerCertificate(); - JsonArrayAuthenticatedRequest stringRequest = new JsonArrayAuthenticatedRequest(url,username,password, new Response.Listener() { + JsonArrayAuthenticatedRequest stringRequest = new JsonArrayAuthenticatedRequest(url,dataHolder.getUsername(),dataHolder.getPassword(), new Response.Listener() { @Override public void onResponse(JSONArray response) { for(int i = 0;i listener){ String url = generateURL("/location/list/hash"); new HttpsTrustManager(mContext).allowServerCertificate(); - JsonObjectAuthenticatedRequest request = new JsonObjectAuthenticatedRequest(Request.Method.GET,url,username,password,null,new Response.Listener() { + JsonObjectAuthenticatedRequest request = new JsonObjectAuthenticatedRequest(Request.Method.GET,url,dataHolder.getUsername(),dataHolder.getPassword(),null,new Response.Listener() { @Override @@ -407,7 +403,7 @@ public List getNearLocations(LocationQuery query, final OnResponseList final List locations = new ArrayList<>(); //Log.d(TAG, "request: "+query.toJSON()); new HttpsTrustManager(mContext).allowServerCertificate(); - JsonArrayFromJsonObjectAuthenticatedRequest request = new JsonArrayFromJsonObjectAuthenticatedRequest(Request.Method.POST,url,username,password,query.toJSON(),null, new Response.Listener() { + JsonArrayFromJsonObjectAuthenticatedRequest request = new JsonArrayFromJsonObjectAuthenticatedRequest(Request.Method.POST,url,dataHolder.getUsername(),dataHolder.getPassword(),query.toJSON(),null, new Response.Listener() { @Override public void onResponse(JSONArray response) { for(int i = 0;i Date: Thu, 18 May 2017 19:20:05 +0100 Subject: [PATCH 3/4] recovered files from merge --- .../app/src/main/AndroidManifest.xml | 8 ++-- .../Utils/Network/ServerActions.java | 47 +------------------ 2 files changed, 5 insertions(+), 50 deletions(-) diff --git a/LocMessClient/app/src/main/AndroidManifest.xml b/LocMessClient/app/src/main/AndroidManifest.xml index 800684d..f9452a8 100644 --- a/LocMessClient/app/src/main/AndroidManifest.xml +++ b/LocMessClient/app/src/main/AndroidManifest.xml @@ -29,7 +29,11 @@ android:name=".LoginActivity" android:label="@string/app_name" android:screenOrientation="portrait" > + + + + @@ -44,11 +48,7 @@ android:label="@string/title_activity_main" android:screenOrientation="portrait" android:theme="@style/AppTheme.NoActionBar"> - - - - getHeaders() throws AuthFailureError { queue.add(strReq); } - public void logout() { - String url = generateURL("/user/logout"); - final boolean[] loggedin = {false}; - HttpsTrustManager.allowAllSSL(); - StringRequest strReq = new StringRequest(Request.Method.GET, - url, - new Response.Listener() { - @Override - public void onResponse(String response) { - Log.d(TAG,"String session id: "+response.toString()); - try { - sessionID = URLEncoder.encode(response,"UTF-8"); - sessionIdURL = "?sessionID="+sessionID; - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - } - } - }, - new Response.ErrorListener() { - @Override - public void onErrorResponse(VolleyError error) { - Log.d(TAG, "Error: " + error.getMessage()); - } - }) { - - @Override - protected Response parseNetworkResponse(NetworkResponse response) { - int mStatusCode = response.statusCode; - return super.parseNetworkResponse(response); - } - - @Override - public Map getHeaders() throws AuthFailureError { - Map headers = new HashMap<>(); - String credentials = username+":"+password; - String auth = "Basic " - + Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP); - headers.put("Content-Type", "application/json"); - headers.put("Authorization", auth); - return headers; - }}; - - queue.add(strReq); - } - private static String generateURL(String path){ return endpoint+path+sessionIdURL; } From 8689050422522aacc4dec673bead1573ae176506 Mon Sep 17 00:00:00 2001 From: Tiago Rosado Date: Thu, 18 May 2017 22:30:44 +0100 Subject: [PATCH 4/4] Fixed bugs on message update --- .../cmu/tg14/locmessclient/ListMessages.java | 48 ++++++------------- .../cmu/tg14/locmessclient/LoginActivity.java | 2 +- .../tg14/locmessclient/RegisterActivity.java | 1 + .../locmessclient/Services/DBService.java | 10 ++-- .../Utils/FeedReaderDbHelper.java | 48 ++++++++++++++----- 5 files changed, 57 insertions(+), 52 deletions(-) diff --git a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/ListMessages.java b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/ListMessages.java index 3992b5f..83fdc2d 100644 --- a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/ListMessages.java +++ b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/ListMessages.java @@ -7,18 +7,16 @@ import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.Uri; -import android.os.AsyncTask; import android.os.Bundle; import android.app.Fragment; +import android.os.Handler; import android.support.design.widget.FloatingActionButton; -import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; -import android.widget.Toast; import java.io.File; import java.util.ArrayList; @@ -44,7 +42,7 @@ public class ListMessages extends Fragment { private ListView mMessageListView; private List mMessageList; private ArrayAdapter arrayAdapter; - + private Handler handler; private List messageContentList; @@ -74,6 +72,8 @@ public void onCreate(Bundle savedInstanceState) { getActivity().setTitle(R.string.fragment_list_messages_title); + handler = new Handler(); + handler.postDelayed(new GetMessageFromDBThread(),500); FloatingActionButton fab = (FloatingActionButton) getActivity().findViewById(R.id.fab); @@ -102,7 +102,7 @@ private void checkInternetConnection(){ dataHolder.setCentralizedMode(isConnected); - Toast.makeText(getActivity(), "has internet connection = " + isConnected, Toast.LENGTH_LONG).show(); + // Toast.makeText(getActivity(), "has internet connection = " + isConnected, Toast.LENGTH_LONG).show(); } private void fillDatabase(Activity activity) { @@ -218,8 +218,6 @@ public void onResume() { arrayAdapter = new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1, messageContentList); - new getMessagesFromDBTask().execute(); - mMessageListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView adapterView, View view, int position, long l) { @@ -234,45 +232,27 @@ public void onItemClick(AdapterView adapterView, View view, int position, lon } - private class getMessagesFromDBTask extends AsyncTask { + private class GetMessageFromDBThread implements Runnable{ private List auxList; - - - public getMessagesFromDBTask() { - auxList = new ArrayList<>(); - } - @Override - protected void onPreExecute() { - super.onPreExecute(); - } + public void run() { + auxList = new ArrayList<>(); - @Override - protected void onPostExecute(Void aVoid) { - super.onPostExecute(aVoid); + FeedReaderDbHelper dbHelper = FeedReaderDbHelper.getInstance(getActivity()); + List dbMessages = dbHelper.getAllNearbyMessages(); mMessageListView.setAdapter(arrayAdapter); mMessageList.clear(); - for(Message m: auxList){ + messageContentList.clear(); + for(Message m: dbMessages){ mMessageList.add(m); messageContentList.add(m.getContent()); } - arrayAdapter.notifyDataSetChanged(); - //Log.d("MessageLocationActivity","notify dataset changed"); - - } - @Override - protected Void doInBackground(Void... params) { - FeedReaderDbHelper dbHelper = FeedReaderDbHelper.getInstance(getActivity()); - List dbMessages = dbHelper.getAllNearbyMessages(); - - for(Message m: dbMessages){ - auxList.add(m); - } - return null; + arrayAdapter.notifyDataSetChanged(); + handler.postDelayed(this,5000); } } diff --git a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/LoginActivity.java b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/LoginActivity.java index 6755aad..fd3acf9 100644 --- a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/LoginActivity.java +++ b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/LoginActivity.java @@ -282,7 +282,7 @@ public void onLoadFinished(Loader cursorLoader, Cursor cursor) { emails.add(cursor.getString(ProfileQuery.ADDRESS)); cursor.moveToNext(); } - + cursor.close(); addEmailsToAutoComplete(emails); } diff --git a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/RegisterActivity.java b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/RegisterActivity.java index d005592..a15a6ff 100644 --- a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/RegisterActivity.java +++ b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/RegisterActivity.java @@ -259,6 +259,7 @@ public void onLoadFinished(Loader cursorLoader, Cursor cursor) { emails.add(cursor.getString(ProfileQuery.ADDRESS)); cursor.moveToNext(); } + cursor.close(); addEmailsToAutoComplete(emails); } diff --git a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/Services/DBService.java b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/Services/DBService.java index a387050..23091e3 100644 --- a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/Services/DBService.java +++ b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/Services/DBService.java @@ -148,10 +148,12 @@ public void run() { boolean isUpdated = checkDBEqualToServerDB(); if(!isUpdated){ - //Log.d("DBService", "Local Locations DB differs from server's, clearing local DB"); - + Log.d("DBService", "Local Locations DB differs from server's, clearing local DB"); + Log.d("DBService", "dd"+dbHelper.getAllLocations().size()); dbHelper.deleteAllLocations(); + Log.d("DBService", "after this "+dbHelper.getAllLocations().size()); getAndInsertAllLocations(); + Log.d("DBService", "after "+dbHelper.getAllLocations().size()); } ////////////////---------------LOCATIONS----------------------///////////// @@ -166,13 +168,13 @@ public void run() { updateMessages(); } - handler.postDelayed(runnable, 5000); + handler.postDelayed(runnable, 20000); } }; runnableManager = Executors.newSingleThreadExecutor().submit(runnable); - handler.postDelayed(runnable, 2000); + handler.postDelayed(runnable, 500); } diff --git a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/Utils/FeedReaderDbHelper.java b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/Utils/FeedReaderDbHelper.java index f38e94c..64541bf 100644 --- a/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/Utils/FeedReaderDbHelper.java +++ b/LocMessClient/app/src/main/java/pt/ulisboa/tecnico/cmu/tg14/locmessclient/Utils/FeedReaderDbHelper.java @@ -199,8 +199,7 @@ public void createLocationTable(SQLiteDatabase db) { public void dropLocation() { SQLiteDatabase db = this.getWritableDatabase(); - if(db.isReadOnly()) - db.execSQL(SQL_DELETE_ENTRIES + FeedEntry.LOCATION_TABLE_NAME); + db.execSQL(SQL_DELETE_ENTRIES + FeedEntry.LOCATION_TABLE_NAME); } public void deleteAllLocations() { @@ -257,8 +256,9 @@ public Location getLocation(String name) throws LocationNotFoundException { } cursor.moveToFirst(); - - return associateLocation(cursor); + Location loc = associateLocation(cursor); + cursor.close(); + return loc; } public ArrayList getAllLocations() { @@ -293,6 +293,8 @@ public ArrayList getAllLocations() { locations.add(associateLocation(cursor)); cursor.moveToNext(); } + cursor.close(); + return locations; } @@ -328,6 +330,8 @@ public ArrayList getAllDescentralizedLocations() { locations.add(associateLocation(cursor)); cursor.moveToNext(); } + cursor.close(); + return locations; } @@ -359,6 +363,8 @@ public ArrayList getAllLocationsNames() { locations.add(cursor.getString(cursor.getColumnIndexOrThrow(FeedEntry.LOCATION_COLUMN_NAME))); cursor.moveToNext(); } + cursor.close(); + return locations; } @@ -404,6 +410,7 @@ public String getLocationsNameHash() throws NoSuchAlgorithmException, IOExceptio bOutput.write(digest.digest(name.getBytes("UTF-8"))); cursor.moveToNext(); } + cursor.close(); byte[] locationsNameHash = digest.digest(bOutput.toByteArray()); @@ -503,8 +510,9 @@ public Message getMessageMule(String uuid) throws MessageMuleNotFoundException { } cursor.moveToFirst(); - - return associateMessageMule(cursor); + Message msg = associateMessageMule(cursor); + cursor.close(); + return msg; } public ArrayList getAllMuleMessages() { @@ -539,6 +547,7 @@ public ArrayList getAllMuleMessages() { messages.add(associateMessageMule(cursor)); cursor.moveToNext(); } + cursor.close(); return messages; } @@ -574,6 +583,7 @@ private Message associateMessageMule(Cursor cursor) { } + return new Message( UUID.fromString(cursor.getString(cursor.getColumnIndexOrThrow(FeedEntry.MULE_MESSAGE_COLUMN_UUID))), cursor.getLong(cursor.getColumnIndexOrThrow(FeedEntry.MULE_MESSAGE_COLUMN_CREATIONTIME)), @@ -716,7 +726,7 @@ private ArrayList getMessageKeys(String uuid, Boolean getWhitelist) thr keys.add(associateMessageKeys(cursor)); cursor.moveToNext(); } - + cursor.close(); return keys; } @@ -822,8 +832,9 @@ public Message getMessage(String uuid) throws MessageNotFoundException { } cursor.moveToFirst(); - - return associateMessage(cursor); + Message temp = associateMessage(cursor); + cursor.close(); + return temp; } public List getMessagesFromUser(String publisher) throws PublisherNotFoundException { @@ -855,6 +866,7 @@ public List getMessagesFromUser(String publisher) throws PublisherNotFo messages.add(associateMessage(cursor)); cursor.moveToNext(); } + cursor.close(); return messages; } @@ -884,6 +896,7 @@ public ArrayList getAllMessages() { messages.add(associateMessage(cursor)); cursor.moveToNext(); } + cursor.close(); return messages; } @@ -910,6 +923,7 @@ public ArrayList getAllNearbyMessages() { messages.add(associateMessage(cursor)); cursor.moveToNext(); } + cursor.close(); return messages; } @@ -936,6 +950,7 @@ public ArrayList getAllMessagesDeletedWhileDecentralized() { messages.add(associateMessage(cursor)); cursor.moveToNext(); } + cursor.close(); return messages; } @@ -962,6 +977,7 @@ public ArrayList getAllMessagesAddedWhileDecentralized() { messages.add(associateMessage(cursor)); cursor.moveToNext(); } + cursor.close(); return messages; } @@ -1205,8 +1221,9 @@ public String getProfileValue(String key) throws ProfileNotFoundException { } cursor.moveToFirst(); - - return associateProfile(cursor).getValue(); + String profile = associateProfile(cursor).getValue(); + cursor.close(); + return profile; } public HashMap getAllProfiles() { @@ -1239,6 +1256,7 @@ public HashMap getAllProfiles() { profiles.put(profile.getKey(), profile.getValue()); cursor.moveToNext(); } + cursor.close(); return profiles; } @@ -1273,6 +1291,7 @@ public List getMyProfiles() { profiles.add(associateProfile(cursor)); cursor.moveToNext(); } + cursor.close(); return profiles; } @@ -1351,6 +1370,7 @@ public ArrayList getAllProfilesToRemoveFromServer() { profiles.add(associateProfile(cursor)); cursor.moveToNext(); } + cursor.close(); return profiles; } @@ -1385,6 +1405,7 @@ public ArrayList getAllProfilesAddedWhileDecentralized() { profiles.add(associateProfile(cursor)); cursor.moveToNext(); } + cursor.close(); return profiles; } @@ -1475,6 +1496,7 @@ public List getListAllServerProfiles() { profiles.add(associateServerProfiles(cursor)); cursor.moveToNext(); } + cursor.close(); return profiles; } @@ -1553,7 +1575,7 @@ public List getMuleProfileWhiteList(String uuid) throws ProfileNotFound whiteList.add(associateProfile(cursor)); cursor.moveToNext(); } - + cursor.close(); return whiteList; } @@ -1589,7 +1611,7 @@ public List getMuleProfileBlackList(String uuid) throws ProfileNotFound whiteList.add(associateProfile(cursor)); cursor.moveToNext(); } - + cursor.close(); return whiteList; }