GeezerVison is showing github.com — fix:申请存储权限 · urdarling/AndroidHttpCapture@56cb4f9
File treeExpand file treeCollapse file tree Open diff view settings java/cn/darkal/networkdiagnosis
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: .travis.yml +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -22,7 +22,7 @@ before_script: | 22 | 22 | - mkdir "$ANDROID_HOME/licenses" || true | 23 | 23 | - echo -e "\n8933bad161af4178b1185d1a37fbf41ea5269c55" > "$ANDROID_HOME/licenses/android-sdk-license" | 24 | 24 | - echo -e "\n84831b9409646a918e30573bab4c9c91346d8abd" > "$ANDROID_HOME/licenses/android-sdk-preview-license" | 25 | | - - echo no | android create avd --force -n test -t android-23 --abi armeabi-v7a | | 25 | + - echo no | android create avd --force -n test -t android-23 | 26 | 26 | - emulator -avd test -no-audio -no-window & | 27 | 27 | - android-wait-for-emulator | 28 | 28 | - adb shell input keyevent 82 & |
Collapse file Copy file name to clipboardExpand all lines: app/build.gradle +5-9Lines changed: 5 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | apply plugin: 'com.android.application' | 2 | | -apply plugin: 'bugly' | 3 | 2 |
| 4 | | -bugly { | 5 | | - upload = false | 6 | | - appId = 'db9f598223' // 注册时分配的App ID | 7 | | - appKey = '119ae85f-0c0b-485c-8ae2-da08e472b9e2' // 注册时分配的App Key | 8 | | -} | 9 | 3 |
| 10 | 4 | android { | 11 | 5 | signingConfigs { | @@ -70,6 +64,9 @@ android { | 70 | 64 |
| 71 | 65 | dependencies { | 72 | 66 | implementation fileTree(include: ['*.jar'], dir: 'libs') | | 67 | + // Bugly上报 | | 68 | + implementation 'com.tencent.bugly:crashreport_upgrade:1.3.5' | | 69 | + | 73 | 70 | testImplementation 'junit:junit:4.12' | 74 | 71 | implementation 'com.jakewharton:butterknife:8.8.1' | 75 | 72 | annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' | @@ -110,9 +107,8 @@ dependencies { | 110 | 107 | // 启用Netty的日志输出(调试用) | 111 | 108 | // compile 'com.noveogroup.android:android-logger:1.3.5' | 112 | 109 | // 文件上传插件 | 113 | | - implementation 'net.gotev:uploadservice:3.0.3' | 114 | | - // Bugly上报 | 115 | | - implementation 'com.tencent.bugly:crashreport_upgrade:latest.release' | | 110 | + implementation 'net.gotev:uploadservice:3.3' | | 111 | + | 116 | 112 | // compile 'com.tencent.bugly:nativecrashreport:latest.release' | 117 | 113 | // 图片显示 | 118 | 114 | implementation 'com.github.bumptech.glide:glide:3.7.0' | |
Collapse file Copy file name to clipboardExpand all lines: app/src/main/AndroidManifest.xml +14Lines changed: 14 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 27 | 27 | android:supportsRtl="true" | 28 | 28 | android:theme="@style/AppTheme"> | 29 | 29 |
| | 30 | + <!-- 配置APP ID --> | | 31 | + <meta-data | | 32 | + android:name="BUGLY_APPID" | | 33 | + android:value="db9f598223" /> | | 34 | + <!-- 配置APP版本号 --> | | 35 | + <meta-data | | 36 | + android:name="BUGLY_APP_VERSION" | | 37 | + android:value="31" /> | | 38 | + | | 39 | + <!-- 配置Bugly调试模式(true或者false)--> | | 40 | + <meta-data | | 41 | + android:name="BUGLY_ENABLE_DEBUG" | | 42 | + android:value="false" /> | | 43 | + | 30 | 44 | <!-- 配置APP渠道号 --> | 31 | 45 | <meta-data | 32 | 46 | android:name="BUGLY_APP_CHANNEL" | |
Collapse file Copy file name to clipboardExpand all lines: app/src/main/java/cn/darkal/networkdiagnosis/Activity/MainActivity.java +8-8Lines changed: 8 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 63 | 63 |
| 64 | 64 | import java.io.File; | 65 | 65 | import java.io.FileInputStream; | 66 | | -import java.io.InputStream; | 67 | | -import java.security.KeyStore; | 68 | 66 | import java.text.SimpleDateFormat; | 69 | 67 | import java.util.ArrayList; | 70 | 68 | import java.util.Date; | | 85 | 83 | import cn.darkal.networkdiagnosis.R; | 86 | 84 | import cn.darkal.networkdiagnosis.SysApplication; | 87 | 85 | import cn.darkal.networkdiagnosis.Utils.DeviceUtils; | 88 | | -import cn.darkal.networkdiagnosis.Utils.FileUtils; | | 86 | +import cn.darkal.networkdiagnosis.Utils.FileUtil; | 89 | 87 | import cn.darkal.networkdiagnosis.Utils.SharedPreferenceUtils; | 90 | 88 | import cn.darkal.networkdiagnosis.Utils.ZipUtils; | 91 | 89 | import cn.darkal.networkdiagnosis.View.LoadingDialog; | @@ -465,6 +463,7 @@ public void installCert() { | 465 | 463 |
| 466 | 464 | if (!isInstallCert) { | 467 | 465 | Toast.makeText(this, "必须安装证书才可实现HTTPS抓包", Toast.LENGTH_LONG).show(); | | 466 | + FileUtil.checkPermission(this); | 468 | 467 | try { | 469 | 468 | byte[] keychainBytes; | 470 | 469 |
| @@ -643,6 +642,7 @@ private void handleUriStartupParams() { | 643 | 642 |
| 644 | 643 | public void createZip(final Runnable callback) { | 645 | 644 | showLoading("打包中"); | | 645 | + FileUtil.checkPermission(this); | 646 | 646 | new Thread(new Runnable() { | 647 | 647 | @Override | 648 | 648 | public void run() { | @@ -702,20 +702,20 @@ public void uploadZip() { | 702 | 702 |
| 703 | 703 | public class MyUploadDelegate implements UploadStatusDelegate { | 704 | 704 | @Override | 705 | | - public void onProgress(UploadInfo uploadInfo) { | | 705 | + public void onProgress(Context context, UploadInfo uploadInfo) { | 706 | 706 | Log.e("~~~~", uploadInfo.getProgressPercent() + ""); | 707 | 707 | } | 708 | 708 |
| 709 | 709 | @Override | 710 | | - public void onError(UploadInfo uploadInfo, Exception exception) { | | 710 | + public void onError(Context context, UploadInfo uploadInfo, ServerResponse serverResponse, Exception exception) { | 711 | 711 | dismissLoading(); | 712 | 712 | Snackbar.make(rootView, "上传失败!", Snackbar.LENGTH_LONG).setAction("Action", null).show(); | 713 | 713 | exception.printStackTrace(); | 714 | 714 | CrashReport.postCatchedException(exception); | 715 | 715 | } | 716 | 716 |
| 717 | 717 | @Override | 718 | | - public void onCompleted(UploadInfo uploadInfo, ServerResponse serverResponse) { | | 718 | + public void onCompleted(Context context, UploadInfo uploadInfo, ServerResponse serverResponse) { | 719 | 719 | try { | 720 | 720 | JSONObject jsonObject = new JSONObject(serverResponse.getBodyAsString()); | 721 | 721 | if (jsonObject.getInt("errId") == 0) { | @@ -733,7 +733,7 @@ public void onCompleted(UploadInfo uploadInfo, ServerResponse serverResponse) { | 733 | 733 | } | 734 | 734 |
| 735 | 735 | @Override | 736 | | - public void onCancelled(UploadInfo uploadInfo) { | | 736 | + public void onCancelled(Context context, UploadInfo uploadInfo) { | 737 | 737 | dismissLoading(); | 738 | 738 | } | 739 | 739 | } | @@ -791,7 +791,7 @@ public void onClick(DialogInterface dialog, int whichButton) { | 791 | 791 | public void run() { | 792 | 792 | String serverUrl = UPLOAD_URL + "?code=" + edtInput.getText() + "&os=Android&module=" + Build.MODEL.replace(" ", "") + "&key=" + key; | 793 | 793 | showLoading("上传中"); | 794 | | - FileUtils.uploadFiles(MainActivity.this, new MyUploadDelegate(), serverUrl, "upload", Environment.getExternalStorageDirectory() + "/test.zip"); | | 794 | + FileUtil.uploadFiles(MainActivity.this, new MyUploadDelegate(), serverUrl, "upload", Environment.getExternalStorageDirectory() + "/test.zip"); | 795 | 795 | } | 796 | 796 | }; | 797 | 797 | createZip(runnable); | |
Collapse file Copy file name to clipboardExpand all lines: app/src/main/java/cn/darkal/networkdiagnosis/Activity/SettingsActivity.java +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 28 | 28 | import cn.darkal.networkdiagnosis.R; | 29 | 29 | import cn.darkal.networkdiagnosis.SysApplication; | 30 | 30 | import cn.darkal.networkdiagnosis.Utils.DeviceUtils; | | 31 | +import cn.darkal.networkdiagnosis.Utils.FileUtil; | 31 | 32 | import cn.darkal.networkdiagnosis.Utils.SharedPreferenceUtils; | 32 | 33 | import cn.darkal.networkdiagnosis.View.LoadingDialog; | 33 | 34 |
| @@ -158,9 +159,12 @@ public boolean onPreferenceChange(Preference preference, Object newValue) { | 158 | 159 | } | 159 | 160 |
| 160 | 161 |
| | 162 | + | 161 | 163 | public void installCert() { | 162 | 164 | final String CERTIFICATE_RESOURCE = Environment.getExternalStorageDirectory() + "/har/littleproxy-mitm.pem"; | 163 | 165 | Toast.makeText(this, "必须安装证书才可实现HTTPS抓包", Toast.LENGTH_LONG).show(); | | 166 | + FileUtil.checkPermission(this); | | 167 | + | 164 | 168 | try { | 165 | 169 | byte[] keychainBytes; | 166 | 170 | FileInputStream is = null; | |
Collapse file Copy file name to clipboardExpand all lines: app/src/main/java/cn/darkal/networkdiagnosis/SysApplication.java +3-4Lines changed: 3 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 7 | 7 | import android.support.multidex.MultiDexApplication; | 8 | 8 | import android.util.Log; | 9 | 9 |
| 10 | | -import com.tencent.bugly.Bugly; | | 10 | + | 11 | 11 | import com.tencent.bugly.crashreport.CrashReport; | 12 | 12 |
| 13 | 13 | import net.gotev.uploadservice.UploadService; | @@ -47,9 +47,8 @@ public void onCreate() { | 47 | 47 | initProxy(); | 48 | 48 | // Gradle automatically generates proper variable as below. | 49 | 49 | UploadService.NAMESPACE = BuildConfig.APPLICATION_ID; | 50 | | - CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(getApplicationContext()); | 51 | | - strategy.setAppChannel("GitHub"); | 52 | | - Bugly.init(getApplicationContext(), "db9f598223", true); | | 50 | + | | 51 | + CrashReport.initCrashReport(getApplicationContext(), "db9f598223", false); | 53 | 52 | } | 54 | 53 |
| 55 | 54 | public void initProxy() { | |
Collapse file Copy file name to clipboardExpand all lines: app/src/main/java/cn/darkal/networkdiagnosis/Utils/FileUtil.java +42Lines changed: 42 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | package cn.darkal.networkdiagnosis.Utils; | 2 | 2 |
| | 3 | +import android.Manifest; | | 4 | +import android.app.Activity; | 3 | 5 | import android.content.Context; | 4 | 6 | import android.content.Intent; | | 7 | +import android.content.pm.PackageManager; | 5 | 8 | import android.net.Uri; | 6 | 9 | import android.os.Environment; | 7 | 10 | import android.provider.MediaStore; | | 11 | +import android.support.v4.app.ActivityCompat; | 8 | 12 | import android.util.Log; | | 13 | +import android.widget.Toast; | | 14 | + | | 15 | +import net.gotev.uploadservice.MultipartUploadRequest; | | 16 | +import net.gotev.uploadservice.UploadStatusDelegate; | 9 | 17 |
| 10 | 18 | import java.io.BufferedOutputStream; | 11 | 19 | import java.io.File; | @@ -434,4 +442,38 @@ public static void deleteFiles(File file){ | 434 | 442 | } | 435 | 443 | }catch (Exception e){} | 436 | 444 | } | | 445 | + | | 446 | + public static void checkPermission(Activity activity) { | | 447 | + //检查权限(NEED_PERMISSION)是否被授权 PackageManager.PERMISSION_GRANTED表示同意授权 | | 448 | + if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) | | 449 | + != PackageManager.PERMISSION_GRANTED) { | | 450 | + //用户已经拒绝过一次,再次弹出权限申请对话框需要给用户一个解释 | | 451 | + if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission | | 452 | + .WRITE_EXTERNAL_STORAGE)) { | | 453 | + Toast.makeText(activity, "请开通相关权限,否则无法正常使用本应用!", Toast.LENGTH_SHORT).show(); | | 454 | + } | | 455 | + //申请权限 | | 456 | + ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); | | 457 | + | | 458 | + } else { | | 459 | + Toast.makeText(activity, "授权成功!", Toast.LENGTH_SHORT).show(); | | 460 | + } | | 461 | + } | | 462 | + | | 463 | + | | 464 | + public static void uploadFiles(Context context, UploadStatusDelegate uploadStatusDelegate, String serverUrlString, String paramNameString, String filesToUploadString) { | | 465 | + final String[] filesToUploadArray = filesToUploadString.split(","); | | 466 | + | | 467 | + for (String fileToUploadPath : filesToUploadArray) { | | 468 | + try { | | 469 | + MultipartUploadRequest req = new MultipartUploadRequest(context, serverUrlString) | | 470 | + .addFileToUpload(fileToUploadPath, paramNameString).setMethod("POST") | | 471 | + .setMaxRetries(3); | | 472 | + | | 473 | + req.setDelegate(uploadStatusDelegate).startUpload(); | | 474 | + } catch (Exception e) { | | 475 | + e.printStackTrace(); | | 476 | + } | | 477 | + } | | 478 | + } | 437 | 479 | } |
Collapse file Copy file name to clipboardExpand all lines: app/src/main/java/cn/darkal/networkdiagnosis/Utils/FileUtils.java -62Lines changed: 0 additions & 62 deletions Collapse file Copy file name to clipboardExpand all lines: build.gradle +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -15,8 +15,8 @@ buildscript { | 15 | 15 |
| 16 | 16 | allprojects { | 17 | 17 | repositories { | 18 | | - google() | 19 | 18 | jcenter() | | 19 | + google() | 20 | 20 | } | 21 | 21 | } | 22 | 22 |
| |
|