Commit 10706c2b authored by Vincent Bourgmayer's avatar Vincent Bourgmayer 💬

Remove get client async task o

parent 259ff9dd
......@@ -12,21 +12,16 @@ import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
import foundation.e.drive.models.SyncedFolder;
import foundation.e.drive.models.SyncedFileState;
/**
* @author Vincent Bourgmayer
* source: https://developer.android.com/training/data-storage/sqlite.html#java
* https://vogella.developpez.com/tutoriels/android/utilisation-base-donnees-sqlite/
*
*/
public final class DbHelper extends SQLiteOpenHelper {
final private static String TAG = DbHelper.class.getSimpleName(); //Tag for log
private static final int DATABASE_VERSION = 19; //20/09/2018
......
......@@ -11,7 +11,6 @@ package foundation.e.drive.fileFilters;
import java.io.File;
import java.io.FileFilter;
/**
* @author Vincent Bourgmayer
* FileFilter for application Settings
......
......@@ -13,7 +13,6 @@ import java.io.FileFilter;
/**
* @author Vincent Bourgmayer
*/
public class FileFilterFactory {
public static FileFilter getFileFilter(String categorie){
FileFilter filter;
......
......@@ -14,7 +14,6 @@ import java.io.FileFilter;
/**
* @author Vincent Bourgmayer
*/
public class OnlyFileFilter implements FileFilter {
@Override
public boolean accept(File pathname) {
......
......@@ -10,7 +10,6 @@ package foundation.e.drive.fileFilters;
import java.io.File;
import java.io.FileFilter;
import foundation.e.drive.utils.AppConstants;
/**
......
......@@ -13,7 +13,6 @@ import android.app.job.JobService;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import foundation.e.drive.receivers.ScreenOffReceiver;
import foundation.e.drive.services.ObserverService;
import foundation.e.drive.utils.CommonUtils;
......
......@@ -27,8 +27,4 @@ public interface ComparableOperation {
* @return RemoteOperation this instance
*/
public RemoteOperation toRemoteOperation();
}
......@@ -10,14 +10,11 @@ package foundation.e.drive.operations;
import android.content.Context;
import android.util.Log;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation;
import java.io.File;
import foundation.e.drive.database.DbHelper;
import foundation.e.drive.models.SyncedFolder;
......@@ -62,7 +59,6 @@ public class CreateInitialFolderRemoteOperation extends RemoteOperation {
return new RemoteOperationResult(RemoteOperationResult.ResultCode.FOLDER_ALREADY_EXISTS);
}
}
return createOperationResult;
}
}
......@@ -10,15 +10,12 @@ package foundation.e.drive.operations;
import android.content.Context;
import android.util.Log;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.resources.files.FileUtils;
import com.owncloud.android.lib.resources.files.model.RemoteFile;
import java.io.File;
import foundation.e.drive.database.DbHelper;
import foundation.e.drive.models.SyncedFileState;
import foundation.e.drive.utils.CommonUtils;
......@@ -27,7 +24,6 @@ import foundation.e.drive.utils.CommonUtils;
* @author Vincent Bourgmayer
* Encapsulate a global download process for a file
*/
public class DownloadFileOperation extends RemoteOperation implements ComparableOperation{
private final static String TAG = DownloadFileOperation.class.getSimpleName();
......
......@@ -30,11 +30,9 @@ import com.owncloud.android.lib.common.operations.OperationCancelledException;
import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import com.owncloud.android.lib.common.utils.Log_OC;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
......@@ -49,20 +47,16 @@ import java.util.concurrent.atomic.AtomicBoolean;
* @author masensio
* @author Vincent Bourgmayer
*/
class DownloadFileRemoteOperation extends RemoteOperation {
class DownloadFileRemoteOperation extends RemoteOperation {
private static final String TAG = DownloadFileRemoteOperation.class.getSimpleName();
private final AtomicBoolean mCancellationRequested = new AtomicBoolean(false);
private long mModificationTimestamp = 0;
private String mEtag = "";
private GetMethod mGet;
private final String mRemotePath;
private final String mLocalFolderPath;
/**
* Constructor
* @param remotePath Path of file on the server
......@@ -95,7 +89,6 @@ import java.util.concurrent.atomic.AtomicBoolean;
return result;
}
private int downloadFile(OwnCloudClient client, File targetFile) throws IOException, OperationCancelledException {
int status = -1;
boolean savedFile = false;
......@@ -158,11 +151,9 @@ import java.util.concurrent.atomic.AtomicBoolean;
client.exhaustResponse(mGet.getResponseBodyAsStream());
// TODO some kind of error control!
}
} else {
client.exhaustResponse(mGet.getResponseBodyAsStream());
}
} catch (Exception e) {
Log_OC.e(TAG, e.getMessage());
} finally {
......@@ -186,5 +177,4 @@ import java.util.concurrent.atomic.AtomicBoolean;
private String getTmpPath() {
return mLocalFolderPath;
}
}
......@@ -10,7 +10,6 @@ package foundation.e.drive.operations;
import android.content.Context;
import android.util.Log;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult;
......@@ -20,10 +19,8 @@ import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import foundation.e.drive.database.DbHelper;
import foundation.e.drive.models.SyncedFolder;
import static org.apache.jackrabbit.webdav.DavConstants.DEPTH_1;
/**
......@@ -31,7 +28,6 @@ import static org.apache.jackrabbit.webdav.DavConstants.DEPTH_1;
* @author Vincent Bourgmayer
* Created by Vincent on 04/05/2018.
*/
public class ListFileRemoteOperation extends RemoteOperation {
private final String TAG = ListFileRemoteOperation.class.getSimpleName();
......
......@@ -10,7 +10,6 @@ package foundation.e.drive.operations;
import com.owncloud.android.lib.common.operations.RemoteOperation;
import com.owncloud.android.lib.resources.files.RemoveFileRemoteOperation;
import foundation.e.drive.models.SyncedFileState;
/**
......
......@@ -10,7 +10,6 @@ package foundation.e.drive.operations;
import android.content.Context;
import android.util.Log;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.UserInfo;
import com.owncloud.android.lib.common.operations.RemoteOperation;
......@@ -22,7 +21,6 @@ import com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation;
import com.owncloud.android.lib.common.operations.RemoteOperationResult.ResultCode;
import java.io.File;
import java.util.ArrayList;
import foundation.e.drive.database.DbHelper;
import foundation.e.drive.models.SyncedFileState;
import foundation.e.drive.utils.CommonUtils;
......
......@@ -12,14 +12,11 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import foundation.e.drive.utils.JobUtils;
/**
* @author Vincent Bourgmayer
*/
public class BatteryStateReceiver extends BroadcastReceiver {
private final static String TAG = BatteryStateReceiver.class.getSimpleName();
......@@ -32,16 +29,13 @@ public class BatteryStateReceiver extends BroadcastReceiver {
Log.e(TAG, "intent Action is null");
} else if ( intentAction.equals(Intent.ACTION_BATTERY_OKAY) ) {
JobUtils.scheduleScannerJob(context);
//Not need to register ScreenOffReceiver. It's done in ScannerJob
}else if(intentAction.equals(Intent.ACTION_BATTERY_LOW)){
JobUtils.stopScheduledJob(context, JobUtils.ScannerJobId);
//JobUtils.stopScheduledJob(context, JobUtils.InitializerJobId);
try {
context.unregisterReceiver(ScreenOffReceiver.getInstance());
}catch(Exception e){
Log.e(TAG, e.toString() );
}
}
}
}
......@@ -13,7 +13,6 @@ import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import foundation.e.drive.database.DbHelper;
import foundation.e.drive.services.InitializerService;
import foundation.e.drive.utils.AppConstants;
......
......@@ -12,7 +12,6 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import foundation.e.drive.services.ObserverService;
import foundation.e.drive.utils.CommonUtils;
......@@ -21,10 +20,8 @@ import foundation.e.drive.utils.CommonUtils;
* This is a broadcast receiver which catch "ACTION_SCREEN_OFF" to start scanning at a moment
* where the user won't need battery or network.
*/
public class ScreenOffReceiver extends BroadcastReceiver {
private final String TAG = ScreenOffReceiver.class.getSimpleName();
private static ScreenOffReceiver instance;
public static ScreenOffReceiver getInstance(){
......@@ -38,7 +35,6 @@ public class ScreenOffReceiver extends BroadcastReceiver {
*/
private ScreenOffReceiver(){}
@Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "onReceive");
......
......@@ -36,7 +36,6 @@ import foundation.e.drive.operations.CreateInitialFolderRemoteOperation;
import foundation.e.drive.receivers.ScreenOffReceiver;
import foundation.e.drive.utils.AppConstants;
import foundation.e.drive.utils.CommonUtils;
import foundation.e.drive.utils.IGetOCClient;
import foundation.e.drive.utils.JobUtils;
import static com.owncloud.android.lib.resources.files.FileUtils.PATH_SEPARATOR;
......@@ -47,7 +46,7 @@ import static foundation.e.drive.utils.AppConstants.SETTINGS_SYNCABLE_CATEGORIES
/**
* @author Vincent Bourgmayer
*/
public class InitializerService extends Service implements OnRemoteOperationListener, IGetOCClient {
public class InitializerService extends Service implements OnRemoteOperationListener {
final private String TAG = InitializerService.class.getSimpleName();
//Complex properties
private int existingRemoteFolderCounter; //@dev-only; Temporarily used to know if all remotePath exist
......@@ -97,10 +96,10 @@ public class InitializerService extends Service implements OnRemoteOperationList
this.mAccount = CommonUtils.getAccount( accountName, accountType, AccountManager.get(this) );
//Get OwnCloudlient
if (this.mAccount != null) {
CommonUtils.getOwnCloudClient( this.mAccount, getApplicationContext(), this);
this.mCloudClient = CommonUtils.getOwnCloudClient( this.mAccount, getApplicationContext());
start();
}else {
Log.w(TAG, "Got account is invalid.");
//JobUtils.stopScheduledJob(getApplicationContext(), JobUtils.initializerJobID);
stopSelf();
}
}
......@@ -110,33 +109,29 @@ public class InitializerService extends Service implements OnRemoteOperationList
/**
* Got DAV's client
* @param result
* start to do its job
*/
@Override
public void onOCClientReceived(Object result) {
Log.i(TAG, "ocOCClientReceived");
try {
this.mCloudClient = (OwnCloudClient) result;
}catch(Exception e){
Log.e(TAG, e.toString() );
public void start() {
Log.i(TAG, "start()");
if (mCloudClient == null){
stopSelf();
return;
}
//Get categories of element to sync
List<String> mSyncCategories = new ArrayList<>();
List<String> syncCategories = new ArrayList<>();
if (CommonUtils.isMediaSyncEnabled(mAccount)) {
mSyncCategories.addAll(Arrays.asList(MEDIA_SYNCABLE_CATEGORIES));
syncCategories.addAll(Arrays.asList(MEDIA_SYNCABLE_CATEGORIES));
}
if (CommonUtils.isSettingsSyncEnabled(mAccount)) {
mSyncCategories.addAll(Arrays.asList(SETTINGS_SYNCABLE_CATEGORIES));
syncCategories.addAll(Arrays.asList(SETTINGS_SYNCABLE_CATEGORIES));
}
//Get SyncedFolders
getInitialSyncedFolders(mSyncCategories);
getInitialSyncedFolders(syncCategories);
this.existingRemoteFolderCounter = 0;
......
......@@ -48,7 +48,6 @@ import foundation.e.drive.operations.RemoveFileOperation;
import foundation.e.drive.operations.UploadFileOperation;
import foundation.e.drive.utils.AppConstants;
import foundation.e.drive.utils.CommonUtils;
import foundation.e.drive.utils.IGetOCClient;
import foundation.e.drive.utils.JobUtils;
import static com.owncloud.android.lib.resources.files.FileUtils.PATH_SEPARATOR;
......@@ -59,7 +58,7 @@ import static foundation.e.drive.utils.AppConstants.INITIALIZATION_HAS_BEEN_DONE
* @author Vincent Bourgmayer
* This service look for remote or looale file to synchronize
*/
public class ObserverService extends Service implements OnRemoteOperationListener, IGetOCClient {
public class ObserverService extends Service implements OnRemoteOperationListener{
private final static String TAG = ObserverService.class.getSimpleName();
private OwnCloudClient mClient;
private List<SyncedFolder> mSyncedFolders; //List of synced folder
......@@ -131,8 +130,6 @@ public class ObserverService extends Service implements OnRemoteOperationListene
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "onStartCommand");
SharedPreferences prefs = this.getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
if (prefs.getBoolean(INITIALIZATION_HAS_BEEN_DONE, false)) {
......@@ -144,10 +141,14 @@ public class ObserverService extends Service implements OnRemoteOperationListene
if (this.mAccount != null ){
if ( CommonUtils.isMediaSyncEnabled(this.mAccount) || CommonUtils.isSettingsSyncEnabled(this.mAccount) ) {
if (mClient == null) //background task
CommonUtils.getOwnCloudClient(this.mAccount, getApplicationContext(), this);
else
this.mClient = CommonUtils.getOwnCloudClient(this.mAccount, getApplicationContext());
if (mClient != null) //background task
begin();
else {
Log.e(TAG, "mClient is null");
this.stopSelf();
}
}else{
this.stopSelf(); //if sync disabled we stop
}
......@@ -193,23 +194,6 @@ public class ObserverService extends Service implements OnRemoteOperationListene
}
}
/**
* To receive the response from CommonUtils.getOwncloudClient()
* @param result OwnCloudClient
*/
@Override
public void onOCClientReceived(Object result) {
Log.i(TAG, "onOCClientReceived()");
if(result != null){
this.mClient = ( OwnCloudClient ) result;
begin();
}else{
//End of the service
Log.e(TAG, "onOCClientReceived(...) return null");
this.stopSelf();
}
}
/**
* Clear cached file unused:
* remove each cached file which isn't in OperationManagerService.lockedSyncedFileState();
......
......@@ -37,7 +37,6 @@ import java.util.Hashtable;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedDeque;
import foundation.e.drive.database.DbHelper;
import foundation.e.drive.operations.ComparableOperation;
import foundation.e.drive.operations.DownloadFileOperation;
......@@ -45,14 +44,12 @@ import foundation.e.drive.operations.RemoveFileOperation;
import foundation.e.drive.operations.UploadFileOperation;
import foundation.e.drive.utils.AppConstants;
import foundation.e.drive.utils.CommonUtils;
import foundation.e.drive.utils.IGetOCClient;
/**
* @author Vincent Bourgmayer
* Service to do upload, remove and download operation.
*/
public class OperationManagerService extends Service implements OnRemoteOperationListener, IGetOCClient{
public class OperationManagerService extends Service implements OnRemoteOperationListener{
private final static String TAG = OperationManagerService.class.getSimpleName();
// Binder given to clients
private final IBinder mBinder = new OperationManagerBinder();
......@@ -117,7 +114,14 @@ public class OperationManagerService extends Service implements OnRemoteOperatio
AccountManager.get(this));
if(this.mAccount != null)
CommonUtils.getOwnCloudClient(this.mAccount, getApplicationContext(), this);
mClient = CommonUtils.getOwnCloudClient(this.mAccount, getApplicationContext());
if(mClient != null) {
startAllThreads();
}else {
Log.e(TAG, "No Client, Can't Work!");
stopSelf();
}
}
......@@ -257,28 +261,6 @@ public class OperationManagerService extends Service implements OnRemoteOperatio
this.lockedfilePath.remove( ((ComparableOperation) operation ).getLocalPath() );
}
/**
* Function called when client instance is build
* @param result OwnCloudClient's instance
*/
@Override
public void onOCClientReceived(Object result) {
try{
this.mClient = (OwnCloudClient) result;
}catch(Exception e){
Log.e(TAG, e.toString() );
this.mClient = null;
}
if(this.mClient != null) {
Log.d(TAG, "Client defined");
startAllThreads();
}else {
Log.e(TAG, "No Client, Can't Work!");
stopSelf();
}
}
/**
* Binder for the class
*/
......
......@@ -12,8 +12,6 @@ package foundation.e.drive.utils;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.Context;
import android.media.MediaScannerConnection;
......@@ -21,16 +19,15 @@ import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.webkit.MimeTypeMap;
import com.owncloud.android.lib.common.OwnCloudBasicCredentials;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.OwnCloudClientFactory;
import com.owncloud.android.lib.common.accounts.AccountUtils;
import com.owncloud.android.lib.resources.files.FileUtils;
import java.io.File;
import foundation.e.drive.receivers.ScreenOffReceiver;
import static foundation.e.drive.utils.AppConstants.MEDIASYNC_PROVIDER_AUTHORITY;
import static foundation.e.drive.utils.AppConstants.SETTINGSYNC_PROVIDER_AUTHORITY;
......@@ -58,30 +55,6 @@ public abstract class CommonUtils {
}
return true;
}
/**
* Send notification to UI.
* @dev-only
* @param context app context
* @param id notification ID
* @param msg Message of the notification
* @param icon Icon to use
* @param pIntent intent to launch when user tap on the notif. Can be null
* @param ticker The ticker message, shown first. Can be null
*/
public static void sendNotification(Context context,int id, String msg, int icon, PendingIntent pIntent,String ticker) {
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context)
.setSmallIcon(icon)
.setContentTitle("/e/ Drive")
.setContentText(msg);
NotificationManager notifyMgr =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if(pIntent != null) builder.setContentIntent(pIntent);
if(ticker != null) builder.setTicker(ticker);
notifyMgr.notify(id, builder.build());
}
/**
......@@ -132,15 +105,23 @@ public abstract class CommonUtils {
/**
* @param context app context
* @param caller Service or activity which asks for OwnCloudClient
* @return boolean true if Task could have been launch
* @return Owncloud client instance or null
*/
public static void getOwnCloudClient( Account account, Context context, IGetOCClient caller) {
public static OwnCloudClient getOwnCloudClient(Account account, Context context) {
Log.i(TAG, "getOwnCloudClient()");
if ( isMediaSyncEnabled(account) || isSettingsSyncEnabled(account) ) {
GetOCClientTask getOCClientTask = new GetOCClientTask( caller);
getOCClientTask.execute(context, account);
Uri serverUri;
OwnCloudClient oc;
try {
serverUri = Uri.parse(AccountUtils.getBaseUrlForAccount(context, account));
oc = OwnCloudClientFactory.createOwnCloudClient(serverUri, context, true);
oc.setCredentials(new OwnCloudBasicCredentials(account.name, AccountManager.get(context).getPassword(account) ) );
}catch (Exception e){
Log.e(TAG, "Can\'t parse serverPath to Uri : "+e.toString() );
oc = null;
}
return oc;
}
/** methods relative to file **/
......
/*
* Copyright © Vincent Bourgmayer (/e/ foundation).
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*/
package foundation.e.drive.utils;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;
import com.owncloud.android.lib.common.OwnCloudBasicCredentials;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.OwnCloudClientFactory;
import com.owncloud.android.lib.common.accounts.AccountUtils;
/**
* @author Vincent Bourgmayer
* AsyncTask to build client
*/
public class GetOCClientTask extends AsyncTask<Object, Void, OwnCloudClient> {
private final static String TAG = GetOCClientTask.class.getSimpleName();
private final IGetOCClient caller;
/**
* Constructor for AsyncTask which has goal to obtain an OwnCloudClient
* @param caller The service/Service which ask for client
*/
public GetOCClientTask( IGetOCClient caller){
this.caller = caller;
}
@Override
protected OwnCloudClient doInBackground(Object[] objects) {
Log.d(TAG, "doInBackground(...)");
final Context context = (Context) objects[0];
final Account account = (Account) objects[1];
Uri serverUri;
try {
serverUri = Uri.parse(AccountUtils.getBaseUrlForAccount(context, account));
}catch (Exception e){
Log.e(TAG, "Can\'t parse serverPath to Uri : "+e.toString() );
return null;
}
OwnCloudClient oc = OwnCloudClientFactory.createOwnCloudClient(serverUri, context, true);
oc.setCredentials(new OwnCloudBasicCredentials(account.name, AccountManager.get(context).getPassword(account) ) );
return oc;
}
@Override
protected void onPostExecute(OwnCloudClient o) {
super.onPostExecute(o);
caller.onOCClientReceived(o);
}
}
/*
* Copyright © Vincent Bourgmayer (/e/ foundation).
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Public License v3.0
* which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/gpl.html
*/
package foundation.e.drive.utils;
/**
* @author Vincent Bourgmayer
*/
public interface IGetOCClient {
void onOCClientReceived(Object result);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment