支持添加请求头

android
jenly1314 6 years ago
parent eb1bbd1b1d
commit dad8814f0a
  1. BIN
      .idea/caches/build_file_checksums.ser
  2. 25
      .idea/codeStyles/Project.xml
  3. 2
      .idea/markdown-navigator/profiles_settings.xml
  4. 15
      README.md
  5. 3
      app-dialog/src/main/java/com/king/app/dialog/AppDialog.java
  6. 4
      app-dialog/src/main/java/com/king/app/dialog/fragment/AppDialogFragment.java
  7. 24
      app-updater/src/main/java/com/king/app/updater/AppUpdater.java
  8. 60
      app-updater/src/main/java/com/king/app/updater/UpdateConfig.java
  9. 31
      app-updater/src/main/java/com/king/app/updater/http/HttpManager.java
  10. 6
      app-updater/src/main/java/com/king/app/updater/http/IHttpManager.java
  11. 10
      app-updater/src/main/java/com/king/app/updater/service/DownloadService.java
  12. 11
      app-updater/src/main/res/xml/app_updater_paths.xml
  13. BIN
      app/release/app-release.apk
  14. 2
      app/release/output.json
  15. 3
      app/src/main/java/com/king/appupdater/MainActivity.java
  16. 4
      versions.gradle

@ -24,31 +24,6 @@
<option name="ITERATION_ELEMENTS_WRAPPING" value="chop_down_if_not_single" />
</formatting-settings>
</DBN-SQL>
<Objective-C-extensions>
<file>
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Import" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Macro" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Typedef" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Enum" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Constant" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Global" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Struct" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="FunctionPredecl" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Function" />
</file>
<class>
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Property" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="Synthesize" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="InitMethod" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="StaticMethod" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="InstanceMethod" />
<option name="com.jetbrains.cidr.lang.util.OCDeclarationKind" value="DeallocMethod" />
</class>
<extensions>
<pair source="cpp" header="h" fileNamingConvention="NONE" />
<pair source="c" header="h" fileNamingConvention="NONE" />
</extensions>
</Objective-C-extensions>
<DBN-PSQL>
<case-options enabled="true">
<option name="KEYWORD_CASE" value="lower" />

@ -1,3 +1,3 @@
<component name="MarkdownNavigator.ProfileManager">
<settings default="" pdf-export="" />
<settings default="" pdf-export="" plain-text-search-scope="Project Files" />
</component>

@ -39,7 +39,7 @@ 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>
@ -47,27 +47,27 @@ AppUpdater for Android 是一个专注于App更新,一键傻瓜式集成App版
<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>
```
@ -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的标题

@ -53,7 +53,7 @@ public enum AppDialog {
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){
@ -137,6 +137,7 @@ public enum AppDialog {
* @return
*/
public String showDialogFragment(FragmentManager fragmentManager,DialogFragment dialogFragment, String tag) {
dismissDialogFragment(fragmentManager);
dialogFragment.show(fragmentManager,tag);
mTag = tag;
return tag;

@ -36,6 +36,7 @@ public class AppDialogFragment extends BaseDialogFragment {
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());
@ -46,7 +47,7 @@ public class AppDialogFragment extends BaseDialogFragment {
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){
@ -56,6 +57,7 @@ public class AppDialogFragment extends BaseDialogFragment {
Button btnDialogOK = rootView.findViewById(config.getOkId());
setText(btnDialogOK,config.getOk());
btnDialogOK.setOnClickListener(config.getOnClickOk() != null ? config.getOnClickOk() : getOnClickDismiss());
}
}
}

@ -8,7 +8,6 @@ import android.content.ServiceConnection;
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;
@ -18,6 +17,8 @@ import com.king.app.updater.http.IHttpManager;
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>
@ -274,6 +275,27 @@ public class AppUpdater {
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;

@ -8,6 +8,9 @@ import android.support.annotation.DrawableRes;
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>
@ -79,6 +82,11 @@ public class UpdateConfig implements Parcelable {
*/
private Integer versionCode;
/**
* 请求头参数
*/
private Map<String,String> mRequestProperty;
public UpdateConfig() {
@ -204,25 +212,24 @@ public class UpdateConfig implements Parcelable {
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<>();
}
}
@ -248,6 +255,14 @@ public class UpdateConfig implements Parcelable {
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) {
@ -266,6 +281,13 @@ public class UpdateConfig implements Parcelable {
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>() {

@ -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;
@ -10,6 +14,7 @@ import java.io.InputStream;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
@ -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;
@ -43,8 +48,8 @@ public class HttpManager implements IHttpManager {
}
@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();
}
@ -58,15 +63,18 @@ public class HttpManager implements IHttpManager {
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;
}
@ -78,17 +86,30 @@ public class HttpManager implements IHttpManager {
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];

@ -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>
@ -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{

@ -7,8 +7,6 @@ import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.net.Uri;
import android.os.Binder;
import android.os.Build;
@ -159,9 +157,9 @@ public class DownloadService extends Service {
}
Log.d(Constants.TAG,"File:" + file);
if(httpManager != null){
httpManager.download(url,path,filename,new AppDownloadCallback(config,callback));
httpManager.download(url,path,filename,config.getRequestProperty(),new AppDownloadCallback(config,callback));
}else{
HttpManager.getInstance().download(url,path,filename,new AppDownloadCallback(config,callback));
HttpManager.getInstance().download(url,path,filename,config.getRequestProperty(),new AppDownloadCallback(config,callback));
}
}
@ -516,6 +514,8 @@ public class DownloadService extends Service {
if(progress!= Constants.NONE && size!=Constants.NONE){
builder.setProgress(size,progress,false);
}else{
builder.setProgress(0,0,true);
}
return builder;
@ -545,7 +545,7 @@ public class DownloadService extends Service {
public class DownloadBinder extends Binder {
public void start(UpdateConfig config){
start(config,null,null);
start(config,null);
}
public void start(UpdateConfig config,UpdateCallback callback){

@ -1,10 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="app_external_path" path="."/>
<root-path name="app_root_path" path="/"/>
<external-path name="app_external_path" path="/"/>
<external-path name="app_updater_path" path=".AppUpdater/"/>
<external-cache-path name="app_external_cache_path" path="."/>
<external-files-path name="app_external_files_path" path="."/>
<files-path name="app_files_path" path="."/>
<cache-path name="app_cache_path" path="."/>
<external-cache-path name="app_external_cache_path" path="/"/>
<external-files-path name="app_external_files_path" path="/"/>
<files-path name="app_files_path" path="/"/>
<cache-path name="app_cache_path" path="/"/>
</paths>

Binary file not shown.

@ -1 +1 @@
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":4,"versionName":"1.0.3","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}]
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":5,"versionName":"1.0.4","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}]

@ -28,8 +28,6 @@ public class MainActivity extends AppCompatActivity {
private final Object mLock = new Object();
public final String TAG = this.getClass().getSimpleName();
private String mUrl = "https://raw.githubusercontent.com/jenly1314/AppUpdater/master/app/release/app-release.apk";
private ProgressBar progressBar;
@ -75,6 +73,7 @@ public class MainActivity extends AppCompatActivity {
private void clickBtn2(){
UpdateConfig config = new UpdateConfig();
config.setUrl(mUrl);
config.addHeader("token","xxxxxx");
new AppUpdater(getContext(),config)
.setUpdateCallback(new UpdateCallback() {

@ -1,7 +1,7 @@
//App
def app_version = [:]
app_version.versionCode = 4
app_version.versionName = "1.0.3"
app_version.versionCode = 5
app_version.versionName = "1.0.4"
ext.app_version = app_version
//build version

Loading…
Cancel
Save