Guest User

Untitled

a guest
Apr 20th, 2016
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.86 KB | None | 0 0
  1. package com.test.digitalexperience.widget;
  2.  
  3. import android.app.DownloadManager;
  4. import android.content.Context;
  5. import android.content.res.TypedArray;
  6. import android.database.Cursor;
  7. import android.graphics.drawable.Drawable;
  8. import android.net.Uri;
  9. import android.os.Environment;
  10. import android.os.Handler;
  11. import android.support.v4.content.ContextCompat;
  12. import android.util.AttributeSet;
  13. import android.view.LayoutInflater;
  14. import android.widget.LinearLayout;
  15.  
  16. import com.test.digitalexperience.R;
  17.  
  18. import java.io.File;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21.  
  22. import ru.noties.filldrawable.FillDrawable;
  23. import ru.noties.filldrawable.FillImageView;
  24.  
  25.  
  26. /**
  27. * Created by VivekH on 20-04-2016.
  28. */
  29. public class testTicketLoaderWidget extends LinearLayout {
  30.  
  31. Context mContext;
  32. static Long downloadRefId;
  33. DownloadManager.Request request;
  34. static DownloadManager downloadmanager;
  35. static DownloadStatInterface prog;
  36. String downloadUrl;
  37.  
  38. public testTicketLoaderWidget(Context context) {
  39. super(context);
  40. mContext = context;
  41. }
  42.  
  43. public testTicketLoaderWidget(Context context, AttributeSet attrs) {
  44. this(context, attrs, 0);
  45. init(context, attrs);
  46. mContext = context;
  47. }
  48.  
  49.  
  50. public testTicketLoaderWidget(Context context, AttributeSet attrs, int defStyleAttr) {
  51. super(context, attrs, defStyleAttr);
  52. init(context, attrs);
  53. mContext = context;
  54. }
  55.  
  56.  
  57. private void init(Context context, AttributeSet attributeSet) {
  58. if (attributeSet != null) {
  59. final TypedArray typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.BBYTicketView);
  60. try {
  61. downloadUrl = typedArray.getString(R.styleable.BBYTicketView_bby_url);
  62.  
  63. } finally {
  64. typedArray.recycle();
  65. }
  66. }
  67.  
  68. LayoutInflater.from(context).inflate(R.layout.bby_ticket_layout, this, true);
  69. final List<FillDrawable> drawables = createDrawables();
  70.  
  71. final FillImageView fillImageView1 = (FillImageView) findViewById(R.id.fill_image_view_1);
  72. final FillImageView fillImageView2 = (FillImageView) findViewById(R.id.fill_image_view_2);
  73. drawables.add(0, fillImageView2.getFillDrawable());
  74. drawables.add(0, fillImageView1.getFillDrawable());
  75.  
  76. final Handler handler = new Handler();
  77. handler.post(new FakeProgress(handler, new FakeProgress.OnProgressChange() {
  78. @Override
  79. public void onProgressChange(float progress) {
  80. for (FillDrawable drawable : drawables) {
  81. drawable.setFillPercent(progress);
  82. }
  83. }
  84. }));
  85.  
  86. Long tsLong = System.currentTimeMillis() / 1000;
  87. String ts = tsLong.toString();
  88. String servicestring = Context.DOWNLOAD_SERVICE;
  89.  
  90. downloadmanager = (DownloadManager) context.getSystemService(servicestring);
  91. Uri uri = Uri
  92. .parse(downloadUrl);
  93. request = new DownloadManager.Request(uri);
  94. request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
  95. File.separator + "DI New" + File.separator + ts + ".mp4");
  96. downloadRefId = downloadmanager.enqueue(request);
  97. }
  98.  
  99. private List<FillDrawable> createDrawables() {
  100.  
  101. final Drawable drawable = ContextCompat.getDrawable(mContext, R.drawable.ticket2);
  102. drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
  103.  
  104. final int alpha = 125;
  105.  
  106. final int rightColor = ContextCompat.getColor(mContext, R.color.bby_white);
  107. final FillDrawable right = new FillDrawable(FillDrawable.FROM_RIGHT, drawable.mutate())
  108. .setNormalColor(ColorUtils.applyAlpha(rightColor, alpha))
  109. .setFillColor(rightColor);
  110.  
  111. final int bottomColor = ContextCompat.getColor(mContext, R.color.bby_white);
  112. final FillDrawable bottom = new FillDrawable(FillDrawable.FROM_BOTTOM, drawable.mutate())
  113. .setNormalColor(ColorUtils.applyAlpha(bottomColor, alpha))
  114. .setFillColor(bottomColor);
  115.  
  116. return new ArrayList<FillDrawable>() {{
  117. add(right);
  118. add(bottom);
  119. }};
  120. }
  121.  
  122. private static class FakeProgress implements Runnable {
  123.  
  124. interface OnProgressChange {
  125. void onProgressChange(float progress);
  126. }
  127.  
  128. private final Handler mHandler;
  129. private final OnProgressChange mOnProgressChange;
  130. private float mPercent;
  131.  
  132. FakeProgress(Handler handler, OnProgressChange onProgressChange) {
  133. mHandler = handler;
  134. mOnProgressChange = onProgressChange;
  135. }
  136.  
  137. @Override
  138. public void run() {
  139.  
  140. DownloadManager.Query q = new DownloadManager.Query();
  141. q.setFilterById(downloadRefId);
  142. Cursor cursor = downloadmanager.query(q);
  143. cursor.moveToFirst();
  144. long bytes_downloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
  145. long bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
  146. cursor.close();
  147.  
  148. if ((bytes_total > 0) && (bytes_downloaded > 0)) {
  149. mPercent = (float) (((double) bytes_downloaded / (double) bytes_total)) * 100;
  150.  
  151. }
  152. final int step = 200;
  153. float animPer = 1.F / 100;
  154.  
  155. mOnProgressChange.onProgressChange(mPercent * animPer);
  156. if (mPercent < 100) {
  157. mHandler.postDelayed(this, step);
  158. } else {
  159. prog.downloadProg(mPercent);
  160. }
  161.  
  162. }
  163. }
  164.  
  165. public interface DownloadStatInterface {
  166. void downloadProg(float progress);
  167. }
  168.  
  169.  
  170. }
Add Comment
Please, Sign In to add comment