Skip to content
This repository has been archived by the owner on Oct 18, 2020. It is now read-only.

Commit

Permalink
Implementing download percentage
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusjdev committed Jul 15, 2019
1 parent 3e23d95 commit 80223ba
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ android {
applicationId "com.d3coding.gmusicapi"
minSdkVersion 28
targetSdkVersion 28
versionCode 25
versionName "0.23"
versionCode 26
versionName "0.24"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
18 changes: 13 additions & 5 deletions app/src/main/java/com/d3coding/gmusicapi/GMusicFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ int getQueue(String uuid) {
if (scan(uuid)) {

new GMusicDB(context).insertUUIDbyDownloads(uuid);
Log.i("Download ALREADY completed:", uuid);
Log.i("Download ALREADY completed", uuid);
return 1;

} else {
boolean t = false;
for (Progress progress : ProgressList)
if (progress.UUID.equals(uuid)) {
t = true;
Log.i("Download ALREADY in queue:", uuid);
Log.i("Download ALREADY in queue", uuid);
break;
}

Expand All @@ -88,7 +88,7 @@ int getQueue(String uuid) {
ProgressList.get(taskId).percentage = 0.0f;

try {
Log.i("Download STARTED:", uuid);
Log.i("Download STARTED", uuid);
ProgressList.get(taskId).doing = Doing.inProgress;

AuthToken authToken = TokenProvider.provideToken(context.getSharedPreferences(context.getString(R.string.preferences_user)
Expand Down Expand Up @@ -116,7 +116,7 @@ int getQueue(String uuid) {

// Success
new GMusicDB(context).insertUUIDbyDownloads(trackMetadata.uuid);
Log.i("Download FINISHED:", uuid);
Log.i("Download FINISHED", uuid);
ProgressList.get(taskId).doing = Doing.completed;
ProgressList.get(taskId).percentage = 100f;
return 1;
Expand All @@ -142,6 +142,13 @@ int getQueue(String uuid) {
}
}

public Progress getQueueStatus(String uuid) {
for (Progress progress : ProgressList)
if (progress.UUID.equals(uuid))
return progress;
return null;
}

public Bitmap getThumbBitmap(String uuid) {
File thumbFile = new File(getPathJPG(uuid));
if (thumbFile.exists())
Expand All @@ -155,7 +162,8 @@ public Bitmap getThumbBitmap(String uuid) {

if (!thumbFile.exists()) {
Files.copy(url.openStream(), Paths.get(thumbFile.getAbsolutePath()), StandardCopyOption.REPLACE_EXISTING);
return Bitmap.createScaledBitmap(BitmapFactory.decodeFile(thumbFile.getAbsolutePath()), 200, 200, false);
//return Bitmap.createScaledBitmap(BitmapFactory.decodeFile(thumbFile.getAbsolutePath()), 200, 200, false);
return BitmapFactory.decodeFile(thumbFile.getAbsolutePath());
}

} catch (MalformedURLException e) {
Expand Down
25 changes: 17 additions & 8 deletions app/src/main/java/com/d3coding/gmusicapi/HomeFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class HomeFragment extends Fragment {

Expand Down Expand Up @@ -169,16 +171,23 @@ public void onViewCreated(@NonNull View layoutView, @Nullable Bundle savedInstan
});

// TODO: getDownloadStatus
if (gmusicFile.getQueue(musicItem.getUUID()) != 0)
synchronized (this) {
((Activity) getContext()).runOnUiThread(() -> {
linearComplete.setVisibility(View.VISIBLE);
linearDownloading.setVisibility(View.GONE);
mAdapter.notifyItemChanged(position);
});
}

final ScheduledExecutorService exec = Executors.newScheduledThreadPool(1);

exec.scheduleAtFixedRate(() -> {
GMusicFile.Progress progress = gmusicFile.getQueueStatus(musicItem.getUUID());

if (progress.percentage <= 100)
Log.i("Progress:", String.valueOf(progress.percentage));

if (progress.doing == GMusicFile.Doing.completed)
exec.shutdown();
}, 100, 100, TimeUnit.MILLISECONDS);

builder.setOnCancelListener((dialog) -> exec.shutdown());
}


vView.findViewById(R.id.opt_bt_play).setOnClickListener((view2) -> {
Uri uri = FileProvider.getUriForFile(getContext(), getContext().getPackageName() + ".fileprovider",
new File(new File(Environment.getExternalStorageDirectory(), "Gmusicapi"), musicItem.getUUID() + ".mp3"));
Expand Down

0 comments on commit 80223ba

Please sign in to comment.