Skip to content

Commit

Permalink
Solved logout issues for db clean and credentials clear
Browse files Browse the repository at this point in the history
  • Loading branch information
tcrosado committed May 18, 2017
1 parent 74a0471 commit 0e3387a
Show file tree
Hide file tree
Showing 15 changed files with 98 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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<Message> dbMessages = dbHelper.getAllNearbyMessages();

for(Message m: dbMessages){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -327,7 +328,6 @@ public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {

@Override
protected Boolean doInBackground(Void... params) {
// TODO: attempt authentication against a network service.

try {

Expand All @@ -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);

Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand All @@ -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("");


}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> dbLocationNames = dbHelper.getAllLocationsNames();

for(String s: dbLocationNames){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Profile> profilesList = dbHelper.getMyProfiles();

for(Profile p: profilesList){
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -50,19 +52,28 @@ public class DBService extends Service implements OnResponseListener<String> {
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()) {
Expand All @@ -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");

Expand Down Expand Up @@ -160,6 +170,8 @@ public void run() {
}
};

runnableManager = Executors.newSingleThreadExecutor().submit(runnable);

handler.postDelayed(runnable, 2000);

}
Expand Down Expand Up @@ -429,8 +441,7 @@ private ArrayList<Location> 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<List<Location>>() {
@Override
public void onHTTPResponse(List<Location> response) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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() {
Expand All @@ -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);
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -1465,8 +1476,8 @@ public List<Profile> 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) {
Expand Down Expand Up @@ -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();
}
}
Loading

0 comments on commit 0e3387a

Please sign in to comment.