it.tidalwave.bluebill.mobile.android.document.AndroidDocumentSender Maven / Gradle / Ivy
The newest version!
/***********************************************************************************************************************
*
* blueBill Mobile - Android - open source birding
* Copyright (C) 2009-2011 by Tidalwave s.a.s. (http://www.tidalwave.it)
*
***********************************************************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
***********************************************************************************************************************
*
* WWW: http://bluebill.tidalwave.it/mobile
* SCM: https://java.net/hg/bluebill-mobile~android-src
*
**********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.android.document;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;
import javax.inject.Provider;
import java.util.Collection;
import java.io.File;
import java.io.IOException;
import it.tidalwave.netbeans.util.Locator;
import it.tidalwave.mobile.io.FileSystem;
import it.tidalwave.mobile.io.MasterFileSystem;
import it.tidalwave.semantic.document.AttachmentZipGenerator;
import it.tidalwave.semantic.document.Sendable;
import it.tidalwave.semantic.document.Document;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import it.tidalwave.bluebill.mobile.android.observation.AndroidObservationsViewController;
import it.tidalwave.util.NotFoundException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import static org.openide.util.NbBundle.*;
import static it.tidalwave.semantic.document.Document.*;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
* @version $Id$
*
**********************************************************************************************************************/
@Immutable @RequiredArgsConstructor @Slf4j
/* package */ class AndroidDocumentSender implements Sendable
{
private static final Class _ = AndroidObservationsViewController.class;
private static final String ATTACHMENTS_FILE_NAME = "spool/attachments.zip";
private final Document document;
@Nonnull
private Provider masterFileSystem = Locator.createProviderFor(MasterFileSystem.class);
/*******************************************************************************************************************
*
* {@inheritDoc}
*
******************************************************************************************************************/
public void sendTo (final @Nonnull Object context, final @Nonnull Collection recipients)
throws IOException
{
log.info("sendTo({}) - {}", recipients, document);
// It seems that using the internal file system it doesn't work
final FileSystem fileSystem = masterFileSystem.get().getExternalFileSystem();
final String[] recipientsAsArray = recipients.toArray(new String[0]);
final Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_EMAIL, recipientsAsArray);
sendIntent.putExtra(Intent.EXTRA_PHONE_NUMBER, recipientsAsArray);
try
{
sendIntent.putExtra(Intent.EXTRA_SUBJECT, document.get(SUBJECT));
sendIntent.putExtra(Intent.EXTRA_TEXT, document.get(BODY));
sendIntent.setType(document.get(MIME_TYPE));
}
catch (NotFoundException e)
{
throw new RuntimeException(e);
}
if (!document.getMultiple(ATTACHMENT).isEmpty())
{
final File attachmentFile = fileSystem.getFile(ATTACHMENTS_FILE_NAME);
log.debug(">>>> there are attachments, generating a zipfile at {}...", attachmentFile);
document.accept(new AttachmentZipGenerator(attachmentFile));
sendIntent.setType("application/zip");
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(attachmentFile));
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
// FIXME: how to delete after sent? We could listen for Activity completion notification,
// but the user might decide to store the document as draft.
}
((Activity)context).startActivity(Intent.createChooser(sendIntent, getMessage(_, "pickAService")));
}
}