Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
sunfusheng committed Feb 18, 2018
1 parent 5021edf commit 81e8b20
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 19 deletions.
12 changes: 2 additions & 10 deletions FirUpdater/src/main/java/com/sunfusheng/FirDownloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class FirDownloader {
private volatile int currLength;//当前总共下载的大小
private volatile int threadCountRunning;//正在运行的线程数
private String stateDownload = DOWNLOAD_INIT;//当前线程状态
private FirNotification firNotification;

private DownloadThread[] threadArr;

Expand All @@ -52,22 +53,13 @@ interface OnDownLoadListener {
}

public FirDownloader(Context context, String loadUrl, String filePath) {
this(context, loadUrl, filePath, DEFAULT_THREAD_COUNT, null);
this(context, loadUrl, filePath, null);
}

public FirDownloader(Context context, String loadUrl, String filePath, OnDownLoadListener onDownLoadListener) {
this(context, loadUrl, filePath, DEFAULT_THREAD_COUNT, onDownLoadListener);
}

public FirDownloader(Context context, String loadUrl, String filePath, int threadCount) {
this(context, loadUrl, filePath, threadCount, null);
}

public FirDownloader(Context context, String loadUrl, String filePath, int threadCount, OnDownLoadListener onDownLoadListener) {
this.context = context;
this.loadUrl = loadUrl;
this.filePath = filePath;
this.threadCount = threadCount;
this.threadCountRunning = 0;
this.onDownLoadListener = onDownLoadListener;
}
Expand Down
58 changes: 58 additions & 0 deletions FirUpdater/src/main/java/com/sunfusheng/FirNotification.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,65 @@
package com.sunfusheng;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

/**
* @author sunfusheng on 2018/2/17.
*/
public class FirNotification {

public static final String CHANNEL_ID = "FirNotification";
public static final int NOTIFICATION_ID = 1234;

private Context context;
private NotificationManager manager;
private NotificationCompat.Builder builder;

public void createBuilder(Context context) {
this.context = context;
manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
builder = new NotificationCompat.Builder(context, CHANNEL_ID);
builder.setSmallIcon(android.R.drawable.stat_sys_download);
builder.setAutoCancel(false);
builder.setOngoing(true);
builder.setShowWhen(false);
builder.setProgress(100, 7, false);
builder.setOngoing(true);
builder.setShowWhen(false);
builder.setDefaults(NotificationCompat.DEFAULT_ALL);
}

public void setContentTitle(String title) {
builder.setContentTitle(title);
}

public void setContentText(String text) {
builder.setContentText(text);
}

public void setIcon(int resId) {
builder.setSmallIcon(resId);
}

public void cancel() {
manager.notify(NOTIFICATION_ID, builder.build());
manager.cancel(NOTIFICATION_ID);
}

public void setContentIntent(Intent intent) {
PendingIntent pIntent = PendingIntent.getActivity(context, 1, intent, 0);
builder.setContentIntent(pIntent);
}

public void notifyNotification(int progress) {
builder.setProgress(100, progress, false);
manager.notify(NOTIFICATION_ID, builder.build());
}

public void notifyNotification() {
manager.notify(NOTIFICATION_ID, builder.build());
}
}
17 changes: 16 additions & 1 deletion FirUpdater/src/main/java/com/sunfusheng/FirUpdater.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public class FirUpdater implements FirDialog.OnClickDownloadDialogListener {
private Context context;
private FirDialog firDialog;
private FirDownloader firDownloader;
private FirNotification firNotification;

private boolean isBackgroundDownload;

public FirUpdater(Context context, String apiToken, String appId) {
this.context = context;
Expand Down Expand Up @@ -46,17 +49,29 @@ public void onClickDownload(DialogInterface dialog) {
String fileName = FirAppInfo.appName + "-V" + FirAppInfo.appVersionName + ".apk";
String filePath = Environment.getExternalStorageDirectory() + File.separator + fileName;

firNotification = new FirNotification();
firNotification.createBuilder(context);
firNotification.setContentTitle("正在下载" + FirAppInfo.appName);

firDownloader = new FirDownloader(context, FirAppInfo.appInstallUrl, filePath);
firDownloader.setOnDownLoadListener(new FirDownloader.OnDownLoadListener() {
@Override
public void onProgress(int totalLength, int currLength, int progress) {
Log.d("--->", "progress: " + progress + " totalLength: " + totalLength + " currLength: " + currLength);
firDialog.showDownloadDialog(context, progress);

if (isBackgroundDownload) {
firNotification.setContentText(progress + "%");
firNotification.notifyNotification(progress);
}
}

@Override
public void onSuccess() {
firDialog.dismissDownloadDialog();
if (isBackgroundDownload) {
firNotification.cancel();
}
FirUpdaterUtils.installApk(context, filePath);
}

Expand All @@ -70,7 +85,7 @@ public void onError() {

@Override
public void onClickBackgroundDownload(DialogInterface dialog) {

isBackgroundDownload = true;
}

@Override
Expand Down
16 changes: 8 additions & 8 deletions FirUpdater/src/main/java/com/sunfusheng/FirUpdaterUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import android.os.Handler;
import android.os.Looper;
import android.support.v4.content.FileProvider;
import android.text.TextUtils;

import java.io.Closeable;
import java.io.File;
Expand Down Expand Up @@ -108,11 +107,7 @@ public static boolean isAppForeground(Context context) {
return false;
}

public static void installApk(Context context, String apkPath) {
if (TextUtils.isEmpty(apkPath)) {
return;
}

public static Intent getInstallApkIntent(Context context, String apkPath) {
try {
File apkFile = new File(apkPath);
if (!apkFile.exists()) {
Expand All @@ -122,17 +117,22 @@ public static void installApk(Context context, String apkPath) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri apkUri;
if (Build.VERSION.SDK_INT >= 24) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
apkUri = FileProvider.getUriForFile(context, getPackageName(context) + ".file_provider", apkFile);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
apkUri = Uri.fromFile(apkFile);
}
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
context.startActivity(intent);
return intent;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

public static void installApk(Context context, String apkPath) {
context.startActivity(getInstallApkIntent(context, apkPath));
}

public static void closeQuietly(final Closeable... closeables) {
Expand Down

0 comments on commit 81e8b20

Please sign in to comment.