diff options
author | vng <viktor.govako@gmail.com> | 2013-03-14 20:16:02 +0400 |
---|---|---|
committer | Alex Zolotarev <alex@maps.me> | 2015-09-23 01:52:14 +0300 |
commit | f4ab0a6827763881477326ea87443c596ba2788c (patch) | |
tree | 12cd5520156c0979973a2fdbe162b14ad6dec80c /android | |
parent | 5b94390e0192362742576f9dddf43d1c4a387aa6 (diff) |
[android] Share bookmarks via E-Mail.
Diffstat (limited to 'android')
8 files changed, 201 insertions, 4 deletions
diff --git a/android/MapsWithMePro/AndroidManifest.xml b/android/MapsWithMePro/AndroidManifest.xml index effd20b2b0..8de4324ee6 100644 --- a/android/MapsWithMePro/AndroidManifest.xml +++ b/android/MapsWithMePro/AndroidManifest.xml @@ -25,19 +25,52 @@ <supports-screens android:largeScreens="true" android:xlargeScreens="true"/> <application android:icon="@drawable/ic_launcher" - android:label="@string/app_name" - android:name="com.mapswithme.maps.MWMApplication"> - + android:label="@string/app_name" + android:name="com.mapswithme.maps.MWMApplication"> + <activity android:name="com.mapswithme.maps.DownloadResourcesActivity" android:label="@string/app_name" android:screenOrientation="behind" android:theme="@style/MWMNoTitle" android:configChanges="orientation|screenLayout|screenSize"> + <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> + + <intent-filter> + <action android:name="android.intent.action.VIEW" /> + <category android:name="android.intent.category.DEFAULT" /> + <data android:host="*" + android:mimeType="application/vnd.google-earth.kmz" /> + </intent-filter> + + <intent-filter> + <action android:name="android.intent.action.VIEW" /> + <category android:name="android.intent.category.DEFAULT" /> + <data android:host="*" + android:mimeType="application/vnd.google-earth.kml+xml" /> + </intent-filter> + + <intent-filter> + <action android:name="android.intent.action.VIEW" /> + <category android:name="android.intent.category.DEFAULT" /> + <data android:scheme="file" + android:host="*" + android:pathPattern=".*\\.kmz" /> + </intent-filter> + + <intent-filter> + <action android:name="android.intent.action.VIEW" /> + <category android:name="android.intent.category.DEFAULT" /> + <data android:scheme="file" + android:host="*" + android:pathPattern=".*\\.kml" /> + </intent-filter> + </activity> + <activity android:name="com.mapswithme.maps.MWMActivity" android:label="@string/app_name" android:screenOrientation="behind" diff --git a/android/jni/com/mapswithme/maps/DownloadResourcesActivity.cpp b/android/jni/com/mapswithme/maps/DownloadResourcesActivity.cpp index dff6e90475..358966988b 100644 --- a/android/jni/com/mapswithme/maps/DownloadResourcesActivity.cpp +++ b/android/jni/com/mapswithme/maps/DownloadResourcesActivity.cpp @@ -318,4 +318,11 @@ extern "C" else return 0; } + + JNIEXPORT void JNICALL + Java_com_mapswithme_maps_DownloadResourcesActivity_loadKMZFile( + JNIEnv * env, jobject thiz, jstring path) + { + g_framework->NativeFramework()->AddBookmarksFile(jni::ToNativeString(env, path)); + } } diff --git a/android/jni/com/mapswithme/maps/bookmarks/data/BookmarkManager.cpp b/android/jni/com/mapswithme/maps/bookmarks/data/BookmarkManager.cpp index 0be31223e7..6b2ad4adf9 100644 --- a/android/jni/com/mapswithme/maps/bookmarks/data/BookmarkManager.cpp +++ b/android/jni/com/mapswithme/maps/bookmarks/data/BookmarkManager.cpp @@ -2,6 +2,9 @@ #include "../../../core/jni_helper.hpp" +#include "../../../../../../../coding/zip_creator.hpp" + + namespace { ::Framework * frm() { return g_framework->NativeFramework(); } @@ -83,5 +86,20 @@ extern "C" { BookmarkAndCategory const bac = frm()->GetBookmark(m2::PointD(px, py)); return jni::GetNewPoint(env, m2::PointI(bac.first, bac.second)); - } + } + + JNIEXPORT jstring JNICALL + Java_com_mapswithme_maps_bookmarks_data_BookmarkManager_saveToKMZFile( + JNIEnv * env, jobject thiz, jint catID, jstring tmpPath) + { + BookmarkCategory * pCat = frm()->GetBmCategory(catID); + if (pCat) + { + string const name = pCat->GetName(); + if (CreateZipFromPathDeflatedAndDefaultCompression(pCat->GetFileName(), jni::ToNativeString(env, tmpPath) + name + ".kmz")) + return jni::ToJavaString(env, name); + } + + return 0; + } } diff --git a/android/res/layout/bookmarks_list.xml b/android/res/layout/bookmarks_list.xml index e7e1b6dae9..045074fb3f 100644 --- a/android/res/layout/bookmarks_list.xml +++ b/android/res/layout/bookmarks_list.xml @@ -23,6 +23,13 @@ android:layout_height="wrap_content" android:text="@string/bookmarks" /> + <Button + android:layout_width="fill_parent" + android:layout_height="wrap_content" + android:layout_margin="5dp" + android:onClick="onSendEMail" + android:text="@string/share_by_email" /> + <TextView android:id="@+id/bookmark_usage_hint" android:layout_width="fill_parent" diff --git a/android/src/com/mapswithme/maps/DownloadResourcesActivity.java b/android/src/com/mapswithme/maps/DownloadResourcesActivity.java index 685efc0b86..be6432773a 100644 --- a/android/src/com/mapswithme/maps/DownloadResourcesActivity.java +++ b/android/src/com/mapswithme/maps/DownloadResourcesActivity.java @@ -1,11 +1,17 @@ package com.mapswithme.maps; import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import android.app.Activity; import android.content.ActivityNotFoundException; +import android.content.ContentResolver; import android.content.Intent; import android.graphics.Color; +import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.view.View; @@ -335,6 +341,8 @@ public class DownloadResourcesActivity extends Activity implements LocationServi if (checkLiteProPackages(isPro)) return; + parseIntentForKMZFile(); + setContentView(R.layout.download_resources); // Create sdcard folder if it doesn't exist @@ -352,6 +360,94 @@ public class DownloadResourcesActivity extends Activity implements LocationServi } } + private String getExtensionFromMime(String mime) + { + final int i = mime.lastIndexOf('.'); + if (i == -1) + return null; + + mime = mime.substring(i+1); + if (mime.equalsIgnoreCase("kmz")) + return ".kmz"; + else if (mime.equalsIgnoreCase("kml+xml")) + return ".kml"; + else + return null; + } + + private void parseIntentForKMZFile() + { + final Intent intent = getIntent(); + if (intent != null) + { + final Uri data = intent.getData(); + if (data != null) + { + String path = null; + File tmpFile = null; + if (!data.getScheme().equalsIgnoreCase("file")) + { + // 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.getExtAppDirectoryPath("tmp") + "Attachment" + ext; + + tmpFile = new File(filePath); + output = new FileOutputStream(tmpFile); + input = resolver.openInputStream(data); + + byte buffer[] = new byte[512 * 1024]; + int read; + while ((read = input.read(buffer)) != -1) + output.write(buffer, 0, read); + output.flush(); + + path = filePath; + } + } + catch (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 (IOException ex) + { + Log.w(TAG, "Close stream error: " + ex); + } + } + } + else + path = data.getPath(); + + if (path != null) + { + Log.d(TAG, "Loading bookmarks file from: " + path); + loadKMZFile(path); + } + else + Log.w(TAG, "Can't get bookmarks file from URI: " + data.getPath()); + + if (tmpFile != null) + tmpFile.delete(); + } + } + } + @Override protected void onStart() { @@ -481,4 +577,5 @@ public class DownloadResourcesActivity extends Activity implements LocationServi private native int startNextFileDownload(Object observer); private native Index findIndexByPos(double lat, double lon); private native void cancelCurrentFile(); + private native void loadKMZFile(String path); } diff --git a/android/src/com/mapswithme/maps/MWMApplication.java b/android/src/com/mapswithme/maps/MWMApplication.java index 4ad1044d0d..b5c077f843 100644 --- a/android/src/com/mapswithme/maps/MWMApplication.java +++ b/android/src/com/mapswithme/maps/MWMApplication.java @@ -100,6 +100,7 @@ public class MWMApplication extends android.app.Application implements MapStorag // Create folders if they don't exist new File(extStoragePath).mkdirs(); new File(extTmpPath).mkdirs(); + new File(getExtAppDirectoryPath("tmp")).mkdir(); // init native framework nativeInit(getApkPath(), diff --git a/android/src/com/mapswithme/maps/bookmarks/BookmarkListActivity.java b/android/src/com/mapswithme/maps/bookmarks/BookmarkListActivity.java index c698769f90..b8d5ccc204 100644 --- a/android/src/com/mapswithme/maps/bookmarks/BookmarkListActivity.java +++ b/android/src/com/mapswithme/maps/bookmarks/BookmarkListActivity.java @@ -1,9 +1,11 @@ package com.mapswithme.maps.bookmarks; import android.content.Intent; +import android.net.Uri; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; +import android.util.Log; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.MenuInflater; @@ -26,6 +28,7 @@ import com.mapswithme.maps.bookmarks.data.ParcelablePoint; public class BookmarkListActivity extends AbstractBookmarkListActivity { + public static final String TAG = "BookmarkListActivity"; public static final String EDIT_CONTENT = "edit_content"; private EditText mSetName; @@ -185,4 +188,33 @@ public class BookmarkListActivity extends AbstractBookmarkListActivity if (mPinAdapter != null) mPinAdapter.startLocationUpdate(); } + + public void onSendEMail(View v) + { + String path = ((MWMApplication) getApplication()).getExtAppDirectoryPath("tmp"); + final String name = mManager.saveToKMZFile(mEditedSet.getId(), path); + if (name == null) + { + // some error occured + return; + } + + final Intent intent = new Intent(Intent.ACTION_SEND); + intent.setType("message/rfc822"); + intent.putExtra(android.content.Intent.EXTRA_SUBJECT, getString(R.string.share_bookmarks_email_subject)); + intent.putExtra(android.content.Intent.EXTRA_TEXT, String.format(getString(R.string.share_bookmarks_email_body), name)); + + path = path + name + ".kmz"; + Log.d(TAG, "KMZ file path = " + path); + intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + path)); + + try + { + startActivity(Intent.createChooser(intent, getString(R.string.share_by_email))); + } + catch (Exception ex) + { + Log.i(TAG, "Can't run E-Mail activity" + ex); + } + } } diff --git a/android/src/com/mapswithme/maps/bookmarks/data/BookmarkManager.java b/android/src/com/mapswithme/maps/bookmarks/data/BookmarkManager.java index 5e6b405376..b7ef899f7c 100644 --- a/android/src/com/mapswithme/maps/bookmarks/data/BookmarkManager.java +++ b/android/src/com/mapswithme/maps/bookmarks/data/BookmarkManager.java @@ -155,4 +155,6 @@ public class BookmarkManager return getAddressInfo(px.x, px.y); } private native AddressInfo getAddressInfo(double px, double py); + + public native String saveToKMZFile(int catID, String tmpPath); } |