Skip to content

Commit

Permalink
Merge pull request #96 from jmpascal/master
Browse files Browse the repository at this point in the history
MOBILE-1631
  • Loading branch information
JM.PASCAL committed Apr 24, 2013
2 parents c089b6a + 9b9e1c0 commit b9837c0
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ private void checkForUpdates()
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == PublicIntent.REQUESTCODE_DECRYPTED)
if (requestCode == PublicIntent.REQUESTCODE_DECRYPTED && CipherUtils.isEncryptionActive(this))
{
String filename = PreferenceManager.getDefaultSharedPreferences(this).getString(
GeneralPreferences.REQUIRES_ENCRYPT, "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public static void download(final Activity activity, final Node node)
Uri uri = Uri.parse(((AbstractDocumentFolderServiceImpl) SessionUtils.getSession(activity).getServiceRegistry()
.getDocumentFolderService()).getDownloadUrl((Document) node));

File dlFile = getDownloadFile(activity, node);
File dlFile = getDownloadFile(activity, node.getName());
if (dlFile == null)
{
MessengerManager.showLongToast(activity, activity.getString(R.string.sdinaccessible));
Expand Down Expand Up @@ -301,11 +301,17 @@ public static void download(final Activity activity, final Node node)
manager.enqueue(request);
}

public static File getDownloadFile(final Activity activity, final Node node)
/**
* Provides a local file where to store preview. This file may be deleted.
* @param activity
* @param node
* @return
*/
public static File getPreviewFile(final Activity activity, final Node node)
{
if (activity != null && node != null && SessionUtils.getAccount(activity) != null)
{
File folder = StorageManager.getDownloadFolder(activity, SessionUtils.getAccount(activity).getUrl(), SessionUtils.getAccount(activity).getUsername());
File folder = StorageManager.getTempFolder(activity, SessionUtils.getAccount(activity).getUrl(), SessionUtils.getAccount(activity).getUsername());
if (folder != null)
{
return new File(folder, node.getName());
Expand All @@ -315,6 +321,26 @@ public static File getDownloadFile(final Activity activity, final Node node)
return null;
}

/**
* Provides file where to store permanently (inside Download) a document downloaded.
* @param activity
* @param node
* @return
*/
public static File getDownloadFile(final Activity activity, final String name)
{
if (activity != null && name != null && SessionUtils.getAccount(activity) != null)
{
File folder = StorageManager.getDownloadFolder(activity, SessionUtils.getAccount(activity).getUrl(), SessionUtils.getAccount(activity).getUsername());
if (folder != null)
{
return new File(folder, name);
}
}

return null;
}

public static class DownloadReceiver extends BroadcastReceiver
{
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ private void onValidateTags()
}
}

//TODO Dead code ?
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.alfresco.mobile.android.application.R;
import org.alfresco.mobile.android.application.fragments.actions.NodeActions;
import org.alfresco.mobile.android.application.fragments.properties.DetailsFragment;
import org.alfresco.mobile.android.application.manager.StorageManager;
import org.alfresco.mobile.android.application.preferences.GeneralPreferences;
import org.alfresco.mobile.android.application.utils.CipherUtils;
import org.alfresco.mobile.android.application.utils.EmailUtils;
Expand Down Expand Up @@ -117,11 +118,11 @@ public Dialog onCreateDialog(final Bundle savedInstanceState)
File dlFile = null;
if (getArguments().containsKey(ARGUMENT_TEMPFILE))
{
dlFile = new File (getArguments().getString(ARGUMENT_TEMPFILE));
dlFile = new File(getArguments().getString(ARGUMENT_TEMPFILE));
}
else
{
dlFile = getDownloadFile();
dlFile = getTempFile();
}

if (dlFile != null)
Expand Down Expand Up @@ -153,6 +154,7 @@ public void onClick(DialogInterface dialog, int item)
return dialog;
}

//TODO Dead code ?
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
Expand All @@ -164,6 +166,12 @@ public void onActivityResult(int requestCode, int resultCode, Intent data)
GeneralPreferences.REQUIRES_ENCRYPT, "");
if (filename != null && filename.length() > 0)
{
File f = new File(filename);
if (StorageManager.isTempFile(getActivity(), f))
{
if (f.delete()) { return; }
}

if (!CipherUtils.encryptFile(getActivity(), filename, true))
{
MessengerManager.showLongToast(getActivity(), getString(R.string.encryption_failed));
Expand All @@ -185,10 +193,10 @@ public void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
}

private File getDownloadFile()
private File getTempFile()
{
if (SessionUtils.getAccount(getActivity()) == null) { return null; }
File tmpFile = NodeActions.getDownloadFile(getActivity(), doc);
File tmpFile = NodeActions.getPreviewFile(getActivity(), doc);
if (tmpFile != null)
{
org.alfresco.mobile.android.api.utils.IOUtils.ensureOrCreatePathAndFile(tmpFile);
Expand Down Expand Up @@ -226,8 +234,8 @@ private void executeAction()
switch (action)
{
case ACTION_OPEN:
MessengerManager.showToast(getActivity(), getActivity().getText(R.string.download_complete)
+ " " + IOUtils.getOriginalFromTempFilename(contentFile.getFileName()));
MessengerManager.showToast(getActivity(), getActivity().getText(R.string.download_complete) + " "
+ IOUtils.getOriginalFromTempFilename(contentFile.getFileName()));

DetailsFragment detailsFragment = (DetailsFragment) getFragmentManager().findFragmentByTag(
DetailsFragment.TAG);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,12 @@ && getArguments().getBoolean(CreateDocumentDialogFragment.ARGUMENT_IS_CREATION))
// An error occurs, notify the user.
ProgressNotification.updateProgress(name, ProgressNotification.FLAG_UPLOAD_IMPORT_ERROR);
}

//Delete the file if it's a temporary file
if (StorageManager.isTempFile(getActivity(), contentFile.getFile()))
{
contentFile.getFile().delete();
}
}

// The upload is done even if it's an error.
Expand Down Expand Up @@ -320,6 +326,12 @@ && getArguments().getBoolean(CreateDocumentDialogFragment.ARGUMENT_IS_CREATION))
}
}
}

//Delete the file if it's a temporary file
if (StorageManager.isTempFile(getActivity(), f.getFile()))
{
contentFile.getFile().delete();
}

// The upload is done. Remove the fragment + the loader
// associated.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ public class DetailsFragment extends MetadataFragment implements OnTabChangeList
protected Integer tabSelection = null;

protected File tempFile = null;

protected PreviewFragment replacementPreviewFragment = null;

public DetailsFragment()
{
}
Expand Down Expand Up @@ -491,17 +491,17 @@ public void onClick(DialogInterface dialog, int item)
public void openin()
{
Bundle b = new Bundle();

if (CipherUtils.isEncryptionActive(getActivity()))
{
tempFile = IOUtils.makeTempFile(NodeActions.getDownloadFile(getActivity(), node));
tempFile = IOUtils.makeTempFile(NodeActions.getPreviewFile(getActivity(), node));
if (replacementPreviewFragment != null)
{
replacementPreviewFragment.setTempFile (tempFile);
replacementPreviewFragment.setTempFile(tempFile);
}
b.putString(DownloadDialogFragment.ARGUMENT_TEMPFILE, tempFile.getPath());
}

b.putParcelable(DownloadDialogFragment.ARGUMENT_DOCUMENT, (Document) node);
b.putInt(DownloadDialogFragment.ARGUMENT_ACTION, DownloadDialogFragment.ACTION_OPEN);
DialogFragment frag = new DownloadDialogFragment();
Expand Down Expand Up @@ -529,7 +529,7 @@ public void onActivityResult(int requestCode, int resultCode, Intent data)
{
tempFile = replacementPreviewFragment.getTempFile();
}
final File dlFile = (tempFile != null ? tempFile : NodeActions.getDownloadFile(getActivity(), node) );
final File dlFile = (tempFile != null ? tempFile : NodeActions.getPreviewFile(getActivity(), node));

long datetime = dlFile.lastModified();
Date d = new Date(datetime);
Expand All @@ -556,15 +556,25 @@ public void onClick(DialogInterface dialog, int item)
{
public void onClick(DialogInterface dialog, int item)
{
if (StorageManager.shouldEncryptDecrypt(getActivity(), dlFile.getPath()))
// File has been changed but is in tmp folder ==> Save to download + encrypt if necessary
File renameFile = IOUtils.renameTimeStampFile(
StorageManager.getDownloadFolder(getActivity(),
SessionUtils.getAccount(getActivity()).getUrl(),
SessionUtils.getAccount(getActivity()).getUsername()), dlFile);
if (renameFile.getName().equals(dlFile.getName()))
{
Log.w(TAG, "Unable to rename the document");
}

if (StorageManager.shouldEncryptDecrypt(getActivity(), renameFile.getPath()))
{
FragmentTransaction fragmentTransaction = getActivity().getFragmentManager()
.beginTransaction();
EncryptionDialogFragment fragment = EncryptionDialogFragment.encrypt(dlFile.getPath());
EncryptionDialogFragment fragment = EncryptionDialogFragment.encrypt(renameFile
.getPath());
fragmentTransaction.add(fragment, fragment.getFragmentTransactionTag());
fragmentTransaction.commit();
}

dialog.dismiss();
}
});
Expand All @@ -573,12 +583,17 @@ public void onClick(DialogInterface dialog, int item)
}
else
{
if (StorageManager.shouldEncryptDecrypt(getActivity(), dlFile.getPath()))
// Delete file
if (!dlFile.delete())
{
FragmentTransaction fragmentTransaction = getActivity().getFragmentManager().beginTransaction();
EncryptionDialogFragment fragment = EncryptionDialogFragment.encrypt(dlFile.getPath());
fragmentTransaction.add(fragment, fragment.getFragmentTransactionTag());
fragmentTransaction.commit();
if (StorageManager.shouldEncryptDecrypt(getActivity(), dlFile.getPath()))
{
FragmentTransaction fragmentTransaction = getActivity().getFragmentManager()
.beginTransaction();
EncryptionDialogFragment fragment = EncryptionDialogFragment.encrypt(dlFile.getPath());
fragmentTransaction.add(fragment, fragment.getFragmentTransactionTag());
fragmentTransaction.commit();
}
}
}
break;
Expand Down Expand Up @@ -867,7 +882,8 @@ public void addPreview(Node n, int layoutId, boolean backstack)
{
replacementPreviewFragment = PreviewFragment.newInstance(n);
replacementPreviewFragment.setSession(alfSession);
FragmentDisplayer.replaceFragment(getActivity(), replacementPreviewFragment, layoutId, PreviewFragment.TAG, backstack);
FragmentDisplayer.replaceFragment(getActivity(), replacementPreviewFragment, layoutId, PreviewFragment.TAG,
backstack);
}

