Android Open Source - bluetoothviewer Email Tools

From Project

Back to project page bluetoothviewer.


The source code is released under:

Apache License

If you think the Android project bluetoothviewer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.bluetoothviewer.util;
/*w ww.j a v a 2 s.c om*/
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import net.bluetoothviewer.R;

import java.text.SimpleDateFormat;
import java.util.Date;

public class EmailTools {

    private static final String TAG = EmailTools.class.getSimpleName();

    private static final String MESSAGE_TYPE = "message/rfc822";

    private static final String HORIZONTAL_RULE = "\n\n---\n\n";

    private EmailTools() {
        // prevent creating utility class

    public static void sendDeviceRecording(Context context, String defaultEmail, String deviceName, String recordedContent) {
        String subject = String.format(context.getString(R.string.fmt_subject_recorded_data), deviceName);
        String messageHeader = String.format(context.getString(R.string.fmt_recorded_from), deviceName);

        StringBuilder builder = new StringBuilder();

        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.putExtra(Intent.EXTRA_EMAIL, new String[]{defaultEmail});
        intent.putExtra(Intent.EXTRA_SUBJECT, subject);
        if (!addAttachmentToIntent(context, deviceName, recordedContent, intent)) {

        intent.putExtra(Intent.EXTRA_TEXT, builder.toString());
        launchEmailApp(context, intent);

    private static String getPackageInfoString(Context context) {
        PackageInfo info = getPackageInfo(context);
        return String.format("[App: %s Version: %d/%s]",
                context.getPackageName(), info.versionCode, info.versionName);

    private static PackageInfo getPackageInfo(Context context) {
        String packageName = context.getPackageName();
        PackageManager manager = context.getPackageManager();
        if (manager != null) {
            try {
                return manager.getPackageInfo(packageName, 0);
            } catch (PackageManager.NameNotFoundException e) {
                Log.e(TAG, "Could not get package info", e);
        return new PackageInfo();

    private static boolean addAttachmentToIntent(Context context, String deviceName, String recordedContent, Intent intent) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("_yyyyMMdd_HHmm");
        String filename = deviceName + dateFormat.format(new Date()) + ".dat";
        try {
            FileOutputStream ostream = context.openFileOutput(filename, Context.MODE_WORLD_READABLE);
            File attachment = context.getFileStreamPath(filename);
            intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(attachment));
            return true;
        } catch (IOException e) {
            Log.e(TAG, "could not create temp file for attachment :(", e);
            return false;

    private static void launchEmailApp(Context context, Intent intent) {
        try {
            context.startActivity(Intent.createChooser(intent, context.getString(R.string.email_client_chooser)));
        } catch (ActivityNotFoundException ex) {
            Toast.makeText(context, context.getString(R.string.no_email_client), Toast.LENGTH_SHORT).show();

Java Source Code List