支持添加请求头

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" /> <option name="ITERATION_ELEMENTS_WRAPPING" value="chop_down_if_not_single" />
</formatting-settings> </formatting-settings>
</DBN-SQL> </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> <DBN-PSQL>
<case-options enabled="true"> <case-options enabled="true">
<option name="KEYWORD_CASE" value="lower" /> <option name="KEYWORD_CASE" value="lower" />

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

@ -39,7 +39,7 @@ AppUpdater for Android 是一个专注于App更新,一键傻瓜式集成App版
<dependency> <dependency>
<groupId>com.king.app</groupId> <groupId>com.king.app</groupId>
<artifactId>app-updater</artifactId> <artifactId>app-updater</artifactId>
<version>1.0.3</version> <version>1.0.4</version>
<type>pom</type> <type>pom</type>
</dependency> </dependency>
@ -47,27 +47,27 @@ AppUpdater for Android 是一个专注于App更新,一键傻瓜式集成App版
<dependency> <dependency>
<groupId>com.king.app</groupId> <groupId>com.king.app</groupId>
<artifactId>app-dialog</artifactId> <artifactId>app-dialog</artifactId>
<version>1.0.3</version> <version>1.0.4</version>
<type>pom</type> <type>pom</type>
</dependency> </dependency>
``` ```
### Gradle: ### Gradle:
```gradle ```gradle
//app-updater //app-updater
implementation 'com.king.app:app-updater:1.0.3' implementation 'com.king.app:app-updater:1.0.4'
//app-dialog //app-dialog
implementation 'com.king.app:app-dialog:1.0.3' implementation 'com.king.app:app-dialog:1.0.4'
``` ```
### Lvy: ### Lvy:
```lvy ```lvy
//app-updater //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> <artifact name='$AID' ext='pom'></artifact>
</dependency> </dependency>
//app-dialog //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> <artifact name='$AID' ext='pom'></artifact>
</dependency> </dependency>
``` ```
@ -132,6 +132,9 @@ AppUpdater for Android 是一个专注于App更新,一键傻瓜式集成App版
## 版本记录 ## 版本记录
#### v1.0.4:2019-6-4
* 支持添加请求头
#### v1.0.3:2019-5-9 #### v1.0.3:2019-5-9
* 新增支持下载APK优先取本地缓存,避免多次下载相同版本的APK文件 * 新增支持下载APK优先取本地缓存,避免多次下载相同版本的APK文件
* AppDialog支持隐藏Dialog的标题 * AppDialog支持隐藏Dialog的标题

