Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.test.digitalexperience.widget;
- import android.app.DownloadManager;
- import android.content.Context;
- import android.content.res.TypedArray;
- import android.database.Cursor;
- import android.graphics.drawable.Drawable;
- import android.net.Uri;
- import android.os.Environment;
- import android.os.Handler;
- import android.support.v4.content.ContextCompat;
- import android.util.AttributeSet;
- import android.view.LayoutInflater;
- import android.widget.LinearLayout;
- import com.test.digitalexperience.R;
- import java.io.File;
- import java.util.ArrayList;
- import java.util.List;
- import ru.noties.filldrawable.FillDrawable;
- import ru.noties.filldrawable.FillImageView;
- /**
- * Created by VivekH on 20-04-2016.
- */
- public class testTicketLoaderWidget extends LinearLayout {
- Context mContext;
- static Long downloadRefId;
- DownloadManager.Request request;
- static DownloadManager downloadmanager;
- static DownloadStatInterface prog;
- String downloadUrl;
- public testTicketLoaderWidget(Context context) {
- super(context);
- mContext = context;
- }
- public testTicketLoaderWidget(Context context, AttributeSet attrs) {
- this(context, attrs, 0);
- init(context, attrs);
- mContext = context;
- }
- public testTicketLoaderWidget(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- init(context, attrs);
- mContext = context;
- }
- private void init(Context context, AttributeSet attributeSet) {
- if (attributeSet != null) {
- final TypedArray typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.BBYTicketView);
- try {
- downloadUrl = typedArray.getString(R.styleable.BBYTicketView_bby_url);
- } finally {
- typedArray.recycle();
- }
- }
- LayoutInflater.from(context).inflate(R.layout.bby_ticket_layout, this, true);
- final List<FillDrawable> drawables = createDrawables();
- final FillImageView fillImageView1 = (FillImageView) findViewById(R.id.fill_image_view_1);
- final FillImageView fillImageView2 = (FillImageView) findViewById(R.id.fill_image_view_2);
- drawables.add(0, fillImageView2.getFillDrawable());
- drawables.add(0, fillImageView1.getFillDrawable());
- final Handler handler = new Handler();
- handler.post(new FakeProgress(handler, new FakeProgress.OnProgressChange() {
- @Override
- public void onProgressChange(float progress) {
- for (FillDrawable drawable : drawables) {
- drawable.setFillPercent(progress);
- }
- }
- }));
- Long tsLong = System.currentTimeMillis() / 1000;
- String ts = tsLong.toString();
- String servicestring = Context.DOWNLOAD_SERVICE;
- downloadmanager = (DownloadManager) context.getSystemService(servicestring);
- Uri uri = Uri
- .parse(downloadUrl);
- request = new DownloadManager.Request(uri);
- request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
- File.separator + "DI New" + File.separator + ts + ".mp4");
- downloadRefId = downloadmanager.enqueue(request);
- }
- private List<FillDrawable> createDrawables() {
- final Drawable drawable = ContextCompat.getDrawable(mContext, R.drawable.ticket2);
- drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
- final int alpha = 125;
- final int rightColor = ContextCompat.getColor(mContext, R.color.bby_white);
- final FillDrawable right = new FillDrawable(FillDrawable.FROM_RIGHT, drawable.mutate())
- .setNormalColor(ColorUtils.applyAlpha(rightColor, alpha))
- .setFillColor(rightColor);
- final int bottomColor = ContextCompat.getColor(mContext, R.color.bby_white);
- final FillDrawable bottom = new FillDrawable(FillDrawable.FROM_BOTTOM, drawable.mutate())
- .setNormalColor(ColorUtils.applyAlpha(bottomColor, alpha))
- .setFillColor(bottomColor);
- return new ArrayList<FillDrawable>() {{
- add(right);
- add(bottom);
- }};
- }
- private static class FakeProgress implements Runnable {
- interface OnProgressChange {
- void onProgressChange(float progress);
- }
- private final Handler mHandler;
- private final OnProgressChange mOnProgressChange;
- private float mPercent;
- FakeProgress(Handler handler, OnProgressChange onProgressChange) {
- mHandler = handler;
- mOnProgressChange = onProgressChange;
- }
- @Override
- public void run() {
- DownloadManager.Query q = new DownloadManager.Query();
- q.setFilterById(downloadRefId);
- Cursor cursor = downloadmanager.query(q);
- cursor.moveToFirst();
- long bytes_downloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
- long bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
- cursor.close();
- if ((bytes_total > 0) && (bytes_downloaded > 0)) {
- mPercent = (float) (((double) bytes_downloaded / (double) bytes_total)) * 100;
- }
- final int step = 200;
- float animPer = 1.F / 100;
- mOnProgressChange.onProgressChange(mPercent * animPer);
- if (mPercent < 100) {
- mHandler.postDelayed(this, step);
- } else {
- prog.downloadProg(mPercent);
- }
- }
- }
- public interface DownloadStatInterface {
- void downloadProg(float progress);
- }
- }
Add Comment
Please, Sign In to add comment