Android: add share feature in the context menu

Known issues: the mimetype returned for .odp files is null and share
does not work on these files.

Change-Id: I61e752763a6934657c30fff3cefc57ac0144ec71
This commit is contained in:
Jacobo Aragunde Pérez
2014-12-03 19:07:11 +00:00
parent 60dd2161bb
commit 04df164860
3 changed files with 22 additions and 0 deletions

View File

@@ -2,4 +2,6 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/context_menu_open"
android:title="@string/open"/>
<item android:id="@+id/context_menu_share"
android:title="@string/share"/>
</menu>

View File

@@ -26,5 +26,7 @@
<string name="action_about">About</string>
<string name="action_parts">Parts</string>
<string name="open">Open</string>
<string name="share">Share</string>
<string name="share_via">Share via</string>
</resources>

View File

@@ -46,6 +46,7 @@ import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.webkit.MimeTypeMap;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
@@ -172,6 +173,9 @@ public class LibreOfficeUIActivity extends LOAbout implements ActionBar.OnNaviga
case R.id.context_menu_open:
open(info.position);
return true;
case R.id.context_menu_share:
share(info.position);
return true;
default:
return super.onContextItemSelected(item);
}
@@ -221,6 +225,20 @@ public class LibreOfficeUIActivity extends LOAbout implements ActionBar.OnNaviga
}
}
private void share(int position) {
File file = filePaths[position];
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
Uri uri = Uri.fromFile(file);
String extension = MimeTypeMap.getFileExtensionFromUrl(uri.toString());
sharingIntent.setType(MimeTypeMap.getSingleton()
.getMimeTypeFromExtension(extension));
sharingIntent.putExtra(android.content.Intent.EXTRA_STREAM, uri);
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
file.getName());
startActivity(Intent.createChooser(sharingIntent,
getString(R.string.share_via)));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();