Skip to content

Commit

Permalink
Merge pull request #53 from Zameelnm96/master
Browse files Browse the repository at this point in the history
Some exception handled
  • Loading branch information
Zameelnm96 authored May 29, 2020
2 parents 03b227f + 2e28de7 commit d66569c
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import android.view.animation.AnimationUtils;
import android.widget.ImageView;


import com.example.acmcovidapplication.db.DatabaseHelper;
import com.google.firebase.auth.FirebaseAuth;

Expand Down Expand Up @@ -40,7 +39,6 @@ protected void onCreate(Bundle savedInstanceState) {
public void run() {

if (FirebaseAuth.getInstance().getCurrentUser() != null) {
String IS_ALLOWED = getString(R.string.is_allowed);
boolean accepted = DatabaseHelper.getInstance(MainActivity.this).isAllowed();

if(accepted){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}

//add the new note
public void addDevice(String userId,double latitude,double longitude) {
public void addDevice(String userId, double latitude, double longitude) {

String select_query = "SELECT * FROM " + TEMPORARY_LOG_TABLE_NAME + " WHERE USER_ID = '" + userId + "'";

Expand All @@ -110,41 +110,40 @@ public void addDevice(String userId,double latitude,double longitude) {
Cursor cursor = db.rawQuery(select_query, null);

if (cursor.moveToFirst()) {
long result = cursor.getInt(1);
long now = (long)System.currentTimeMillis()/10000;
long difference = now -result;

if (difference> update_time * 6) {

DeviceModel deviceModel = new DeviceModel();
deviceModel.setUserID(userId);
if(latitude != 0 && longitude != 0) {
deviceModel.setLatitude(latitude);
deviceModel.setLongitude(longitude);
}
new InsertDeviceAsync(this).execute(deviceModel);
ContentValues cv = new ContentValues();
cv.put("USER_ID", userId);
cv.put("TIMESTAMP_UP",(long)(System.currentTimeMillis()/10000));
db.update(TEMPORARY_LOG_TABLE_NAME, cv, " USER_ID = '" + userId + "'", null);
Log.d(TAG, "addDevice: log table updated");

} else {

Log.d(TAG, "addDevice: no need to insert");
long result = cursor.getInt(1);
long now = (long) System.currentTimeMillis() / 10000;
long difference = now - result;

if (difference > update_time * 6) {

DeviceModel deviceModel = new DeviceModel();
deviceModel.setUserID(userId);
if (latitude != 0 && longitude != 0) {
deviceModel.setLatitude(latitude);
deviceModel.setLongitude(longitude);
}
new InsertDeviceAsync(this).execute(deviceModel);
ContentValues cv = new ContentValues();
cv.put("USER_ID", userId);
cv.put("TIMESTAMP_UP", (long) (System.currentTimeMillis() / 10000));
db.update(TEMPORARY_LOG_TABLE_NAME, cv, " USER_ID = '" + userId + "'", null);
Log.d(TAG, "addDevice: log table updated");

} else {

Log.d(TAG, "addDevice: no need to insert");
}


} else {
ContentValues cv = new ContentValues();
cv.put("USER_ID", userId);
cv.put("TIMESTAMP_UP",(long)(System.currentTimeMillis()/10000));
cv.put("TIMESTAMP_UP", (long) (System.currentTimeMillis() / 10000));
Log.d(TAG, "addDevice: added to log table");
db.insert(TEMPORARY_LOG_TABLE_NAME, null, cv);
DeviceModel deviceModel= new DeviceModel();
DeviceModel deviceModel = new DeviceModel();
deviceModel.setUserID(userId);
if(latitude != 0 && longitude != 0) {
if (latitude != 0 && longitude != 0) {
deviceModel.setLatitude(latitude);
deviceModel.setLongitude(longitude);
}
Expand Down Expand Up @@ -175,7 +174,7 @@ public ArrayList<DeviceModel> getDevices() {
deviceModel.setID(cursor.getInt(cursor.getColumnIndex("ID")));
deviceModel.setUserID(cursor.getString(1));
deviceModel.setTimeStamp(cursor.getString(2));
deviceModel.setLatitude(cursor.getDouble(cursor.getColumnIndex("LATITUDE")) );
deviceModel.setLatitude(cursor.getDouble(cursor.getColumnIndex("LATITUDE")));
deviceModel.setLongitude(cursor.getDouble(cursor.getColumnIndex("LONGITUDE")));
arrayList.add(deviceModel);
} while (cursor.moveToNext());
Expand Down Expand Up @@ -208,7 +207,7 @@ public void deleteAllDevice() {
Log.d(TAG, "deleteAllDevice: called from alarm manager");
}

public void deleteAllTempData(){
public void deleteAllTempData() {
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
sqLiteDatabase.execSQL("DELETE FROM " + TEMPORARY_LOG_TABLE_NAME);
Log.d(TAG, "deleteAllTempData: called from by alarm manager");
Expand Down Expand Up @@ -237,7 +236,8 @@ public void insertLocationTrackable(boolean allowed) {

sqLiteDatabase.update(APP_DATA_TABLE_NAME, newValues, "ID=1", null);
}
public void insertUserId(String user_id) {

public void insertUserId(String user_id) {
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues newValues = new ContentValues();
newValues.put("USER_ID", user_id);
Expand All @@ -248,7 +248,6 @@ public void insertUserId(String user_id) {
}



public String getUserId() {
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery("SELECT * FROM " + APP_DATA_TABLE_NAME + " WHERE ID = 1", null);
Expand Down Expand Up @@ -283,7 +282,6 @@ public boolean isLocationTrackable() {
}



private static class DeleteDeviceAsync extends AsyncTask<Integer, Void, Void> {
DatabaseHelper database;

Expand Down Expand Up @@ -317,10 +315,8 @@ protected Void doInBackground(DeviceModel... deviceModels) {
ContentValues values = new ContentValues();
values.put("USERID", deviceModels[0].getUserID());

values.put("LATITUDE",deviceModels[0].getLatitude());
values.put("LONGITUDE",deviceModels[0].getLongitude());


values.put("LATITUDE", deviceModels[0].getLatitude());
values.put("LONGITUDE", deviceModels[0].getLongitude());


//inserting new row
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
Expand All @@ -18,6 +19,7 @@
import static com.example.acmcovidapplication.Util.isMyServiceRunning;

public class permission_list extends AppCompatActivity implements Switch.OnCheckedChangeListener {
private static final String TAG = "permission_list" ;
Switch aSwitch;
@Override
protected void onCreate(Bundle savedInstanceState) {
Expand Down Expand Up @@ -51,13 +53,16 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
@Override
protected void onResume() {
super.onResume();
Log.d(TAG, "onResume: is running");
if (!EasyPermissions.hasPermissions(this, Util.getPermissions()) ) {
DatabaseHelper.getInstance(this).insertAllowed(false);
stopService(new Intent(this,CustomService.class));
Intent intent = new Intent(this, appPermission.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);

} else if( !isMyServiceRunning(this,CustomService.class)) {
Log.d(TAG, "onResume: service is not running");
Intent serviceIntent = new Intent(this, CustomService.class);

ContextCompat.startForegroundService(this, serviceIntent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,12 +222,12 @@ public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onCreate: bluetooth is disabled");
setBluetooth(true);
} else {
if (database_helper.isAllowed()
if (DatabaseHelper.getInstance(this).isAllowed()
&& deviceId != null) {
setupBeacon(deviceId);
} else {
if (deviceId == null) {
deviceId = database_helper.getUserId();
if (DatabaseHelper.getInstance(this).isAllowed() && deviceId == null) {
deviceId = DatabaseHelper.getInstance(this).getUserId();
setupBeacon(deviceId);
} else {
stopSelf();
Expand All @@ -242,7 +242,14 @@ public int onStartCommand(Intent intent, int flags, int startId) {
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(mReceiver);
Log.d(TAG, "onDestroy: called");
try {
unregisterReceiver(mReceiver);
}
catch (IllegalArgumentException iae){
Log.d(TAG, "onDestroy: not registered"); // check how this cause
}

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent, 0);
Expand All @@ -253,6 +260,7 @@ public void onDestroy() {
scheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);
scheduler.cancel(123);
}
beaconManager.unbind(this);
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.os.Bundle;
import android.view.View;

import com.example.acmcovidapplication.db.DatabaseHelper;
import com.example.acmcovidapplication.services.CustomService;

import androidx.appcompat.app.AppCompatActivity;
Expand Down Expand Up @@ -37,6 +38,7 @@ public void shareApp(View view) {
protected void onResume() {
super.onResume();
if (!EasyPermissions.hasPermissions(this, permissions)) {
DatabaseHelper.getInstance(this).insertAllowed(false);
stopService(new Intent(this, CustomService.class));
Intent intent = new Intent(this, appPermission.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
Expand Down

0 comments on commit d66569c

Please sign in to comment.