Skip to content

Commit

Permalink
CRYPTO: Fix encrypted file opening
Browse files Browse the repository at this point in the history
  • Loading branch information
VishalNehra committed Aug 20, 2017
1 parent 6415578 commit b7894a2
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
import com.amaze.filemanager.filesystem.BaseFile;
import com.amaze.filemanager.filesystem.HFile;
import com.amaze.filemanager.filesystem.MediaStoreHack;
import com.amaze.filemanager.services.DeleteTask;
import com.amaze.filemanager.services.asynctasks.LoadList;
import com.amaze.filemanager.ui.LayoutElement;
import com.amaze.filemanager.ui.dialogs.GeneralDialogCreation;
Expand Down Expand Up @@ -171,6 +172,8 @@ public class MainFragment extends android.support.v4.app.Fragment {
private Bitmap mFolderBitmap;
private CustomFileObserver customFileObserver;
private DataUtils dataUtils = DataUtils.getInstance();
private boolean isEncryptOpen = false; // do we have to open a file when service is begin destroyed
private BaseFile encryptBaseFile; // the cached base file which we're to open, delete it later

// defines the current visible tab, default either 0 or 1
//private int mCurrentTab;
Expand Down Expand Up @@ -798,14 +801,25 @@ public void onReceive(Context context, Intent intent) {
case FILE:
// local file system don't need an explicit load, we've set an observer to
// take actions on creation/moving/deletion/modification of file on current path
break;
//break;
default:
updateList();
break;
}
}
};

private BroadcastReceiver decryptReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {

if (isEncryptOpen && encryptBaseFile != null) {
getMainActivity().getFutils().openFile(new File(encryptBaseFile.getPath()), getMainActivity());
isEncryptOpen = false;
}
}
};

