|
| 1 | +package com.joe.common.crash; |
| 2 | + |
| 3 | +import android.Manifest; |
| 4 | +import android.annotation.SuppressLint; |
| 5 | +import android.content.ClipData; |
| 6 | +import android.content.ClipboardManager; |
| 7 | +import android.content.Context; |
| 8 | +import android.content.Intent; |
| 9 | +import android.content.pm.PackageManager; |
| 10 | +import android.graphics.Bitmap; |
| 11 | +import android.graphics.Canvas; |
| 12 | +import android.net.Uri; |
| 13 | +import android.os.Build; |
| 14 | +import android.os.Bundle; |
| 15 | +import android.os.Environment; |
| 16 | +import android.support.annotation.NonNull; |
| 17 | +import android.support.v4.app.ActivityCompat; |
| 18 | +import android.support.v4.content.ContextCompat; |
| 19 | +import android.support.v4.content.FileProvider; |
| 20 | +import android.support.v7.app.AppCompatActivity; |
| 21 | +import android.util.Log; |
| 22 | +import android.view.MenuItem; |
| 23 | +import android.view.View; |
| 24 | +import android.widget.ImageView; |
| 25 | +import android.widget.PopupMenu; |
| 26 | +import android.widget.ScrollView; |
| 27 | +import android.widget.TextView; |
| 28 | +import android.widget.Toast; |
| 29 | + |
| 30 | +import com.joe.common.R; |
| 31 | +import com.tbruyelle.rxpermissions2.RxPermissions; |
| 32 | + |
| 33 | +import java.io.File; |
| 34 | +import java.io.FileNotFoundException; |
| 35 | +import java.io.FileOutputStream; |
| 36 | +import java.io.IOException; |
| 37 | +import java.text.SimpleDateFormat; |
| 38 | + |
| 39 | +/** |
| 40 | + * desc: CrashActivity.java |
| 41 | + * author: Joe |
| 42 | + * created at: 2019/1/23 下午2:17 |
| 43 | + */ |
| 44 | +public class CrashActivity extends AppCompatActivity { |
| 45 | + |
| 46 | + public static final String CRASH_MODEL = "crash_model"; |
| 47 | + @SuppressLint("SimpleDateFormat") |
| 48 | + private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm"); |
| 49 | + private CrashModel model; |
| 50 | + |
| 51 | + @Override |
| 52 | + protected void onCreate(Bundle savedInstanceState) { |
| 53 | + super.onCreate(savedInstanceState); |
| 54 | + setContentView(R.layout.activity_crash); |
| 55 | + model = getIntent().getParcelableExtra(CRASH_MODEL); |
| 56 | + if (model == null) { |
| 57 | + return; |
| 58 | + } |
| 59 | + TextView tv_packageName = findViewById(R.id.tv_packageName); |
| 60 | + TextView textMessage = findViewById(R.id.textMessage); |
| 61 | + TextView tv_className = findViewById(R.id.tv_className); |
| 62 | + TextView tv_methodName = findViewById(R.id.tv_methodName); |
| 63 | + TextView tv_lineNumber = findViewById(R.id.tv_lineNumber); |
| 64 | + TextView tv_exceptionType = findViewById(R.id.tv_exceptionType); |
| 65 | + TextView tv_fullException = findViewById(R.id.tv_fullException); |
| 66 | + TextView tv_time = findViewById(R.id.tv_time); |
| 67 | + TextView tv_model = findViewById(R.id.tv_model); |
| 68 | + TextView tv_brand = findViewById(R.id.tv_brand); |
| 69 | + TextView tv_version = findViewById(R.id.tv_version); |
| 70 | + ImageView iv_more = findViewById(R.id.iv_more); |
| 71 | +// |
| 72 | + tv_packageName.setText(model.getPackageName()); |
| 73 | + textMessage.setText(model.getExceptionMsg()); |
| 74 | + tv_className.setText(model.getFileName()); |
| 75 | + tv_methodName.setText(model.getMethodName()); |
| 76 | + tv_lineNumber.setText(String.valueOf(model.getLineNumber())); |
| 77 | + tv_exceptionType.setText(model.getExceptionType()); |
| 78 | + tv_fullException.setText(model.getFullException()); |
| 79 | + tv_time.setText(df.format(model.getTime())); |
| 80 | + |
| 81 | + tv_model.setText(model.getDevice().getModel()); |
| 82 | + tv_brand.setText(model.getDevice().getBrand()); |
| 83 | + tv_version.setText(model.getDevice().getVersion()); |
| 84 | + |
| 85 | + iv_more.setOnClickListener(v -> { |
| 86 | + PopupMenu menu = new PopupMenu(CrashActivity.this, v); |
| 87 | + menu.inflate(R.menu.menu_more); |
| 88 | + menu.show(); |
| 89 | + menu.setOnMenuItemClickListener(item -> { |
| 90 | + int id = item.getItemId(); |
| 91 | + if (id == R.id.menu_copy_text) { |
| 92 | + String crashText = getShareText(model); |
| 93 | + ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); |
| 94 | + if (cm != null) { |
| 95 | + ClipData mClipData = ClipData.newPlainText("crash", crashText); |
| 96 | + cm.setPrimaryClip(mClipData); |
| 97 | + showToast("拷贝成功"); |
| 98 | + } |
| 99 | + } else if (id == R.id.menu_share_text) { |
| 100 | + String crashText = getShareText(model); |
| 101 | + shareText(crashText); |
| 102 | + } else if (id == R.id.menu_share_image) { |
| 103 | + if (ContextCompat.checkSelfPermission(CrashActivity.this, |
| 104 | + Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED || |
| 105 | + ContextCompat.checkSelfPermission(CrashActivity.this, |
| 106 | + Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) { |
| 107 | + requestPermission(Manifest.permission.READ_EXTERNAL_STORAGE, |
| 108 | + Manifest.permission.WRITE_EXTERNAL_STORAGE); |
| 109 | + } else { |
| 110 | + shareImage(); |
| 111 | + } |
| 112 | + } |
| 113 | + return true; |
| 114 | + }); |
| 115 | + |
| 116 | + }); |
| 117 | + |
| 118 | + } |
| 119 | + |
| 120 | + private String getShareText(CrashModel model) { |
| 121 | + StringBuilder builder = new StringBuilder(); |
| 122 | + |
| 123 | + builder.append("崩溃信息:") |
| 124 | + .append("\n") |
| 125 | + .append(model.getExceptionMsg()) |
| 126 | + .append("\n"); |
| 127 | + builder.append("\n"); |
| 128 | + |
| 129 | + builder.append("类名:") |
| 130 | + .append(model.getFileName()).append("\n"); |
| 131 | + builder.append("\n"); |
| 132 | + |
| 133 | + builder.append("包名:") |
| 134 | + .append(model.getPackageName()).append("\n"); |
| 135 | + builder.append("\n"); |
| 136 | + |
| 137 | + builder.append("方法:").append(model.getMethodName()).append("\n"); |
| 138 | + builder.append("\n"); |
| 139 | + |
| 140 | + builder.append("行数:").append(model.getLineNumber()).append("\n"); |
| 141 | + builder.append("\n"); |
| 142 | + |
| 143 | + builder.append("类型:").append(model.getExceptionType()).append("\n"); |
| 144 | + builder.append("\n"); |
| 145 | + |
| 146 | + builder.append("时间:").append(df.format(model.getTime())).append("\n"); |
| 147 | + builder.append("\n"); |
| 148 | + |
| 149 | + builder.append("设备名称:").append(model.getDevice().getModel()).append("\n"); |
| 150 | + builder.append("\n"); |
| 151 | + |
| 152 | + builder.append("设备厂商:").append(model.getDevice().getBrand()).append("\n"); |
| 153 | + builder.append("\n"); |
| 154 | + |
| 155 | + builder.append("系统版本:").append(model.getDevice().getVersion()).append("\n"); |
| 156 | + builder.append("\n"); |
| 157 | + |
| 158 | + builder.append("全部信息:") |
| 159 | + .append("\n") |
| 160 | + .append(model.getFullException()).append("\n"); |
| 161 | + |
| 162 | + return builder.toString(); |
| 163 | + } |
| 164 | + |
| 165 | + private void shareText(String text) { |
| 166 | + Intent intent = new Intent(Intent.ACTION_SEND); |
| 167 | + intent.setType("text/plain"); |
| 168 | + intent.putExtra(Intent.EXTRA_SUBJECT, "崩溃日志:"); |
| 169 | + intent.putExtra(Intent.EXTRA_TEXT, text); |
| 170 | + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 171 | + startActivity(Intent.createChooser(intent, "分享到")); |
| 172 | + } |
| 173 | + |
| 174 | + |
| 175 | + @SuppressLint("CheckResult") |
| 176 | + private void requestPermission(String... permissions) { |
| 177 | + new RxPermissions(this).request(permissions) |
| 178 | + .subscribe(granted -> { |
| 179 | + if (granted) { |
| 180 | + // I can control the camera now |
| 181 | + } else { |
| 182 | + // Oups permission denied |
| 183 | + } |
| 184 | + }); |
| 185 | + } |
| 186 | + |
| 187 | + |
| 188 | + public Bitmap getBitmapByView(ScrollView view) { |
| 189 | + if (view == null) return null; |
| 190 | + int height = 0; |
| 191 | + for (int i = 0; i < view.getChildCount(); i++) { |
| 192 | + height += view.getChildAt(i).getHeight(); |
| 193 | + } |
| 194 | + Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), height, Bitmap.Config.ARGB_8888); |
| 195 | + Canvas canvas = new Canvas(bitmap); |
| 196 | + canvas.drawRGB(255, 255, 255); |
| 197 | + view.draw(canvas); |
| 198 | + return bitmap; |
| 199 | + } |
| 200 | + |
| 201 | + private File BitmapToFile(Bitmap bitmap) { |
| 202 | + if (bitmap == null) return null; |
| 203 | + String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) |
| 204 | + .getAbsolutePath(); |
| 205 | + File imageFile = new File(path, "crash-" + df.format(model.getTime()) + ".jpg"); |
| 206 | + try { |
| 207 | + FileOutputStream out = new FileOutputStream(imageFile); |
| 208 | + bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); |
| 209 | + out.flush(); |
| 210 | + out.close(); |
| 211 | + bitmap.recycle(); |
| 212 | + } catch (FileNotFoundException e) { |
| 213 | + e.printStackTrace(); |
| 214 | + } catch (IOException e) { |
| 215 | + e.printStackTrace(); |
| 216 | + } |
| 217 | + return imageFile; |
| 218 | + } |
| 219 | + |
| 220 | + private void shareImage() { |
| 221 | + if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { |
| 222 | + showToast("未插入sd卡"); |
| 223 | + return; |
| 224 | + } |
| 225 | + File file = BitmapToFile(getBitmapByView(findViewById(R.id.scrollView))); |
| 226 | + if (file == null || !file.exists()) { |
| 227 | + showToast("图片文件不存在"); |
| 228 | + return; |
| 229 | + } |
| 230 | + |
| 231 | + Intent intent = new Intent(); |
| 232 | + intent.setAction(Intent.ACTION_SEND); |
| 233 | + intent.setType("image/*"); |
| 234 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { |
| 235 | + Uri contentUri = FileProvider.getUriForFile(getApplicationContext(), |
| 236 | + getApplicationContext().getPackageName() + ".crashfileprovider", file); |
| 237 | + intent.putExtra(Intent.EXTRA_STREAM, contentUri); |
| 238 | + } else { |
| 239 | + intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file)); |
| 240 | + } |
| 241 | + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 242 | + intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); |
| 243 | + startActivity(Intent.createChooser(intent, "分享图片")); |
| 244 | + } |
| 245 | + |
| 246 | + private void showToast(String text) { |
| 247 | + Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show(); |
| 248 | + } |
| 249 | + |
| 250 | + @Override |
| 251 | + public void onBackPressed() { |
| 252 | + this.finish(); |
| 253 | + } |
| 254 | +} |
0 commit comments