public void addComments(Node n, int layoutId, boolean backstack)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public void openin()

if (CipherUtils.isEncryptionActive(getActivity()))
{
tempFile = IOUtils.makeTempFile(NodeActions.getDownloadFile(getActivity(), node));
tempFile = IOUtils.makeTempFile(NodeActions.getPreviewFile(getActivity(), node));
b.putString(DownloadDialogFragment.ARGUMENT_TEMPFILE, tempFile.getPath());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public static boolean isEncryptableLocation(Context context, String filename)

public static boolean isTempFile (Context c, File file)
{
if (file == null) return false;
File tempFolder = StorageManager.getTempFolder(c,
SessionUtils.getAccount(c).getUrl(),
SessionUtils.getAccount(c).getUsername());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public class GeneralPreferences extends PreferenceFragment
public static final String PRIVATE_FOLDERS = "privatefolders";

private static final String PRIVATE_FOLDERS_BUTTON = "privatefoldersbutton";

public static final String IS_TEMP = "documentTemp";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
******************************************************************************/
package org.alfresco.mobile.android.application.utils;

import java.io.File;
import java.io.IOException;
import java.net.URI;

import org.alfresco.mobile.android.application.R;
import org.alfresco.mobile.android.application.manager.StorageManager;
import org.alfresco.mobile.android.application.preferences.GeneralPreferences;
import org.alfresco.mobile.android.ui.manager.MessengerManager;

Expand All @@ -44,19 +47,35 @@ public static boolean createMailWithAttachment(Fragment fr, String subject, Stri
{
try
{
if (CipherUtils.isEncrypted(fr.getActivity(), attachment.getPath(), true)
&& CipherUtils.decryptFile(fr.getActivity(), attachment.getPath()))
// If it comes from tmp folder we move it to download folder with timestamping.
File tmpFile = new File(new URI(attachment.toString()));
File f = new File(new URI(attachment.toString()));
if (StorageManager.isTempFile(fr.getActivity(), f))
{
tmpFile = IOUtils.renameTimeStampFile(
StorageManager.getDownloadFolder(fr.getActivity(), SessionUtils.getAccount(fr.getActivity())
.getUrl(), SessionUtils.getAccount(fr.getActivity()).getUsername()), f);

if (CipherUtils.isEncryptionActive(fr.getActivity())){
PreferenceManager.getDefaultSharedPreferences(fr.getActivity()).edit()
.putString(GeneralPreferences.REQUIRES_ENCRYPT, tmpFile.getPath()).commit();
}
}

if (CipherUtils.isEncrypted(fr.getActivity(), tmpFile.getPath(), true)
&& CipherUtils.decryptFile(fr.getActivity(), tmpFile.getPath()))
{
PreferenceManager.getDefaultSharedPreferences(fr.getActivity()).edit()
.putString(GeneralPreferences.REQUIRES_ENCRYPT, attachment.getPath()).commit();
.putString(GeneralPreferences.REQUIRES_ENCRYPT, tmpFile.getPath()).commit();
}

Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_SUBJECT, subject);
i.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(content));
i.putExtra(Intent.EXTRA_STREAM, attachment);
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(tmpFile));
i.setType("text/plain");
fr.startActivityForResult(Intent.createChooser(i, fr.getString(R.string.send_email)), requestCode);
fr.getActivity().startActivityForResult(Intent.createChooser(i, fr.getString(R.string.send_email)),
requestCode);

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ public static File makeTempFile(File f)

return f;
}

public static File renameTimeStampFile(File parentFolder, File f)
{
String timeStamp = new SimpleDateFormat("yyyyddMM_HHmmss-").format(new Date());
File newFile = new File(parentFolder, timeStamp + f.getName());
if (f.renameTo(newFile)) return newFile;
return f;
}

public static File returnTempFileToOriginal(File f)
{
Expand Down

0 comments on commit b9837c0

Please sign in to comment.