A little refactor before merge to develop

This commit is contained in:
Mieszko Wrzeszczyński 2018-10-11 20:57:57 +02:00
parent ae5c8d91e2
commit 850d588744
4 changed files with 28 additions and 82 deletions

View File

@ -6,6 +6,8 @@
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-feature <uses-feature
android:name="android.hardware.location" android:name="android.hardware.location"
@ -24,19 +26,6 @@
android:theme="@style/AppTheme" android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning"> tools:ignore="GoogleAppIndexingWarning">
<receiver android:name=".utils.BroadcastLocalizationHandler"
android:enabled="true"
android:exported="false"
>
<intent-filter>
<action android:name="background.location.broadcast">
</action>
</intent-filter>
</receiver>
<activity android:name=".activity.StartupActivity" <activity android:name=".activity.StartupActivity"
android:label="@string/title_activity_startup" android:label="@string/title_activity_startup"
android:launchMode="singleInstance"> android:launchMode="singleInstance">
@ -61,7 +50,11 @@
</activity> </activity>
<service <service
android:name=".service.BackgroundLocalizationService"/> android:name=".service.BackgroundLocalizationService"
android:exported="false"
android:launchMode="singleTop"
android:enabled="true"
/>
</application> </application>

View File

@ -343,7 +343,7 @@ public class LoginActivity extends AppCompatActivity implements LoaderCallbacks<
@Override @Override
protected Boolean doInBackground(Void... params) { protected Boolean doInBackground(Void... params) {
LdapUser user = new LdapUser("admin@admin",mPassword,"admin",(isTutor)?"Tutor":"Student","string","string",mEmail); LdapUser user = new LdapUser(mEmail,mPassword,"admin",(isTutor)?"Tutor":"Student","string","string",mEmail);
disposable.add( disposable.add(
ldapService ldapService

View File

@ -42,9 +42,6 @@ public class MainActivity extends AppCompatActivity {
private MapFragment mapFragment; private MapFragment mapFragment;
private NotificationFragment notificationFragment; private NotificationFragment notificationFragment;
private ProfileFragment profileFragment; private ProfileFragment profileFragment;
public static IntentFilter backgroundLocalizationService = null;
private BroadcastReceiver broadcastReceiver = null;
private static final int REQUEST_PERMISSIONS = 100; private static final int REQUEST_PERMISSIONS = 100;
private boolean boolean_permission = false; private boolean boolean_permission = false;
@ -96,7 +93,6 @@ public class MainActivity extends AppCompatActivity {
final FloatingActionButton button = findViewById(R.id.logoutButton); final FloatingActionButton button = findViewById(R.id.logoutButton);
button.setOnClickListener(view -> { button.setOnClickListener(view -> {
PrefUtils.cleanUserLocalStorage(getApplicationContext()); PrefUtils.cleanUserLocalStorage(getApplicationContext());
unRegisterLocalizationService();
Intent i = getBaseContext().getPackageManager() Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(getBaseContext().getPackageName()); .getLaunchIntentForPackage(getBaseContext().getPackageName());
@ -108,7 +104,6 @@ public class MainActivity extends AppCompatActivity {
if (isTutor) { if (isTutor) {
fn_permission(); fn_permission();
this.broadcastReceiver = new BroadcastLocalizationHandler();
} }
} }
@ -132,18 +127,15 @@ public class MainActivity extends AppCompatActivity {
} else { } else {
if (isTutor) { if (isTutor) {
Intent intent = new Intent(getApplicationContext(), BackgroundLocalizationService.class); Intent intent = new Intent(getApplicationContext(), BackgroundLocalizationService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent); startForegroundService(intent);
} else { } else {
startService(intent); startService(intent);
} }
} else {
} else {
Toast.makeText(getApplicationContext(), "Please enable the gps", Toast.LENGTH_SHORT).show(); Toast.makeText(getApplicationContext(), "Please enable the gps", Toast.LENGTH_SHORT).show();
} }
} }
@ -157,7 +149,6 @@ public class MainActivity extends AppCompatActivity {
case REQUEST_PERMISSIONS: { case REQUEST_PERMISSIONS: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
boolean_permission = true; boolean_permission = true;
} else { } else {
Toast.makeText(getApplicationContext(), "Please allow the permission", Toast.LENGTH_LONG).show(); Toast.makeText(getApplicationContext(), "Please allow the permission", Toast.LENGTH_LONG).show();
} }
@ -168,37 +159,16 @@ public class MainActivity extends AppCompatActivity {
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
backgroundLocalizationService = new IntentFilter(BackgroundLocalizationService.str_receiver);
if (isTutor)
registerReceiver(this.broadcastReceiver, backgroundLocalizationService);
} }
@Override @Override
protected void onPause() { protected void onPause() {
super.onPause(); super.onPause();
if (isTutor)
unregisterReceiver(this.broadcastReceiver);
} }
protected void onDestroy() { protected void onDestroy() {
unRegisterLocalizationService();
super.onDestroy(); super.onDestroy();
} }
private void unRegisterLocalizationService() {
if (this.broadcastReceiver != null) {
try {
LocalBroadcastManager.getInstance(this).unregisterReceiver(broadcastReceiver);
Intent intent = new Intent(this, BackgroundLocalizationService.class);
stopService(intent);
} catch (IllegalArgumentException e) {
Log.d("Destroy app", "RECIEVER UNREGISTER ERROR");
}
}
}
} }

View File

@ -44,16 +44,16 @@ public class BackgroundLocalizationService extends Service {
private static final float LOCATION_DISTANCE = 10f; private static final float LOCATION_DISTANCE = 10f;
private Handler mHandler = new Handler(); private Handler mHandler = new Handler();
private static long notify_interval = 10000; private static long notify_interval = 10000;
double latitude, longitude;
boolean isGPSEnable = false;
boolean isNetworkEnable = false;
Location mLastLocation; Location mLastLocation;
Intent intent; Intent intent;
LocationManager locationManager; LocationManager locationManager;
Location location; Location location;
double latitude, longitude;
boolean isGPSEnable = false;
boolean isNetworkEnable = false;
private class LocationListener implements android.location.LocationListener { private class LocationListener implements android.location.LocationListener {
@ -66,7 +66,7 @@ public class BackgroundLocalizationService extends Service {
public void onLocationChanged(Location location) { public void onLocationChanged(Location location) {
Log.e(TAG, "onLocationChanged: " + location); Log.e(TAG, "onLocationChanged: " + location);
mLastLocation.set(location); mLastLocation.set(location);
sendToBroadcast(mLastLocation); fn_update(mLastLocation);
} }
@Override @Override
@ -88,7 +88,7 @@ public class BackgroundLocalizationService extends Service {
LocationListener[] mLocationListeners = new LocationListener[]{ LocationListener[] mLocationListeners = new LocationListener[]{
new LocationListener(LocationManager.GPS_PROVIDER), new LocationListener(LocationManager.GPS_PROVIDER),
new LocationListener(LocationManager.NETWORK_PROVIDER), new LocationListener(LocationManager.NETWORK_PROVIDER),
new LocationListener(LocationManager.PASSIVE_PROVIDER) //new LocationListener(LocationManager.PASSIVE_PROVIDER)
}; };
@Override @Override
@ -114,13 +114,10 @@ public class BackgroundLocalizationService extends Service {
else { else {
Notification notification = new NotificationCompat.Builder(this, "NOTIFICATION_CHANNEL") Notification notification = new NotificationCompat.Builder(this, "NOTIFICATION_CHANNEL")
.setContentText("Content").build(); .setContentText("Content").build();
startForeground(1001, notification); startForeground(1001, notification);
} }
initializeLocationManager(); initializeLocationManager();
try { try {
@ -148,14 +145,14 @@ public class BackgroundLocalizationService extends Service {
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
Log.d(TAG, "gps provider does not exist " + ex.getMessage()); Log.d(TAG, "gps provider does not exist " + ex.getMessage());
} }
Timer mTimer = new Timer(); Timer mTimer = new Timer();
mTimer.schedule(new TimerTaskToGetLocation(), 5, notify_interval); mTimer.schedule(new TimerTaskToGetLocation(), 5, notify_interval);
intent = new Intent(str_receiver); intent = new Intent(str_receiver);
} }
@RequiresApi(api = Build.VERSION_CODES.O) @RequiresApi(api = Build.VERSION_CODES.O)
private void startMyOwnForeground(){ private void startMyOwnForeground() {
String NOTIFICATION_CHANNEL_ID = "com.example.simpleapp"; String NOTIFICATION_CHANNEL_ID = "com.example.simpleapp";
String channelName = "My Background Service"; String channelName = "My Background Service";
@ -199,9 +196,9 @@ public class BackgroundLocalizationService extends Service {
return; return;
} }
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, mLocationListeners[1]); locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, mLocationListeners[1]);
if (locationManager!=null){ if (locationManager != null) {
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location!=null){ if (location != null) {
latitude = location.getLatitude(); latitude = location.getLatitude();
longitude = location.getLongitude(); longitude = location.getLongitude();
@ -237,7 +234,7 @@ public class BackgroundLocalizationService extends Service {
return; return;
} }
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location!=null){ if (location != null) {
latitude = location.getLatitude(); latitude = location.getLatitude();
longitude = location.getLongitude(); longitude = location.getLongitude();
fn_update(location); fn_update(location);
@ -252,33 +249,18 @@ public class BackgroundLocalizationService extends Service {
} }
private class TimerTaskToGetLocation extends TimerTask {
private class TimerTaskToGetLocation extends TimerTask{
@Override @Override
public void run() { public void run() {
mHandler.post(BackgroundLocalizationService.this::fn_getlocation); mHandler.post(BackgroundLocalizationService.this::fn_getlocation);
} }
} }
private void fn_update(Location location){ private void fn_update(Location location) {
intent.putExtra("latitude",location.getLatitude()); intent.putExtra("latitude", location.getLatitude());
intent.putExtra("longitude",location.getLongitude()); intent.putExtra("longitude", location.getLongitude());
Log.e(TAG, String.valueOf(location));
new Task(location).execute(); new Task(location).execute();
// sendBroadcast(intent);
}
private void sendToBroadcast(Location location) {
Log.e("sendToBroadcast", String.valueOf(location));
intent.putExtra("latitude",location.getLatitude());
intent.putExtra("longitude",location.getLongitude());
new Task(location).execute();
//sendBroadcast(intent);
}
private void getLocation() {
sendToBroadcast(mLastLocation);
} }
@Override @Override
@ -344,9 +326,10 @@ public class BackgroundLocalizationService extends Service {
Log.e("CoordinateService onSuccess", String.valueOf(coord)); Log.e("CoordinateService onSuccess", String.valueOf(coord));
} }
@SuppressLint("LongLogTag")
@Override @Override
public void onError(Throwable e) { public void onError(Throwable e) {
Log.e("LoginError", "onError: " + e.getMessage()); Log.e("CoordinateService onError", e.getMessage());
} }
})); }));
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {