diff options
Diffstat (limited to 'android/src/com/mapswithme/maps/DownloadResourcesActivity.java')
-rw-r--r-- | android/src/com/mapswithme/maps/DownloadResourcesActivity.java | 537 |
1 files changed, 251 insertions, 286 deletions
diff --git a/android/src/com/mapswithme/maps/DownloadResourcesActivity.java b/android/src/com/mapswithme/maps/DownloadResourcesActivity.java index ecef34327f..54653eece4 100644 --- a/android/src/com/mapswithme/maps/DownloadResourcesActivity.java +++ b/android/src/com/mapswithme/maps/DownloadResourcesActivity.java @@ -16,22 +16,22 @@ import android.widget.TextView; import android.widget.Toast; import com.mapswithme.country.StorageOptions; -import com.mapswithme.maps.MWMActivity.MapTask; -import com.mapswithme.maps.MWMActivity.OpenUrlTask; import com.mapswithme.maps.MapStorage.Index; +import com.mapswithme.maps.MwmActivity.MapTask; +import com.mapswithme.maps.MwmActivity.OpenUrlTask; import com.mapswithme.maps.api.Const; import com.mapswithme.maps.api.ParsedMwmRequest; import com.mapswithme.maps.base.BaseMwmFragmentActivity; import com.mapswithme.maps.location.LocationHelper; import com.mapswithme.util.ConnectionState; import com.mapswithme.util.Constants; +import com.mapswithme.util.UiUtils; import com.mapswithme.util.Utils; import com.mapswithme.util.Yota; import com.mapswithme.util.statistics.Statistics; import java.io.File; import java.io.FileOutputStream; -import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -50,16 +50,14 @@ public class DownloadResourcesActivity extends BaseMwmFragmentActivity private static final int ERR_NO_MORE_FILES = -5; private static final int ERR_FILE_IN_PROGRESS = -6; - private MWMApplication mApplication = null; - private MapStorage mMapStorage = null; - private int mSlotId = 0; - private TextView mMsgView = null; - private TextView mLocationMsgView = null; - private ProgressBar mProgress = null; - private Button mButton = null; - private CheckBox mDownloadCountryCheckBox = null; - private Index mCountryIndex = null; + private TextView mTvMessage; + private TextView mTvLocation; + private ProgressBar mProgress; + private Button mBtnDownload; + private CheckBox mChbDownloadCountry; + private int mStorageSlotId; + private Index mCountryIndex; private MapTask mMapTaskToForward; private static final int DOWNLOAD = 0; @@ -69,8 +67,8 @@ public class DownloadResourcesActivity extends BaseMwmFragmentActivity private static final int PROCEED_TO_MAP = 4; private static final int BTN_COUNT = 5; - private View.OnClickListener mBtnListeners[] = null; - private String mBtnNames[] = null; + private View.OnClickListener mBtnListeners[]; + private String mBtnNames[]; private final IntentProcessor[] mIntentProcessors = { new GeoIntentProcessor(), @@ -83,18 +81,174 @@ public class DownloadResourcesActivity extends BaseMwmFragmentActivity }; public static final String EXTRA_COUNTRY_INDEX = ".extra.index"; - public static final String EXTRA_AUTODOWNLOAD_CONTRY = ".extra.autodownload"; + public static final String EXTRA_AUTODOWNLOAD_COUNTRY = ".extra.autodownload"; public static final String EXTRA_UPDATE_COUNTRIES = ".extra.update.countries"; + @Override + protected void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + + Utils.keepScreenOn(true, getWindow()); + suggestRemoveLiteOrSamsung(); + + final boolean dispatched = dispatchIntent(); + if (!dispatched) + parseIntentForKmzFile(); + + setContentView(R.layout.activity_download_resources); + initViewsAndListeners(); + mStorageSlotId = MapStorage.INSTANCE.subscribe(this); + + if (prepareFilesDownload()) + { + setAction(DOWNLOAD); + + if (ConnectionState.isWifiConnected()) + onDownloadClicked(mBtnDownload); + } + } + + @Override + protected void onResume() + { + super.onResume(); + + LocationHelper.INSTANCE.addLocationListener(this); + } + + @Override + protected void onPause() + { + super.onPause(); + + LocationHelper.INSTANCE.removeLocationListener(this); + } + + @Override + protected void onDestroy() + { + super.onDestroy(); + + mProgress = null; + MapStorage.INSTANCE.unsubscribe(mStorageSlotId); + } + + // TODO change communication with native thread so that it does not send us callbacks after activity destroy + @SuppressWarnings("unused") + public void onDownloadProgress(int currentTotal, int currentProgress, int globalTotal, int globalProgress) + { + if (mProgress != null) + mProgress.setProgress(globalProgress); + } + + // TODO change communication with native thread so that it does not send us callbacks after activity destroy + @SuppressWarnings("unused") + public void onDownloadFinished(int errorCode) + { + if (errorCode == ERR_DOWNLOAD_SUCCESS) + { + final int res = startNextFileDownload(this); + if (res == ERR_NO_MORE_FILES) + finishFilesDownload(res); + } + else + finishFilesDownload(errorCode); + } + + @Override + public void onLocationUpdated(final Location l) + { + if (mCountryIndex != null) + return; + + final double lat = l.getLatitude(); + final double lon = l.getLongitude(); + Log.i(TAG, "Searching for country name at location lat=" + lat + ", lon=" + lon); + + mCountryIndex = Framework.nativeGetCountryIndex(lat, lon); + if (mCountryIndex == null) + return; + + UiUtils.show(mTvLocation); + + final int countryStatus = MapStorage.INSTANCE.countryStatus(mCountryIndex); + final String name = MapStorage.INSTANCE.countryName(mCountryIndex); + + if (countryStatus == MapStorage.ON_DISK) + mTvLocation.setText(String.format(getString(R.string.download_location_map_up_to_date), name)); + else + { + final CheckBox checkBox = (CheckBox) findViewById(R.id.chb__download_country); + UiUtils.show(checkBox); + + String locationText; + String checkBoxText; + + if (countryStatus == MapStorage.ON_DISK_OUT_OF_DATE) + { + locationText = getString(R.string.download_location_update_map_proposal); + checkBoxText = String.format(getString(R.string.update_country_ask), name); + } + else + { + locationText = getString(R.string.download_location_map_proposal); + checkBoxText = String.format(getString(R.string.download_country_ask), name); + } + + mTvLocation.setText(locationText); + checkBox.setText(checkBoxText); + } + + LocationHelper.INSTANCE.removeLocationListener(this); + } + + @Override + public void onCompassUpdated(long time, double magneticNorth, double trueNorth, double accuracy) {} + + @Override + public void onLocationError(int errorCode) {} + + + @Override + public void onCountryStatusChanged(MapStorage.Index idx) + { + final int status = MapStorage.INSTANCE.countryStatus(idx); + + if (status == MapStorage.ON_DISK) + showMap(); + } + + @Override + public void onCountryProgress(MapStorage.Index idx, long current, long total) + { + // Important check - activity can be destroyed + // but notifications from downloading thread are coming. + if (mProgress != null) + mProgress.setProgress((int) current); + } + + private Intent getPackageIntent(String s) + { + return getPackageManager().getLaunchIntentForPackage(s); + } + + private void suggestRemoveLiteOrSamsung() + { + if (!Yota.isFirstYota() && + (Utils.isPackageInstalled(Constants.Package.MWM_LITE_PACKAGE) || Utils.isPackageInstalled(Constants.Package.MWM_SAMSUNG_PACKAGE))) + Toast.makeText(this, R.string.suggest_uninstall_lite, Toast.LENGTH_LONG).show(); + } + private void setDownloadMessage(int bytesToDownload) { Log.d(TAG, "prepareFilesDownload, bytesToDownload:" + bytesToDownload); if (bytesToDownload < Constants.MB) - mMsgView.setText(String.format(getString(R.string.download_resources), + mTvMessage.setText(String.format(getString(R.string.download_resources), (float) bytesToDownload / Constants.KB, getString(R.string.kb))); else - mMsgView.setText(String.format(getString(R.string.download_resources), + mTvMessage.setText(String.format(getString(R.string.download_resources), (float) bytesToDownload / Constants.MB, getString(R.string.mb))); } @@ -103,17 +257,12 @@ public class DownloadResourcesActivity extends BaseMwmFragmentActivity { final int bytes = getBytesToDownload(); - // Show map if no any downloading needed. if (bytes == 0) { - showMapView(); + showMap(); return false; } - // Do initialization once. - if (mMapStorage == null) - initDownloading(); - if (bytes > 0) { setDownloadMessage(bytes); @@ -122,26 +271,19 @@ public class DownloadResourcesActivity extends BaseMwmFragmentActivity mProgress.setProgress(0); } else - { finishFilesDownload(bytes); - } return true; } - private void initDownloading() + private void initViewsAndListeners() { - // Get GUI elements and subscribe to map storage (for country downloading). - mMapStorage = MapStorage.INSTANCE; - mSlotId = mMapStorage.subscribe(this); - - mMsgView = (TextView) findViewById(R.id.download_resources_message); - mProgress = (ProgressBar) findViewById(R.id.download_resources_progress); - mButton = (Button) findViewById(R.id.download_resources_button); - mDownloadCountryCheckBox = (CheckBox) findViewById(R.id.download_country_checkbox); - mLocationMsgView = (TextView) findViewById(R.id.download_resources_location_message); + mTvMessage = (TextView) findViewById(R.id.tv__download_message); + mProgress = (ProgressBar) findViewById(R.id.pb__download_resources); + mBtnDownload = (Button) findViewById(R.id.btn__download_resources); + mChbDownloadCountry = (CheckBox) findViewById(R.id.chb__download_country); + mTvLocation = (TextView) findViewById(R.id.tv__location); - // Initialize button states. mBtnListeners = new View.OnClickListener[BTN_COUNT]; mBtnNames = new String[BTN_COUNT]; @@ -179,15 +321,12 @@ public class DownloadResourcesActivity extends BaseMwmFragmentActivity public void onClick(View v) { onProceedToMapClicked(v); } }; mBtnNames[PROCEED_TO_MAP] = getString(R.string.download_resources_continue); - - // Start listening the location. - LocationHelper.INSTANCE.addLocationListener(this); } private void setAction(int action) { - mButton.setOnClickListener(mBtnListeners[action]); - mButton.setText(mBtnNames[action]); + mBtnDownload.setOnClickListener(mBtnListeners[action]); + mBtnDownload.setText(mBtnNames[action]); } private void doDownload() @@ -196,7 +335,7 @@ public class DownloadResourcesActivity extends BaseMwmFragmentActivity finishFilesDownload(ERR_NO_MORE_FILES); } - public void onDownloadClicked(View v) + private void onDownloadClicked(View v) { setAction(PAUSE); doDownload(); @@ -216,7 +355,6 @@ public class DownloadResourcesActivity extends BaseMwmFragmentActivity private void onTryAgainClicked(View v) { - // Initialize downloading from the beginning. if (prepareFilesDownload()) { setAction(PAUSE); @@ -226,7 +364,7 @@ public class DownloadResourcesActivity extends BaseMwmFragmentActivity private void onProceedToMapClicked(View v) { - showMapView(); + showMap(); } public String getErrorMessage(int res) @@ -255,39 +393,36 @@ public class DownloadResourcesActivity extends BaseMwmFragmentActivity return getString(id); } - public void showMapView() + private void showMap() { - // Continue with Main UI initialization (MWMActivity) - final Intent mwmActivityIntent = new Intent(this, MWMActivity.class); + final Intent intent = new Intent(this, MwmActivity.class); - // Disable animation because MWMActivity should appear exactly over this one - // Intent.FLAG_ACTIVITY_REORDER_TO_FRONT - mwmActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_CLEAR_TOP); + // Disable animation because MwmActivity should appear exactly over this one + intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION | Intent.FLAG_ACTIVITY_CLEAR_TOP); // Add saved task to forward to map activity. if (mMapTaskToForward != null) { - mwmActivityIntent.putExtra(MWMActivity.EXTRA_TASK, mMapTaskToForward); + intent.putExtra(MwmActivity.EXTRA_TASK, mMapTaskToForward); mMapTaskToForward = null; } - startActivity(mwmActivityIntent); + startActivity(intent); finish(); } - public void finishFilesDownload(int result) + private void finishFilesDownload(int result) { if (result == ERR_NO_MORE_FILES) { - if (mCountryIndex != null && mDownloadCountryCheckBox.isChecked()) + if (mCountryIndex != null && mChbDownloadCountry.isChecked()) { - mDownloadCountryCheckBox.setVisibility(View.GONE); - mLocationMsgView.setVisibility(View.GONE); - mMsgView.setText(String.format(getString(R.string.downloading_country_can_proceed), - mMapStorage.countryName(mCountryIndex))); + UiUtils.hide(mChbDownloadCountry, mTvLocation); + mTvMessage.setText(String.format(getString(R.string.downloading_country_can_proceed), + MapStorage.INSTANCE.countryName(mCountryIndex))); - mProgress.setMax((int) mMapStorage.countryRemoteSizeInBytes(mCountryIndex, StorageOptions.MAP_OPTION_MAP_ONLY)); + mProgress.setMax((int) MapStorage.INSTANCE.countryRemoteSizeInBytes(mCountryIndex, StorageOptions.MAP_OPTION_MAP_ONLY)); mProgress.setProgress(0); Framework.downloadCountry(mCountryIndex); @@ -295,78 +430,17 @@ public class DownloadResourcesActivity extends BaseMwmFragmentActivity setAction(PROCEED_TO_MAP); } else - showMapView(); + showMap(); } else { - mMsgView.setText(getErrorMessage(result)); - mMsgView.setTextColor(Color.RED); + mTvMessage.setText(getErrorMessage(result)); + mTvMessage.setTextColor(Color.RED); setAction(TRY_AGAIN); } } - @Override - public void onCountryStatusChanged(MapStorage.Index idx) - { - final int status = mMapStorage.countryStatus(idx); - - if (status == MapStorage.ON_DISK) - showMapView(); - } - - @Override - public void onCountryProgress(MapStorage.Index idx, long current, long total) - { - // Important check - activity can be destroyed - // but notifications from downloading thread are coming. - if (mProgress != null) - mProgress.setProgress((int) current); - } - - private Intent getPackageIntent(String s) - { - return getPackageManager().getLaunchIntentForPackage(s); - } - - private boolean checkLiteProPackages() - { - if (!Yota.isFirstYota() && - (getPackageIntent(Constants.Package.MWM_LITE_PACKAGE) != null || - getPackageIntent(Constants.Package.MWM_SAMSUNG_PACKAGE) != null)) - Toast.makeText(this, R.string.suggest_uninstall_lite, Toast.LENGTH_LONG).show(); - - return false; - } - - @Override - protected void onCreate(Bundle savedInstanceState) - { - super.onCreate(savedInstanceState); - - // Do not turn off the screen while downloading needed resources - getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); - - mApplication = (MWMApplication) getApplication(); - - if (checkLiteProPackages()) - return; - - final boolean dispatched = dispatchIntent(); - if (!dispatched) - parseIntentForKMZFile(); - - setContentView(R.layout.download_resources); - - if (prepareFilesDownload()) - { - setAction(DOWNLOAD); - - if (ConnectionState.isWifiConnected()) - onDownloadClicked(mButton); - } - } - private boolean dispatchIntent() { final Intent intent = getIntent(); @@ -400,180 +474,71 @@ public class DownloadResourcesActivity extends BaseMwmFragmentActivity return null; } - private void parseIntentForKMZFile() + private void parseIntentForKmzFile() { final Intent intent = getIntent(); - if (intent != null) + if (intent == null) + return; + + final Uri data = intent.getData(); + if (data == null) + return; + + String path = null; + File tmpFile = null; + final String scheme = data.getScheme(); + if (scheme != null && !scheme.equalsIgnoreCase(Constants.Url.DATA_SCHEME_FILE)) { - final Uri data = intent.getData(); - if (data != null) + // scheme is "content" or "http" - need to download file first + InputStream input = null; + OutputStream output = null; + + try { - String path = null; - File tmpFile = null; - final String scheme = data.getScheme(); - if (scheme != null && !scheme.equalsIgnoreCase(Constants.Url.DATA_SCHEME_FILE)) + final ContentResolver resolver = getContentResolver(); + final String ext = getExtensionFromMime(resolver.getType(data)); + if (ext != null) { - // scheme is "content" or "http" - need to download file first - InputStream input = null; - OutputStream output = null; - - try - { - final ContentResolver resolver = getContentResolver(); - final String ext = getExtensionFromMime(resolver.getType(data)); - if (ext != null) - { - final String filePath = mApplication.getTempPath() + "Attachment" + ext; - - tmpFile = new File(filePath); - output = new FileOutputStream(tmpFile); - input = resolver.openInputStream(data); - - final byte buffer[] = new byte[Constants.MB / 2]; - int read; - while ((read = input.read(buffer)) != -1) - output.write(buffer, 0, read); - output.flush(); - - path = filePath; - } - } catch (final Exception ex) - { - Log.w(TAG, "Attachment not found or io error: " + ex); - } finally - { - try - { - if (input != null) - input.close(); - if (output != null) - output.close(); - } catch (final IOException ex) - { - Log.w(TAG, "Close stream error: " + ex); - } - } - } - else - path = data.getPath(); + final String filePath = MwmApplication.get().getTempPath() + "Attachment" + ext; - boolean success = false; - if (path != null) - { - Log.d(TAG, "Loading bookmarks file from: " + path); - success = loadKMZFile(path); - } - else - Log.w(TAG, "Can't get bookmarks file from URI: " + data); + tmpFile = new File(filePath); + output = new FileOutputStream(tmpFile); + input = resolver.openInputStream(data); - if (tmpFile != null) - tmpFile.delete(); + final byte buffer[] = new byte[Constants.MB / 2]; + int read; + while ((read = input.read(buffer)) != -1) + output.write(buffer, 0, read); + output.flush(); - Utils.toastShortcut(this, success ? R.string.load_kmz_successful : R.string.load_kmz_failed); + path = filePath; + } + } catch (final Exception ex) + { + Log.w(TAG, "Attachment not found or io error: " + ex); + } finally + { + Utils.closeStream(input); + Utils.closeStream(output); } } - } - - @Override - protected void onDestroy() - { - super.onDestroy(); - - LocationHelper.INSTANCE.removeLocationListener(this); - - if (mMapStorage != null) - mMapStorage.unsubscribe(mSlotId); - } - - @Override - protected void onPause() - { - super.onPause(); - - LocationHelper.INSTANCE.removeLocationListener(this); - } - - @Override - protected void onResume() - { - super.onResume(); - - LocationHelper.INSTANCE.addLocationListener(this); - } - - public void onDownloadProgress(int currentTotal, int currentProgress, int globalTotal, int globalProgress) - { - if (mProgress != null) - mProgress.setProgress(globalProgress); - } - - public void onDownloadFinished(int errorCode) - { - if (errorCode == ERR_DOWNLOAD_SUCCESS) - { - final int res = startNextFileDownload(this); - if (res == ERR_NO_MORE_FILES) - finishFilesDownload(res); - } else - finishFilesDownload(errorCode); - } + path = data.getPath(); - @Override - public void onLocationUpdated(final Location l) - { - if (mCountryIndex == null) + boolean success = false; + if (path != null) { - final double lat = l.getLatitude(); - final double lon = l.getLongitude(); - Log.i(TAG, "Searching for country name at location lat=" + lat + ", lon=" + lon); - - mCountryIndex = Framework.nativeGetCountryIndex(lat, lon); - if (mCountryIndex != null) - { - mLocationMsgView.setVisibility(View.VISIBLE); - - final int countryStatus = mMapStorage.countryStatus(mCountryIndex); - final String name = mMapStorage.countryName(mCountryIndex); - - if (countryStatus == MapStorage.ON_DISK) - mLocationMsgView.setText(String.format(getString(R.string.download_location_map_up_to_date), name)); - else - { - final CheckBox checkBox = (CheckBox) findViewById(R.id.download_country_checkbox); - checkBox.setVisibility(View.VISIBLE); - - String msgViewText; - String checkBoxText; - - if (countryStatus == MapStorage.ON_DISK_OUT_OF_DATE) - { - msgViewText = getString(R.string.download_location_update_map_proposal); - checkBoxText = String.format(getString(R.string.update_country_ask), name); - } - else - { - msgViewText = getString(R.string.download_location_map_proposal); - checkBoxText = String.format(getString(R.string.download_country_ask), name); - } - - mLocationMsgView.setText(msgViewText); - checkBox.setText(checkBoxText); - } - - LocationHelper.INSTANCE.removeLocationListener(this); - } + Log.d(TAG, "Loading bookmarks file from: " + path); + success = loadKmzFile(path); } - } + else + Log.w(TAG, "Can't get bookmarks file from URI: " + data); - @Override - public void onCompassUpdated(long time, double magneticNorth, double trueNorth, double accuracy) - { - } + if (tmpFile != null) + //noinspection ResultOfMethodCallIgnored + tmpFile.delete(); - @Override - public void onLocationError(int errorCode) - { + Utils.toastShortcut(this, success ? R.string.load_kmz_successful : R.string.load_kmz_failed); } private class GeoIntentProcessor implements IntentProcessor @@ -666,7 +631,7 @@ public class DownloadResourcesActivity extends BaseMwmFragmentActivity ParsedMwmRequest.setCurrentRequest(request); Statistics.INSTANCE.trackApiCall(request); - if (!request.isPickPointMode()) + if (!ParsedMwmRequest.isPickPointMode()) mMapTaskToForward = new OpenUrlTask(apiUrl); return true; } @@ -707,10 +672,10 @@ public class DownloadResourcesActivity extends BaseMwmFragmentActivity public boolean processIntent(Intent intent) { final Index index = (Index) intent.getSerializableExtra(EXTRA_COUNTRY_INDEX); - final boolean autoDownload = intent.getBooleanExtra(EXTRA_AUTODOWNLOAD_CONTRY, false); + final boolean autoDownload = intent.getBooleanExtra(EXTRA_AUTODOWNLOAD_COUNTRY, false); if (autoDownload) Statistics.INSTANCE.trackDownloadCountryNotificationClicked(); - mMapTaskToForward = new MWMActivity.ShowCountryTask(index, autoDownload); + mMapTaskToForward = new MwmActivity.ShowCountryTask(index, autoDownload); org.alohalytics.Statistics.logEvent("OpenCountryTaskProcessor::processIntent", new String[]{"autoDownload", String.valueOf(autoDownload)}, LocationHelper.INSTANCE.getLastLocation()); return true; } @@ -728,7 +693,7 @@ public class DownloadResourcesActivity extends BaseMwmFragmentActivity public boolean processIntent(Intent intent) { org.alohalytics.Statistics.logEvent("UpdateCountryProcessor::processIntent"); - mMapTaskToForward = new MWMActivity.UpdateCountryTask(); + mMapTaskToForward = new MwmActivity.UpdateCountryTask(); return true; } } @@ -739,5 +704,5 @@ public class DownloadResourcesActivity extends BaseMwmFragmentActivity private native void cancelCurrentFile(); - private native boolean loadKMZFile(String path); + private native boolean loadKmzFile(String path); } |