Skip to content

Commit

Permalink
fix:申请存储权限
Browse files Browse the repository at this point in the history
  • Loading branch information
darkal committed Jul 8, 2018
1 parent 03875ac commit 56cb4f9
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 85 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ before_script:
- mkdir "$ANDROID_HOME/licenses" || true
- echo -e "\n8933bad161af4178b1185d1a37fbf41ea5269c55" > "$ANDROID_HOME/licenses/android-sdk-license"
- echo -e "\n84831b9409646a918e30573bab4c9c91346d8abd" > "$ANDROID_HOME/licenses/android-sdk-preview-license"
- echo no | android create avd --force -n test -t android-23 --abi armeabi-v7a
- echo no | android create avd --force -n test -t android-23
- emulator -avd test -no-audio -no-window &
- android-wait-for-emulator
- adb shell input keyevent 82 &
14 changes: 5 additions & 9 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
apply plugin: 'com.android.application'
apply plugin: 'bugly'

bugly {
upload = false
appId = 'db9f598223' // 注册时分配的App ID
appKey = '119ae85f-0c0b-485c-8ae2-da08e472b9e2' // 注册时分配的App Key
}

android {
signingConfigs {
Expand Down Expand Up @@ -70,6 +64,9 @@ android {

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
// Bugly上报
implementation 'com.tencent.bugly:crashreport_upgrade:1.3.5'

testImplementation 'junit:junit:4.12'
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
Expand Down Expand Up @@ -110,9 +107,8 @@ dependencies {
// 启用Netty的日志输出(调试用)
// compile 'com.noveogroup.android:android-logger:1.3.5'
// 文件上传插件
implementation 'net.gotev:uploadservice:3.0.3'
// Bugly上报
implementation 'com.tencent.bugly:crashreport_upgrade:latest.release'
implementation 'net.gotev:uploadservice:3.3'

// compile 'com.tencent.bugly:nativecrashreport:latest.release'
// 图片显示
implementation 'com.github.bumptech.glide:glide:3.7.0'
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@
android:supportsRtl="true"
android:theme="@style/AppTheme">

<!-- 配置APP ID -->
<meta-data
android:name="BUGLY_APPID"
android:value="db9f598223" />
<!-- 配置APP版本号 -->
<meta-data
android:name="BUGLY_APP_VERSION"
android:value="31" />

<!-- 配置Bugly调试模式(true或者false)-->
<meta-data
android:name="BUGLY_ENABLE_DEBUG"
android:value="false" />

<!-- 配置APP渠道号 -->
<meta-data
android:name="BUGLY_APP_CHANNEL"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.KeyStore;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
Expand All @@ -85,7 +83,7 @@
import cn.darkal.networkdiagnosis.R;
import cn.darkal.networkdiagnosis.SysApplication;
import cn.darkal.networkdiagnosis.Utils.DeviceUtils;
import cn.darkal.networkdiagnosis.Utils.FileUtils;
import cn.darkal.networkdiagnosis.Utils.FileUtil;
import cn.darkal.networkdiagnosis.Utils.SharedPreferenceUtils;
import cn.darkal.networkdiagnosis.Utils.ZipUtils;
import cn.darkal.networkdiagnosis.View.LoadingDialog;
Expand Down Expand Up @@ -465,6 +463,7 @@ public void installCert() {

if (!isInstallCert) {
Toast.makeText(this, "必须安装证书才可实现HTTPS抓包", Toast.LENGTH_LONG).show();
FileUtil.checkPermission(this);
try {
byte[] keychainBytes;

Expand Down Expand Up @@ -643,6 +642,7 @@ private void handleUriStartupParams() {

public void createZip(final Runnable callback) {
showLoading("打包中");
FileUtil.checkPermission(this);
new Thread(new Runnable() {
@Override
public void run() {
Expand Down Expand Up @@ -702,20 +702,20 @@ public void uploadZip() {

public class MyUploadDelegate implements UploadStatusDelegate {
@Override
public void onProgress(UploadInfo uploadInfo) {
public void onProgress(Context context, UploadInfo uploadInfo) {
Log.e("~~~~", uploadInfo.getProgressPercent() + "");
}

@Override
public void onError(UploadInfo uploadInfo, Exception exception) {
public void onError(Context context, UploadInfo uploadInfo, ServerResponse serverResponse, Exception exception) {
dismissLoading();
Snackbar.make(rootView, "上传失败!", Snackbar.LENGTH_LONG).setAction("Action", null).show();
exception.printStackTrace();
CrashReport.postCatchedException(exception);
}

@Override
public void onCompleted(UploadInfo uploadInfo, ServerResponse serverResponse) {
public void onCompleted(Context context, UploadInfo uploadInfo, ServerResponse serverResponse) {
try {
JSONObject jsonObject = new JSONObject(serverResponse.getBodyAsString());
if (jsonObject.getInt("errId") == 0) {
Expand All @@ -733,7 +733,7 @@ public void onCompleted(UploadInfo uploadInfo, ServerResponse serverResponse) {
}

@Override
public void onCancelled(UploadInfo uploadInfo) {
public void onCancelled(Context context, UploadInfo uploadInfo) {
dismissLoading();
}
}
Expand Down Expand Up @@ -791,7 +791,7 @@ public void onClick(DialogInterface dialog, int whichButton) {
public void run() {
String serverUrl = UPLOAD_URL + "?code=" + edtInput.getText() + "&os=Android&module=" + Build.MODEL.replace(" ", "") + "&key=" + key;
showLoading("上传中");
FileUtils.uploadFiles(MainActivity.this, new MyUploadDelegate(), serverUrl, "upload", Environment.getExternalStorageDirectory() + "/test.zip");
FileUtil.uploadFiles(MainActivity.this, new MyUploadDelegate(), serverUrl, "upload", Environment.getExternalStorageDirectory() + "/test.zip");
}
};
createZip(runnable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import cn.darkal.networkdiagnosis.R;
import cn.darkal.networkdiagnosis.SysApplication;
import cn.darkal.networkdiagnosis.Utils.DeviceUtils;
import cn.darkal.networkdiagnosis.Utils.FileUtil;
import cn.darkal.networkdiagnosis.Utils.SharedPreferenceUtils;
import cn.darkal.networkdiagnosis.View.LoadingDialog;

Expand Down Expand Up @@ -158,9 +159,12 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
}



public void installCert() {
final String CERTIFICATE_RESOURCE = Environment.getExternalStorageDirectory() + "/har/littleproxy-mitm.pem";
Toast.makeText(this, "必须安装证书才可实现HTTPS抓包", Toast.LENGTH_LONG).show();
FileUtil.checkPermission(this);

try {
byte[] keychainBytes;
FileInputStream is = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import android.support.multidex.MultiDexApplication;
import android.util.Log;

import com.tencent.bugly.Bugly;

import com.tencent.bugly.crashreport.CrashReport;

import net.gotev.uploadservice.UploadService;
Expand Down Expand Up @@ -47,9 +47,8 @@ public void onCreate() {
initProxy();
// Gradle automatically generates proper variable as below.
UploadService.NAMESPACE = BuildConfig.APPLICATION_ID;
CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(getApplicationContext());
strategy.setAppChannel("GitHub");
Bugly.init(getApplicationContext(), "db9f598223", true);

CrashReport.initCrashReport(getApplicationContext(), "db9f598223", false);
}

public void initProxy() {
Expand Down
42 changes: 42 additions & 0 deletions app/src/main/java/cn/darkal/networkdiagnosis/Utils/FileUtil.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
package cn.darkal.networkdiagnosis.Utils;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.widget.Toast;

import net.gotev.uploadservice.MultipartUploadRequest;
import net.gotev.uploadservice.UploadStatusDelegate;

import java.io.BufferedOutputStream;
import java.io.File;
Expand Down Expand Up @@ -434,4 +442,38 @@ public static void deleteFiles(File file){
}
}catch (Exception e){}
}

public static void checkPermission(Activity activity) {
//检查权限(NEED_PERMISSION)是否被授权 PackageManager.PERMISSION_GRANTED表示同意授权
if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
//用户已经拒绝过一次,再次弹出权限申请对话框需要给用户一个解释
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission
.WRITE_EXTERNAL_STORAGE)) {
Toast.makeText(activity, "请开通相关权限,否则无法正常使用本应用!", Toast.LENGTH_SHORT).show();
}
//申请权限
ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);

} else {
Toast.makeText(activity, "授权成功!", Toast.LENGTH_SHORT).show();
}
}


public static void uploadFiles(Context context, UploadStatusDelegate uploadStatusDelegate, String serverUrlString, String paramNameString, String filesToUploadString) {
final String[] filesToUploadArray = filesToUploadString.split(",");

for (String fileToUploadPath : filesToUploadArray) {
try {
MultipartUploadRequest req = new MultipartUploadRequest(context, serverUrlString)
.addFileToUpload(fileToUploadPath, paramNameString).setMethod("POST")
.setMaxRetries(3);

req.setDelegate(uploadStatusDelegate).startUpload();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
62 changes: 0 additions & 62 deletions app/src/main/java/cn/darkal/networkdiagnosis/Utils/FileUtils.java

This file was deleted.

2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ buildscript {

allprojects {
repositories {
google()
jcenter()
google()
}
}

Expand Down

0 comments on commit 56cb4f9

Please sign in to comment.