部分源码
public class NumberProgressBar extends View {
private Context mContext;
/**
* The max progress, default is 100
*/
private int mMax = 100;
/**
* current progress, can not exceed the max progress.
*/
private int mProgress = 0;
/**
* the progress area bar color
*/
private int mReachedBarColor;
/**
* the bar unreached area color.
*/
private int mUnreachedBarColor;
/**
* the progress text color.
*/
private int mTextColor;
/**
* the progress text size
*/
private float mTextSize;
/**
* the height of the reached area
*/
private float mReachedBarHeight;
/**
* the height of the unreached area
*/
private float mUnreachedBarHeight;
private final int default_text_color = Color.rgb(66, 145, 241);
private final int default_reached_color = Color.rgb(66, 145, 241);
private final int default_unreached_color = Color.rgb(204, 204, 204);
private final float default_progress_text_offset;
private final float default_text_size;
private final float default_reached_bar_height;
private final float default_unreached_bar_height;
/**
* for save and restore instance of progressbar.
*/
private static final String INSTANCE_STATE = "saved_instance";
private static final String INSTANCE_TEXT_COLOR = "text_color";
private static final String INSTANCE_TEXT_SIZE = "text_size";
private static final String INSTANCE_REACHED_BAR_HEIGHT = "reached_bar_height";
private static final String INSTANCE_REACHED_BAR_COLOR = "reached_bar_color";
private static final String INSTANCE_UNREACHED_BAR_HEIGHT = "unreached_bar_height";
private static final String INSTANCE_UNREACHED_BAR_COLOR = "unreached_bar_color";
private static final String INSTANCE_MAX = "max";
private static final String INSTANCE_PROGRESS = "progress";
/**
* the width of the text that to be drawn
*/
private float mDrawTextWidth;
/**
* the drawn text start
*/
private float mDrawTextStart;
/**
* the drawn text end
*/
private float mDrawTextEnd;
/**
* the text that to be drawn in onDraw()
*/
private String mCurrentDrawText;
/**
* the Paint of the reached area.
*/
private Paint mReachedBarPaint;
/**
* the Painter of the unreached area.
*/
private Paint mUnreachedBarPaint;
/**
* the Painter of the progress text.
*/
private Paint mTextPaint;
/**
* Unreached Bar area to draw rect.
*/
private RectF mUnreachedRectF = new RectF(0, 0, 0, 0);
/**
* reached bar area rect.
*/
private RectF mReachedRectF = new RectF(0, 0, 0, 0);
/**
* the progress text offset.
*/
private float mOffset;
/**
* determine if need to draw unreached area
*/
private boolean mDrawUnreachedBar = true;
private boolean mDrawReachedBar = true;
public NumberProgressBar(Context context) {
this(context, null);
}
public NumberProgressBar(Context context, AttributeSet attrs) {
this(context, attrs, R.attr.numberProgressBarStyle);
}
public NumberProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
default_reached_bar_height = dp2px(1.5f);
default_unreached_bar_height = dp2px(1.0f);
default_text_size = sp2px(10);
default_progress_text_offset = dp2px(3.0f);
//load styled attributes.
final TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.NumberProgressBar, defStyleAttr, 0);
mReachedBarColor = attributes.getColor(R.styleable.NumberProgressBar_progress_reached_color, default_reached_color);
mUnreachedBarColor = attributes.getColor(R.styleable.NumberProgressBar_progress_unreached_color, default_unreached_color);
mTextColor = attributes.getColor(R.styleable.NumberProgressBar_progress_text_color, default_text_color);
mTextSize = attributes.getDimension(R.styleable.NumberProgressBar_progress_text_size, default_text_size);
mReachedBarHeight = attributes.getDimension(R.styleable.NumberProgressBar_progress_reached_bar_height, default_reached_bar_height);
mUnreachedBarHeight = attributes.getDimension(R.styleable.NumberProgressBar_progress_unreached_bar_height, default_unreached_bar_height);
mOffset = attributes.getDimension(R.styleable.NumberProgressBar_progress_text_offset, default_progress_text_offset);
setProgress(attributes.getInt(R.styleable.NumberProgressBar_progress, 0));
setMax(attributes.getInt(R.styleable.NumberProgressBar_max, 100));
//
attributes.recycle();
initializePainters();
}
@Override
protected int getSuggestedMinimumWidth() {
return (int) mTextSize;
}
@Override
protected int getSuggestedMinimumHeight() {
return Math.max((int) mTextSize, Math.max((int) mReachedBarHeight, (int) mUnreachedBarHeight));
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(measure(widthMeasureSpec, true), measure(heightMeasureSpec, false));
}
private int measure(int measureSpec, boolean isWidth) {
int result;
int mode = MeasureSpec.getMode(measureSpec);
int size = MeasureSpec.getSize(measureSpec);
int padding = isWidth ? getPaddingLeft() + getPaddingRight() : getPaddingTop() + getPaddingBottom();
if (mode == MeasureSpec.EXACTLY) {
result = size;
} else {
result = isWidth ? getSuggestedMinimumWidth() : getSuggestedMinimumHeight();
result += padding;
if (mode == MeasureSpec.AT_MOST) {
if (isWidth) {
result = Math.max(result, size);
} else {
result = Math.min(result, size);
}
}
}
return result;
}
@Override
protected void onDraw(Canvas canvas) {
calculateDrawRectF();
if (mDrawReachedBar) {
canvas.drawRect(mReachedRectF, mReachedBarPaint);
}
if (mDrawUnreachedBar) {
canvas.drawRect(mUnreachedRectF, mUnreachedBarPaint);
}
canvas.drawText(mCurrentDrawText, mDrawTextStart, mDrawTextEnd, mTextPaint);
}
private void initializePainters() {
mReachedBarPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mReachedBarPaint.setColor(mReachedBarColor);
mUnreachedBarPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mUnreachedBarPaint.setColor(mUnreachedBarColor);
mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mTextPaint.setColor(mTextColor);
mTextPaint.setTextSize(mTextSize);
}
private void calculateDrawRectF() {
mCurrentDrawText = String.format("%d%%", getProgress() * 100 / getMax());
mDrawTextWidth = mTextPaint.measureText(mCurrentDrawText);
if (getProgress() == 0) {
mDrawReachedBar = false;
mDrawTextStart = getPaddingLeft();
} else {
mDrawReachedBar = true;
mReachedRectF.left = getPaddingLeft();
mReachedRectF.top = getHeight() / 2.0f - mReachedBarHeight / 2.0f;
mReachedRectF.right = (getWidth() - getPaddingLeft() - getPaddingRight()) / (getMax() * 1.0f) * getProgress() - mOffset + getPaddingLeft();
mReachedRectF.bottom = getHeight() / 2.0f + mReachedBarHeight / 2.0f;
mDrawTextStart = (mReachedRectF.right + mOffset);
}
mDrawTextEnd = (int) ((getHeight() / 2.0f) - ((mTextPaint.descent() + mTextPaint.ascent()) / 2.0f));
if ((mDrawTextStart + mDrawTextWidth) >= getWidth() - getPaddingRight()) {
mDrawTextStart = getWidth() - getPaddingRight() - mDrawTextWidth;
mReachedRectF.right = mDrawTextStart - mOffset;
}
float unreachBarStart = mDrawTextStart + mDrawTextWidth + mOffset;
if (unreachBarStart >= getWidth() - getPaddingRight()) {
mDrawUnreachedBar = false;
} else {
mDrawUnreachedBar = true;
mUnreachedRectF.left = unreachBarStart;
mUnreachedRectF.right = getWidth() - getPaddingRight();
mUnreachedRectF.top = getHeight() / 2.0f + -mUnreachedBarHeight / 2.0f;
mUnreachedRectF.bottom = getHeight() / 2.0f + mUnreachedBarHeight / 2.0f;
}
}
/**
* get progress text color
*
* @return progress text color
*/
public int getTextColor() {
return mTextColor;
}
/**
* get progress text size
*
* @return progress text size
*/
public float getProgressTextSize() {
return mTextSize;
}
public int getUnreachedBarColor() {
return mUnreachedBarColor;
}
public int getReachedBarColor() {
return mReachedBarColor;
}
public int getProgress() {
return mProgress;
}
public int getMax() {
return mMax;
}
public float getReachedBarHeight() {
return mReachedBarHeight;
}
public float getUnreachedBarHeight() {
return mUnreachedBarHeight;
}
public void setProgressTextSize(float TextSize) {
this.mTextSize = TextSize;
mTextPaint.setTextSize(mTextSize);
invalidate();
}
public void setProgressTextColor(int TextColor) {
this.mTextColor = TextColor;
mTextPaint.setColor(mTextColor);
invalidate();
}
public void setUnreachedBarColor(int BarColor) {
this.mUnreachedBarColor = BarColor;
mUnreachedBarPaint.setColor(mReachedBarColor);
invalidate();
}
public void setReachedBarColor(int ProgressColor) {
this.mReachedBarColor = ProgressColor;
mReachedBarPaint.setColor(mReachedBarColor);
invalidate();
}
public void setMax(int Max) {
if (Max > 0) {
this.mMax = Max;
invalidate();
}
}
public void incrementProgressBy(int by) {
if (by > 0) {
setProgress(getProgress() + by);
}
}
public void setProgress(int Progress) {
if (Progress <= getMax() && Progress >= 0) {
this.mProgress = Progress;
invalidate();
}
}
@Override
protected Parcelable onSaveInstanceState() {
final Bundle bundle = new Bundle();
bundle.putParcelable(INSTANCE_STATE, super.onSaveInstanceState());
bundle.putInt(INSTANCE_TEXT_COLOR, getTextColor());
bundle.putFloat(INSTANCE_TEXT_SIZE, getProgressTextSize());
bundle.putFloat(INSTANCE_REACHED_BAR_HEIGHT, getReachedBarHeight());
bundle.putFloat(INSTANCE_UNREACHED_BAR_HEIGHT, getUnreachedBarHeight());
bundle.putInt(INSTANCE_REACHED_BAR_COLOR, getReachedBarColor());
bundle.putInt(INSTANCE_UNREACHED_BAR_COLOR, getUnreachedBarColor());
bundle.putInt(INSTANCE_MAX, getMax());
bundle.putInt(INSTANCE_PROGRESS, getProgress());
return bundle;
}
@Override
protected void onRestoreInstanceState(Parcelable state) {
if (state instanceof Bundle) {
final Bundle bundle = (Bundle) state;
mTextColor = bundle.getInt(INSTANCE_TEXT_COLOR);
mTextSize = bundle.getFloat(INSTANCE_TEXT_SIZE);
mReachedBarHeight = bundle.getFloat(INSTANCE_REACHED_BAR_HEIGHT);
mUnreachedBarHeight = bundle.getFloat(INSTANCE_UNREACHED_BAR_HEIGHT);
mReachedBarColor = bundle.getInt(INSTANCE_REACHED_BAR_COLOR);
mUnreachedBarColor = bundle.getInt(INSTANCE_UNREACHED_BAR_COLOR);
initializePainters();
setMax(bundle.getInt(INSTANCE_MAX));
setProgress(bundle.getInt(INSTANCE_PROGRESS));
super.onRestoreInstanceState(bundle.getParcelable(INSTANCE_STATE));
return;
}
super.onRestoreInstanceState(state);
}
public float dp2px(float dp) {
final float scale = getResources().getDisplayMetrics().density;
return dp * scale + 0.5f;
}
public float sp2px(float sp) {
final float scale = getResources().getDisplayMetrics().scaledDensity;
return sp * scale;
}
}
/**
* 用DownloadManager来实现版本更新
*
*/
public class DownloadService extends Service {
public static final int HANDLE_DOWNLOAD = 0x001;
public static final String BUNDLE_KEY_DOWNLOAD_URL = "download_url";
public static final float UNBIND_SERVICE = 2.0F;
private Activity activity;
private DownloadBinder binder;
private DownloadManager downloadManager;
private DownloadChangeObserver downloadObserver;
private BroadcastReceiver downLoadBroadcast;
private ScheduledExecutorService scheduledExecutorService;
//下载任务ID
private long downloadId;
private String downloadUrl;
private OnProgressListener onProgressListener;
public Handler downLoadHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (onProgressListener != null && HANDLE_DOWNLOAD == msg.what) {
//被除数可以为0,除数必须大于0
if (msg.arg1 >= 0 && msg.arg2 > 0) {
onProgressListener.onProgress(msg.arg1 / (float) msg.arg2);
}
}
}
};
private Runnable progressRunnable = new Runnable() {
@Override
public void run() {
updateProgress();
}
};
@Override
public void onCreate() {
super.onCreate();
binder = new DownloadBinder();
}
@Override
public IBinder onBind(Intent intent) {
downloadUrl = intent.getStringExtra(BUNDLE_KEY_DOWNLOAD_URL);
Log.e("Apk下载路径传递成功","Apk下载路径传递成功:" + downloadUrl);
downloadApk(downloadUrl);
return binder;
}
/**
* 下载最新APK
*/
private void downloadApk(String url) {
downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
downloadObserver = new DownloadChangeObserver();
registerContentObserver();
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
/**设置用于下载时的网络状态*/
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
/**设置通知栏是否可见*/
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
/**设置漫游状态下是否可以下载*/
request.setAllowedOverRoaming(false);
/**如果我们希望下载的文件可以被系统的Downloads应用扫描到并管理,
我们需要调用Request对象的setVisibleInDownloadsUi方法,传递参数true.*/
request.setVisibleInDownloadsUi(true);
/**设置文件保存路径*/
request.setDestinationInExternalFilesDir(getApplicationContext(), Environment.DIRECTORY_DOWNLOADS, "xxxxxxxx.apk");
/**将下载请求放入队列, return下载任务的ID*/
downloadId = downloadManager.enqueue(request);
registerBroadcast();
}
/**
* 注册广播
*/
private void registerBroadcast() {
/**注册service 广播 1.任务完成时 2.进行中的任务被点击*/
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
intentFilter.addAction(DownloadManager.ACTION_NOTIFICATION_CLICKED);
registerReceiver(downLoadBroadcast = new DownLoadBroadcast(), intentFilter);
}
/**
* 注销广播
*/
private void unregisterBroadcast() {
if (downLoadBroadcast != null) {
unregisterReceiver(downLoadBroadcast);
downLoadBroadcast = null;
}
}
/**
* 注册ContentObserver
*/
private void registerContentObserver() {
/** observer download change **/
if (downloadObserver != null) {
getContentResolver().registerContentObserver(Uri.parse("content://downloads/my_downloads"), true, downloadObserver);
}
}
/**
* 注销ContentObserver
*/
private void unregisterContentObserver() {
if (downloadObserver != null) {
getContentResolver().unregisterContentObserver(downloadObserver);
}
}
/**
* 关闭定时器,线程等操作
*/
private void close() {
if (scheduledExecutorService != null && !scheduledExecutorService.isShutdown()) {
scheduledExecutorService.shutdown();
}
if (downLoadHandler != null) {
downLoadHandler.removeCallbacksAndMessages(null);
}
}
/**
* 发送Handler消息更新进度和状态
*/
private void updateProgress() {
int[] bytesAndStatus = getBytesAndStatus(downloadId);
downLoadHandler.sendMessage(downLoadHandler.obtainMessage(HANDLE_DOWNLOAD, bytesAndStatus[0], bytesAndStatus[1], bytesAndStatus[2]));
}
/**
* 通过query查询下载状态,包括已下载数据大小,总大小,下载状态
*
* @param downloadId
* @return
*/
private int[] getBytesAndStatus(long downloadId) {
int[] bytesAndStatus = new int[]{
-1, -1, 0
};
DownloadManager.Query query = new DownloadManager.Query().setFilterById(downloadId);
Cursor cursor = null;
try {
cursor = downloadManager.query(query);
if (cursor != null && cursor.moveToFirst()) {
//已经下载文件大小
bytesAndStatus[0] = cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
//下载文件的总大小
bytesAndStatus[1] = cursor.getInt(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
//下载状态
bytesAndStatus[2] = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
}
} finally {
if (cursor != null) {
cursor.close();
}
}
return bytesAndStatus;
}
/**
* 绑定此DownloadService的Activity实例
*
* @param activity
*/
public void setTargetActivity(Activity activity) {
this.activity = activity;
}
/**
* 接受下载完成广播
*/
private class DownLoadBroadcast extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
long downId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
switch (intent.getAction()) {
case DownloadManager.ACTION_DOWNLOAD_COMPLETE:
if (downloadId == downId && downId != -1 && downloadManager != null) {
Uri downIdUri = downloadManager.getUriForDownloadedFile(downloadId);
close();
if (downIdUri != null) {
Log.e("APK存储路径为","广播监听下载完成,APK存储路径为 :" + downIdUri.getPath());
Uri path=null;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
//6.0 以下
path = downloadManager.getUriForDownloadedFile(downloadId);
SPUtil.put(Constant.SP_DOWNLOAD_PATH, path.getPath());
} else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
//6.0 — 7.0
// path = Uri.fromFile(queryDownloadedApk(context, downloadId));
path = downloadManager.getUriForDownloadedFile(downloadId);
SPUtil.put(Constant.SP_DOWNLOAD_PATH, path.getPath());
} else {
//7.0 以上
File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "xxxxxxx.apk");
path = FileProvider.getUriForFile(context, "包名.fileprovider", file);
SPUtil.put(Constant.SP_DOWNLOAD_PATH, getFPUriToPath(context, path));
}
installApk(path);
}
if (onProgressListener != null) {
onProgressListener.onProgress(UNBIND_SERVICE);
}
}
break;
default:
break;
}
}
}
/**
* 安装APK
*/
public void installApk(Uri apkPath) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(apkPath, "application/vnd.android.package-archive");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
List<ResolveInfo> resolveLists = activity.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
// 然后全部授权
for (ResolveInfo resolveInfo : resolveLists){
String packageName = resolveInfo.activityInfo.packageName;
activity.grantUriPermission(packageName, apkPath, Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
}
activity.startActivity(intent);
}
/**
* 监听下载进度
*/
private class DownloadChangeObserver extends ContentObserver {
public DownloadChangeObserver() {
super(downLoadHandler);
scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
}
/**
* 当所监听的Uri发生改变时,就会回调此方法
*
* @param selfChange 此值意义不大, 一般情况下该回调值false
*/
@Override
public void onChange(boolean selfChange) {
if (scheduledExecutorService != null&&!scheduledExecutorService.isShutdown())
scheduledExecutorService.scheduleAtFixedRate(progressRunnable, 0, 2, TimeUnit.SECONDS);
}
}
public class DownloadBinder extends Binder {
/**
* 返回当前服务的实例
*
* @return
*/
public DownloadService getService() {
return DownloadService.this;
}
}
public interface OnProgressListener {
/**
* 下载进度
*
* @param fraction 已下载/总大小
*/
void onProgress(float fraction);
}
/**
* 对外开发的方法
*
* @param onProgressListener
*/
public void setOnProgressListener(OnProgressListener onProgressListener) {
this.onProgressListener = onProgressListener;
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterBroadcast();
unregisterContentObserver();
Log.e("下载任务服务销毁","下载任务服务销毁");
}
/**
* 通过url 获取真实的存储路径
* @param context
* @param uri
* @return
*/
public String getFPUriToPath(Context context, Uri uri) {
try {
List<PackageInfo> packs = context.getPackageManager().getInstalledPackages(PackageManager.GET_PROVIDERS);
if (packs != null) {
String fileProviderClassName = FileProvider.class.getName();
for (PackageInfo pack : packs) {
ProviderInfo[] providers = pack.providers;
if (providers != null) {
for (ProviderInfo provider : providers) {
if (uri.getAuthority().equals(provider.authority)) {
if (provider.name.equalsIgnoreCase(fileProviderClassName)) {
Class<FileProvider> fileProviderClass = FileProvider.class;
try {
Method getPathStrategy = fileProviderClass.getDeclaredMethod("getPathStrategy", Context.class, String.class);
getPathStrategy.setAccessible(true);
Object invoke = getPathStrategy.invoke(null, context, uri.getAuthority());
if (invoke != null) {
String PathStrategyStringClass = FileProvider.class.getName() + "$PathStrategy";
Class<?> PathStrategy = Class.forName(PathStrategyStringClass);
Method getFileForUri = PathStrategy.getDeclaredMethod("getFileForUri", Uri.class);
getFileForUri.setAccessible(true);
Object invoke1 = getFileForUri.invoke(invoke, uri);
if (invoke1 instanceof File) {
String filePath = ((File) invoke1).getAbsolutePath();
return filePath;
}
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
break;
}
break;
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}