Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
e
os
eDrive
Commits
dbcfd22f
Commit
dbcfd22f
authored
Dec 12, 2019
by
vince-bourgmayer
Browse files
add method to setUEH for thread
parent
50eda989
Changes
2
Hide whitespace changes
Inline
Side-by-side
app/src/main/java/foundation/e/drive/utils/CommonUtils.java
View file @
dbcfd22f
...
@@ -13,6 +13,7 @@ import android.accounts.Account;
...
@@ -13,6 +13,7 @@ import android.accounts.Account;
import
android.accounts.AccountManager
;
import
android.accounts.AccountManager
;
import
android.app.NotificationManager
;
import
android.app.NotificationManager
;
import
android.app.PendingIntent
;
import
android.app.PendingIntent
;
import
android.app.Service
;
import
android.content.ContentResolver
;
import
android.content.ContentResolver
;
import
android.content.Context
;
import
android.content.Context
;
import
android.media.MediaScannerConnection
;
import
android.media.MediaScannerConnection
;
...
@@ -40,6 +41,25 @@ import static foundation.e.drive.utils.AppConstants.SETTINGSYNC_PROVIDER_AUTHORI
...
@@ -40,6 +41,25 @@ import static foundation.e.drive.utils.AppConstants.SETTINGSYNC_PROVIDER_AUTHORI
public
abstract
class
CommonUtils
{
public
abstract
class
CommonUtils
{
final
private
static
String
TAG
=
CommonUtils
.
class
.
getSimpleName
();
final
private
static
String
TAG
=
CommonUtils
.
class
.
getSimpleName
();
/**
* Set ServiceUncaughtExceptionHandler to be the MainThread Exception Handler
* Or update the service which use it
* @param service current service
*/
public
static
void
setServiceUnCaughtExceptionHandler
(
Service
service
){
Thread
.
UncaughtExceptionHandler
defaultUEH
=
Thread
.
getDefaultUncaughtExceptionHandler
();
if
(
defaultUEH
.
getClass
().
getSimpleName
().
equals
(
ServiceExceptionHandler
.
class
.
getSimpleName
())){
Log
.
d
(
"ObserverService"
,
"ServiceExceptionHandler already set!"
);
((
ServiceExceptionHandler
)
defaultUEH
).
setService
(
service
);
}
else
{
Thread
.
setDefaultUncaughtExceptionHandler
(
new
ServiceExceptionHandler
(
service
));
}
}
/**
/**
* Unregister from screeOffReceiver component
* Unregister from screeOffReceiver component
* @param context app context
* @param context app context
...
...
app/src/main/java/foundation/e/drive/utils/ServiceExceptionHandler.java
View file @
dbcfd22f
...
@@ -7,6 +7,8 @@
...
@@ -7,6 +7,8 @@
*/
*/
package
foundation.e.drive.utils
;
package
foundation.e.drive.utils
;
import
android.app.Service
;
import
android.app.Service
;
import
android.content.Context
;
import
android.content.SharedPreferences
;
import
android.os.Environment
;
import
android.os.Environment
;
import
android.util.Log
;
import
android.util.Log
;
...
@@ -17,14 +19,23 @@ import java.io.PrintWriter;
...
@@ -17,14 +19,23 @@ import java.io.PrintWriter;
import
java.io.StringWriter
;
import
java.io.StringWriter
;
import
java.lang.Thread.UncaughtExceptionHandler
;
import
java.lang.Thread.UncaughtExceptionHandler
;
import
foundation.e.drive.services.OperationManagerService
;
/**
/**
* @author Vincent Bourgmayer
* @author Vincent Bourgmayer
*/
*/
public
class
ServiceExceptionHandler
implements
UncaughtExceptionHandler
{
public
class
ServiceExceptionHandler
implements
UncaughtExceptionHandler
{
private
final
static
String
TAG
=
ServiceExceptionHandler
.
class
.
getSimpleName
();
private
final
static
String
TAG
=
ServiceExceptionHandler
.
class
.
getSimpleName
();
private
UncaughtExceptionHandler
defaultUEH
;
private
UncaughtExceptionHandler
defaultUEH
;
private
Service
service
;
Service
service
;
/**
* Change the service that could trigger an UncaughtException
* @param service service instance
*/
public
void
setService
(
Service
service
){
this
.
service
=
service
;
}
public
ServiceExceptionHandler
(
Service
service
)
{
public
ServiceExceptionHandler
(
Service
service
)
{
this
.
service
=
service
;
this
.
service
=
service
;
...
@@ -33,6 +44,14 @@ public class ServiceExceptionHandler implements UncaughtExceptionHandler{
...
@@ -33,6 +44,14 @@ public class ServiceExceptionHandler implements UncaughtExceptionHandler{
@Override
@Override
public
void
uncaughtException
(
Thread
t
,
Throwable
e
)
{
public
void
uncaughtException
(
Thread
t
,
Throwable
e
)
{
Log
.
d
(
TAG
,
"Service class: "
+
service
.
getClass
().
getSimpleName
());
//IF OMS is crashing, set settings that it runs to false;
if
(
service
.
getClass
().
getSimpleName
().
equals
(
OperationManagerService
.
class
.
getSimpleName
())){
service
.
getSharedPreferences
(
AppConstants
.
SHARED_PREFERENCE_NAME
,
Context
.
MODE_PRIVATE
)
.
edit
()
.
putBoolean
(
AppConstants
.
KEY_OMS_IS_WORKING
,
false
)
.
apply
();
}
if
(
isExternalStorageAvailable
()
&&
!
isExternalStorageReadOnly
()){
if
(
isExternalStorageAvailable
()
&&
!
isExternalStorageReadOnly
()){
//Get TimeStamp
//Get TimeStamp
...
@@ -42,22 +61,19 @@ public class ServiceExceptionHandler implements UncaughtExceptionHandler{
...
@@ -42,22 +61,19 @@ public class ServiceExceptionHandler implements UncaughtExceptionHandler{
String
fileName
=
"eDrive-crash-"
+
timestamp
+
".log"
;
String
fileName
=
"eDrive-crash-"
+
timestamp
+
".log"
;
File
downloadDir
=
service
.
getApplication
().
getExternalFilesDir
(
"Logs"
);
File
downloadDir
=
service
.
getApplication
().
getExternalFilesDir
(
"Logs"
);
//File downloadDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath());
File
logFile
=
new
File
(
downloadDir
,
fileName
);
File
logFile
=
new
File
(
downloadDir
,
fileName
);
try
{
try
{
FileOutputStream
fos
=
new
FileOutputStream
(
logFile
);
FileOutputStream
fos
=
new
FileOutputStream
(
logFile
);
fos
.
write
(
service
.
getClass
().
getSimpleName
().
getBytes
());
fos
.
write
(
getStackTraceAsString
(
e
).
getBytes
());
fos
.
write
(
getStackTraceAsString
(
e
).
getBytes
());
fos
.
close
();
fos
.
close
();
logFile
.
setReadable
(
true
,
false
);
logFile
.
setReadable
(
true
,
false
);
//DID NOT WORKS: CommonUtils.doActionMediaScannerConnexionScanFile(service, logFile.getCanonicalPath());
}
catch
(
IOException
exception
)
{
}
catch
(
IOException
exception
)
{
exception
.
printStackTrace
();
exception
.
printStackTrace
();
}
}
}
}
//source: https://stackoverflow.com/questions/9050962/rethrow-uncaughtexceptionhandler-exception-after-logging-it/9050990#9050990
//source: https://stackoverflow.com/questions/9050962/rethrow-uncaughtexceptionhandler-exception-after-logging-it/9050990#9050990
if
(
defaultUEH
!=
null
){
if
(
defaultUEH
!=
null
){
defaultUEH
.
uncaughtException
(
t
,
e
);
defaultUEH
.
uncaughtException
(
t
,
e
);
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment