Merge background task into develop #14

Merged
s416204 merged 17 commits from background-to-merge into develop 2018-10-12 00:12:34 +02:00
8 changed files with 153 additions and 1259 deletions
Showing only changes of commit 1773b09aa3 - Show all commits

View File

@ -7,6 +7,13 @@
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature
android:name="android.hardware.location"
android:required="true" />
<uses-feature
android:name="android.hardware.location.gps"
android:required="true" />
<application
android:allowBackup="true"

View File

@ -1,5 +1,6 @@
package com.uam.wmi.findmytutor.activity;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
@ -16,6 +17,7 @@ import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;
import com.uam.wmi.findmytutor.service.BackgroundLocalizationService;
import android.support.design.widget.FloatingActionButton;
@ -28,7 +30,6 @@ import android.view.MenuItem;
import com.mapbox.mapboxsdk.Mapbox;
import com.uam.wmi.findmytutor.R;
import com.uam.wmi.findmytutor.service.GoogleService;
import com.uam.wmi.findmytutor.utils.BroadcastLocalizationHandler;
import com.uam.wmi.findmytutor.utils.PrefUtils;
@ -94,12 +95,12 @@ public class MainActivity extends AppCompatActivity {
final FloatingActionButton button = findViewById(R.id.logoutButton);
button.setOnClickListener(view -> {
PrefUtils.cleanUserLocalStorage(getApplicationContext());
unRegisterLocalizationService();
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(getBaseContext().getPackageName());
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
finish();
unRegisterLocalizationService();
startActivity(i);
});
@ -128,12 +129,12 @@ public class MainActivity extends AppCompatActivity {
REQUEST_PERMISSIONS);
}
} else {
boolean_permission = true;
if (boolean_permission && isTutor) {
if (!PrefUtils.isLoggedIn(getApplicationContext())) {
Intent intent = new Intent(getApplicationContext(), GoogleService.class);
if (isTutor) {
Intent intent = new Intent(getApplicationContext(), BackgroundLocalizationService.class);
startService(intent);
}
} else {
Toast.makeText(getApplicationContext(), "Please enable the gps", Toast.LENGTH_SHORT).show();
}
@ -159,7 +160,7 @@ public class MainActivity extends AppCompatActivity {
@Override
protected void onResume() {
super.onResume();
backgroundLocalizationService = new IntentFilter(GoogleService.str_receiver);
backgroundLocalizationService = new IntentFilter(BackgroundLocalizationService.str_receiver);
if (isTutor)
registerReceiver(this.broadcastReceiver, backgroundLocalizationService);
@ -182,7 +183,8 @@ public class MainActivity extends AppCompatActivity {
if (this.broadcastReceiver != null) {
try {
LocalBroadcastManager.getInstance(this).unregisterReceiver(broadcastReceiver);
Intent intent = new Intent(this, GoogleService.class);
Intent intent = new Intent(this, BackgroundLocalizationService.class);
stopService(intent);
} catch (IllegalArgumentException e) {
Log.d("Destroy app", "RECIEVER UNREGISTER ERROR");
@ -190,3 +192,7 @@ public class MainActivity extends AppCompatActivity {
}
}
}

View File

@ -1,402 +0,0 @@
package com.uam.wmi.findmytutor.activity;
/*
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;
import com.uam.wmi.findmytutor.service.BackgroundLocalizationService;
import android.support.design.widget.FloatingActionButton;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.support.annotation.NonNull;
import android.view.MenuItem;
import com.mapbox.mapboxsdk.Mapbox;
import com.uam.wmi.findmytutor.R;
import com.uam.wmi.findmytutor.utils.BroadcastLocalizationHandler;
import com.uam.wmi.findmytutor.utils.PrefUtils;
public class MainActivity extends AppCompatActivity {
private BottomNavigationView mMainNav;
private FrameLayout mMainFrame;
private boolean isTutor;
private MapFragment mapFragment;
private NotificationFragment notificationFragment;
private ProfileFragment profileFragment;
public static IntentFilter backgroundLocalizationService = null;
private BroadcastReceiver broadcastReceiver = null;
private static final int REQUEST_PERMISSIONS = 100;
private boolean boolean_permission = false;
<<<<<<< HEAD
Double latitude, longitude;
*/
/* CompositeDisposable disposable = new CompositeDisposable();
CoordinateService coordinateService = ApiClient.getClient(getApplicationContext())
.create(CoordinateService.class);*//*
=======
>>>>>>> 182e3e473e48046200ec4a0bc047a3386e258f18
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Mapbox.getInstance(this, getString(R.string.access_token));
setContentView(R.layout.activity_main);
mMainFrame = (FrameLayout) findViewById(R.id.main_frame);
mMainNav = (BottomNavigationView) findViewById(R.id.main_nav);
isTutor = PrefUtils.getIsTutor(getApplicationContext());
if (!isTutor) {
mMainNav.findViewById(R.id.nav_profile).setVisibility(View.GONE);
}
mapFragment = new MapFragment();
notificationFragment = new NotificationFragment();
profileFragment = new ProfileFragment();
// Default frag here
setFragment(mapFragment);
mMainNav.setSelectedItemId(R.id.nav_map);
mMainNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_map:
setFragment(mapFragment);
return true;
case R.id.nav_notif:
setFragment(notificationFragment);
return true;
case R.id.nav_profile:
setFragment(profileFragment);
return true;
default:
return false;
}
}
});
// Logout button
final FloatingActionButton button = findViewById(R.id.logoutButton);
button.setOnClickListener(view -> {
PrefUtils.cleanUserLocalStorage(getApplicationContext());
unRegisterLocalizationService();
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(getBaseContext().getPackageName());
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
finish();
startActivity(i);
});
if (isTutor) {
fn_permission();
this.broadcastReceiver = new BroadcastLocalizationHandler();
}
}
private void setFragment(Fragment fragment) {
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_frame, fragment);
fragmentTransaction.commit();
}
private void fn_permission() {
if ((ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
if ((ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION))) {
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION
},
REQUEST_PERMISSIONS);
}
} else {
boolean_permission = true;
if (boolean_permission) {
if (!PrefUtils.isLoggedIn(getApplicationContext())) {
Intent intent = new Intent(getApplicationContext(), BackgroundLocalizationService.class);
startService(intent);
}
} else {
Toast.makeText(getApplicationContext(), "Please enable the gps", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_PERMISSIONS: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
boolean_permission = true;
} else {
Toast.makeText(getApplicationContext(), "Please allow the permission", Toast.LENGTH_LONG).show();
}
}
}
}
@Override
protected void onResume() {
super.onResume();
backgroundLocalizationService = new IntentFilter(BackgroundLocalizationService.str_receiver);
if (isTutor)
registerReceiver(this.broadcastReceiver, backgroundLocalizationService);
}
@Override
protected void onPause() {
super.onPause();
if (isTutor)
unregisterReceiver(this.broadcastReceiver);
}
protected void onDestroy() {
unRegisterLocalizationService();
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");
}
}
}
}
*/
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;
import android.support.design.widget.FloatingActionButton;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.support.annotation.NonNull;
import android.view.MenuItem;
import com.mapbox.mapboxsdk.Mapbox;
import com.uam.wmi.findmytutor.R;
import com.uam.wmi.findmytutor.service.GoogleService;
import com.uam.wmi.findmytutor.utils.BroadcastLocalizationHandler;
import com.uam.wmi.findmytutor.utils.PrefUtils;
public class MainActivity extends AppCompatActivity {
private BottomNavigationView mMainNav;
private FrameLayout mMainFrame;
private boolean isTutor;
private MapFragment mapFragment;
private NotificationFragment notificationFragment;
private ProfileFragment profileFragment;
public static IntentFilter backgroundLocalizationService = null;
private BroadcastReceiver broadcastReceiver = null;
private static final int REQUEST_PERMISSIONS = 100;
private boolean boolean_permission = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Mapbox.getInstance(this, getString(R.string.access_token));
setContentView(R.layout.activity_main);
mMainFrame = (FrameLayout) findViewById(R.id.main_frame);
mMainNav = (BottomNavigationView) findViewById(R.id.main_nav);
isTutor = PrefUtils.getIsTutor(getApplicationContext());
if (!isTutor) {
mMainNav.findViewById(R.id.nav_profile).setVisibility(View.GONE);
}
mapFragment = new MapFragment();
notificationFragment = new NotificationFragment();
profileFragment = new ProfileFragment();
// Default frag here
setFragment(mapFragment);
mMainNav.setSelectedItemId(R.id.nav_map);
mMainNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_map:
setFragment(mapFragment);
return true;
case R.id.nav_notif:
setFragment(notificationFragment);
return true;
case R.id.nav_profile:
setFragment(profileFragment);
return true;
default:
return false;
}
}
});
// Logout button
final FloatingActionButton button = findViewById(R.id.logoutButton);
button.setOnClickListener(view -> {
PrefUtils.cleanUserLocalStorage(getApplicationContext());
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(getBaseContext().getPackageName());
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
finish();
unRegisterLocalizationService();
startActivity(i);
});
if (isTutor) {
fn_permission();
this.broadcastReceiver = new BroadcastLocalizationHandler();
}
}
private void setFragment(Fragment fragment) {
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_frame, fragment);
fragmentTransaction.commit();
}
private void fn_permission() {
if ((ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
if ((ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION))) {
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION
},
REQUEST_PERMISSIONS);
}
} else {
boolean_permission = true;
if (boolean_permission && isTutor) {
if (!PrefUtils.isLoggedIn(getApplicationContext())) {
Intent intent = new Intent(getApplicationContext(), GoogleService.class);
startService(intent);
}
} else {
Toast.makeText(getApplicationContext(), "Please enable the gps", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_PERMISSIONS: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
boolean_permission = true;
} else {
Toast.makeText(getApplicationContext(), "Please allow the permission", Toast.LENGTH_LONG).show();
}
}
}
}
@Override
protected void onResume() {
super.onResume();
backgroundLocalizationService = new IntentFilter(GoogleService.str_receiver);
if (isTutor)
registerReceiver(this.broadcastReceiver, backgroundLocalizationService);
}
@Override
protected void onPause() {
super.onPause();
if (isTutor)
unregisterReceiver(this.broadcastReceiver);
}
protected void onDestroy() {
unRegisterLocalizationService();
super.onDestroy();
}
private void unRegisterLocalizationService() {
if (this.broadcastReceiver != null) {
try {
LocalBroadcastManager.getInstance(this).unregisterReceiver(broadcastReceiver);
Intent intent = new Intent(this, GoogleService.class);
stopService(intent);
} catch (IllegalArgumentException e) {
Log.d("Destroy app", "RECIEVER UNREGISTER ERROR");
}
}
}
}

