Skip to content

Commit

Permalink
0.9.4 & fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
xinpengfei520 committed Mar 20, 2019
1 parent c979c48 commit 4f93297
Show file tree
Hide file tree
Showing 14 changed files with 244 additions and 91 deletions.
38 changes: 27 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
# pgyer-android-app-update

A light-weighted library which can update android app by pgyer app store.
A light-weighted library which can update android app by pgyer App Store.

## step 1:
[Download Demo](https://www.pgyer.com/android_app_update)

## 1.Usage

### step 1:

add below code in your app *AndroidManifest.xml* file.

```
<!-- 蒲公英 apiKey -->
<meta-data
android:name="PGYER_API_KEY"
android:value="your app pgyer apiKey" />
<!-- 蒲公英 appKey -->
<meta-data
android:name="PGYER_APP_KEY"
android:value="your app pgyer apiKey" />
<!-- 蒲公英 apiKey -->
<meta-data
android:name="PGYER_API_KEY"
android:value="your app pgyer apiKey" />
<!-- 蒲公英 appKey -->
<meta-data
android:name="PGYER_APP_KEY"
android:value="your app pgyer apiKey" />
```

## step 2:
### step 2:

```
implementation 'com.xsir:PgyerAndroidAppUpdate:<latest-version>'
Expand All @@ -33,6 +37,18 @@ In MainActivity onCreate() method:
PgyerApi.checkUpdate(this);
```

## 2.Update Log

### v0.9.4

- 自定义 FileProvider 防止和集成 app 的 FileProvider 冲突;
- 保证 FileProvider authorities 的唯一性;
- 抽取几个常量类和工具类;

## TODO

// TODO: 2019/3/20 使用构建者模式增加更新参数配置,使用线程池

## LICENSE

[Apache2.0](https://github.com/xinpengfei520/pgyer-android-app-update/blob/master/LICENSE)
10 changes: 5 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
applicationId "com.xsir.pgyerappupdate"
minSdkVersion versions.minSdkVersion
targetSdkVersion versions.targetSdkVersion
versionCode 10
versionName "1.9"
versionCode 11
versionName "1.9.1"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

Expand Down Expand Up @@ -61,8 +61,8 @@ dependencies {
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
//implementation project(':library')
implementation 'com.xsir:PgyerAndroidAppUpdate:0.9'
implementation project(':library')
//implementation 'com.xsir:PgyerAndroidAppUpdate:0.9'
}

def getApkFullPath() {
Expand Down Expand Up @@ -91,7 +91,7 @@ def readProperties(key) {
}

static def getUpdateDescription() {
return '1.发布V0.9版本库\n2.修复一些bug;\n3.提升用户体验!'
return '1.发布 V0.9.4 版本库\n2.修复一些bug;\n3.提升用户体验!'
}

task("uploadApk") {
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ buildscript {

'supportLibrary' : '27.1.1',

'libReleaseCode' : 12,
'libReleaseName' : '0.9.3'
'libReleaseCode' : 13,
'libReleaseName' : '0.9.4'
]


Expand Down
4 changes: 2 additions & 2 deletions library/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
android:windowSoftInputMode="stateHidden|stateAlwaysHidden" />

<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.FileProvider"
android:name=".provider.CustomFileProvider"
android:authorities="com.xsir.pgyerappupdate.library.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.xsir.pgyerappupdate.library.cons.Constants;
import com.xsir.pgyerappupdate.library.service.DownLoadService;
import com.xsir.pgyerappupdate.library.utils.AppInfoUtils;
import com.xsir.pgyerappupdate.library.utils.HttpClientUtils;
import com.xsir.pgyerappupdate.library.utils.ManifestUtils;
import com.xsir.pgyerappupdate.library.utils.PermissionUtils;
import com.xsir.pgyerappupdate.library.utils.ThreadUtils;
import com.xsir.pgyerappupdate.library.utils.XLogUtils;

import org.json.JSONException;
import org.json.JSONObject;
Expand All @@ -25,7 +26,6 @@
public class PgyerApi {

private static final String TAG = "PgyerApi";
private static final String PGYER_APP_CHECK_URL = "https://www.pgyer.com/apiv2/app/check";

/**
* 检查更新 APP
Expand All @@ -41,13 +41,13 @@ public static void checkUpdate(final Activity activity) {
PermissionUtils.getInstance().request(activity, permissions, new PermissionUtils.OnPermissionCallback() {
@Override
public void onGranted() {
Log.i(TAG, "onGranted()");
XLogUtils.i(TAG, "onGranted()");
sendNetworkRequest(activity);
}

@Override
public void onDenied() {
Log.e(TAG, "onDenied()");
XLogUtils.e(TAG, "onDenied()");
new AlertDialog.Builder(activity)
.setTitle("温馨提示")
.setCancelable(false)
Expand All @@ -64,25 +64,25 @@ public void onDenied() {
* @param activity 当前 APP 对象
*/
private static void sendNetworkRequest(final Activity activity) {
String apiKey = ManifestUtils.getMetaDataValueByName(activity, ManifestUtils.PGYER_API_KEY);
String appKey = ManifestUtils.getMetaDataValueByName(activity, ManifestUtils.PGYER_APP_KEY);
String apiKey = ManifestUtils.getMetaDataValueByName(activity, Constants.PGYER_API_KEY);
String appKey = ManifestUtils.getMetaDataValueByName(activity, Constants.PGYER_APP_KEY);

if (TextUtils.isEmpty(apiKey) || TextUtils.isEmpty(appKey)) {
throw new IllegalArgumentException("apiKey or appKey is empty string, please config in AndroidManifest.xml file.");
}

// 拼接请求的参数
String postStringParam = String.format("_api_key=%s&appKey=%s&buildVersion=%s&buildBuildVersion=%s", apiKey, appKey, "", "");
HttpClientUtils.post(PGYER_APP_CHECK_URL, postStringParam, new HttpClientUtils.OnRequestCallBack() {
HttpClientUtils.post(Constants.PGYER_APP_CHECK_URL, postStringParam, new HttpClientUtils.OnRequestCallBack() {
@Override
public void onSuccess(String json) {
Log.i(TAG, "onSuccess():" + json);
XLogUtils.i(TAG, "onSuccess():" + json);
parseJson(activity, json);
}

@Override
public void onError(final String errorMsg) {
Log.e(TAG, "onError():" + errorMsg);
XLogUtils.e(TAG, "onError():" + errorMsg);
ThreadUtils.runOnUiThread(new Runnable() {
@Override
public void run() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.xsir.pgyerappupdate.library.cons;

/**
* Created by x-sir on 2019/3/20 :)
* Function:常量类
*/
public class Constants {

/**
* 蒲公英 apiKey
*/
public static final String PGYER_API_KEY = "PGYER_API_KEY";
/**
* 蒲公英 appKey
*/
public static final String PGYER_APP_KEY = "PGYER_APP_KEY";

/**
* 蒲公英更新接口(v2)
*/
public static final String PGYER_APP_CHECK_URL = "https://www.pgyer.com/apiv2/app/check";

/**
* FileProvider authority
*/
public static final String AUTHORITY = "com.xsir.pgyerappupdate.library.FileProvider";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.xsir.pgyerappupdate.library.provider;

import android.support.v4.content.FileProvider;

/**
* Created by x-sir on 2019/3/20 :)
* Function:custom file provider.
*/
public class CustomFileProvider extends FileProvider {

/**
* empty parameters constructor
*/
public CustomFileProvider() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@
import android.os.Environment;
import android.os.IBinder;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.MimeTypeMap;
import android.widget.Toast;

import com.xsir.pgyerappupdate.library.cons.Constants;
import com.xsir.pgyerappupdate.library.provider.CustomFileProvider;
import com.xsir.pgyerappupdate.library.utils.ToastUtils;
import com.xsir.pgyerappupdate.library.utils.XLogUtils;

import java.io.File;

Expand All @@ -34,14 +35,12 @@ public class DownLoadService extends Service {
private static final String APK_NAME = "apkName";
private static final String DOWNLOAD_URL = "downloadUrl";
private static final String TAG = "DownLoadService";
private static final String SUFFIX_PROVIDER = ".FileProvider";

private void initDownManager() {
manager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
receiver = new DownloadCompleteReceiver();
DownloadManager.Request down = new DownloadManager.Request(Uri.parse(url));
down.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE
| DownloadManager.Request.NETWORK_WIFI);
down.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
down.setAllowedOverRoaming(false);
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
String mimeString = mimeTypeMap.getMimeTypeFromExtension(MimeTypeMap.getFileExtensionFromUrl(url));
Expand All @@ -54,6 +53,12 @@ private void initDownManager() {
registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}

@Override
public void onCreate() {
super.onCreate();
ToastUtils.showShort(getApplicationContext(), "正在下载中...");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
url = intent.getStringExtra(DOWNLOAD_URL);
Expand All @@ -62,22 +67,25 @@ public int onStartCommand(Intent intent, int flags, int startId) {
File file = new File(path);
if (file.exists()) {
boolean deleteResult = deleteFileWithPath(path);
Log.i(TAG, "deleteResult===" + deleteResult);
XLogUtils.i(TAG, "deleteResult===" + deleteResult);
}

try {
initDownManager();
} catch (Exception e) {
e.printStackTrace();

try {
Uri uri = Uri.parse("market://details?id=" + getPackageName());
Intent intent0 = new Intent(Intent.ACTION_VIEW, uri);
intent0.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent0);
} catch (Exception ex) {
showShort("下载失败!");
e.printStackTrace();
ToastUtils.showShort(getApplicationContext(), "下载失败!");
}
}

return Service.START_NOT_STICKY;
}

Expand All @@ -102,7 +110,7 @@ public void onReceive(Context context, Intent intent) {
if (manager.getUriForDownloadedFile(downId) != null) {
installAPK(context, getRealFilePath(context, manager.getUriForDownloadedFile(downId)));
} else {
showShort("下载失败!");
ToastUtils.showShort(getApplicationContext(), "下载失败!");
}

DownLoadService.this.stopSelf();
Expand All @@ -114,7 +122,7 @@ private void installAPK(Context context, String path) {
if (file.exists()) {
openFile(file, context);
} else {
showShort("下载失败!");
ToastUtils.showShort(getApplicationContext(), "下载失败!");
}
}
}
Expand Down Expand Up @@ -158,12 +166,14 @@ public void openFile(File file, Context context) {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Log.i(TAG, context.getApplicationContext().getPackageName() + SUFFIX_PROVIDER);
Uri uriForFile = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + SUFFIX_PROVIDER, file);
Log.i(TAG, uriForFile.getPath());
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri uriForFile = CustomFileProvider.getUriForFile(context, Constants.AUTHORITY, file);
XLogUtils.i(TAG, "file uri path:" + uriForFile.getPath());

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.setDataAndType(uriForFile, context.getContentResolver().getType(uriForFile));

} else {
intent.setDataAndType(Uri.fromFile(file), getMIMEType(file));
}
Expand All @@ -172,7 +182,7 @@ public void openFile(File file, Context context) {
context.startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
showShort("没有找到打开此类文件的程序!");
ToastUtils.showShort(getApplicationContext(), "没有找到打开此类文件的程序!");
}
}

Expand All @@ -189,22 +199,15 @@ public static boolean deleteFileWithPath(String filePath) {
if (f.isFile()) {
return f.delete();
}

return false;
}

public static void startAction(Context context, String url, String apkName) {
Intent intent = new Intent(context, DownLoadService.class);
intent.putExtra(DOWNLOAD_URL, url);
intent.putExtra(APK_NAME, apkName);
Toast.makeText(context, "正在下载中...", Toast.LENGTH_SHORT).show();
context.startService(intent);
}

private void showShort(String message) {
if (!TextUtils.isEmpty(message)) {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
}
}

}

Loading

0 comments on commit 4f93297

Please sign in to comment.