Skip to content

Commit

Permalink
支持添加请求头
Browse files Browse the repository at this point in the history
  • Loading branch information
jenly1314 committed Jun 4, 2019
1 parent eb1bbd1 commit dad8814
Show file tree
Hide file tree
Showing 16 changed files with 125 additions and 75 deletions.
Binary file modified .idea/caches/build_file_checksums.ser
Binary file not shown.
25 changes: 0 additions & 25 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/markdown-navigator/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,35 +39,35 @@ AppUpdater for Android 是一个专注于App更新,一键傻瓜式集成App版
<dependency>
<groupId>com.king.app</groupId>
<artifactId>app-updater</artifactId>
<version>1.0.3</version>
<version>1.0.4</version>
<type>pom</type>
</dependency>
//app-dialog
<dependency>
<groupId>com.king.app</groupId>
<artifactId>app-dialog</artifactId>
<version>1.0.3</version>
<version>1.0.4</version>
<type>pom</type>
</dependency>
```
### Gradle:
```gradle
//app-updater
implementation 'com.king.app:app-updater:1.0.3'
implementation 'com.king.app:app-updater:1.0.4'
//app-dialog
implementation 'com.king.app:app-dialog:1.0.3'
implementation 'com.king.app:app-dialog:1.0.4'
```
### Lvy:
```lvy
//app-updater
<dependency org='com.king.app' name='app-dialog' rev='1.0.3'>
<dependency org='com.king.app' name='app-dialog' rev='1.0.4'>
<artifact name='$AID' ext='pom'></artifact>
</dependency>
//app-dialog
<dependency org='com.king.app' name='app-dialog' rev='1.0.3'>
<dependency org='com.king.app' name='app-dialog' rev='1.0.4'>
<artifact name='$AID' ext='pom'></artifact>
</dependency>
```
Expand Down Expand Up @@ -132,6 +132,9 @@ AppUpdater for Android 是一个专注于App更新,一键傻瓜式集成App版

## 版本记录

#### v1.0.4:2019-6-4
* 支持添加请求头

#### v1.0.3:2019-5-9
* 新增支持下载APK优先取本地缓存,避免多次下载相同版本的APK文件
* AppDialog支持隐藏Dialog的标题
Expand Down
3 changes: 2 additions & 1 deletion app-dialog/src/main/java/com/king/app/dialog/AppDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public View createAppDialogView(@NonNull Context context,@NonNull AppDialogConfi
btnDialogCancel.setVisibility(config.isHideCancel() ? View.GONE : View.VISIBLE);

try{
//不强制要求要有横线
//不强制要求要有中间的线
View line = view.findViewById(R.id.line);
line.setVisibility(config.isHideCancel() ? View.GONE : View.VISIBLE);
}catch (Exception e){
Expand Down Expand Up @@ -137,6 +137,7 @@ public String showDialogFragment(FragmentManager fragmentManager,DialogFragment
* @return
*/
public String showDialogFragment(FragmentManager fragmentManager,DialogFragment dialogFragment, String tag) {
dismissDialogFragment(fragmentManager);
dialogFragment.show(fragmentManager,tag);
mTag = tag;
return tag;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public void init(View rootView){
if(config!=null){
TextView tvDialogTitle = rootView.findViewById(config.getTitleId());
setText(tvDialogTitle,config.getTitle());
tvDialogTitle.setVisibility(config.isHideTitle() ? View.GONE : View.VISIBLE);

TextView tvDialogContent = rootView.findViewById(config.getContentId());
setText(tvDialogContent,config.getContent());
Expand All @@ -46,7 +47,7 @@ public void init(View rootView){
btnDialogCancel.setVisibility(config.isHideCancel() ? View.GONE : View.VISIBLE);

try{
//不强制要求要有横线
//不强制要求要有中间的线
View line = rootView.findViewById(R.id.line);
line.setVisibility(config.isHideCancel() ? View.GONE : View.VISIBLE);
}catch (Exception e){
Expand All @@ -56,6 +57,7 @@ public void init(View rootView){
Button btnDialogOK = rootView.findViewById(config.getOkId());
setText(btnDialogOK,config.getOk());
btnDialogOK.setOnClickListener(config.getOnClickOk() != null ? config.getOnClickOk() : getOnClickDismiss());

}
}
}
24 changes: 23 additions & 1 deletion app-updater/src/main/java/com/king/app/updater/AppUpdater.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import android.os.IBinder;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;

Expand All @@ -18,6 +17,8 @@
import com.king.app.updater.service.DownloadService;
import com.king.app.updater.util.PermissionUtils;

import java.util.Map;


/**
* @author Jenly <a href="mailto:jenly1314@gmail.com">Jenly</a>
Expand Down Expand Up @@ -274,6 +275,27 @@ public Builder setVersionCode(Integer versionCode) {
return this;
}

/**
* 请求头添加参数
* @param key
* @param value
* @return
*/
public Builder addHeader(String key, String value){
mConfig.addHeader(key,value);
return this;
}

/**
* 请求头添加参数
* @param headers
* @return
*/
public Builder addHeader(Map<String,String> headers){
mConfig.addHeader(headers);
return this;
}

public AppUpdater build(@NonNull Context context){
AppUpdater appUpdater = new AppUpdater(context,mConfig);
return appUpdater;
Expand Down
60 changes: 41 additions & 19 deletions app-updater/src/main/java/com/king/app/updater/UpdateConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

import com.king.app.updater.constant.Constants;

import java.util.HashMap;
import java.util.Map;


/**
* @author Jenly <a href="mailto:jenly1314@gmail.com">Jenly</a>
Expand Down Expand Up @@ -79,6 +82,11 @@ public class UpdateConfig implements Parcelable {
*/
private Integer versionCode;

/**
* 请求头参数
*/
private Map<String,String> mRequestProperty;


public UpdateConfig() {

Expand Down Expand Up @@ -204,25 +212,24 @@ public void setVersionCode(Integer versionCode){
this.versionCode = versionCode;
}

@Override
public String toString() {
return "UpdateConfig{" +
"mUrl='" + mUrl + '\'' +
", mPath='" + mPath + '\'' +
", mFilename='" + mFilename + '\'' +
", isShowNotification=" + isShowNotification +
", isInstallApk=" + isInstallApk +
", mNotificationIcon=" + mNotificationIcon +
", mNotificationId=" + mNotificationId +
", mChannelId='" + mChannelId + '\'' +
", mChannelName='" + mChannelName + '\'' +
", mAuthority='" + mAuthority + '\'' +
", isReDownload=" + isReDownload +
", isShowPercentage=" + isShowPercentage +
", isVibrate=" + isVibrate +
", isSound=" + isSound +
", versionCode=" + versionCode +
'}';
public Map<String, String> getRequestProperty() {
return mRequestProperty;
}

public void addHeader(String key, String value){
initRequestProperty();
mRequestProperty.put(key,value);
}

public void addHeader(Map<String,String> headers){
initRequestProperty();
mRequestProperty.putAll(headers);
}

private void initRequestProperty(){
if(mRequestProperty == null){
mRequestProperty = new HashMap<>();
}
}


Expand All @@ -248,6 +255,14 @@ public void writeToParcel(Parcel dest, int flags) {
dest.writeByte(this.isVibrate ? (byte) 1 : (byte) 0);
dest.writeByte(this.isSound ? (byte) 1 : (byte) 0);
dest.writeValue(this.versionCode);
dest.writeInt(mRequestProperty!=null ? this.mRequestProperty.size():0);
if(mRequestProperty!=null){
for (Map.Entry<String, String> entry : this.mRequestProperty.entrySet()) {
dest.writeString(entry.getKey());
dest.writeString(entry.getValue());
}
}

}

protected UpdateConfig(Parcel in) {
Expand All @@ -266,6 +281,13 @@ protected UpdateConfig(Parcel in) {
this.isVibrate = in.readByte() != 0;
this.isSound = in.readByte() != 0;
this.versionCode = (Integer) in.readValue(Integer.class.getClassLoader());
int mRequestPropertySize = in.readInt();
this.mRequestProperty = new HashMap<>(mRequestPropertySize);
for (int i = 0; i < mRequestPropertySize; i++) {
String key = in.readString();
String value = in.readString();
this.mRequestProperty.put(key, value);
}
}

public static final Creator<UpdateConfig> CREATOR = new Creator<UpdateConfig>() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.king.app.updater.http;

import android.os.AsyncTask;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.Log;

import com.king.app.updater.constant.Constants;
import com.king.app.updater.util.SSLSocketFactoryUtils;

import java.io.File;
Expand All @@ -10,6 +14,7 @@
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

import javax.net.ssl.HttpsURLConnection;

Expand All @@ -20,7 +25,7 @@ public class HttpManager implements IHttpManager {

private static final int DEFAULT_TIME_OUT = 20000;

private int mTimeout = DEFAULT_TIME_OUT;
private int mTimeout;

private static HttpManager INSTANCE;

Expand All @@ -43,8 +48,8 @@ public HttpManager(int timeout){
}

@Override
public void download(String url, String path, String filename, DownloadCallback callback) {
new DownloadTask(url,path,filename,callback).execute();
public void download(String url, String path, String filename, @Nullable Map<String,String> requestProperty, DownloadCallback callback) {
new DownloadTask(url,path,filename,requestProperty,callback).execute();
}


Expand All @@ -58,15 +63,18 @@ private class DownloadTask extends AsyncTask<Void,Integer,File> {

private String filename;

private Map<String,String> requestProperty;

private DownloadCallback callback;

private Exception exception;

public DownloadTask(String url,String path,String filename,DownloadCallback callback){
public DownloadTask(String url, String path, String filename ,@Nullable Map<String,String> requestProperty, DownloadCallback callback){
this.url = url;
this.path = path;
this.filename = filename;
this.callback = callback;
this.requestProperty = requestProperty;

}

Expand All @@ -78,17 +86,30 @@ protected File doInBackground(Void... voids) {
HttpsURLConnection.setDefaultHostnameVerifier(SSLSocketFactoryUtils.createTrustAllHostnameVerifier());
HttpURLConnection connect = (HttpURLConnection)new URL(url).openConnection();
connect.setRequestMethod("GET");
connect.setRequestProperty("Accept-Encoding", "identity");

connect.setReadTimeout(mTimeout);
connect.setConnectTimeout(mTimeout);

if(requestProperty!=null){
for(Map.Entry<String,String> entry : requestProperty.entrySet()){
connect.setRequestProperty(entry.getKey(),entry.getValue());
}
}

connect.connect();
int responseCode = connect.getResponseCode();

Log.d(Constants.TAG,"Content-Type:" + connect.getContentType());
if(responseCode == HttpURLConnection.HTTP_OK){

InputStream is = connect.getInputStream();

int length = connect.getContentLength();

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
length = (int)connect.getContentLengthLong();
}

int progress = 0;

byte[] buffer = new byte[4096];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.king.app.updater.http;

import android.support.annotation.Nullable;

import java.io.File;
import java.io.Serializable;
import java.util.Map;

/**
* @author Jenly <a href="mailto:jenly1314@gmail.com">Jenly</a>
Expand All @@ -14,9 +17,10 @@ public interface IHttpManager {
* @param url
* @param path
* @param filename
* @param requestProperty
* @param callback
*/
void download(String url,String path,String filename,DownloadCallback callback);
void download(String url, String path, String filename, @Nullable Map<String,String> requestProperty, DownloadCallback callback);


interface DownloadCallback extends Serializable{
Expand Down
Loading

0 comments on commit dad8814

Please sign in to comment.