Skip to content

Commit

Permalink
完成更新模块
Browse files Browse the repository at this point in the history
  • Loading branch information
woshidasusu committed Apr 10, 2017
1 parent a8856d6 commit 395aa02
Show file tree
Hide file tree
Showing 17 changed files with 87 additions and 320 deletions.
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<!-- Used to load images from the gallery content provider. -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<!-- Used to load images for contact photos. -->
<uses-permission android:name="android.permission.READ_CONTACTS"/>

Expand Down
1 change: 0 additions & 1 deletion app/src/main/java/com/dasu/gank/GankApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public void onCreate() {
initGreenDao();
mConfigSP = new SPUtils(getApplicationContext(), "config");
registerNetStateListener();
UpdateController.checkUpdate(getApplicationContext(), new UpdateDialog(getApplicationContext()));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* gank.io 的 api
*/
interface ApiGank {
interface GankApi {
/**
* 获取发布过干货的日期
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public class GankController {
private static final String TAG = GankController.class.getSimpleName();

private static ApiGank getGankApi() {
private static GankApi getGankApi() {
return GankApiSingleton.mInstance;
}

Expand All @@ -31,7 +31,7 @@ public static void getDayGankData(String year, String month, String day, Callbac
}

private static class GankApiSingleton{
private static ApiGank mInstance = RetrofitHelper.newRetrofit(BuildConfig.GAND_SERVICE).create(ApiGank.class);
private static GankApi mInstance = RetrofitHelper.newRetrofit(BuildConfig.GAND_SERVICE).create(GankApi.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* fir.im 的 api,VMS指版本管理服务器
*/
interface ApiVMS {
interface VMSApi {

@GET("apps/latest/{id}")
Call<VersionResEntity> queryVersion(@Path("id") String id, @Query("api_token") String apiToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public class VMSController {
private static final String TAG = VMSController.class.getSimpleName();

private static ApiVMS getVMSApi() {
private static VMSApi getVMSApi() {
return VMSApiSingleton.sInstance;
}

Expand All @@ -31,7 +31,7 @@ public static void queryVersion(Callback<VersionResEntity> callback) {
}

private static class VMSApiSingleton {
private static ApiVMS sInstance = RetrofitHelper.newRetrofit(BuildConfig.VMS).create(ApiVMS.class);
private static VMSApi sInstance = RetrofitHelper.newRetrofit(BuildConfig.VMS).create(VMSApi.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import com.dasu.gank.mode.entity.VersionResEntity;
import com.dasu.gank.utils.FileUtils;
Expand Down Expand Up @@ -82,7 +83,7 @@ protected Boolean doInBackground(String... params) {
int progress = (int) ((1.0f * downloadLength / fileSize) * 100);
publishProgress(progress);
}
mApkPath = FileUtils.getAppDownloadDirectory() + File.separator + mVersionInfo.getName();
mApkPath = FileUtils.getAppDownloadDirectory() + File.separator + mVersionInfo.getVersion() + ".apk";
FileUtils.copyFile(tempFile, new File(mApkPath));
return true;
} catch (Exception e) {
Expand All @@ -108,13 +109,15 @@ protected void onProgressUpdate(Integer... values) {
if (mUpdateListener != null) {
//values[0]:当前下载的进度,取值 0~100
mUpdateListener.onDownloading(values[0]);
Log.d(TAG, "onProgressUpdate: " + values[0]);
}
}

@Override
protected void onPostExecute(Boolean isSucceed) {
if (mUpdateListener != null) {
mUpdateListener.onDownloadFinish(isSucceed, mApkPath);
Log.d(TAG, "onPostExecute: " + isSucceed + " " + mApkPath);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.dasu.gank.mode.net.update;

import android.content.Context;
import android.util.Log;

import com.dasu.gank.mode.entity.VersionResEntity;
import com.dasu.gank.mode.net.retrofit.VMSController;
Expand Down Expand Up @@ -30,6 +31,7 @@ public static void checkUpdate(final Context context, final OnCheckUpdateListene
public void onResponse(Call<VersionResEntity> call, Response<VersionResEntity> response) {
VersionResEntity version = response.body() != null ? response.body() : null;
if (version != null) {
Log.d(TAG, "onResponse: " + version.toString());
String newVersionCode = version.getVersion();
if (!newVersionCode.equals(AppUtils.getAppVersionCode(context))) {
//需要更新,小版本更新时可选择,大版本更新时强制更新
Expand All @@ -44,15 +46,18 @@ public void onResponse(Call<VersionResEntity> call, Response<VersionResEntity> r
//选择更新
listener.onPreUpdate(false, version);
}
UpdateHelper.downloadApk(context, version, listener);
}
}
}

@Override
public void onFailure(Call<VersionResEntity> call, Throwable t) {

Log.e(TAG, "onFailure: " + t.getMessage());
}
});
}

public static void downloadApk(Context context, VersionResEntity version, OnCheckUpdateListener listener) {
UpdateHelper.downloadApk(context, version, listener);
}
}
43 changes: 43 additions & 0 deletions app/src/main/java/com/dasu/gank/mode/net/update/UpdateHelper.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.dasu.gank.mode.net.update;

import android.content.Context;
import android.text.TextUtils;

import com.dasu.gank.mode.entity.VersionResEntity;
import com.dasu.gank.utils.FileUtils;

import java.io.File;

/**
* Created by dasu on 2017/4/8.
*
*/

class UpdateHelper {
Expand All @@ -14,11 +19,49 @@ class UpdateHelper {
private OnCheckUpdateListener mUpdateListener;


/**
* 下载前先检查本地是否有已经下载好的apk
*
* @param context
* @param versionInfo
* @param listener
*/
static void downloadApk(Context context, VersionResEntity versionInfo, OnCheckUpdateListener listener) {
boolean hasApk = checkLocalApk(context, versionInfo, listener);
if (hasApk) {
return;
}
UpdateAsyncTask updateAsyncTask = new UpdateAsyncTask(context, versionInfo, listener);
updateAsyncTask.execute();
}

/**
* 检查本地是否有已经下载好的 apk,并删除无效 apk,只保留最新版本的 apk
*
* @param context
* @param versionInfo
* @param listener
* @return true 本地有最新版本的 apk
*/
private static boolean checkLocalApk(Context context, VersionResEntity versionInfo, OnCheckUpdateListener listener) {
if (TextUtils.isEmpty(FileUtils.getAppDownloadDirectory())) {
return false;
}
File apkDir = new File(FileUtils.getAppDownloadDirectory());
for (File f : apkDir.listFiles()) {
if (!f.isDirectory() && f.getName().endsWith(".apk")) {
String v = f.getName().substring(0, f.getName().lastIndexOf('.'));
if (v.equals(versionInfo.getVersion())) {
listener.onDownloadFinish(true, f.getAbsolutePath());
return true;
} else {
f.delete();
}
}
}
return false;
}




Expand Down

This file was deleted.

Loading

0 comments on commit 395aa02

Please sign in to comment.