public void home() {
ma.loadlist((ma.home), false, OpenMode.FILE);
}
Expand Down Expand Up @@ -871,15 +885,15 @@ public void onListItemClicked(boolean isBackButton, int position, LayoutElement
if (!e.isDirectory() &&
e.getDesc().endsWith(CryptUtil.CRYPT_EXTENSION)) {
// decrypt the file
getMainActivity().isEncryptOpen = true;
isEncryptOpen = true;

getMainActivity().encryptBaseFile = new BaseFile(getActivity().getExternalCacheDir().getPath()
encryptBaseFile = new BaseFile(getActivity().getExternalCacheDir().getPath()
+ "/"
+ e.generateBaseFile().getName().replace(CryptUtil.CRYPT_EXTENSION, ""));

EncryptDecryptUtils.decryptFile(getContext(), getMainActivity(), ma, openMode,
e.generateBaseFile(), getActivity().getExternalCacheDir().getPath(),
utilsProvider);
utilsProvider, true);
return;
}

Expand Down Expand Up @@ -1467,6 +1481,10 @@ public void onResume() {
super.onResume();
(getActivity()).registerReceiver(receiver2, new IntentFilter("loadlist"));

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {

(getActivity()).registerReceiver(decryptReceiver, new IntentFilter(EncryptDecryptUtils.DECRYPT_BROADCAST));
}
//startFileObserver();
fixIcons(false);
}
Expand All @@ -1475,6 +1493,11 @@ public void onResume() {
public void onPause() {
super.onPause();
(getActivity()).unregisterReceiver(receiver2);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {

(getActivity()).unregisterReceiver(decryptReceiver);
}
}

@Override
Expand Down Expand Up @@ -1583,6 +1606,16 @@ private LayoutElement addTo(BaseFile mFile) {
@Override
public void onDestroy() {
super.onDestroy();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {

if (!isEncryptOpen && encryptBaseFile != null) {
// we've opened the file and are ready to delete it
ArrayList<BaseFile> baseFiles = new ArrayList<>();
baseFiles.add(encryptBaseFile);
new DeleteTask(getMainActivity().getContentResolver(), getActivity()).execute(baseFiles);
}
}
}

public void hide(String path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.amaze.filemanager.utils.OpenMode;
import com.amaze.filemanager.utils.ProgressHandler;
import com.amaze.filemanager.utils.ServiceWatcherUtil;
import com.amaze.filemanager.utils.files.EncryptDecryptUtils;

import java.util.ArrayList;

Expand All @@ -37,6 +38,7 @@ public class EncryptService extends Service {
public static final String TAG_OPEN_MODE = "open_mode";
public static final String TAG_CRYPT_MODE = "crypt_mode"; // ordinal of type of service
// expected (encryption or decryption)
public static final String TAG_BROADCAST_RESULT = "broadcast_result";

private static final int ID_NOTIFICATION = 27978;

Expand All @@ -57,6 +59,7 @@ public class EncryptService extends Service {
private CryptEnum cryptEnum;
private ArrayList<HFile> failedOps = new ArrayList<>();
private ProgressListener progressListener;
private boolean broadcastResult = false;

@Override
public void onCreate() {
Expand All @@ -71,6 +74,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {

baseFile = intent.getParcelableExtra(TAG_SOURCE);
cryptEnum = CryptEnum.values()[intent.getIntExtra(TAG_CRYPT_MODE, CryptEnum.ENCRYPT.ordinal())];
broadcastResult = intent.getBooleanExtra(TAG_BROADCAST_RESULT, false);

openMode = OpenMode.values()[intent.getIntExtra(TAG_OPEN_MODE, OpenMode.UNKNOWN.ordinal())];
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Expand Down Expand Up @@ -167,8 +171,15 @@ protected void onPostExecute(Void aVoid) {

serviceWatcherUtil.stopWatch();
generateNotification(failedOps, cryptEnum==CryptEnum.ENCRYPT ? false : true);
Intent intent = new Intent("loadlist");
sendBroadcast(intent);

if (!broadcastResult) {

Intent intent = new Intent("loadlist");
sendBroadcast(intent);
} else {
Intent intent = new Intent(EncryptDecryptUtils.DECRYPT_BROADCAST);
sendBroadcast(intent);
}
stopSelf();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public void onButtonPressed(Intent intent, String password) {
case R.id.decrypt:
EncryptDecryptUtils.decryptFile(context, mainActivity, mainFragment,
mainFragment.openMode, rowItem.generateBaseFile(),
rowItem.generateBaseFile().getParent(context), utilitiesProvider);
rowItem.generateBaseFile().getParent(context), utilitiesProvider, false);
return true;
case R.id.return_select:
mainFragment.returnIntentResults(rowItem.generateBaseFile());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

public class EncryptDecryptUtils {

public static final String DECRYPT_BROADCAST = "decrypt_broadcast";
/**
* Queries database to map path and password.
* Starts the encryption process after database query
Expand All @@ -52,14 +53,16 @@ public static void startEncryption(Context c, final String path, final String pa

public static void decryptFile(Context c, final MainActivity mainActivity, final MainFragment main, OpenMode openMode,
BaseFile sourceFile, String decryptPath,
UtilitiesProviderInterface utilsProvider) {
UtilitiesProviderInterface utilsProvider,
boolean broadcastResult) {

Intent decryptIntent = new Intent(main.getContext(), EncryptService.class);
decryptIntent.putExtra(EncryptService.TAG_OPEN_MODE, openMode.ordinal());
decryptIntent.putExtra(EncryptService.TAG_CRYPT_MODE,
EncryptService.CryptEnum.DECRYPT.ordinal());
decryptIntent.putExtra(EncryptService.TAG_SOURCE, sourceFile);
decryptIntent.putExtra(EncryptService.TAG_DECRYPT_PATH, decryptPath);
decryptIntent.putExtra(EncryptService.TAG_BROADCAST_RESULT, broadcastResult);
SharedPreferences preferences1 = PreferenceManager.getDefaultSharedPreferences(main.getContext());

EncryptedEntry encryptedEntry = null;
Expand Down
10 changes: 5 additions & 5 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,16 @@
android:key="showPermissions"
android:summary="@string/rootPrefSummary"
android:title="@string/rootPref"/>
<com.amaze.filemanager.ui.views.preference.CheckBox
android:defaultValue="false"
android:key="rootmode"
android:summary="@string/rootmodesummary"
android:title="@string/rootmode"/>
<com.amaze.filemanager.ui.views.preference.CheckBox
android:defaultValue="false"
android:key="typeablepaths"
android:summary="@string/typeablepaths_summary"
android:title="@string/typeablepaths_title"/>
<com.amaze.filemanager.ui.views.preference.CheckBox
android:defaultValue="false"
android:key="rootmode"
android:summary="@string/rootmodesummary"
android:title="@string/rootmode"/>
</PreferenceCategory>

<PreferenceCategory android:title="@string/about">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,12 @@
import android.app.Activity;
import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentSender;
import android.content.ServiceConnection;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.database.Cursor;
Expand All @@ -49,7 +46,6 @@
import android.os.Environment;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.service.quicksettings.TileService;
import android.support.annotation.NonNull;
import android.support.design.widget.AppBarLayout;
Expand Down Expand Up @@ -112,7 +108,6 @@
import com.amaze.filemanager.fragments.preference_fragments.QuickAccessPref;
import com.amaze.filemanager.services.CopyService;
import com.amaze.filemanager.services.DeleteTask;
import com.amaze.filemanager.services.EncryptService;
import com.amaze.filemanager.services.asynctasks.CopyFileCheck;
import com.amaze.filemanager.services.asynctasks.MoveFiles;
import com.amaze.filemanager.ui.dialogs.GeneralDialogCreation;
Expand Down Expand Up @@ -140,7 +135,6 @@
import com.amaze.filemanager.utils.Utils;
import com.amaze.filemanager.utils.color.ColorUsage;
import com.amaze.filemanager.utils.files.Futils;
import com.amaze.filemanager.utils.files.GenericCopyUtil;
import com.amaze.filemanager.utils.theme.AppTheme;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
Expand All @@ -160,20 +154,13 @@
import com.google.android.gms.plus.model.people.Person;
import com.readystatesoftware.systembartint.SystemBarTintManager;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;

import eu.chainfire.libsuperuser.Shell;
import jcifs.smb.SmbFile;

import static android.os.Build.VERSION.SDK_INT;
import static com.amaze.filemanager.fragments.preference_fragments.Preffrag.PREFERENCE_SHOW_SIDEBAR_FOLDERS;
Expand Down Expand Up @@ -296,8 +283,6 @@ public class MainActivity extends ThemedActivity implements
public static Handler handler;

private static HandlerThread handlerThread;
public boolean isEncryptOpen = false; // do we have to open a file when service is begin destroyed
public BaseFile encryptBaseFile; // the cached base file which we're to open, delete it later

private static final int REQUEST_CODE_CLOUD_LIST_KEYS = 5463;
private static final int REQUEST_CODE_CLOUD_LIST_KEY = 5472;
Expand Down Expand Up @@ -1250,9 +1235,6 @@ protected void onPause() {
unregisterReceiver(mainActivityHelper.mNotificationReceiver);
unregisterReceiver(receiver2);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
unbindService(mEncryptServiceConnection);

if (SDK_INT >= Build.VERSION_CODES.KITKAT) {
unregisterReceiver(mOtgReceiver);
}
Expand Down Expand Up @@ -1281,56 +1263,8 @@ public void onResume() {
otgFilter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
registerReceiver(mOtgReceiver, otgFilter);
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// let's register encryption service to know when we've decrypted
Intent encryptIntent = new Intent(this, EncryptService.class);
bindService(encryptIntent, mEncryptServiceConnection, 0);

if (!isEncryptOpen && encryptBaseFile != null) {
// we've opened the file and are ready to delete it
// don't move this to ondestroy as we'll be getting destroyed and starting
// an async task just before it is not a good idea
ArrayList<BaseFile> baseFiles = new ArrayList<>();
baseFiles.add(encryptBaseFile);
new DeleteTask(getContentResolver(), this).execute(baseFiles);
}
}
}

ServiceConnection mEncryptServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {

}

@Override
public void onServiceDisconnected(ComponentName name) {

if (isEncryptOpen && encryptBaseFile != null) {
if (getCurrentMainFragment() != null) {
switch (getCurrentMainFragment().openMode) {
case OTG:
getFutils().openFile(OTGUtil.getDocumentFile(encryptBaseFile.getPath(),
MainActivity.this, false), MainActivity.this);
break;
case SMB:
try {
MainFragment.launchSMB(new SmbFile(encryptBaseFile.getPath()),
encryptBaseFile.getSize(), MainActivity.this);
} catch (MalformedURLException e) {
e.printStackTrace();
}
default:
getFutils().openFile(new File(encryptBaseFile.getPath()), MainActivity.this);
}
} else
getFutils().openFile(new File(encryptBaseFile.getPath()), MainActivity.this);
isEncryptOpen = false;
}
}
};

/**
* Receiver to check if a USB device is connected at the runtime of application
* If device is not connected at runtime (i.e. it was connected when the app was closed)
Expand Down

0 comments on commit b7894a2

Please sign in to comment.