[android] Share bookmarks via E-Mail.

This commit is contained in:
vng 2013-03-14 19:16:02 +03:00 committed by Alex Zolotarev
parent 5b94390e01
commit f4ab0a6827
8 changed files with 201 additions and 4 deletions

View file

@ -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"

View file

@ -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));
}
}

View file

@ -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;
}
}

View file

@ -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"

View file

@ -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);
}

View file

@ -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(),

View file

@ -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);
}
}
}

View file

@ -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);
}