Fix bug with GPS turned-on after startup view. Remove dummy flags.

This commit is contained in:
vng 2012-06-13 01:16:35 -07:00 committed by Alex Zolotarev
parent f317b43863
commit 007ce9b66d
3 changed files with 104 additions and 73 deletions

View file

@ -49,14 +49,13 @@ public class DownloadResourcesActivity extends Activity implements LocationServi
private CheckBox mDownloadCountryCheckBox = null;
private LocationService mLocationService = null;
private String mCountryName = null;
private boolean mHasLocation = false;
private WakeLock mWakeLock = null;
private int getBytesToDownload()
{
return getBytesToDownload(
mApplication.getApkPath(),
mApplication.getDataStoragePath());
return getBytesToDownload(mApplication.getApkPath(),
mApplication.getDataStoragePath());
}
private void disableAutomaticStandby()
@ -84,14 +83,14 @@ public class DownloadResourcesActivity extends Activity implements LocationServi
if (bytesToDownload < 1024 * 1024)
mMsgView.setText(String.format(getString(R.string.download_resources),
bytesToDownload * 1.0f / 1024,
(float)bytesToDownload / 1024,
getString(R.string.kb)));
else
mMsgView.setText(String.format(getString(R.string.download_resources,
bytesToDownload * 1.0f / 1024 / 1024,
(float)bytesToDownload / 1024 / 1024,
getString(R.string.mb))));
/// set normal text color
// set normal text color
mMsgView.setTextColor(Color.WHITE);
}
@ -169,7 +168,6 @@ public class DownloadResourcesActivity extends Activity implements LocationServi
showMapView();
}
public String getErrorMessage(int res)
{
int id;
@ -188,9 +186,11 @@ public class DownloadResourcesActivity extends Activity implements LocationServi
{
// Continue with Main UI initialization (MWMActivity)
Intent mwmActivityIntent = new Intent(this, MWMActivity.class);
// Disable animation because MWMActivity should appear exactly over this one
mwmActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(mwmActivityIntent);
finish();
}
@ -199,7 +199,7 @@ public class DownloadResourcesActivity extends Activity implements LocationServi
enableAutomaticStandby();
if (result == ERR_NO_MORE_FILES)
{
if (mHasLocation && mDownloadCountryCheckBox.isChecked())
if (mCountryName != null && mDownloadCountryCheckBox.isChecked())
{
mDownloadCountryCheckBox.setVisibility(View.GONE);
mLocationMsgView.setVisibility(View.GONE);
@ -282,6 +282,7 @@ public class DownloadResourcesActivity extends Activity implements LocationServi
mTryAgainButton = (Button)findViewById(R.id.download_resources_button_tryagain);
mDownloadCountryCheckBox = (CheckBox)findViewById(R.id.download_country_checkbox);
mLocationMsgView = (TextView)findViewById(R.id.download_resources_location_message);
prepareFilesDownload();
}
}
@ -289,6 +290,8 @@ public class DownloadResourcesActivity extends Activity implements LocationServi
@Override
protected void onDestroy()
{
super.onDestroy();
if (mLocationService != null)
{
mLocationService.stopUpdate(this);
@ -296,8 +299,24 @@ public class DownloadResourcesActivity extends Activity implements LocationServi
}
mMapStorage.unsubscribe(mSlotId);
}
super.onDestroy();
@Override
protected void onPause()
{
super.onPause();
if (mLocationService != null)
mLocationService.stopUpdate(this);
}
@Override
protected void onResume()
{
super.onResume();
if (mLocationService != null)
mLocationService.startUpdate(this);
}
public void onDownloadProgress(int currentTotal, int currentProgress, int globalTotal, int globalProgress)
@ -317,24 +336,19 @@ public class DownloadResourcesActivity extends Activity implements LocationServi
else
finishFilesDownload(errorCode);
}
private boolean mReceivedFirstEvent = false;
private int mLocationsCount = 0;
private final int mLocationsTryCount = 0;
@Override
public void onLocationUpdated(long time, double lat, double lon, float accuracy)
{
if (mReceivedFirstEvent)
if (mCountryName == null)
{
if (mLocationsCount == mLocationsTryCount)
findViewById(R.id.download_resources_location_progress).setVisibility(View.GONE);
Log.i(TAG, "Searching for country name at location lat=" + lat + ", lon=" + lon);
mCountryName = findCountryByPos(lat, lon);
if (mCountryName != null)
{
findViewById(R.id.download_resources_location_progress).setVisibility(View.GONE);
Log.i(TAG, "Searching for country name at location lat=" + lat + ", lon=" + lon);
mCountryName = findCountryByPos(lat, lon);
mHasLocation = true;
if (mMapStorage.countryStatus(mMapStorage.findIndexByName(mCountryName)) == MapStorage.ON_DISK)
mLocationMsgView.setText(String.format(getString(R.string.download_location_map_up_to_date), mCountryName));
else
@ -348,9 +362,6 @@ public class DownloadResourcesActivity extends Activity implements LocationServi
mLocationService.stopUpdate(this);
mLocationService = null;
}
Log.d(TAG, "tryCount:" + mLocationsCount);
++mLocationsCount;
}
}
@ -362,8 +373,6 @@ public class DownloadResourcesActivity extends Activity implements LocationServi
@Override
public void onLocationStatusChanged(int status)
{
if (status == LocationService.FIRST_EVENT)
mReceivedFirstEvent = true;
}
private native void moveMaps(String fromFolder, String toFolder);

View file

@ -12,7 +12,6 @@ import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
@ -187,12 +186,14 @@ public class MWMActivity extends NvEventQueueActivity implements LocationService
public void onMyPositionClicked(View v)
{
v.setBackgroundResource(R.drawable.myposition_button_normal);
final boolean isLocationActive = v.isSelected();
if (isLocationActive)
mApplication.getLocationService().stopUpdate(this);
else
mApplication.getLocationService().startUpdate(this);
v.setSelected(!isLocationActive);
// Store active state of My Position
SharedPreferences.Editor prefsEdit = getSharedPreferences(mApplication.getPackageName(), MODE_PRIVATE).edit();
prefsEdit.putBoolean(PREFERENCES_MYPOSITION, !isLocationActive);
@ -297,9 +298,9 @@ public class MWMActivity extends NvEventQueueActivity implements LocationService
@Override
public void onLocationStatusChanged(int newStatus)
{
Log.d("LOCATION", "status: " + newStatus);
if (newStatus == LocationService.FIRST_EVENT)
findViewById(R.id.map_button_myposition).setBackgroundResource(R.drawable.myposition_button_found);
nativeLocationStatusChanged(newStatus);
}
@ -355,6 +356,7 @@ public class MWMActivity extends NvEventQueueActivity implements LocationService
protected void onStart()
{
super.onStart();
// Restore My Position state on startup/activity recreation
SharedPreferences prefs = getSharedPreferences(mApplication.getPackageName(), MODE_PRIVATE);
final boolean isMyPositionEnabled = prefs.getBoolean(PREFERENCES_MYPOSITION, false);
@ -372,7 +374,7 @@ public class MWMActivity extends NvEventQueueActivity implements LocationService
@Override
protected void onPause()
{
//< stop update only if it's started in OnRenderingInitialized
// stop update only if it's started in OnRenderingInitialized
if (!m_shouldStartLocationService)
if (findViewById(R.id.map_button_myposition).isSelected())
mApplication.getLocationService().stopUpdate(this);
@ -424,19 +426,23 @@ public class MWMActivity extends NvEventQueueActivity implements LocationService
private void onAboutDialogClicked()
{
LayoutInflater inflater = LayoutInflater.from(this);
View alertDialogView = inflater.inflate(R.layout.about, null);
WebView myWebView = (WebView) alertDialogView.findViewById(R.id.webview_about);
myWebView.loadUrl("file:///android_asset/about.html");
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(alertDialogView);
builder.setTitle(R.string.about);
builder.setPositiveButton(R.string.close, new DialogInterface.OnClickListener() {
new AlertDialog.Builder(this)
.setView(alertDialogView)
.setTitle(R.string.about)
.setPositiveButton(R.string.close, new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
public void onClick(DialogInterface dialog, int which)
{
dialog.cancel();
}
}).show();
})
.show();
}
// Initialized to invalid combination to force update on the first check
@ -450,11 +456,13 @@ public class MWMActivity extends NvEventQueueActivity implements LocationService
if (Environment.MEDIA_MOUNTED.equals(state))
{
available = writeable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state))
}
else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state))
{
available = true;
writeable = false;
} else
}
else
available = writeable = false;
if (m_storageAvailable != available || m_storageWriteable != writeable)
@ -471,6 +479,7 @@ public class MWMActivity extends NvEventQueueActivity implements LocationService
{
// Add local maps to the model
nativeStorageConnected();
// enable downloader button and dismiss blocking popup
findViewById(R.id.map_button_download).setVisibility(View.VISIBLE);
if (m_storageDisconnectedDialog != null)
@ -480,6 +489,7 @@ public class MWMActivity extends NvEventQueueActivity implements LocationService
{
// Add local maps to the model
nativeStorageConnected();
// disable downloader button and dismiss blocking popup
findViewById(R.id.map_button_download).setVisibility(View.INVISIBLE);
if (m_storageDisconnectedDialog != null)
@ -489,14 +499,16 @@ public class MWMActivity extends NvEventQueueActivity implements LocationService
{
// Remove local maps from the model
nativeStorageDisconnected();
// enable downloader button and show blocking popup
findViewById(R.id.map_button_download).setVisibility(View.VISIBLE);
if (m_storageDisconnectedDialog == null)
{
m_storageDisconnectedDialog = new AlertDialog.Builder(this).create();
m_storageDisconnectedDialog.setTitle(R.string.external_storage_is_not_available);
m_storageDisconnectedDialog.setMessage(getString(R.string.disconnect_usb_cable));
m_storageDisconnectedDialog.setCancelable(false);
m_storageDisconnectedDialog = new AlertDialog.Builder(this)
.setTitle(R.string.external_storage_is_not_available)
.setMessage(getString(R.string.disconnect_usb_cable))
.setCancelable(false)
.create();
}
m_storageDisconnectedDialog.show();
}
@ -512,6 +524,7 @@ public class MWMActivity extends NvEventQueueActivity implements LocationService
updateExternalStorageState();
}
};
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
filter.addAction(Intent.ACTION_MEDIA_REMOVED);
@ -524,6 +537,7 @@ public class MWMActivity extends NvEventQueueActivity implements LocationService
filter.addAction(Intent.ACTION_MEDIA_NOFS);
filter.addDataScheme("file");
registerReceiver(m_externalStorageReceiver, filter);
updateExternalStorageState();
}

View file

@ -53,10 +53,8 @@ public class LocationService implements LocationListener, SensorEventListener, W
/// To calculate true north for compass
private GeomagneticField m_magneticField = null;
/// true when GPS is on
private boolean m_isActive = false;
// @TODO Refactor to deliver separate first update notification to each provider,
// or do not use it at all in the location service logic
private boolean m_reportFirstUpdate = true;
private WakeLock m_wakeLock = null;
private MWMApplication mApplication = null;
@ -64,7 +62,7 @@ public class LocationService implements LocationListener, SensorEventListener, W
public LocationService(MWMApplication application)
{
mApplication = application;
// Acquire a reference to the system Location Manager
m_locationManager = (LocationManager) mApplication.getSystemService(Context.LOCATION_SERVICE);
m_sensorManager = (SensorManager) mApplication.getSystemService(Context.SENSOR_SERVICE);
@ -123,6 +121,7 @@ public class LocationService implements LocationListener, SensorEventListener, W
if (!m_isActive)
{
List<String> enabledProviders = m_locationManager.getProviders(true);
// Remove passive provider, we don't use it in the current implementation
for (int i = 0; i < enabledProviders.size(); ++i)
if (enabledProviders.get(i).equals("passive"))
@ -130,6 +129,7 @@ public class LocationService implements LocationListener, SensorEventListener, W
enabledProviders.remove(i);
break;
}
if (enabledProviders.size() == 0)
{
// Use WiFi BSSIDS and Google Internet location service if no other options are available
@ -137,9 +137,11 @@ public class LocationService implements LocationListener, SensorEventListener, W
if (com.mapswithme.util.ConnectionState.isConnected(mApplication))
{
observer.onLocationStatusChanged(STARTED);
if (m_wifiScanner == null)
m_wifiScanner = new WifiLocation();
m_wifiScanner.StartScan(mApplication, this);
disableAutomaticStandby();
}
else
@ -148,33 +150,43 @@ public class LocationService implements LocationListener, SensorEventListener, W
else
{
m_isActive = true;
observer.onLocationStatusChanged(STARTED);
disableAutomaticStandby();
Location lastKnown = null;
for (String provider : enabledProviders)
{
// @TODO change frequency and accuracy to save battery
if (m_locationManager.isProviderEnabled(provider))
{
m_locationManager.requestLocationUpdates(provider, 0, 0, this);
// Send last known location for faster startup.
// It should pass filter in the handler below.
final Location lastKnown = m_locationManager.getLastKnownLocation(provider);
if (lastKnown != null)
onLocationChanged(lastKnown);
// Remember last known location
final Location l = m_locationManager.getLastKnownLocation(provider);
if (l != null)
{
if (lastKnown == null || isBetterLocation(l, lastKnown))
lastKnown = l;
}
}
}
if (m_sensorManager != null)
{
// How often compass is updated
final int COMPASS_REFRESH_MKS = SensorManager.SENSOR_DELAY_NORMAL;//SensorManager.SENSOR_DELAY_UI;
// How often compass is updated (may be SensorManager.SENSOR_DELAY_UI)
final int COMPASS_REFRESH_MKS = SensorManager.SENSOR_DELAY_NORMAL;
if (m_accelerometer != null)
m_sensorManager.registerListener(this, m_accelerometer, COMPASS_REFRESH_MKS);
if (m_magnetometer != null)
m_sensorManager.registerListener(this, m_magnetometer, COMPASS_REFRESH_MKS);
}
// Pass last known location only in the end of all registerListener
// in case, when we want to disconnect in listener.
onLocationChanged(lastKnown);
}
}
else
@ -184,18 +196,21 @@ public class LocationService implements LocationListener, SensorEventListener, W
public void stopUpdate(Listener observer)
{
m_observers.remove(observer);
// Stop only if no more observers are subscribed
if (m_observers.size() == 0)
{
m_locationManager.removeUpdates(this);
if (m_sensorManager != null)
m_sensorManager.unregisterListener(this);
m_magneticField = null;
m_lastLocation = null;
m_isActive = false;
m_reportFirstUpdate = true;
m_magneticField = null;
enableAutomaticStandby();
}
observer.onLocationStatusChanged(STOPPED);
}
@ -226,7 +241,8 @@ public class LocationService implements LocationListener, SensorEventListener, W
if (isSignificantlyNewer)
return true;
else if (isSignificantlyOlder)
{ // If the new location is more than two minutes older, it must be worse
{
// If the new location is more than two minutes older, it must be worse
return false;
}
@ -239,8 +255,7 @@ public class LocationService implements LocationListener, SensorEventListener, W
return true; // Because new location is at least not too old (< 2mins from now)
// Check whether the new location fix is more or less accurate
final int accuracyDelta = (int) (newLocation.getAccuracy()
- currentBestLocation.getAccuracy());
final int accuracyDelta = (int) (newLocation.getAccuracy() - currentBestLocation.getAccuracy());
final boolean isLessAccurate = accuracyDelta > 0;
final boolean isMoreAccurate = accuracyDelta < 0;
final boolean isSignificantlyLessAccurate = accuracyDelta > 200;
@ -264,29 +279,28 @@ public class LocationService implements LocationListener, SensorEventListener, W
return provider1.equals(provider2);
}
// *************** Notification Handlers ******************
private final static float HUNDRED_METRES = 100.0f;
//@Override
@Override
public void onLocationChanged(Location l)
{
if (isBetterLocation(l, m_lastLocation))
{
if (m_reportFirstUpdate)
{
m_reportFirstUpdate = false;
if (m_lastLocation == null)
notifyStatusChanged(FIRST_EVENT);
}
// Used for more precise compass updates
if (m_sensorManager != null)
{
// Recreate magneticField if location has changed significantly
if (m_magneticField == null || (m_lastLocation == null || l.distanceTo(m_lastLocation) > HUNDRED_METRES))
m_magneticField = new GeomagneticField((float)l.getLatitude(), (float)l.getLongitude(), (float)l.getAltitude(), l.getTime());
if (m_magneticField == null ||
(m_lastLocation == null || l.distanceTo(m_lastLocation) > HUNDRED_METRES))
{
m_magneticField = new GeomagneticField((float)l.getLatitude(), (float)l.getLongitude(),
(float)l.getAltitude(), l.getTime());
}
}
notifyLocationUpdated(l.getTime(), l.getLatitude(), l.getLongitude(), l.getAccuracy());
m_lastLocation = l;
}
@ -317,7 +331,6 @@ public class LocationService implements LocationListener, SensorEventListener, W
private float[] m_gravity = null;
private float[] m_geomagnetic = null;
//@Override
@Override
public void onSensorChanged(SensorEvent event)
{
@ -367,35 +380,30 @@ public class LocationService implements LocationListener, SensorEventListener, W
}
}
//@Override
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy)
{
//Log.d(TAG, "Compass accuracy changed to " + String.valueOf(accuracy));
}
//@Override
@Override
public void onProviderDisabled(String provider)
{
Log.d(TAG, "Disabled location provider: " + provider);
}
//@Override
@Override
public void onProviderEnabled(String provider)
{
Log.d(TAG, "Enabled location provider: " + provider);
}
//@Override
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
Log.d(TAG, String.format("Status changed for location provider: %s to %d", provider, status));
}
//@Override
@Override
public void onWifiLocationUpdated(Location l)
{