diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java index 51c65fb..fe9cabd 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/MainActivity.java @@ -14,80 +14,14 @@ public class MainActivity extends ActionBarActivity { private static final int LOCATION_SETTINGS_REQUEST_ID = 1; private static final String CLASS_TAG = MainActivity.class.getSimpleName(); - private static final long MIN_TIME = 60000; - private static final float MIN_DISTANCE = 100; - private boolean gpsLocationEnabled; - private boolean networkLocationEnabled; - private boolean locationEnabled; - private LocationManager locationService; - private String locationProvider; - private HomeFragment homeFrag; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - Toolbar toolbar = (Toolbar) findViewById(R.id.navbar); - setSupportActionBar(toolbar); +// Toolbar toolbar = (Toolbar) findViewById(R.id.navbar); +// setSupportActionBar(toolbar); - locationService = (LocationManager) getSystemService(LOCATION_SERVICE); - checkLocationSourceAvailability(); - - if (!locationEnabled) { - Intent locationSettingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); - startActivityForResult(locationSettingsIntent, LOCATION_SETTINGS_REQUEST_ID); - } - - } - - @Override - protected void onResume() { - super.onResume(); - - homeFrag = (HomeFragment) getSupportFragmentManager().findFragmentById(R.id.home); - locationService.requestLocationUpdates(locationProvider, - MIN_TIME, - MIN_DISTANCE, - homeFrag); - } - - @Override - protected void onPause() { - super.onPause(); - locationService.removeUpdates(homeFrag); - } - - @Override - protected void onActivityResult(int requestCode, int resultCode, Intent data) { - - switch (requestCode) { - case LOCATION_SETTINGS_REQUEST_ID: - checkLocationSourceAvailability(); - if (!locationEnabled) { - // sadly, nothing to do except from notifing user that program is not enable working - Toast.makeText(this, "Sorry location services are not working." + - " Program cannot work properly - check location settings to allow program working correctly", - Toast.LENGTH_LONG); - } - break; - default: - super.onActivityResult(requestCode, resultCode, data); - } - } - - private void checkLocationSourceAvailability() { - gpsLocationEnabled = locationService.isProviderEnabled(LocationManager.GPS_PROVIDER); - networkLocationEnabled = locationService.isProviderEnabled(LocationManager.NETWORK_PROVIDER); - locationEnabled = gpsLocationEnabled || networkLocationEnabled; - if (gpsLocationEnabled) { - locationProvider = LocationManager.GPS_PROVIDER; - return; - } - - if (networkLocationEnabled) { - locationProvider = LocationManager.NETWORK_PROVIDER; - return; - } } @Override diff --git a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java index 1194ffe..9346e9a 100644 --- a/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java +++ b/app/src/main/java/pl/tpolgrabia/urbanexplorer/fragments/HomeFragment.java @@ -1,9 +1,12 @@ package pl.tpolgrabia.urbanexplorer.fragments; +import android.content.Intent; import android.location.Location; import android.location.LocationListener; +import android.location.LocationManager; import android.os.Bundle; +import android.provider.Settings; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; @@ -11,19 +14,49 @@ import android.view.View; import android.view.ViewGroup; import android.widget.TextView; +import android.widget.Toast; import pl.tpolgrabia.urbanexplorer.R; +import static android.content.Context.LOCATION_SERVICE; + /** * A simple {@link Fragment} subclass. */ public class HomeFragment extends Fragment implements LocationListener { private static final String CLASS_TAG = HomeFragment.class.getSimpleName(); + private static final long MIN_TIME = 60000; + private static final float MIN_DISTANCE = 100; + private static final int LOCATION_SETTINGS_REQUEST_ID = 1; + private boolean gpsLocationEnabled; + private boolean networkLocationEnabled; + private boolean locationEnabled; + private LocationManager locationService; + private String locationProvider; + private HomeFragment homeFrag; + private boolean locationServicesActivated = false; public HomeFragment() { // Required empty public constructor } + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + locationService = (LocationManager) getActivity().getSystemService(LOCATION_SERVICE); + + checkLocationSourceAvailability(); + + if (!locationEnabled) { + Intent locationSettingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); + startActivityForResult(locationSettingsIntent, LOCATION_SETTINGS_REQUEST_ID); + return; + } + + Toast.makeText(getActivity(), "Created", Toast.LENGTH_LONG).show(); + } + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -61,4 +94,60 @@ public class HomeFragment extends Fragment implements LocationListener { public void onProviderDisabled(String provider) { Log.i(CLASS_TAG, "Provider " + provider + " disabled"); } + + @Override + public void onResume() { + super.onResume(); + + homeFrag = (HomeFragment) getChildFragmentManager().findFragmentById(R.id.home_frag); + if (locationProvider != null) { + locationService.requestLocationUpdates(locationProvider, + MIN_TIME, + MIN_DISTANCE, + homeFrag); + locationServicesActivated = true; + Toast.makeText(getActivity(), "Location resumed", Toast.LENGTH_LONG).show(); + } + } + + @Override + public void onPause() { + super.onPause(); + if (locationServicesActivated) { + locationService.removeUpdates(homeFrag); + } + } + + private void checkLocationSourceAvailability() { + gpsLocationEnabled = locationService.isProviderEnabled(LocationManager.GPS_PROVIDER); + networkLocationEnabled = locationService.isProviderEnabled(LocationManager.NETWORK_PROVIDER); + locationEnabled = gpsLocationEnabled || networkLocationEnabled; + if (gpsLocationEnabled) { + locationProvider = LocationManager.GPS_PROVIDER; + return; + } + + if (networkLocationEnabled) { + locationProvider = LocationManager.NETWORK_PROVIDER; + return; + } + } + + @Override + public void onActivityResult(int requestCode, int resultCode, Intent data) { + + switch (requestCode) { + case LOCATION_SETTINGS_REQUEST_ID: + checkLocationSourceAvailability(); + if (!locationEnabled) { + // sadly, nothing to do except from notifing user that program is not enable working + Toast.makeText(getActivity(), "Sorry location services are not working." + + " Program cannot work properly - check location settings to allow program working correctly", + Toast.LENGTH_LONG).show(); + } + break; + default: + super.onActivityResult(requestCode, resultCode, data); + } + } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 8b70d69..0f7d333 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -10,11 +10,11 @@ android:paddingBottom="@dimen/activity_vertical_margin" tools:context="pl.tpolgrabia.urbanexplorer.MainActivity"> - + + + - +