diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml
new file mode 100644
index 0000000000000000000000000000000000000000..681f41ae2aee4749eb4ddda94f8c6a76c825c825
--- /dev/null
+++ b/.idea/codeStyles/Project.xml
@@ -0,0 +1,116 @@
+
+
+
+
+
+
+
+
+
+
+
+
+ xmlns:android
+
+ ^$
+
+
+
+
+
+
+
+
+ xmlns:.*
+
+ ^$
+
+
+ BY_NAME
+
+
+
+
+
+
+ .*:id
+
+ http://schemas.android.com/apk/res/android
+
+
+
+
+
+
+
+
+ .*:name
+
+ http://schemas.android.com/apk/res/android
+
+
+
+
+
+
+
+
+ name
+
+ ^$
+
+
+
+
+
+
+
+
+ style
+
+ ^$
+
+
+
+
+
+
+
+
+ .*
+
+ ^$
+
+
+ BY_NAME
+
+
+
+
+
+
+ .*
+
+ http://schemas.android.com/apk/res/android
+
+
+ ANDROID_ATTRIBUTE_ORDER
+
+
+
+
+
+
+ .*
+
+ .*
+
+
+ BY_NAME
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/app/src/main/java/foundation/e/drive/fileFilters/CrashlogsFileFilter.java b/app/src/main/java/foundation/e/drive/fileFilters/CrashlogsFileFilter.java
new file mode 100644
index 0000000000000000000000000000000000000000..3c902cb73cfa1b15a1b48ff20e50a1b293120afa
--- /dev/null
+++ b/app/src/main/java/foundation/e/drive/fileFilters/CrashlogsFileFilter.java
@@ -0,0 +1,42 @@
+package foundation.e.drive.fileFilters;
+
+import java.io.File;
+import java.io.FileFilter;
+
+import foundation.e.drive.utils.ServiceExceptionHandler;
+
+public class CrashlogsFileFilter implements FileFilter {
+ private final static long max_timestamp_delta = 864000000; //10 days in ms (240*3600*1000)
+
+ @Override
+ public boolean accept(File pathname) {
+ String fileTimestamp = extractTimestamp(pathname.getName(),
+ ServiceExceptionHandler.LOG_FILE_NAME_PREFIX,
+ ServiceExceptionHandler.LOG_FILE_EXTENSION);
+
+ long timestamp;
+ try {
+ timestamp = Long.parseLong(fileTimestamp);
+ }catch (NumberFormatException e){
+ //Can't parse the extracted timestamp
+ //This file has not the expected name. It must be removed
+ return true;
+ }
+
+ //if current Date - file date >= max deta allowed
+ return ((System.currentTimeMillis() - timestamp ) >= max_timestamp_delta);
+ }
+
+ /**
+ * Extract the timestamp from the name of the file
+ * UnitTested!
+ * @param fileName Filename
+ * @param prefix prefix to ignore
+ * @param extension extension to ignore
+ * @return the timestamp extracted from the name
+ */
+ private String extractTimestamp(String fileName, String prefix, String extension){
+ return fileName.substring(prefix.length(), (fileName.length() - extension.length()));
+ }
+
+}
diff --git a/app/src/main/java/foundation/e/drive/services/InitializerService.java b/app/src/main/java/foundation/e/drive/services/InitializerService.java
index ee3f8b26dda29d44f650613cc88618eeacb156fb..29c6d0da887c33c5fb0f35afffd1ca6622059c5b 100644
--- a/app/src/main/java/foundation/e/drive/services/InitializerService.java
+++ b/app/src/main/java/foundation/e/drive/services/InitializerService.java
@@ -34,6 +34,8 @@ import foundation.e.drive.receivers.ScreenOffReceiver;
import foundation.e.drive.utils.AppConstants;
import foundation.e.drive.utils.CommonUtils;
import foundation.e.drive.utils.JobUtils;
+import foundation.e.drive.utils.ServiceExceptionHandler;
+
import static com.owncloud.android.lib.resources.files.FileUtils.PATH_SEPARATOR;
import static foundation.e.drive.utils.AppConstants.INITIALFOLDERS_NUMBER;
import static foundation.e.drive.utils.AppConstants.MEDIA_SYNCABLE_CATEGORIES;
@@ -63,6 +65,8 @@ public class InitializerService extends Service implements OnRemoteOperationList
@Override
public int onStartCommand( Intent intent, int flags, int startId ) {
Log.i(TAG, "onStartCommand(...)");
+
+ CommonUtils.setServiceUnCaughtExceptionHandler(this);
//Get account
SharedPreferences prefs = this.getSharedPreferences( AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE );
diff --git a/app/src/main/java/foundation/e/drive/services/ObserverService.java b/app/src/main/java/foundation/e/drive/services/ObserverService.java
index 0514e32827e7a4dc645ff5714dc798f480434ae2..6cf06395b6d618bfcecf7b6fd1dba227e20987ae 100644
--- a/app/src/main/java/foundation/e/drive/services/ObserverService.java
+++ b/app/src/main/java/foundation/e/drive/services/ObserverService.java
@@ -37,6 +37,7 @@ import java.util.ListIterator;
import java.util.Map;
import foundation.e.drive.database.DbHelper;
+import foundation.e.drive.fileFilters.CrashlogsFileFilter;
import foundation.e.drive.fileFilters.FileFilterFactory;
import foundation.e.drive.fileFilters.OnlyFileFilter;
import foundation.e.drive.models.SyncedFolder;
@@ -51,6 +52,7 @@ import static com.owncloud.android.lib.resources.files.FileUtils.PATH_SEPARATOR;
import static foundation.e.drive.utils.AppConstants.INITIALIZATION_HAS_BEEN_DONE;
import foundation.e.drive.utils.DavClientProvider;
import foundation.e.drive.utils.JobUtils;
+import foundation.e.drive.utils.ServiceExceptionHandler;
/**
* @author Vincent Bourgmayer
@@ -75,16 +77,12 @@ public class ObserverService extends Service implements OnRemoteOperationListene
this.mSyncedFolders = null;
}
- @Override
- public void onCreate() {
- Log.i(TAG, "onCreate()");
- super.onCreate();
- }
-
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand("+startId+")");
+ CommonUtils.setServiceUnCaughtExceptionHandler(this);
+
SharedPreferences prefs = this.getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
String accountName = prefs.getString(AccountManager.KEY_ACCOUNT_NAME, "");
String accountType = prefs.getString(AccountManager.KEY_ACCOUNT_TYPE, "");
@@ -145,7 +143,7 @@ public class ObserverService extends Service implements OnRemoteOperationListene
return super.onStartCommand( intent, flags, startId );
}
- /* Common methods */
+ //Common methods
/**
* Start to bind this service to OperationManagerService or start scan if binding is already set.
* Method to factorise code that is called from different place
@@ -154,12 +152,36 @@ public class ObserverService extends Service implements OnRemoteOperationListene
Log.i(TAG, "begin()");
this.isWorking = true;
clearCachedFile();
+ deleteOldestCrashlogs();
startScan(true);
}
+ /**
+ * This method remove all the crash-logs file
+ * in external dir that are 10 days or more old.
+ */
+ private void deleteOldestCrashlogs(){
+ Log.i(TAG, "deleteOldestCrashLogs()");
+ File[] fileToRemove = getExternalFilesDir(ServiceExceptionHandler.CRASH_LOG_FOLDER)
+ .listFiles(new CrashlogsFileFilter());
+
+ int counter = 0;
+ for (File file : fileToRemove) {
+ try {
+ file.delete();
+ ++counter;
+ }catch (SecurityException e){
+ e.printStackTrace();
+ }
+ }
+ Log.d(TAG, counter+" old crashlogs file.s deleted");
+ }
+
+
/**
* Clear cached file unused:
* remove each cached file which isn't in OperationManagerService.lockedSyncedFileState();
+ * @TODO rewrite this method!
*/
private void clearCachedFile(){
Log.i(TAG, "clearCachedFile()");
@@ -201,7 +223,6 @@ public class ObserverService extends Service implements OnRemoteOperationListene
}
Log.d(TAG, logFolderList.toString());
-
if (remote) {
OwnCloudClient client = DavClientProvider.getInstance().getClientInstance(mAccount, getApplicationContext());
if (client != null) {
@@ -249,6 +270,7 @@ public class ObserverService extends Service implements OnRemoteOperationListene
@Override
public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result ) {
Log.i( TAG, "onRemoteOperationFinish()" );
+
if( operation instanceof ListFileRemoteOperation){
if( result.isSuccess() ){
List