Skip to content

Commit

Permalink
More robust email creation
Browse files Browse the repository at this point in the history
  • Loading branch information
pgmccann committed May 8, 2018
1 parent e6169bd commit 01b215d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 7 deletions.
10 changes: 10 additions & 0 deletions catch/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_api_key"/>

<provider
android:name=".providers.GenericFileProvider"
android:authorities="uk.ac.masts.sifids"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>

</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
Expand Down Expand Up @@ -55,6 +57,7 @@
import uk.ac.masts.sifids.entities.FisheryOffice;
import uk.ac.masts.sifids.entities.Gear;
import uk.ac.masts.sifids.entities.Port;
import uk.ac.masts.sifids.providers.GenericFileProvider;

/**
* Created by pgm5 on 21/02/2018.
Expand Down Expand Up @@ -344,12 +347,16 @@ public void onClick(View view) {
}

private void createAndEmailFile() {
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(Fish1Form.MAILTO, fish1Form.getEmail(), null));
Intent emailIntent = new Intent();
emailIntent.setAction(Intent.ACTION_SEND);
emailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
emailIntent.setType("vnd.android.cursor.dir/email");
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{fish1Form.getEmail()});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.fish_1_form_email_subject));
emailIntent.putExtra(Intent.EXTRA_TEXT, getString(R.string.fish_1_form_email_text));
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{fish1Form.getEmail()});
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(String.format(Fish1Form.ATTACHMENT_URL, createFileToSend().getAbsoluteFile())));
startActivity(Intent.createChooser(emailIntent, getString(R.string.fish_1_form_email_intent_title)));
emailIntent.putExtra(Intent.EXTRA_STREAM, GenericFileProvider.getUriForFile(this, "uk.ac.masts.sifids", createFileToSend()));
startActivityForResult(emailIntent, 101);
}

private void saveForm() {
Expand Down Expand Up @@ -424,6 +431,7 @@ public void run() {

}
Intent i = new Intent(EditFish1FormActivity.this, Fish1FormsActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
EditFish1FormActivity.this.finish();
EditFish1FormActivity.this.startActivity(i);
}
Expand Down Expand Up @@ -545,11 +553,15 @@ public void run() {
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
if (
requestCode == PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE
if (requestCode == PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE
&& grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
createAndEmailFile();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
createAndEmailFile();
}
}, 500);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ public void run() {
}
Intent i = new Intent(EditFish1FormRowActivity.this, EditFish1FormActivity.class);
i.putExtra(Fish1Form.ID, EditFish1FormRowActivity.this.formId);
i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
EditFish1FormRowActivity.this.finish();
EditFish1FormRowActivity.this.startActivity(i);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package uk.ac.masts.sifids.providers;

import android.support.v4.content.FileProvider;

public class GenericFileProvider extends FileProvider {
}
4 changes: 4 additions & 0 deletions catch/src/main/res/xml/provider_paths.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>

0 comments on commit 01b215d

Please sign in to comment.