|
| 1 | +package com.tns; |
| 2 | + |
| 3 | +import java.io.ByteArrayOutputStream; |
| 4 | +import java.io.File; |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.PrintStream; |
| 7 | +import java.io.UnsupportedEncodingException; |
| 8 | + |
| 9 | +import android.app.Activity; |
| 10 | +import android.app.PendingIntent; |
| 11 | +import android.app.PendingIntent.CanceledException; |
| 12 | +import android.content.ClipData; |
| 13 | +import android.content.ClipboardManager; |
| 14 | +import android.content.Context; |
| 15 | +import android.content.Intent; |
| 16 | +import android.graphics.drawable.GradientDrawable; |
| 17 | +import android.text.method.ScrollingMovementMethod; |
| 18 | +import android.util.Log; |
| 19 | +import android.view.View; |
| 20 | +import android.view.View.OnClickListener; |
| 21 | +import android.widget.Button; |
| 22 | +import android.widget.LinearLayout; |
| 23 | +import android.widget.TextView; |
| 24 | + |
| 25 | +class ErrorReport |
| 26 | +{ |
| 27 | + public static final String ERROR_FILE_NAME = "hasError"; |
| 28 | + private final Activity activity; |
| 29 | + |
| 30 | + private final static String EXTRA_NATIVESCRIPT_ERROR_REPORT = "NativeScriptErrorMessage"; |
| 31 | + private final static String EXTRA_ERROR_REPORT_MSG = "msg"; |
| 32 | + private final static int EXTRA_ERROR_REPORT_VALUE = 1; |
| 33 | + |
| 34 | + public ErrorReport(Activity activity) |
| 35 | + { |
| 36 | + this.activity = activity; |
| 37 | + } |
| 38 | + |
| 39 | + static boolean startActivity(final Context context, String errorMessage) |
| 40 | + { |
| 41 | + final Intent intent = getIntent(context); |
| 42 | + if (intent == null) |
| 43 | + { |
| 44 | + return false; // (if in release mode) don't do anything |
| 45 | + } |
| 46 | + |
| 47 | + intent.putExtra(EXTRA_ERROR_REPORT_MSG, errorMessage); |
| 48 | + |
| 49 | + createErrorFile(context); |
| 50 | + |
| 51 | + try |
| 52 | + { |
| 53 | + startPendingErrorActivity(context, intent); |
| 54 | + } |
| 55 | + catch (CanceledException e) |
| 56 | + { |
| 57 | + Log.d("ErrorReport", "Couldn't send pending intent! Exception: " + e.getMessage()); |
| 58 | + } |
| 59 | + |
| 60 | + killProcess(context); |
| 61 | + |
| 62 | + return true; |
| 63 | + } |
| 64 | + |
| 65 | + static void killProcess(Context context) |
| 66 | + { |
| 67 | + // finish current activity and all below it first |
| 68 | + if (context instanceof Activity) |
| 69 | + { |
| 70 | + ((Activity) context).finishAffinity(); |
| 71 | + } |
| 72 | + |
| 73 | + // kill process |
| 74 | + android.os.Process.killProcess(android.os.Process.myPid()); |
| 75 | + } |
| 76 | + |
| 77 | + static void startPendingErrorActivity(Context context, Intent intent) throws CanceledException |
| 78 | + { |
| 79 | + PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); |
| 80 | + |
| 81 | + pendingIntent.send(context, 0, intent); |
| 82 | + } |
| 83 | + |
| 84 | + static String getErrorMessage(Throwable ex) |
| 85 | + { |
| 86 | + String content; |
| 87 | + PrintStream ps = null; |
| 88 | + |
| 89 | + try |
| 90 | + { |
| 91 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 92 | + ps = new PrintStream(baos); |
| 93 | + ex.printStackTrace(ps); |
| 94 | + |
| 95 | + try |
| 96 | + { |
| 97 | + content = baos.toString("US-ASCII"); |
| 98 | + } |
| 99 | + catch (UnsupportedEncodingException e) |
| 100 | + { |
| 101 | + content = e.getMessage(); |
| 102 | + } |
| 103 | + } |
| 104 | + finally |
| 105 | + { |
| 106 | + if (ps != null) |
| 107 | + ps.close(); |
| 108 | + } |
| 109 | + |
| 110 | + return content; |
| 111 | + } |
| 112 | + |
| 113 | + static Intent getIntent(Context context) |
| 114 | + { |
| 115 | + Class<?> errorActivityClass = Platform.getErrorActivityClass(); // can be null or can be provided beforehand |
| 116 | + |
| 117 | + // if in debug and errorActivityClass is not provided use ErrorReportActivity class |
| 118 | + if (errorActivityClass == null && JsDebugger.isDebuggableApp(context)) |
| 119 | + { |
| 120 | + errorActivityClass = ErrorReportActivity.class; |
| 121 | + } |
| 122 | + |
| 123 | + // if not in debug mode should return null and use the errorActivityClass implementation provided |
| 124 | + if (errorActivityClass == null) |
| 125 | + { |
| 126 | + return null; |
| 127 | + } |
| 128 | + |
| 129 | + Intent intent = new Intent(context, errorActivityClass); |
| 130 | + |
| 131 | + intent.putExtra(EXTRA_NATIVESCRIPT_ERROR_REPORT, EXTRA_ERROR_REPORT_VALUE); |
| 132 | + intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); |
| 133 | + |
| 134 | + return intent; |
| 135 | + } |
| 136 | + |
| 137 | + static boolean hasIntent(Intent intent) |
| 138 | + { |
| 139 | + int value = intent.getIntExtra(EXTRA_NATIVESCRIPT_ERROR_REPORT, 0); |
| 140 | + |
| 141 | + return value == EXTRA_ERROR_REPORT_VALUE; |
| 142 | + } |
| 143 | + |
| 144 | + void buildUI() |
| 145 | + { |
| 146 | + Context context = activity; |
| 147 | + Intent intent = activity.getIntent(); |
| 148 | + final String msg = intent.getStringExtra(EXTRA_ERROR_REPORT_MSG); |
| 149 | + |
| 150 | + // container |
| 151 | + LinearLayout layout = new LinearLayout(context); |
| 152 | + layout.setOrientation(LinearLayout.VERTICAL); |
| 153 | + activity.setContentView(layout); |
| 154 | + |
| 155 | + // header |
| 156 | + TextView txtHeader = new TextView(context); |
| 157 | + txtHeader.setText("Unhandled Exception"); |
| 158 | + |
| 159 | + // error + stacktrace |
| 160 | + TextView txtErrorMsg = new TextView(context); |
| 161 | + txtErrorMsg.setText(msg); |
| 162 | + txtErrorMsg.setHeight(1000); |
| 163 | + txtErrorMsg.setMovementMethod(new ScrollingMovementMethod()); |
| 164 | + |
| 165 | + // copy button |
| 166 | + Button copyToClipboard = new Button(context); |
| 167 | + copyToClipboard.setText("Copy to clipboard"); |
| 168 | + copyToClipboard.setOnClickListener(new OnClickListener() |
| 169 | + { |
| 170 | + @Override |
| 171 | + public void onClick(View v) |
| 172 | + { |
| 173 | + |
| 174 | + ClipboardManager clipboard = (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE); |
| 175 | + ClipData clip = ClipData.newPlainText("nsError", msg); |
| 176 | + clipboard.setPrimaryClip(clip); |
| 177 | + } |
| 178 | + }); |
| 179 | + |
| 180 | + layout.addView(txtHeader); |
| 181 | + layout.addView(txtErrorMsg); |
| 182 | + layout.addView(copyToClipboard); |
| 183 | + } |
| 184 | + |
| 185 | + private static void createErrorFile(final Context context) |
| 186 | + { |
| 187 | + try |
| 188 | + { |
| 189 | + File errFile = new File(context.getFilesDir(), ERROR_FILE_NAME); |
| 190 | + errFile.createNewFile(); |
| 191 | + } |
| 192 | + catch (IOException e) |
| 193 | + { |
| 194 | + Log.d("ErrorReport", e.getMessage()); |
| 195 | + } |
| 196 | + } |
| 197 | +} |
0 commit comments