View File

@ -1,217 +0,0 @@
package com.uam.wmi.findmytutor.activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;
import com.uam.wmi.findmytutor.model.Coordinate;
import com.uam.wmi.findmytutor.model.LdapUser;
import com.uam.wmi.findmytutor.network.ApiClient;
import com.uam.wmi.findmytutor.service.CoordinateService;
import com.uam.wmi.findmytutor.service.GoogleService;
import android.support.design.widget.FloatingActionButton;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.support.annotation.NonNull;
import android.view.MenuItem;
import com.mapbox.mapboxsdk.Mapbox;
import com.uam.wmi.findmytutor.R;
import com.uam.wmi.findmytutor.service.LdapService;
import com.uam.wmi.findmytutor.service.LocationService;
import com.uam.wmi.findmytutor.utils.BroadcastLocalizationHandler;
import com.uam.wmi.findmytutor.utils.PrefUtils;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import java.io.IOException;
import java.util.List;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.observers.DisposableSingleObserver;
import io.reactivex.schedulers.Schedulers;
import timber.log.Timber;
public class MainActivity extends AppCompatActivity {
private BottomNavigationView mMainNav;
private FrameLayout mMainFrame;
private boolean isTutor;
private MapFragment mapFragment;
private NotificationFragment notificationFragment;
private ProfileFragment profileFragment;
public static Intent backgroundLocalizationService = null;
;
private BroadcastReceiver broadcastReceiver = null;
private static final int REQUEST_PERMISSIONS = 100;
private boolean boolean_permission = false;
Double latitude, longitude;
/* CompositeDisposable disposable = new CompositeDisposable();
CoordinateService coordinateService = ApiClient.getClient(getApplicationContext())
.create(CoordinateService.class);*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Mapbox.getInstance(this, getString(R.string.access_token));
setContentView(R.layout.activity_main);
mMainFrame = (FrameLayout) findViewById(R.id.main_frame);
mMainNav = (BottomNavigationView) findViewById(R.id.main_nav);
isTutor = PrefUtils.getIsTutor(getApplicationContext());
if (!isTutor) {
mMainNav.findViewById(R.id.nav_profile).setVisibility(View.GONE);
}
mapFragment = new MapFragment();
notificationFragment = new NotificationFragment();
profileFragment = new ProfileFragment();
// Default frag here
setFragment(mapFragment);
mMainNav.setSelectedItemId(R.id.nav_map);
mMainNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_map:
setFragment(mapFragment);
return true;
case R.id.nav_notif:
setFragment(notificationFragment);
return true;
case R.id.nav_profile:
setFragment(profileFragment);
return true;
default:
return false;
}
}
});
// Logout button
final FloatingActionButton button = findViewById(R.id.logoutButton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PrefUtils.storeIsServiceRunning(getApplicationContext(), false);
//stopService(MainActivity.backgroundLocalizationService);
SharedPreferences preferences = getSharedPreferences("fmtPrefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.clear().commit();
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
}
});
fn_permission();
this.broadcastReceiver = new BroadcastLocalizationHandler();
}
private void setFragment(Fragment fragment) {
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_frame, fragment);
fragmentTransaction.commit();
}
private void fn_permission() {
if ((ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
if ((ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION))) {
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION
},
REQUEST_PERMISSIONS);
}
} else {
boolean_permission = true;
if (boolean_permission) {
if (!PrefUtils.getIsServiceRunning(getApplicationContext())) {
Intent intent = new Intent(getApplicationContext(), GoogleService.class);
startService(intent);
} else {
Toast.makeText(getApplicationContext(), "Service is already running", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext(), "Please enable the gps", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_PERMISSIONS: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
boolean_permission = true;
} else {
Toast.makeText(getApplicationContext(), "Please allow the permission", Toast.LENGTH_LONG).show();
}
}
}
}
@Override
protected void onResume() {
super.onResume();
registerReceiver(this.broadcastReceiver, new IntentFilter(GoogleService.str_receiver));
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(this.broadcastReceiver);
}
}

View File

@ -1,416 +0,0 @@
package com.uam.wmi.findmytutor.activity;
/*
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;
import com.uam.wmi.findmytutor.model.Coordinate;
import com.uam.wmi.findmytutor.model.LdapUser;
import com.uam.wmi.findmytutor.network.ApiClient;
import com.uam.wmi.findmytutor.service.CoordinateService;
import com.uam.wmi.findmytutor.service.GoogleService;
import android.support.design.widget.FloatingActionButton;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.support.annotation.NonNull;
import android.view.MenuItem;
import com.mapbox.mapboxsdk.Mapbox;
import com.uam.wmi.findmytutor.R;
import com.uam.wmi.findmytutor.service.LdapService;
import com.uam.wmi.findmytutor.service.LocationService;
import com.uam.wmi.findmytutor.utils.BroadcastLocalizationHandler;
import com.uam.wmi.findmytutor.utils.PrefUtils;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import java.io.IOException;
import java.util.List;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.observers.DisposableSingleObserver;
import io.reactivex.schedulers.Schedulers;
import timber.log.Timber;
public class MainActivity extends AppCompatActivity {
private BottomNavigationView mMainNav;
private FrameLayout mMainFrame;
private boolean isTutor;
private MapFragment mapFragment;
private NotificationFragment notificationFragment;
private ProfileFragment profileFragment;
public static Intent backgroundLocalizationService = null;
;
private BroadcastReceiver broadcastReceiver = null;
private static final int REQUEST_PERMISSIONS = 100;
private boolean boolean_permission = false;
Double latitude, longitude;
*/
/* CompositeDisposable disposable = new CompositeDisposable();
CoordinateService coordinateService = ApiClient.getClient(getApplicationContext())
.create(CoordinateService.class);*//*
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Mapbox.getInstance(this, getString(R.string.access_token));
setContentView(R.layout.activity_main);
mMainFrame = (FrameLayout) findViewById(R.id.main_frame);
mMainNav = (BottomNavigationView) findViewById(R.id.main_nav);
isTutor = PrefUtils.getIsTutor(getApplicationContext());
if (!isTutor) {
mMainNav.findViewById(R.id.nav_profile).setVisibility(View.GONE);
}
mapFragment = new MapFragment();
notificationFragment = new NotificationFragment();
profileFragment = new ProfileFragment();
// Default frag here
setFragment(mapFragment);
mMainNav.setSelectedItemId(R.id.nav_map);
mMainNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_map:
setFragment(mapFragment);
return true;
case R.id.nav_notif:
setFragment(notificationFragment);
return true;
case R.id.nav_profile:
setFragment(profileFragment);
return true;
default:
return false;
}
}
});
// Logout button
final FloatingActionButton button = findViewById(R.id.logoutButton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
PrefUtils.storeIsServiceRunning(getApplicationContext(), false);
//stopService(MainActivity.backgroundLocalizationService);
SharedPreferences preferences = getSharedPreferences("fmtPrefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.clear().commit();
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
}
});
fn_permission();
this.broadcastReceiver = new BroadcastLocalizationHandler();
}
private void setFragment(Fragment fragment) {
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_frame, fragment);
fragmentTransaction.commit();
}
private void fn_permission() {
if ((ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
if ((ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION))) {
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION
},
REQUEST_PERMISSIONS);
}
} else {
boolean_permission = true;
if (boolean_permission) {
if (!PrefUtils.getIsServiceRunning(getApplicationContext())) {
Intent intent = new Intent(getApplicationContext(), GoogleService.class);
startService(intent);
} else {
Toast.makeText(getApplicationContext(), "Service is already running", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext(), "Please enable the gps", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_PERMISSIONS: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
boolean_permission = true;
} else {
Toast.makeText(getApplicationContext(), "Please allow the permission", Toast.LENGTH_LONG).show();
}
}
}
}
@Override
protected void onResume() {
super.onResume();
registerReceiver(this.broadcastReceiver, new IntentFilter(GoogleService.str_receiver));
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(this.broadcastReceiver);
}
}
*/
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;
import android.support.design.widget.FloatingActionButton;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.support.annotation.NonNull;
import android.view.MenuItem;
import com.mapbox.mapboxsdk.Mapbox;
import com.uam.wmi.findmytutor.R;
import com.uam.wmi.findmytutor.service.GoogleService;
import com.uam.wmi.findmytutor.utils.BroadcastLocalizationHandler;
import com.uam.wmi.findmytutor.utils.PrefUtils;
public class MainActivity extends AppCompatActivity {
private BottomNavigationView mMainNav;
private FrameLayout mMainFrame;
private boolean isTutor;
private MapFragment mapFragment;
private NotificationFragment notificationFragment;
private ProfileFragment profileFragment;
public static IntentFilter backgroundLocalizationService = null;
private BroadcastReceiver broadcastReceiver = null;
private static final int REQUEST_PERMISSIONS = 100;
private boolean boolean_permission = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Mapbox.getInstance(this, getString(R.string.access_token));
setContentView(R.layout.activity_main);
mMainFrame = (FrameLayout) findViewById(R.id.main_frame);
mMainNav = (BottomNavigationView) findViewById(R.id.main_nav);
isTutor = PrefUtils.getIsTutor(getApplicationContext());
if (!isTutor) {
mMainNav.findViewById(R.id.nav_profile).setVisibility(View.GONE);
}
mapFragment = new MapFragment();
notificationFragment = new NotificationFragment();
profileFragment = new ProfileFragment();
// Default frag here
setFragment(mapFragment);
mMainNav.setSelectedItemId(R.id.nav_map);
mMainNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_map:
setFragment(mapFragment);
return true;
case R.id.nav_notif:
setFragment(notificationFragment);
return true;
case R.id.nav_profile:
setFragment(profileFragment);
return true;
default:
return false;
}
}
});
// Logout button
final FloatingActionButton button = findViewById(R.id.logoutButton);
button.setOnClickListener(view -> {
PrefUtils.cleanUserLocalStorage(getApplicationContext());
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(getBaseContext().getPackageName());
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
finish();
unRegisterLocalizationService();
startActivity(i);
});
if (isTutor) {
fn_permission();
this.broadcastReceiver = new BroadcastLocalizationHandler();
}
}
private void setFragment(Fragment fragment) {
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_frame, fragment);
fragmentTransaction.commit();
}
private void fn_permission() {
if ((ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
if ((ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION))) {
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION
},
REQUEST_PERMISSIONS);
}
} else {
boolean_permission = true;
if (boolean_permission && isTutor) {
if (!PrefUtils.isLoggedIn(getApplicationContext())) {
Intent intent = new Intent(getApplicationContext(), GoogleService.class);
startService(intent);
}
} else {
Toast.makeText(getApplicationContext(), "Please enable the gps", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_PERMISSIONS: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
boolean_permission = true;
} else {
Toast.makeText(getApplicationContext(), "Please allow the permission", Toast.LENGTH_LONG).show();
}
}
}
}
@Override
protected void onResume() {
super.onResume();
backgroundLocalizationService = new IntentFilter(GoogleService.str_receiver);
if (isTutor)
registerReceiver(this.broadcastReceiver, backgroundLocalizationService);
}
@Override
protected void onPause() {
super.onPause();
if (isTutor)
unregisterReceiver(this.broadcastReceiver);
}
protected void onDestroy() {
unRegisterLocalizationService();
super.onDestroy();
}
private void unRegisterLocalizationService() {
if (this.broadcastReceiver != null) {
try {
LocalBroadcastManager.getInstance(this).unregisterReceiver(broadcastReceiver);
Intent intent = new Intent(this, GoogleService.class);
stopService(intent);
} catch (IllegalArgumentException e) {
Log.d("Destroy app", "RECIEVER UNREGISTER ERROR");
}
}
}
}

View File

@ -1,194 +0,0 @@
package com.uam.wmi.findmytutor.activity;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;
import com.uam.wmi.findmytutor.service.BackgroundLocalizationService;
import android.support.design.widget.FloatingActionButton;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.support.annotation.NonNull;
import android.view.MenuItem;
import com.mapbox.mapboxsdk.Mapbox;
import com.uam.wmi.findmytutor.R;
import com.uam.wmi.findmytutor.utils.BroadcastLocalizationHandler;
import com.uam.wmi.findmytutor.utils.PrefUtils;
public class MainActivity extends AppCompatActivity {
private BottomNavigationView mMainNav;
private FrameLayout mMainFrame;
private boolean isTutor;
private MapFragment mapFragment;
private NotificationFragment notificationFragment;
private ProfileFragment profileFragment;
public static IntentFilter backgroundLocalizationService = null;
private BroadcastReceiver broadcastReceiver = null;
private static final int REQUEST_PERMISSIONS = 100;
private boolean boolean_permission = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Mapbox.getInstance(this, getString(R.string.access_token));
setContentView(R.layout.activity_main);
mMainFrame = (FrameLayout) findViewById(R.id.main_frame);
mMainNav = (BottomNavigationView) findViewById(R.id.main_nav);
isTutor = PrefUtils.getIsTutor(getApplicationContext());
if (!isTutor) {
mMainNav.findViewById(R.id.nav_profile).setVisibility(View.GONE);
}
mapFragment = new MapFragment();
notificationFragment = new NotificationFragment();
profileFragment = new ProfileFragment();
// Default frag here
setFragment(mapFragment);
mMainNav.setSelectedItemId(R.id.nav_map);
mMainNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_map:
setFragment(mapFragment);
return true;
case R.id.nav_notif:
setFragment(notificationFragment);
return true;
case R.id.nav_profile:
setFragment(profileFragment);
return true;
default:
return false;
}
}
});
// Logout button
final FloatingActionButton button = findViewById(R.id.logoutButton);
button.setOnClickListener(view -> {
PrefUtils.cleanUserLocalStorage(getApplicationContext());
unRegisterLocalizationService();
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage(getBaseContext().getPackageName());
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
finish();
startActivity(i);
});
if (isTutor) {
fn_permission();
this.broadcastReceiver = new BroadcastLocalizationHandler();
}
}
private void setFragment(Fragment fragment) {
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_frame, fragment);
fragmentTransaction.commit();
}
private void fn_permission() {
if ((ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
if ((ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION))) {
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION
},
REQUEST_PERMISSIONS);
}
} else {
boolean_permission = true;
if (boolean_permission) {
if (!PrefUtils.isLoggedIn(getApplicationContext())) {
Intent intent = new Intent(getApplicationContext(), BackgroundLocalizationService.class);
startService(intent);
}
} else {
Toast.makeText(getApplicationContext(), "Please enable the gps", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_PERMISSIONS: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
boolean_permission = true;
} else {
Toast.makeText(getApplicationContext(), "Please allow the permission", Toast.LENGTH_LONG).show();
}
}
}
}
@Override
protected void onResume() {
super.onResume();
backgroundLocalizationService = new IntentFilter(BackgroundLocalizationService.str_receiver);
if (isTutor)
registerReceiver(this.broadcastReceiver, backgroundLocalizationService);
}
@Override
protected void onPause() {
super.onPause();
if (isTutor)
unregisterReceiver(this.broadcastReceiver);
}
protected void onDestroy() {
unRegisterLocalizationService();
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

@ -1,5 +1,7 @@
package com.uam.wmi.findmytutor.service;
import android.Manifest;
import android.app.Notification;
import android.app.Service;
import android.content.Intent;
import android.content.pm.PackageManager;
@ -9,6 +11,7 @@ import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.content.Context;
@ -26,6 +29,14 @@ public class BackgroundLocalizationService extends Service {
private static long notify_interval = 100000;
Location mLastLocation;
Intent intent;
LocationManager locationManager;
Location location;
double latitude, longitude;
boolean isGPSEnable = false;
boolean isNetworkEnable = false;
private class LocationListener implements android.location.LocationListener {
@ -73,7 +84,7 @@ public class BackgroundLocalizationService extends Service {
Log.e(TAG, "onStartCommand");
super.onStartCommand(intent, flags, startId);
return START_NOT_STICKY;
return START_STICKY;
}
@Override
@ -81,6 +92,11 @@ public class BackgroundLocalizationService extends Service {
Log.e(TAG, "onCreate");
Notification notification = new NotificationCompat.Builder(this, "NOTIFICATION_CHANNEL")
.setContentText("Content").build();
startForeground(1001, notification);
initializeLocationManager();
try {
@ -112,9 +128,111 @@ public class BackgroundLocalizationService extends Service {
Timer mTimer = new Timer();
mTimer.schedule(new TimerTaskToGetLocation(), 5, notify_interval);
intent = new Intent(str_receiver);
}
private void fn_getlocation() {
locationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
isGPSEnable = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
isNetworkEnable = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnable && !isNetworkEnable) {
} else {
if (isGPSEnable) {
location = null;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, mLocationListeners[1]);
if (locationManager!=null){
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location!=null){
latitude = location.getLatitude();
longitude = location.getLongitude();
fn_update(location);
}
}
}
if (isNetworkEnable) {
location = null;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, mLocationListeners[0]);
if (locationManager != null) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location!=null){
latitude = location.getLatitude();
longitude = location.getLongitude();
fn_update(location);
}
}
}
}
}
private class TimerTaskToGetLocation extends TimerTask{
@Override
public void run() {
mHandler.post(BackgroundLocalizationService.this::fn_getlocation);
}
}
private void fn_update(Location location){
intent.putExtra("latitude",location.getLatitude());
intent.putExtra("longitude",location.getLongitude());
sendBroadcast(intent);
}
private void sendToBroadcast(Location location) {
Log.e("sendToBroadcast", String.valueOf(location));
intent.putExtra("latitude",location.getLatitude());
intent.putExtra("longitude",location.getLongitude());
sendBroadcast(intent);
}
private void getLocation() {
sendToBroadcast(mLastLocation);
}
@Override
public void onDestroy() {
Log.e(TAG, "onDestroy");
@ -140,24 +258,4 @@ public class BackgroundLocalizationService extends Service {
}
}
private class TimerTaskToGetLocation extends TimerTask {
@Override
public void run() {
mHandler.post(BackgroundLocalizationService.this::getLocation);
}
}
private void sendToBroadcast(Location location) {
Log.e("sendToBroadcast", String.valueOf(location));
intent.putExtra("latitude",location.getLatitude());
intent.putExtra("longitude",location.getLongitude());
sendBroadcast(intent);
}
private void getLocation() {
sendToBroadcast(mLastLocation);
}
}

View File

@ -66,4 +66,16 @@ public class PrefUtils {
SharedPreferences.Editor editor = preferences.edit();
editor.clear().apply();
}
public static void storeIsServiceRunning(Context context, Boolean flag) {
SharedPreferences.Editor editor = getSharedPreferences(context).edit();
editor.putBoolean("IS_SERVIS_RUNNING", flag);
editor.apply();
}
public static boolean getIsServiceRunning(Context context) {
return getSharedPreferences(context).getBoolean("IS_SERVIS_RUNNING", false);
}
}