Creato ExceptionHandler e PermissionsHelper.
This commit is contained in:
parent
70af7b7396
commit
d4ce614ab5
BIN
.idea/caches/build_file_checksums.ser
generated
BIN
.idea/caches/build_file_checksums.ser
generated
Binary file not shown.
@ -9,10 +9,11 @@ android {
|
||||
applicationId "it.integry.integrywmsnative"
|
||||
minSdkVersion 21
|
||||
targetSdkVersion 28
|
||||
versionCode 5
|
||||
versionName "1.0.2"
|
||||
versionCode 6
|
||||
versionName "1.0.3"
|
||||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
|
||||
}
|
||||
|
||||
buildTypes {
|
||||
release {
|
||||
minifyEnabled false
|
||||
@ -48,6 +49,9 @@ dependencies {
|
||||
implementation 'org.parceler:parceler-api:1.1.10'
|
||||
annotationProcessor 'org.parceler:parceler:1.1.10'
|
||||
implementation 'com.annimon:stream:1.2.1'
|
||||
|
||||
implementation 'com.github.someshkumar049:permissionmadeeasy:1.1.1'
|
||||
|
||||
//MVVM
|
||||
implementation 'android.arch.lifecycle:extensions:1.1.1'
|
||||
annotationProcessor "android.arch.lifecycle:compiler:1.1.1"
|
||||
|
||||
@ -30,6 +30,7 @@ import it.integry.integrywmsnative.core.interfaces.IRecyclerFragment;
|
||||
import it.integry.integrywmsnative.core.interfaces.ISelectAllFragment;
|
||||
import it.integry.integrywmsnative.core.settings.SettingsManager;
|
||||
import it.integry.integrywmsnative.core.update.UpdatesManager;
|
||||
import it.integry.integrywmsnative.core.utility.PermissionsHelper;
|
||||
import it.integry.integrywmsnative.core.utility.UtilitySettings;
|
||||
import it.integry.integrywmsnative.databinding.ActivityMainBinding;
|
||||
import it.integry.integrywmsnative.gest.accettazione.MainAccettazioneFragment;
|
||||
@ -45,18 +46,20 @@ import it.integry.plugins.waterfalltoolbar.WaterfallToolbar;
|
||||
public class MainActivity extends AppCompatActivity
|
||||
implements NavigationView.OnNavigationItemSelectedListener {
|
||||
|
||||
//@BindView(R.id.drawer_username) TextView mDrawerUsername;
|
||||
//@BindView(R.id.drawer_logoAzienda) ImageView mDrawerLogo;
|
||||
|
||||
private ActivityMainBinding mBinding;
|
||||
|
||||
private boolean firstCheckExecution = true;
|
||||
private boolean mIsOnline = false;
|
||||
|
||||
|
||||
private PermissionsHelper permissionsHelper = new PermissionsHelper(this);
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
permissionsHelper.askPermissions();
|
||||
|
||||
if(SettingsManager.i().user.username == null && SettingsManager.i().user.password == null){
|
||||
startLoginActivity();
|
||||
} else {
|
||||
|
||||
@ -11,6 +11,7 @@ import com.orhanobut.logger.Logger;
|
||||
|
||||
import it.integry.integrywmsnative.core.REST.watcher.ServerStatusChecker;
|
||||
import it.integry.integrywmsnative.core.barcode_reader.BarcodeManager;
|
||||
import it.integry.integrywmsnative.core.exception.ExceptionsHandler;
|
||||
import it.integry.integrywmsnative.core.settings.SettingsManager;
|
||||
import it.integry.integrywmsnative.core.settings.Stash;
|
||||
import it.integry.integrywmsnative.core.update.UpdatesManager;
|
||||
@ -30,6 +31,8 @@ public class MainApplication extends Application {
|
||||
super.onCreate();
|
||||
Stash.init(this);
|
||||
|
||||
new ExceptionsHandler(this);
|
||||
|
||||
SettingsManager.init(this);
|
||||
ServerStatusChecker.init();
|
||||
BarcodeManager.init(this);
|
||||
|
||||
@ -8,375 +8,60 @@ import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Build;
|
||||
import android.os.Debug;
|
||||
import android.os.Environment;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ExceptionsHandler implements Thread.UncaughtExceptionHandler{
|
||||
import it.integry.integrywmsnative.core.utility.UtilityLogger;
|
||||
|
||||
public class ExceptionsHandler implements Thread.UncaughtExceptionHandler {
|
||||
|
||||
private ExceptionsHandler(Activity activity){
|
||||
private final static String TAG = ExceptionsHandler.class.getSimpleName();
|
||||
private final static String ERROR_FILE = ExceptionsHandler.class.getSimpleName() + ".error";
|
||||
|
||||
mPackageName = getPackageName(activity);
|
||||
private final Context context;
|
||||
private final Thread.UncaughtExceptionHandler rootHandler;
|
||||
|
||||
public ExceptionsHandler(Context context) {
|
||||
this.context = context;
|
||||
// we should store the current exception handler -- to invoke it for all not handled exceptions ...
|
||||
rootHandler = Thread.getDefaultUncaughtExceptionHandler();
|
||||
// we replace the exception handler now with us -- we will properly dispatch the exceptions ...
|
||||
Thread.setDefaultUncaughtExceptionHandler(this);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static ExceptionsHandler getINSTANCE(Activity activity){
|
||||
|
||||
if (mExceptionsHandler == null){
|
||||
|
||||
mExceptionsHandler = new ExceptionsHandler(activity);
|
||||
}
|
||||
|
||||
return mExceptionsHandler;
|
||||
}
|
||||
|
||||
|
||||
private static String getPackageName(Context pContext){
|
||||
|
||||
String packageName = "";
|
||||
|
||||
try{
|
||||
|
||||
ActivityManager activityManager = (ActivityManager) pContext
|
||||
.getSystemService(Context.ACTIVITY_SERVICE);
|
||||
|
||||
if (Build.VERSION.SDK_INT > 20){
|
||||
|
||||
packageName = activityManager.getRunningAppProcesses().get(0).processName;
|
||||
}else{
|
||||
|
||||
// <uses-permission android:name="android.permission.GET_TASKS" />
|
||||
packageName = activityManager.getRunningTasks(1).get(0).topActivity
|
||||
.getPackageName();
|
||||
}
|
||||
|
||||
// There is a limit to the tag length of 23.
|
||||
packageName = packageName
|
||||
.substring(0, packageName.length() > 22 ? 22 : packageName.length());
|
||||
|
||||
}catch (Exception ex){
|
||||
}
|
||||
|
||||
if (packageName.isEmpty()){
|
||||
packageName = pContext.getPackageName();
|
||||
}
|
||||
|
||||
return packageName;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public static void toCatch(Activity activity){
|
||||
|
||||
Thread.setDefaultUncaughtExceptionHandler(getINSTANCE(activity));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
public static void logError(String message){
|
||||
|
||||
if (message.isEmpty()){
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
logError(new Throwable(message.trim()));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public static void logError(Throwable exception){
|
||||
|
||||
try{
|
||||
|
||||
logCrash(exception);
|
||||
|
||||
}catch (Exception e){
|
||||
|
||||
Log.e(mPackageName, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Return the last error message
|
||||
public static String getErrorMessage(){
|
||||
|
||||
return mErrorMessage;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public static void setErrorMessage(String errMsg){
|
||||
|
||||
mErrorMessage = errMsg;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Return the last crash information
|
||||
public static ApplicationErrorReport.CrashInfo crashInfo(){
|
||||
|
||||
return mCrashInfo;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private static String getAppLabel(Context pContext){
|
||||
|
||||
PackageManager lPackageManager = pContext.getPackageManager();
|
||||
|
||||
ApplicationInfo lApplicationInfo = null;
|
||||
|
||||
try{
|
||||
|
||||
lApplicationInfo = lPackageManager
|
||||
.getApplicationInfo(pContext.getApplicationInfo().packageName, 0);
|
||||
|
||||
}catch (final PackageManager.NameNotFoundException e){
|
||||
}
|
||||
|
||||
return (String) (lApplicationInfo != null ? lPackageManager
|
||||
.getApplicationLabel(lApplicationInfo) : "Unknown");
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static boolean inDebugger(){
|
||||
|
||||
// If in Debugger Environment
|
||||
boolean debugging = Debug.isDebuggerConnected();
|
||||
|
||||
return debugging;
|
||||
}
|
||||
|
||||
|
||||
@NonNull
|
||||
private static String errorMsg(Throwable exception, String exceptError){
|
||||
|
||||
if (!exceptError.contains("error")){
|
||||
|
||||
mReportBuilder.append(reportError(exception));
|
||||
}
|
||||
|
||||
if (!exceptError.contains("callstack")){
|
||||
|
||||
mReportBuilder.append(reportCallStack(exception));
|
||||
}
|
||||
|
||||
if (!exceptError.contains("deviceinfo")){
|
||||
|
||||
mReportBuilder.append(reportDeviceInfo());
|
||||
}
|
||||
|
||||
if (!exceptError.contains("firmware")){
|
||||
|
||||
mReportBuilder.append(reportFirmware());
|
||||
}
|
||||
|
||||
return mReportBuilder.toString();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private static String reportError(Throwable exception){
|
||||
|
||||
mCrashInfo = new ApplicationErrorReport.CrashInfo(exception);
|
||||
|
||||
if (mCrashInfo.exceptionMessage == null){
|
||||
|
||||
mErrorMessage = "<unknown error>";
|
||||
}else{
|
||||
|
||||
mErrorMessage = mCrashInfo.exceptionMessage
|
||||
.replace(": " + mCrashInfo.exceptionClassName, "");
|
||||
}
|
||||
|
||||
String throwFile = mCrashInfo.throwFileName == null ? "<unknown file>"
|
||||
: mCrashInfo.throwFileName;
|
||||
|
||||
return "\n************ " + mCrashInfo.exceptionClassName + " ************\n"
|
||||
+ mErrorMessage + LINE_SEPARATOR
|
||||
+ "\n File: " + throwFile
|
||||
+ "\n Method: " + mCrashInfo.throwMethodName + "()"
|
||||
+ "\n Line No.: " + Integer.toString(mCrashInfo.throwLineNumber)
|
||||
+ LINE_SEPARATOR;
|
||||
// + "Class: " + crashInfo.throwClassName + LINE_SEPARATOR
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private static String reportCallStack(Throwable exception){
|
||||
|
||||
StringWriter stackTrace = new StringWriter();
|
||||
|
||||
exception.printStackTrace(new PrintWriter(stackTrace));
|
||||
|
||||
String callStack = stackTrace.toString();
|
||||
|
||||
String errMsg = exception.toString();
|
||||
|
||||
return "\n************ CALLSTACK ************\n"
|
||||
+ callStack.replace(errMsg, "")
|
||||
+ LINE_SEPARATOR;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private static String reportDeviceInfo(){
|
||||
|
||||
return "\n************ DEVICE INFORMATION ***********\n"
|
||||
+ "Brand: "
|
||||
+ Build.BRAND
|
||||
+ LINE_SEPARATOR
|
||||
+ "Device: "
|
||||
+ Build.DEVICE
|
||||
+ LINE_SEPARATOR
|
||||
+ "Model: "
|
||||
+ Build.MODEL
|
||||
+ LINE_SEPARATOR
|
||||
+ "Id: "
|
||||
+ Build.ID
|
||||
+ LINE_SEPARATOR
|
||||
+ "Product: "
|
||||
+ Build.PRODUCT
|
||||
+ LINE_SEPARATOR;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private static String reportFirmware(){
|
||||
|
||||
return "\n************ FIRMWARE ************\n"
|
||||
+ "SDK: "
|
||||
+ Build.VERSION.SDK_INT
|
||||
+ LINE_SEPARATOR
|
||||
+ "Release: "
|
||||
+ Build.VERSION.RELEASE
|
||||
+ LINE_SEPARATOR
|
||||
+ "Incremental: "
|
||||
+ Build.VERSION.INCREMENTAL
|
||||
+ LINE_SEPARATOR;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// Empty the report as it is begin re-populated.
|
||||
private static void reportEmptied(){
|
||||
|
||||
// No need to empty
|
||||
if (mReportBuilder.length() == 0){
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
mReportBuilder.setLength(0);
|
||||
|
||||
mReportBuilder.trimToSize();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void uncaughtException(Thread thread, Throwable exception){
|
||||
|
||||
// Don't re-enter -- avoid infinite loops if crash-reporting crashes.
|
||||
if (mCrashing) return;
|
||||
|
||||
mCrashing = true;
|
||||
|
||||
catchException(thread, exception);
|
||||
|
||||
defaultExceptionHandler(thread, exception);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public String catchException(Thread thread, Throwable exception){
|
||||
|
||||
String errorMsg = "";
|
||||
|
||||
try{
|
||||
|
||||
errorMsg = logCrash(exception);
|
||||
|
||||
}catch (Exception ex){
|
||||
|
||||
Log.e(mPackageName, ex.getMessage());
|
||||
public void uncaughtException(final Thread thread, final Throwable ex) {
|
||||
try {
|
||||
UtilityLogger.errorMe(new Exception(ex));
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Exception Logger failed!", e);
|
||||
}
|
||||
|
||||
return errorMsg;
|
||||
appendToFile(new Exception(ex));
|
||||
}
|
||||
|
||||
public void appendToFile(Exception e) {
|
||||
try {
|
||||
File yourFile = new File(Environment.getExternalStorageDirectory(), "log.txt");
|
||||
|
||||
|
||||
|
||||
public static void defaultExceptionHandler(Thread thread, Throwable exception){
|
||||
|
||||
try{
|
||||
|
||||
// Execute the old handler.
|
||||
if (mOldHandler != null){
|
||||
|
||||
mOldHandler.uncaughtException(thread, exception);
|
||||
}
|
||||
|
||||
}catch (Exception ex){
|
||||
|
||||
Log.e(mPackageName, ex.getMessage());
|
||||
//String dataFolder = yourFile.getPath();
|
||||
FileWriter fstream = new FileWriter(yourFile.getPath(), true);
|
||||
BufferedWriter out = new BufferedWriter(fstream);
|
||||
PrintWriter pWriter = new PrintWriter(out, true);
|
||||
e.printStackTrace(pWriter);
|
||||
}
|
||||
catch (Exception ie) {
|
||||
throw new RuntimeException("Could not write Exception to file", ie);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public static String logCrash(Throwable exception){
|
||||
|
||||
return errorMsg(exception, "deviceinfo firmware");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void onDestroy(){
|
||||
|
||||
mExceptionsHandler = null;
|
||||
}
|
||||
|
||||
// Prevents infinite loops.
|
||||
private static volatile boolean mCrashing = false;
|
||||
|
||||
private static final StringBuilder mReportBuilder = new StringBuilder();
|
||||
|
||||
private static final String LINE_SEPARATOR = "\n";
|
||||
|
||||
private static final Thread.UncaughtExceptionHandler mOldHandler = Thread
|
||||
.getDefaultUncaughtExceptionHandler();
|
||||
|
||||
private static ExceptionsHandler mExceptionsHandler;
|
||||
|
||||
private static String mPackageName;
|
||||
|
||||
private static ApplicationErrorReport.CrashInfo mCrashInfo;
|
||||
|
||||
private static String mErrorMessage = "";
|
||||
}
|
||||
@ -0,0 +1,46 @@
|
||||
package it.integry.integrywmsnative.core.utility;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import com.somesh.permissionmadeeasy.enums.Permission;
|
||||
import com.somesh.permissionmadeeasy.helper.PermissionHelper;
|
||||
import com.somesh.permissionmadeeasy.intefaces.PermissionListener;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import it.integry.integrywmsnative.MainApplication;
|
||||
import it.integry.integrywmsnative.R;
|
||||
|
||||
public class PermissionsHelper implements PermissionListener {
|
||||
|
||||
private Activity mActivity;
|
||||
private Integer REQUEST_CODE = 1;
|
||||
|
||||
public PermissionsHelper(Activity activity) {
|
||||
this.mActivity = activity;
|
||||
}
|
||||
|
||||
public void askPermissions() {
|
||||
PermissionHelper permissionHelper = PermissionHelper.Builder()
|
||||
.with(this.mActivity)
|
||||
.requestCode(REQUEST_CODE)
|
||||
.setPermissionResultCallback(this)
|
||||
.askFor(Permission.STORAGE)
|
||||
.rationalMessage(mActivity.getResources().getString(R.string.permission_request_message)) //Optional
|
||||
.build();
|
||||
|
||||
permissionHelper.requestPermissions();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onPermissionsGranted(int i, ArrayList<String> arrayList) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPermissionsDenied(int i, ArrayList<String> arrayList) {
|
||||
|
||||
}
|
||||
}
|
||||
@ -57,6 +57,10 @@ public class RettificaGiacenzeFragment extends Fragment implements ITitledFragme
|
||||
mBinding.setViewmodel(mRettificaGiacenzeViewModel);
|
||||
|
||||
init();
|
||||
|
||||
Integer a = null;
|
||||
String b = a.toString();
|
||||
|
||||
return mBinding.getRoot();
|
||||
}
|
||||
|
||||
|
||||
@ -22,6 +22,8 @@
|
||||
<string name="reset">Resetta</string>
|
||||
<string name="dispatched">Evasi</string>
|
||||
|
||||
<string name="permission_request_message">Questi permessi sono necessari al funzionamento dell\'app</string>
|
||||
|
||||
|
||||
<string name="action_settings">Impostazioni</string>
|
||||
<string name="action_customize_server_settings">Personalizza impostazioni azienda</string>
|
||||
|
||||
@ -13,6 +13,8 @@
|
||||
<string name="completed">Completed</string>
|
||||
<string name="search">Search...</string>
|
||||
|
||||
<string name="permission_request_message">Permissions are required for app to work properly</string>
|
||||
|
||||
|
||||
<string name="ord_acq_testata"><![CDATA[#<b>%s</b> of <b>%s</b>]]></string>
|
||||
<string name="ord_ven_testata"><![CDATA[#<b>%s</b> of <b>%s</b>]]></string>
|
||||
|
||||
@ -15,7 +15,7 @@ buildscript {
|
||||
google()
|
||||
}
|
||||
dependencies {
|
||||
classpath "com.android.tools.build:gradle:$gradle_version"
|
||||
classpath "com.android.tools.build:gradle:3.2.1"
|
||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||
|
||||
// NOTE: Do not place your application dependencies here; they belong
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user