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"> - - - - 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) { @@ -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()); @@ -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 = new FeedReaderDbHelper(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 8b8b8cd..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 @@ -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; @@ -281,7 +282,7 @@ public void onLoadFinished(Loader cursorLoader, Cursor cursor) { emails.add(cursor.getString(ProfileQuery.ADDRESS)); cursor.moveToNext(); } - + cursor.close(); addEmailsToAutoComplete(emails); } @@ -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 e239291..7c02ca0 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.Network.ServerActions; @@ -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,9 +230,16 @@ public void onFragmentInteraction(Uri uri) { private void logout() { // delete all sensitive data from database // TODO : do I need to stop services? - // TODO : >>>>> PUT THIS ON ASSYNCTASK <<<<< - new FeedReaderDbHelper(getApplicationContext()).dropDatabase(activity); - new ServerActions(activity).logout(); + 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 4264436..c3a430d 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 3dd3686..7300d7e 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/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 9b22d9d..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 @@ -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"); @@ -138,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----------------------///////////// @@ -156,11 +168,13 @@ public void run() { updateMessages(); } - handler.postDelayed(runnable, 5000); + handler.postDelayed(runnable, 20000); } }; - handler.postDelayed(runnable, 2000); + runnableManager = Executors.newSingleThreadExecutor().submit(runnable); + + handler.postDelayed(runnable, 500); } @@ -429,8 +443,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 2113884..1a86fa4 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,7 +199,6 @@ public void createLocationTable(SQLiteDatabase db) { public void dropLocation() { SQLiteDatabase db = this.getWritableDatabase(); - db.execSQL(SQL_DELETE_ENTRIES + FeedEntry.LOCATION_TABLE_NAME); } @@ -209,7 +217,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); } @@ -247,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() { @@ -283,6 +293,8 @@ public ArrayList getAllLocations() { locations.add(associateLocation(cursor)); cursor.moveToNext(); } + cursor.close(); + return locations; } @@ -318,6 +330,8 @@ public ArrayList getAllDescentralizedLocations() { locations.add(associateLocation(cursor)); cursor.moveToNext(); } + cursor.close(); + return locations; } @@ -349,6 +363,8 @@ public ArrayList getAllLocationsNames() { locations.add(cursor.getString(cursor.getColumnIndexOrThrow(FeedEntry.LOCATION_COLUMN_NAME))); cursor.moveToNext(); } + cursor.close(); + return locations; } @@ -394,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()); @@ -493,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() { @@ -529,6 +547,7 @@ public ArrayList getAllMuleMessages() { messages.add(associateMessageMule(cursor)); cursor.moveToNext(); } + cursor.close(); return messages; } @@ -564,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)), @@ -705,7 +725,7 @@ private ArrayList getMessageKeys(String uuid, Boolean getWhitelist) thr keys.add(associateMessageKeys(cursor)); cursor.moveToNext(); } - + cursor.close(); return keys; } @@ -763,9 +783,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 { @@ -812,8 +831,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 { @@ -845,6 +865,7 @@ public List getMessagesFromUser(String publisher) throws PublisherNotFo messages.add(associateMessage(cursor)); cursor.moveToNext(); } + cursor.close(); return messages; } @@ -874,6 +895,7 @@ public ArrayList getAllMessages() { messages.add(associateMessage(cursor)); cursor.moveToNext(); } + cursor.close(); return messages; } @@ -900,6 +922,7 @@ public ArrayList getAllNearbyMessages() { messages.add(associateMessage(cursor)); cursor.moveToNext(); } + cursor.close(); return messages; } @@ -926,6 +949,7 @@ public ArrayList getAllMessagesDeletedWhileDecentralized() { messages.add(associateMessage(cursor)); cursor.moveToNext(); } + cursor.close(); return messages; } @@ -952,6 +976,7 @@ public ArrayList getAllMessagesAddedWhileDecentralized() { messages.add(associateMessage(cursor)); cursor.moveToNext(); } + cursor.close(); return messages; } @@ -988,7 +1013,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); @@ -1193,8 +1220,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() { @@ -1227,6 +1255,7 @@ public HashMap getAllProfiles() { profiles.put(profile.getKey(), profile.getValue()); cursor.moveToNext(); } + cursor.close(); return profiles; } @@ -1261,6 +1290,7 @@ public List getMyProfiles() { profiles.add(associateProfile(cursor)); cursor.moveToNext(); } + cursor.close(); return profiles; } @@ -1339,6 +1369,7 @@ public ArrayList getAllProfilesToRemoveFromServer() { profiles.add(associateProfile(cursor)); cursor.moveToNext(); } + cursor.close(); return profiles; } @@ -1373,6 +1404,7 @@ public ArrayList getAllProfilesAddedWhileDecentralized() { profiles.add(associateProfile(cursor)); cursor.moveToNext(); } + cursor.close(); return profiles; } @@ -1463,13 +1495,14 @@ public List getListAllServerProfiles() { profiles.add(associateServerProfiles(cursor)); cursor.moveToNext(); } + cursor.close(); return profiles; } 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) { @@ -1541,7 +1574,7 @@ public List getMuleProfileWhiteList(String uuid) throws ProfileNotFound whiteList.add(associateProfile(cursor)); cursor.moveToNext(); } - + cursor.close(); return whiteList; } @@ -1577,7 +1610,7 @@ public List getMuleProfileBlackList(String uuid) throws ProfileNotFound whiteList.add(associateProfile(cursor)); cursor.moveToNext(); } - + cursor.close(); return whiteList; } @@ -1597,6 +1630,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 912980d..6742aaf 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.220.63"; + private final static String addr = "193.136.167.224"; 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 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; } 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