Commit 5c953be0 authored by Lily Tsai's avatar Lily Tsai

add code to core to try and log in / fix database crashes when restarts

parent 127b0042
Subproject commit 9b0f55007d93bbe13e48f2750d0b887c880a8add Subproject commit 9d89587c13406c3fd4a52ac1f6b3067ccc3405c8
...@@ -55,6 +55,7 @@ dependencies { ...@@ -55,6 +55,7 @@ dependencies {
//implementation fileTree(include: ['*.jar'], dir: '../ebcutils/build/libs') //implementation fileTree(include: ['*.jar'], dir: '../ebcutils/build/libs')
implementation fileTree(include: ['*.jar'], dir: 'libs') implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:support-v4:26.1.0' implementation 'com.android.support:support-v4:26.1.0'
implementation 'com.android.support:design:26.1.0'
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', { androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations' exclude group: 'com.android.support', module: 'support-annotations'
}) })
......
...@@ -14,8 +14,6 @@ ...@@ -14,8 +14,6 @@
<uses-feature android:name="android.hardware.bluetooth_le" /> <uses-feature android:name="android.hardware.bluetooth_le" />
<uses-permission android:name="android.permission.VIBRATE" />
<application> <application>
<service android:name=".encounterformation.EncounterFormationService" /> <service android:name=".encounterformation.EncounterFormationService" />
...@@ -35,6 +33,7 @@ ...@@ -35,6 +33,7 @@
android:exported="true" android:exported="true"
android:readPermission="READPERM" android:readPermission="READPERM"
android:writePermission="WRITEPERM" /> android:writePermission="WRITEPERM" />
</application> </application>
</manifest> </manifest>
\ No newline at end of file
...@@ -195,9 +195,7 @@ public class EncounterBasedCommunication { ...@@ -195,9 +195,7 @@ public class EncounterBasedCommunication {
* @param context The calling application context * @param context The calling application context
*/ */
public void initialize(Context context, int configResId) { public void initialize(Context context, int configResId) {
// so we can connect to jni code to do stuff
SDDR_Native.c_mallocRadio(); SDDR_Native.c_mallocRadio();
Preferences preferences = new Preferences(context); Preferences preferences = new Preferences(context);
GlobalObjectRegistry.addObject(preferences); GlobalObjectRegistry.addObject(preferences);
......
...@@ -10,11 +10,13 @@ import android.content.UriMatcher; ...@@ -10,11 +10,13 @@ import android.content.UriMatcher;
import android.database.Cursor; import android.database.Cursor;
import android.database.SQLException; import android.database.SQLException;
import android.net.Uri; import android.net.Uri;
import android.util.Log;
import net.sqlcipher.database.SQLiteDatabase; import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteOpenHelper; import net.sqlcipher.database.SQLiteOpenHelper;
import org.mpisws.database.DBModel; import org.mpisws.database.DBModel;
import org.mpisws.embeddedsocial.ESClient;
import org.mpisws.helpers.ESCredentials; import org.mpisws.helpers.ESCredentials;
import org.mpisws.helpers.GlobalObjectRegistry; import org.mpisws.helpers.GlobalObjectRegistry;
import org.mpisws.encounters.encounterhistory.EncounterHistoryAPM; import org.mpisws.encounters.encounterhistory.EncounterHistoryAPM;
...@@ -22,6 +24,7 @@ import org.mpisws.encounters.encounterhistory.EncounterHistoryAPM; ...@@ -22,6 +24,7 @@ import org.mpisws.encounters.encounterhistory.EncounterHistoryAPM;
import java.util.ArrayList; import java.util.ArrayList;
public abstract class ContentProviderBase extends ContentProvider { public abstract class ContentProviderBase extends ContentProvider {
private final String TAG = ContentProviderBase.class.getSimpleName();
private final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); private final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
protected SQLiteOpenHelper dbHelper; protected SQLiteOpenHelper dbHelper;
...@@ -119,6 +122,7 @@ public abstract class ContentProviderBase extends ContentProvider { ...@@ -119,6 +122,7 @@ public abstract class ContentProviderBase extends ContentProvider {
@Override @Override
public synchronized int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { public synchronized int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
if (GlobalObjectRegistry.getObject(ESCredentials.class) == null) return -1; if (GlobalObjectRegistry.getObject(ESCredentials.class) == null) return -1;
Log.d(TAG, "User handle is " + GlobalObjectRegistry.getObject(ESCredentials.class).getUserHandle());
final SQLiteDatabase db = dbHelper.getWritableDatabase(GlobalObjectRegistry.getObject(ESCredentials.class).getUserHandle()); final SQLiteDatabase db = dbHelper.getWritableDatabase(GlobalObjectRegistry.getObject(ESCredentials.class).getUserHandle());
final MatchType matchType = codeToModel(matcher.match(uri)); final MatchType matchType = codeToModel(matcher.match(uri));
final String realselection = matchType.isRow() ? addPKIDConditionToWhereClause(selection, uri.getLastPathSegment()) : selection; final String realselection = matchType.isRow() ? addPKIDConditionToWhereClause(selection, uri.getLastPathSegment()) : selection;
......
...@@ -15,9 +15,12 @@ import android.util.Log; ...@@ -15,9 +15,12 @@ import android.util.Log;
import org.apache.commons.lang3.tuple.Pair; import org.apache.commons.lang3.tuple.Pair;
import org.mpisws.embeddedsocial.ESClient; import org.mpisws.embeddedsocial.ESClient;
import org.mpisws.encounters.lib.Preferences;
import org.mpisws.helpers.GlobalObjectRegistry;
import org.mpisws.helpers.Identifier; import org.mpisws.helpers.Identifier;
import org.mpisws.encounters.encounterhistory.bridges.EncounterEntriesBridge; import org.mpisws.encounters.encounterhistory.bridges.EncounterEntriesBridge;
import org.mpisws.encounters.encounterhistory.events.EncounterConfirmEvent; import org.mpisws.encounters.encounterhistory.events.EncounterConfirmEvent;
import org.mpisws.helpers.Utils;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
...@@ -57,6 +60,9 @@ public class EncounterFormationCore implements Runnable { ...@@ -57,6 +60,9 @@ public class EncounterFormationCore implements Runnable {
private void initialize() { private void initialize() {
Looper.prepare(); Looper.prepare();
if (SDDR_Native.c_RadioPtr <= 0) {
SDDR_Native.c_mallocRadio();
}
Log.v(TAG, "Initialize SDDRCore on thread " + Thread.currentThread().getName()); Log.v(TAG, "Initialize SDDRCore on thread " + Thread.currentThread().getName());
this.bluetoothManager = (BluetoothManager) mService.getSystemService(Context.BLUETOOTH_SERVICE); this.bluetoothManager = (BluetoothManager) mService.getSystemService(Context.BLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManager.getAdapter(); mBluetoothAdapter = bluetoothManager.getAdapter();
...@@ -68,6 +74,17 @@ public class EncounterFormationCore implements Runnable { ...@@ -68,6 +74,17 @@ public class EncounterFormationCore implements Runnable {
changeEpochTime = System.currentTimeMillis() + CHANGE_EPOCH_TIME; changeEpochTime = System.currentTimeMillis() + CHANGE_EPOCH_TIME;
encounterConfirmationAndEpochLinking = new EncounterConfirmationAndEpochLinking(mService); encounterConfirmationAndEpochLinking = new EncounterConfirmationAndEpochLinking(mService);
// deal with login credentials here
Preferences preferences;
if ((preferences = GlobalObjectRegistry.getObject(Preferences.class)) == null) {
preferences = new Preferences(mService);
GlobalObjectRegistry.addObject(preferences);
}
// try to get ES Credentials
Log.d(TAG, "UserHandle, Auth: " + preferences.getUserHandle() + ", " + preferences.getAuthorizationToken());
ESClient.getInstance().setESCredentials(preferences.getUserHandle(), preferences.getAuthorizationToken());
new EncounterEntriesBridge(mService).finalizeOldEncounters(); new EncounterEntriesBridge(mService).finalizeOldEncounters();
} }
...@@ -79,7 +96,8 @@ public class EncounterFormationCore implements Runnable { ...@@ -79,7 +96,8 @@ public class EncounterFormationCore implements Runnable {
} }
public void stop() { public void stop() {
//SDDR_Native.c_freeRadio(); if (SDDR_Native.c_RadioPtr > 0)
SDDR_Native.c_freeRadio();
mAdvertiser.stopAdvertising(); mAdvertiser.stopAdvertising();
mScannerProcessor.stopScanning(); mScannerProcessor.stopScanning();
} }
......
package org.mpisws.encounters.encounterformation; package org.mpisws.encounters.encounterformation;
import android.Manifest; import android.Manifest;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service; import android.app.Service;
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager; import android.bluetooth.BluetoothManager;
...@@ -15,13 +12,6 @@ import android.support.v4.app.ActivityCompat; ...@@ -15,13 +12,6 @@ import android.support.v4.app.ActivityCompat;
import android.util.Log; import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
import org.mpisws.encounters.R;
import org.mpisws.helpers.ESCredentials;
import org.mpisws.helpers.GlobalObjectRegistry;
import static android.app.NotificationChannel.DEFAULT_CHANNEL_ID;
import static android.support.v4.app.NotificationCompat.PRIORITY_LOW;
/** /**
* Created by tslilyai on 10/27/17. * Created by tslilyai on 10/27/17.
*/ */
...@@ -74,30 +64,12 @@ public class EncounterFormationService extends Service { ...@@ -74,30 +64,12 @@ public class EncounterFormationService extends Service {
@Override @Override
public int onStartCommand(Intent intent, int flags, int startID) { public int onStartCommand(Intent intent, int flags, int startID) {
/*if (GlobalObjectRegistry.getObject(ESCredentials.class) == null ||
GlobalObjectRegistry.getObject(ESCredentials.class).getUserHandle() == null) {
Intent dialogIntent = new Intent(this, Login.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialogIntent);
}*/
if (intent == null) { if (intent == null) {
return START_STICKY; return START_STICKY;
} }
if (intent.getExtras().containsKey("@string.start_sddr_service")) { if (intent.getExtras().containsKey("@string.start_sddr_service")) {
check_and_start_core(); check_and_start_core();
Intent notificationIntent = new Intent(this, getApplicationContext().getClass());
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new Notification.Builder(this, DEFAULT_CHANNEL_ID)
.setContentTitle("EbC")
.setContentText("Forming encounters")
.setSmallIcon(R.drawable.ic_ebc_formation)
.setContentIntent(pendingIntent)
.setTicker("EbCTicker?")
.build();
startForeground(FOREGROUND_SERVICE_ON, notification);
return START_STICKY; return START_STICKY;
} }
......
...@@ -184,7 +184,7 @@ public class EncounterEntriesBridge extends AbstractEncountersBridge<MEncounterE ...@@ -184,7 +184,7 @@ public class EncounterEntriesBridge extends AbstractEncountersBridge<MEncounterE
String whereClause = "(" + PEncounterEntries.Columns.timestampEnd + " > ? OR " String whereClause = "(" + PEncounterEntries.Columns.timestampEnd + " > ? OR "
+ PEncounterEntries.Columns.timestampEnd + " = -1)" + PEncounterEntries.Columns.timestampEnd + " = -1)"
+ " AND " + PEncounterEntries.Columns.sharedSecret + " IS NOT NULL"; + " AND " + PEncounterEntries.Columns.sharedSecret + " IS NOT NULL";
String sortOrder = PEncounterEntries.Columns.timestampStart + " ASC"; String sortOrder = PEncounterEntries.Columns.timestampEnd + " ASC";
final Cursor cursor = context.getContentResolver().query(EncounterHistoryAPM.getContentURI(EncounterHistoryAPM.encounterEntries), final Cursor cursor = context.getContentResolver().query(EncounterHistoryAPM.getContentURI(EncounterHistoryAPM.encounterEntries),
null, whereClause, selectionArgs, sortOrder); null, whereClause, selectionArgs, sortOrder);
List<MEncounterEntry> locs = new ArrayList(); List<MEncounterEntry> locs = new ArrayList();
......
...@@ -16,11 +16,9 @@ import org.mpisws.helpers.GlobalObjectRegistry; ...@@ -16,11 +16,9 @@ import org.mpisws.helpers.GlobalObjectRegistry;
public class Preferences { public class Preferences {
private static final String PREF_FILE_NAME = "com.microsoft.embeddedsocial.pref"; private static final String PREF_FILE_NAME = "com.microsoft.embeddedsocial.pref";
private static final String INSTANCE_ID = "instance_id";
private static final String USER_HANDLE = "userhandle"; private static final String USER_HANDLE = "userhandle";
private static final String AUTHORIZATION = "authorization"; private static final String AUTHORIZATION = "authorization";
private static final String encEntryDBTimestamp = "encEntrys"; private static final String encEntryDBTimestamp = "encEntrys";
private static final String locationDBTimestamp = "locations";
private static final String ebCKey = "ebCKey"; private static final String ebCKey = "ebCKey";
private static final String latestReadMessageCursor = "lrmc"; private static final String latestReadMessageCursor = "lrmc";
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment