[android] Upload osm changes when application is hidden.

This commit is contained in:
Dmitry Yunitsky 2016-01-29 20:15:55 +03:00 committed by Sergey Yershov
parent d7a10cffa8
commit 51d302dd67
7 changed files with 78 additions and 14 deletions

View file

@ -94,4 +94,18 @@ Java_com_mapswithme_maps_editor_Editor_nativeGetNearbyStreets(JNIEnv * env, jcla
env->SetObjectArrayElement(jStreets, i, jni::TScopedLocalRef(env, jni::ToJavaString(env, streets[i])).get());
return jStreets;
}
JNIEXPORT jboolean JNICALL
Java_com_mapswithme_maps_editor_Editor_nativeHasSomethingToUpload(JNIEnv * env, jclass clazz)
{
return Editor::Instance().HaveSomethingToUpload();
}
JNIEXPORT void JNICALL
Java_com_mapswithme_maps_editor_Editor_nativeUploadChanges(JNIEnv * env, jclass clazz, jstring token, jstring secret)
{
Editor::Instance().UploadChanges(jni::ToNativeString(env, token),
jni::ToNativeString(env, secret),
{{"version", "TODO android"}}, nullptr);
}
} // extern "C"

View file

@ -17,6 +17,7 @@ import com.mapswithme.country.CountryItem;
import com.mapswithme.maps.background.AppBackgroundTracker;
import com.mapswithme.maps.background.Notifier;
import com.mapswithme.maps.bookmarks.data.BookmarkManager;
import com.mapswithme.maps.editor.Editor;
import com.mapswithme.maps.location.TrackRecorder;
import com.mapswithme.maps.sound.TtsPlayer;
import com.mapswithme.util.Config;
@ -117,6 +118,7 @@ public class MwmApplication extends Application
mPrefs = getSharedPreferences(getString(R.string.pref_file_name), MODE_PRIVATE);
mBackgroundTracker = new AppBackgroundTracker();
TrackRecorder.init();
Editor.init();
}
public void initNativeCore()

View file

@ -13,12 +13,14 @@ import com.mapswithme.country.ActiveCountryTree;
import com.mapswithme.maps.Framework;
import com.mapswithme.maps.MwmApplication;
import com.mapswithme.maps.R;
import com.mapswithme.maps.editor.Editor;
import com.mapswithme.util.LocationUtils;
public class WorkerService extends IntentService
{
private static final String ACTION_CHECK_UPDATE = "com.mapswithme.maps.action.update";
private static final String ACTION_DOWNLOAD_COUNTRY = "com.mapswithme.maps.action.download_country";
private static final String ACTION_UPLOAD_OSM_CHANGES = "com.mapswithme.maps.action.upload_osm_changes";
private static final MwmApplication APP = MwmApplication.get();
private static final SharedPreferences PREFS = MwmApplication.prefs();
@ -26,8 +28,6 @@ public class WorkerService extends IntentService
/**
* Starts this service to check map updates available with the given parameters. If the
* service is already performing a task this action will be queued.
*
* @see IntentService
*/
static void startActionCheckUpdate(Context context)
{
@ -39,8 +39,6 @@ public class WorkerService extends IntentService
/**
* Starts this service to check if map download for current location is available. If the
* service is already performing a task this action will be queued.
*
* @see IntentService
*/
static void startActionDownload(Context context)
{
@ -49,6 +47,16 @@ public class WorkerService extends IntentService
context.startService(intent);
}
/**
* Starts this service to upload map edits to osm servers.
*/
public static void startActionUploadOsmChanges()
{
final Intent intent = new Intent(MwmApplication.get(), WorkerService.class);
intent.setAction(WorkerService.ACTION_UPLOAD_OSM_CHANGES);
MwmApplication.get().startService(intent);
}
public WorkerService()
{
super("WorkerService");
@ -76,10 +84,12 @@ public class WorkerService extends IntentService
case ACTION_DOWNLOAD_COUNTRY:
handleActionCheckLocation();
break;
case ACTION_UPLOAD_OSM_CHANGES:
handleActionUploadOsmChanges();
break;
}
}
}
private static void handleActionCheckUpdate()
{
if (!Framework.nativeIsDataVersionChanged() || ActiveCountryTree.isLegacyMode())
@ -113,6 +123,11 @@ public class WorkerService extends IntentService
}
}
private void handleActionUploadOsmChanges()
{
Editor.uploadChanges();
}
/**
* Adds notification if current location isnt expired.
*

View file

@ -5,6 +5,7 @@ import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import java.lang.annotation.Retention;
@ -131,6 +132,7 @@ public class MapObject implements Parcelable
return mIsDroppedPin;
}
@Nullable
public String getMetadata(Metadata.MetadataType type)
{
return mMetadata.getMetadata(type);

View file

@ -4,6 +4,7 @@ import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;
@ -135,9 +136,7 @@ public class Metadata implements Parcelable
return true;
}
/**
* @return null if metadata doesn't exist
*/
@Nullable
public String getMetadata(MetadataType type)
{
return mMetadataMap.get(type);

View file

@ -1,9 +1,11 @@
package com.mapswithme.maps.editor;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.WorkerThread;
import com.mapswithme.maps.bookmarks.data.Metadata;
import com.mapswithme.maps.MwmApplication;
import com.mapswithme.maps.background.AppBackgroundTracker;
import com.mapswithme.maps.background.WorkerService;
/**
@ -12,8 +14,23 @@ import com.mapswithme.maps.bookmarks.data.Metadata;
*/
public final class Editor
{
private static AppBackgroundTracker.OnTransitionListener sOsmUploader = new AppBackgroundTracker.OnTransitionListener()
{
@Override
public void onTransit(boolean foreground)
{
if (!foreground)
WorkerService.startActionUploadOsmChanges();
}
};
private Editor() {}
public static void init()
{
MwmApplication.backgroundTracker().addListener(sOsmUploader);
}
public static boolean hasEditableAttributes()
{
return Editor.nativeGetEditableMetadata().length != 0 ||
@ -21,7 +38,15 @@ public final class Editor
Editor.nativeIsNameEditable();
}
public static native @NonNull int[] nativeGetEditableMetadata();
public static void uploadChanges()
{
if (nativeHasSomethingToUpload() &&
OsmOAuth.isAuthorized())
nativeUploadChanges(OsmOAuth.getAuthToken(), OsmOAuth.getAuthSecret());
}
@NonNull
public static native int[] nativeGetEditableMetadata();
public static native void nativeSetMetadata(int type, String value);
@ -31,7 +56,13 @@ public final class Editor
public static native boolean nativeIsNameEditable();
@NonNull
public static native String[] nativeGetNearbyStreets();
public static native void nativeSetName(String name);
public static native @NonNull String[] nativeGetNearbyStreets();
public static native boolean nativeHasSomethingToUpload();
@WorkerThread
public static native void nativeUploadChanges(String token, String secret);
}

View file

@ -137,7 +137,7 @@ public class EditorHostFragment extends BaseMwmToolbarFragment
editMapObject();
break;
case CUISINE:
final String cuisine = ((CuisineFragment) getChildFragmentManager().findFragmentByTag(CuisineFragment.class.getName())).getCuisine();
String cuisine = ((CuisineFragment) getChildFragmentManager().findFragmentByTag(CuisineFragment.class.getName())).getCuisine();
mEditedObject.addMetadata(Metadata.MetadataType.FMD_CUISINE.toInt(), cuisine);
editMapObject();
break;
@ -146,7 +146,8 @@ public class EditorHostFragment extends BaseMwmToolbarFragment
Editor.nativeSetMetadata(Metadata.MetadataType.FMD_PHONE_NUMBER.toInt(), editorFragment.getPhone());
Editor.nativeSetMetadata(Metadata.MetadataType.FMD_WEBSITE.toInt(), editorFragment.getWebsite());
Editor.nativeSetMetadata(Metadata.MetadataType.FMD_EMAIL.toInt(), editorFragment.getEmail());
Editor.nativeSetMetadata(Metadata.MetadataType.FMD_CUISINE.toInt(), mEditedObject.getMetadata(Metadata.MetadataType.FMD_CUISINE));
cuisine = mEditedObject.getMetadata(Metadata.MetadataType.FMD_CUISINE);
Editor.nativeSetMetadata(Metadata.MetadataType.FMD_CUISINE.toInt(), cuisine == null ? "" : cuisine);
Editor.nativeSetMetadata(Metadata.MetadataType.FMD_INTERNET.toInt(), editorFragment.getWifi());
Editor.nativeSetName(editorFragment.getName());
Editor.nativeEditFeature(editorFragment.getStreet(), editorFragment.getHouseNumber());