From 9afdf4f0736c791e35ba040f965f2e812f3d6956 Mon Sep 17 00:00:00 2001 From: Narinder Rana Date: Mon, 1 Feb 2021 14:29:45 +0530 Subject: [PATCH 1/3] handle null pointer exception --- .../services/OperationManagerService.java | 103 +++++++++--------- 1 file changed, 54 insertions(+), 49 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/services/OperationManagerService.java b/app/src/main/java/foundation/e/drive/services/OperationManagerService.java index eb0999c2..f9364efd 100644 --- a/app/src/main/java/foundation/e/drive/services/OperationManagerService.java +++ b/app/src/main/java/foundation/e/drive/services/OperationManagerService.java @@ -239,59 +239,64 @@ public class OperationManagerService extends Service implements OnRemoteOperatio public int onStartCommand(Intent intent, int flags, int startId) { Log.i(TAG, "onStartCommand()"); - CommonUtils.setServiceUnCaughtExceptionHandler(this); - - Bundle extras = intent.getExtras(); - Log.d(TAG, "OperationManagerService recieved "+(extras == null ? "null extras": extras.size()+" operations to perform") ); - - if(extras != null) { - //Load operation from intent - this.mOperationsQueue = new ConcurrentLinkedDeque<>(); - for (String key : extras.keySet()) { - - Parcelable parcelableObject = extras.getParcelable(key); - if (key.equals("account")) { - this.mAccount = (Account) parcelableObject; - } else if (parcelableObject.getClass().getName().equals(DownloadFileOperation.class.getName())) { - DownloadFileOperation download = (DownloadFileOperation) parcelableObject; - download.setContext(getApplicationContext()); - mOperationsQueue.add(download); - } else if (parcelableObject.getClass().getName().equals(UploadFileOperation.class.getName())) { - UploadFileOperation upload = (UploadFileOperation) parcelableObject; - upload.setContext(getApplicationContext()); - mOperationsQueue.add(upload); - } else if (parcelableObject.getClass().getName().equals(RemoveFileOperation.class.getName())) { - mOperationsQueue.add((RemoveFileOperation) parcelableObject); + try{ + CommonUtils.setServiceUnCaughtExceptionHandler(this); + + Bundle extras = intent.getExtras(); + Log.d(TAG, "OperationManagerService recieved "+(extras == null ? "null extras": extras.size()+" operations to perform") ); + + if(extras != null) { + //Load operation from intent + this.mOperationsQueue = new ConcurrentLinkedDeque<>(); + for (String key : extras.keySet()) { + + Parcelable parcelableObject = extras.getParcelable(key); + if (key.equals("account")) { + this.mAccount = (Account) parcelableObject; + } else if (parcelableObject.getClass().getName().equals(DownloadFileOperation.class.getName())) { + DownloadFileOperation download = (DownloadFileOperation) parcelableObject; + download.setContext(getApplicationContext()); + mOperationsQueue.add(download); + } else if (parcelableObject.getClass().getName().equals(UploadFileOperation.class.getName())) { + UploadFileOperation upload = (UploadFileOperation) parcelableObject; + upload.setContext(getApplicationContext()); + mOperationsQueue.add(upload); + } else if (parcelableObject.getClass().getName().equals(RemoveFileOperation.class.getName())) { + mOperationsQueue.add((RemoveFileOperation) parcelableObject); + } } - } - - if(mAccount == null || mOperationsQueue.isEmpty()){ - Log.w(TAG, "No account or Operation queue is empty"); - return super.onStartCommand(intent, flags, startId); - } - - //Initialize class's field - this.workerAmount = 4; //This variable could be replace later by an option in settings - - this.mThreadPool = new Thread[workerAmount]; - this.mThreadWorkingState = new boolean[workerAmount]; - this.mHandler = new OperationManagerHandler(this); - this.mStartedOperations = new Hashtable(); - mClient = DavClientProvider.getInstance().getClientInstance(mAccount, getApplicationContext()); - if (mClient != null) { - getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE) - .edit() - .putBoolean(AppConstants.KEY_OMS_IS_WORKING, true) - .apply(); + if(mAccount == null || mOperationsQueue.isEmpty()){ + Log.w(TAG, "No account or Operation queue is empty"); + return super.onStartCommand(intent, flags, startId); + } - startAllThreads(); - } else { - Log.w(TAG, "No Client, Can't Work!"); - stopSelf(); + //Initialize class's field + this.workerAmount = 4; //This variable could be replace later by an option in settings + + this.mThreadPool = new Thread[workerAmount]; + this.mThreadWorkingState = new boolean[workerAmount]; + this.mHandler = new OperationManagerHandler(this); + this.mStartedOperations = new Hashtable(); + + mClient = DavClientProvider.getInstance().getClientInstance(mAccount, getApplicationContext()); + if (mClient != null) { + getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE) + .edit() + .putBoolean(AppConstants.KEY_OMS_IS_WORKING, true) + .apply(); + + startAllThreads(); + } else { + Log.w(TAG, "No Client, Can't Work!"); + stopSelf(); + } + }else{ + Log.w(TAG, "Intent's extras is null."); } - }else{ - Log.w(TAG, "Intent's extras is null."); + }catch (Exception ex){ + Log.e("Exception", ex.getMessage()); + ex.printStackTrace(); } return super.onStartCommand(intent, flags, startId); -- GitLab From d1beeb0d67f29fda034bc85d155486b3397a43a2 Mon Sep 17 00:00:00 2001 From: Narinder Rana Date: Thu, 4 Feb 2021 11:00:28 +0530 Subject: [PATCH 2/3] debugging for intent null or intent action is null --- .../e/drive/services/OperationManagerService.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/app/src/main/java/foundation/e/drive/services/OperationManagerService.java b/app/src/main/java/foundation/e/drive/services/OperationManagerService.java index f9364efd..7cad93a0 100644 --- a/app/src/main/java/foundation/e/drive/services/OperationManagerService.java +++ b/app/src/main/java/foundation/e/drive/services/OperationManagerService.java @@ -242,6 +242,12 @@ public class OperationManagerService extends Service implements OnRemoteOperatio try{ CommonUtils.setServiceUnCaughtExceptionHandler(this); + if (null == intent || null == intent.getAction ()) { + String source = null == intent ? "intent" : "action"; + Log.e (TAG, source + " was null, flags=" + flags + " bits=" + Integer.toBinaryString (flags)); + return START_STICKY; + } + Bundle extras = intent.getExtras(); Log.d(TAG, "OperationManagerService recieved "+(extras == null ? "null extras": extras.size()+" operations to perform") ); -- GitLab From 53a9b73b851fd8e1cb662f308fe2d41e4f2dcf81 Mon Sep 17 00:00:00 2001 From: Narinder Rana Date: Thu, 4 Feb 2021 19:37:14 +0530 Subject: [PATCH 3/3] handle null pointer exception :Remove START_STICKY --- .../foundation/e/drive/services/OperationManagerService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/foundation/e/drive/services/OperationManagerService.java b/app/src/main/java/foundation/e/drive/services/OperationManagerService.java index 7cad93a0..7054df66 100644 --- a/app/src/main/java/foundation/e/drive/services/OperationManagerService.java +++ b/app/src/main/java/foundation/e/drive/services/OperationManagerService.java @@ -245,7 +245,7 @@ public class OperationManagerService extends Service implements OnRemoteOperatio if (null == intent || null == intent.getAction ()) { String source = null == intent ? "intent" : "action"; Log.e (TAG, source + " was null, flags=" + flags + " bits=" + Integer.toBinaryString (flags)); - return START_STICKY; + //return START_STICKY; } Bundle extras = intent.getExtras(); -- GitLab