@ -53,7 +53,7 @@ public enum AppDialog {
btnDialogCancel.setVisibility(config.isHideCancel() ? View.GONE : View.VISIBLE); btnDialogCancel.setVisibility(config.isHideCancel() ? View.GONE : View.VISIBLE);
try{ try{
//不强制要求要有线 //不强制要求要有中间的线
View line = view.findViewById(R.id.line); View line = view.findViewById(R.id.line);
line.setVisibility(config.isHideCancel() ? View.GONE : View.VISIBLE); line.setVisibility(config.isHideCancel() ? View.GONE : View.VISIBLE);
}catch (Exception e){ }catch (Exception e){
@ -137,6 +137,7 @@ public enum AppDialog {
* @return * @return
*/ */
public String showDialogFragment(FragmentManager fragmentManager,DialogFragment dialogFragment, String tag) { public String showDialogFragment(FragmentManager fragmentManager,DialogFragment dialogFragment, String tag) {
dismissDialogFragment(fragmentManager);
dialogFragment.show(fragmentManager,tag); dialogFragment.show(fragmentManager,tag);
mTag = tag; mTag = tag;
return tag; return tag;

@ -36,6 +36,7 @@ public class AppDialogFragment extends BaseDialogFragment {
if(config!=null){ if(config!=null){
TextView tvDialogTitle = rootView.findViewById(config.getTitleId()); TextView tvDialogTitle = rootView.findViewById(config.getTitleId());
setText(tvDialogTitle,config.getTitle()); setText(tvDialogTitle,config.getTitle());
tvDialogTitle.setVisibility(config.isHideTitle() ? View.GONE : View.VISIBLE);
TextView tvDialogContent = rootView.findViewById(config.getContentId()); TextView tvDialogContent = rootView.findViewById(config.getContentId());
setText(tvDialogContent,config.getContent()); setText(tvDialogContent,config.getContent());
@ -46,7 +47,7 @@ public class AppDialogFragment extends BaseDialogFragment {
btnDialogCancel.setVisibility(config.isHideCancel() ? View.GONE : View.VISIBLE); btnDialogCancel.setVisibility(config.isHideCancel() ? View.GONE : View.VISIBLE);
try{ try{
//不强制要求要有线 //不强制要求要有中间的线
View line = rootView.findViewById(R.id.line); View line = rootView.findViewById(R.id.line);
line.setVisibility(config.isHideCancel() ? View.GONE : View.VISIBLE); line.setVisibility(config.isHideCancel() ? View.GONE : View.VISIBLE);
}catch (Exception e){ }catch (Exception e){
@ -56,6 +57,7 @@ public class AppDialogFragment extends BaseDialogFragment {
Button btnDialogOK = rootView.findViewById(config.getOkId()); Button btnDialogOK = rootView.findViewById(config.getOkId());
setText(btnDialogOK,config.getOk()); setText(btnDialogOK,config.getOk());
btnDialogOK.setOnClickListener(config.getOnClickOk() != null ? config.getOnClickOk() : getOnClickDismiss()); btnDialogOK.setOnClickListener(config.getOnClickOk() != null ? config.getOnClickOk() : getOnClickDismiss());
} }
} }
} }

@ -8,7 +8,6 @@ import android.content.ServiceConnection;
import android.os.IBinder; import android.os.IBinder;
import android.support.annotation.DrawableRes; import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; 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.service.DownloadService;
import com.king.app.updater.util.PermissionUtils; import com.king.app.updater.util.PermissionUtils;
import java.util.Map;
/** /**
* @author Jenly <a href="mailto:jenly1314@gmail.com">Jenly</a> * @author Jenly <a href="mailto:jenly1314@gmail.com">Jenly</a>
@ -274,6 +275,27 @@ public class AppUpdater {
return this; 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){ public AppUpdater build(@NonNull Context context){
AppUpdater appUpdater = new AppUpdater(context,mConfig); AppUpdater appUpdater = new AppUpdater(context,mConfig);
return appUpdater; return appUpdater;

@ -8,6 +8,9 @@ import android.support.annotation.DrawableRes;
import com.king.app.updater.constant.Constants; 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> * @author Jenly <a href="mailto:jenly1314@gmail.com">Jenly</a>
@ -79,6 +82,11 @@ public class UpdateConfig implements Parcelable {
*/ */
private Integer versionCode; private Integer versionCode;
/**
* 请求头参数
*/
private Map<String,String> mRequestProperty;
public UpdateConfig() { public UpdateConfig() {
@ -204,25 +212,24 @@ public class UpdateConfig implements Parcelable {
this.versionCode = versionCode; this.versionCode = versionCode;
} }
@Override public Map<String, String> getRequestProperty() {
public String toString() { return mRequestProperty;
return "UpdateConfig{" + }
"mUrl='" + mUrl + '\'' +
", mPath='" + mPath + '\'' + public void addHeader(String key, String value){
", mFilename='" + mFilename + '\'' + initRequestProperty();
", isShowNotification=" + isShowNotification + mRequestProperty.put(key,value);
", isInstallApk=" + isInstallApk + }
", mNotificationIcon=" + mNotificationIcon +
", mNotificationId=" + mNotificationId + public void addHeader(Map<String,String> headers){
", mChannelId='" + mChannelId + '\'' + initRequestProperty();
", mChannelName='" + mChannelName + '\'' + mRequestProperty.putAll(headers);
", mAuthority='" + mAuthority + '\'' + }
", isReDownload=" + isReDownload +
", isShowPercentage=" + isShowPercentage + private void initRequestProperty(){
", isVibrate=" + isVibrate + if(mRequestProperty == null){
", isSound=" + isSound + mRequestProperty = new HashMap<>();
", versionCode=" + versionCode + }
'}';
} }
@ -248,6 +255,14 @@ public class UpdateConfig implements Parcelable {
dest.writeByte(this.isVibrate ? (byte) 1 : (byte) 0); dest.writeByte(this.isVibrate ? (byte) 1 : (byte) 0);
dest.writeByte(this.isSound ? (byte) 1 : (byte) 0); dest.writeByte(this.isSound ? (byte) 1 : (byte) 0);
dest.writeValue(this.versionCode); 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) { protected UpdateConfig(Parcel in) {
@ -266,6 +281,13 @@ public class UpdateConfig implements Parcelable {
this.isVibrate = in.readByte() != 0; this.isVibrate = in.readByte() != 0;
this.isSound = in.readByte() != 0; this.isSound = in.readByte() != 0;
this.versionCode = (Integer) in.readValue(Integer.class.getClassLoader()); 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>() { public static final Creator<UpdateConfig> CREATOR = new Creator<UpdateConfig>() {

@ -1,7 +1,11 @@
package com.king.app.updater.http; package com.king.app.updater.http;
import android.os.AsyncTask; 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 com.king.app.updater.util.SSLSocketFactoryUtils;
import java.io.File; import java.io.File;
@ -10,6 +14,7 @@ import java.io.InputStream;
import java.net.ConnectException; import java.net.ConnectException;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HttpsURLConnection;
@ -20,7 +25,7 @@ public class HttpManager implements IHttpManager {
private static final int DEFAULT_TIME_OUT = 20000; private static final int DEFAULT_TIME_OUT = 20000;
private int mTimeout = DEFAULT_TIME_OUT; private int mTimeout;
private static HttpManager INSTANCE; private static HttpManager INSTANCE;
@ -43,8 +48,8 @@ public class HttpManager implements IHttpManager {
} }
@Override @Override
public void download(String url, String path, String filename, DownloadCallback callback) { public void download(String url, String path, String filename, @Nullable Map<String,String> requestProperty, DownloadCallback callback) {
new DownloadTask(url,path,filename,callback).execute(); new DownloadTask(url,path,filename,requestProperty,callback).execute();
} }
@ -58,15 +63,18 @@ public class HttpManager implements IHttpManager {
private String filename; private String filename;
private Map<String,String> requestProperty;
private DownloadCallback callback; private DownloadCallback callback;
private Exception exception; 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.url = url;
this.path = path; this.path = path;
this.filename = filename; this.filename = filename;
this.callback = callback; this.callback = callback;
this.requestProperty = requestProperty;
} }
@ -78,17 +86,30 @@ public class HttpManager implements IHttpManager {
HttpsURLConnection.setDefaultHostnameVerifier(SSLSocketFactoryUtils.createTrustAllHostnameVerifier()); HttpsURLConnection.setDefaultHostnameVerifier(SSLSocketFactoryUtils.createTrustAllHostnameVerifier());
HttpURLConnection connect = (HttpURLConnection)new URL(url).openConnection(); HttpURLConnection connect = (HttpURLConnection)new URL(url).openConnection();
connect.setRequestMethod("GET"); connect.setRequestMethod("GET");
connect.setRequestProperty("Accept-Encoding", "identity");
connect.setReadTimeout(mTimeout); connect.setReadTimeout(mTimeout);
connect.setConnectTimeout(mTimeout); connect.setConnectTimeout(mTimeout);
if(requestProperty!=null){
for(Map.Entry<String,String> entry : requestProperty.entrySet()){
connect.setRequestProperty(entry.getKey(),entry.getValue());
}
}
connect.connect(); connect.connect();
int responseCode = connect.getResponseCode(); int responseCode = connect.getResponseCode();
Log.d(Constants.TAG,"Content-Type:" + connect.getContentType());
if(responseCode == HttpURLConnection.HTTP_OK){ if(responseCode == HttpURLConnection.HTTP_OK){
InputStream is = connect.getInputStream(); InputStream is = connect.getInputStream();
int length = connect.getContentLength(); int length = connect.getContentLength();
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
length = (int)connect.getContentLengthLong();
}
int progress = 0; int progress = 0;
byte[] buffer = new byte[4096]; byte[] buffer = new byte[4096];

@ -1,7 +1,10 @@
package com.king.app.updater.http; package com.king.app.updater.http;
import android.support.annotation.Nullable;
import java.io.File; import java.io.File;
import java.io.Serializable; import java.io.Serializable;
import java.util.Map;
/** /**
* @author Jenly <a href="mailto:jenly1314@gmail.com">Jenly</a> * @author Jenly <a href="mailto:jenly1314@gmail.com">Jenly</a>
@ -14,9 +17,10 @@ public interface IHttpManager {
* @param url * @param url
* @param path * @param path
* @param filename * @param filename
* @param requestProperty
* @param callback * @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{ interface DownloadCallback extends Serializable{

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

@ -1,10 +1,11 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<paths> <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-path name="app_updater_path" path=".AppUpdater/"/>
<external-cache-path name="app_external_cache_path" path="."/> <external-cache-path name="app_external_cache_path" path="/"/>
<external-files-path name="app_external_files_path" path="."/> <external-files-path name="app_external_files_path" path="/"/>
<files-path name="app_files_path" path="."/> <files-path name="app_files_path" path="/"/>
<cache-path name="app_cache_path" path="."/> <cache-path name="app_cache_path" path="/"/>
</paths> </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(); 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 String mUrl = "https://raw.githubusercontent.com/jenly1314/AppUpdater/master/app/release/app-release.apk";
private ProgressBar progressBar; private ProgressBar progressBar;
@ -75,6 +73,7 @@ public class MainActivity extends AppCompatActivity {
private void clickBtn2(){ private void clickBtn2(){
UpdateConfig config = new UpdateConfig(); UpdateConfig config = new UpdateConfig();
config.setUrl(mUrl); config.setUrl(mUrl);
config.addHeader("token","xxxxxx");
new AppUpdater(getContext(),config) new AppUpdater(getContext(),config)
.setUpdateCallback(new UpdateCallback() { .setUpdateCallback(new UpdateCallback() {

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

Loading…
Cancel
Save