Skip to content

Commit

Permalink
Removed download manager
Browse files Browse the repository at this point in the history
  • Loading branch information
CaptainDaVinci committed Sep 1, 2019
1 parent f48539c commit 4b26fef
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import android.Manifest;
import android.app.AlertDialog;
import android.app.DownloadManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
Expand All @@ -23,7 +23,7 @@
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
Expand All @@ -43,19 +43,25 @@
import com.example.application.musicdownloader.query.Query;
import com.google.firebase.remoteconfig.FirebaseRemoteConfig;

import java.io.File;
import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.ResponseBody;
import okio.Buffer;
import okio.BufferedSink;
import okio.BufferedSource;
import okio.Okio;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends AppCompatActivity {
public static final String TAG = "MUSICDL";
private EditText inputText;
private TextView statusTextView;
private LinearLayout progress;
private TextView statusTextView, progressPercent;
private ProgressBar spinningProgress, progressBar;
private Query query;

@Override
Expand All @@ -70,7 +76,9 @@ protected void onCreate(Bundle savedInstanceState) {
Log.i(TAG, "Starting main activity");
inputText = findViewById(R.id.input_text);
statusTextView = findViewById(R.id.status_text);
progress = findViewById(R.id.progressLayout);
spinningProgress = findViewById(R.id.spinning_progress);
progressBar = findViewById(R.id.progress_bar);
progressPercent = findViewById(R.id.progress_percent);

FirebaseRemoteConfig firebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
firebaseRemoteConfig.setDefaultsAsync(R.xml.remote_config);
Expand All @@ -91,6 +99,11 @@ protected void onCreate(Bundle savedInstanceState) {
});
}

private void setStatus(String msg, int color) {
statusTextView.setTextColor(color);
statusTextView.setText(msg);
}

public void buttonClicked(final View view) {
Log.i(TAG, "Download button clicked");

Expand All @@ -99,7 +112,7 @@ public void buttonClicked(final View view) {
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}

statusTextView.setText("");
setStatus("", Color.BLACK);
query = new Query();
query.setSearch(inputText.getText().toString());

Expand Down Expand Up @@ -138,9 +151,9 @@ public void buttonClicked(final View view) {

public void clearButton(View view) {
inputText.setText("");
statusTextView.setText("");
progress.setVisibility(View.GONE);
};
setStatus("", Color.BLACK);
spinningProgress.setVisibility(View.GONE);
}

private boolean hasNetwork() {
ConnectivityManager connectivityManager
Expand Down Expand Up @@ -185,7 +198,9 @@ public void onRequestPermissionsResult(int requestCode,
}

void searchQuery() {
progress.setVisibility(View.VISIBLE);
setStatus("Searching", Color.BLACK);
spinningProgress.setVisibility(View.VISIBLE);

YouTubeDataService service = APIClientInstance.getYouTubeRetrofitInstance().create(YouTubeDataService.class);
Call<YouTubeData> call = service.getYouTubeData("snippet",
query.getSearch(),
Expand All @@ -198,8 +213,8 @@ void searchQuery() {
public void onResponse(Call<YouTubeData> call, Response<YouTubeData> response) {
Log.i(TAG, "Obtained YouTube response");
if (!response.isSuccessful() || response.body() == null) {
progress.setVisibility(View.GONE);
statusTextView.setText("Oh, snap! Search failed.");
spinningProgress.setVisibility(View.GONE);
setStatus("Oh, snap! Search failed", Color.BLACK);
Log.d(TAG, "YouTube error: " + response.message());
} else {
showResponse(response.body());
Expand All @@ -208,15 +223,16 @@ public void onResponse(Call<YouTubeData> call, Response<YouTubeData> response) {

@Override
public void onFailure(Call<YouTubeData> call, Throwable t) {
progress.setVisibility(View.GONE);
statusTextView.setText("Oh, snap! Search failed.");
spinningProgress.setVisibility(View.GONE);
setStatus("Oh, snap! Search failed", Color.BLACK);
Log.d(TAG, "YouTube error: " + t.getMessage());
}
});
}

private void showResponse(final YouTubeData data) {
progress.setVisibility(View.GONE);
spinningProgress.setVisibility(View.GONE);
setStatus("", Color.BLACK);

LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.activity_dialog, null);
Expand All @@ -234,7 +250,7 @@ private void showResponse(final YouTubeData data) {
break;

case DialogInterface.BUTTON_NEGATIVE:
statusTextView.setText("Cancelled");
setStatus("Cancelled", Color.BLACK);
break;
}
};
Expand Down Expand Up @@ -271,7 +287,9 @@ public void onResponse(okhttp3.Call call, okhttp3.Response response) {
}

private void getDownloadLink(final YouTubeData data) {
progress.setVisibility(View.VISIBLE);
spinningProgress.setVisibility(View.VISIBLE);
setStatus("Converting...", Color.BLACK);

ServerDataService service = APIClientInstance.getServerRetrofitInstance().create(ServerDataService.class);
Call<ServerData> call = service.getDownloadLink(data.getId(),
query.getEncoding().toString(),
Expand All @@ -280,12 +298,12 @@ private void getDownloadLink(final YouTubeData data) {
call.enqueue(new Callback<ServerData>() {
@Override
public void onResponse(Call<ServerData> call, Response<ServerData> response) {
progress.setVisibility(View.GONE);
spinningProgress.setVisibility(View.GONE);
Log.i(TAG, "Obtained server response");

if (!response.isSuccessful() || response.body() == null) {
Log.d(TAG, "Server error: " + response.message());
statusTextView.setText("Oh, snap! Conversion failed.");
setStatus("Oh, snap! Conversion failed", Color.BLACK);
} else {
String downloadLink = response.body().getDownloadLink();
Log.d(TAG, "Download link: " + downloadLink);
Expand All @@ -295,29 +313,78 @@ public void onResponse(Call<ServerData> call, Response<ServerData> response) {

@Override
public void onFailure(Call<ServerData> call, Throwable t) {
progress.setVisibility(View.GONE);
spinningProgress.setVisibility(View.GONE);
Log.d(TAG, "Server error: " + t.getMessage());
statusTextView.setText("Oh, snap! Conversion failed.");
setStatus("Oh, snap! Conversion failed.", Color.BLACK);
}
});
}

private void downloadFile(String downloadLink, String fileName) {
Log.i(TAG, "Setting up download manager");
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(downloadLink));
request.setTitle(fileName);
request.setDescription("Music-DL");
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);

DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
if (manager != null) {
manager.enqueue(request);
Log.i(TAG, "Download started");
} else {
statusTextView.setText("Oops! Download failed");
if (downloadLink == null) {
setStatus("Failed to get download link", Color.BLACK);
return;
}

/*setStatus("Opening link in browser...", Color.BLACK);
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(downloadLink));
startActivity(browserIntent); */

OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url(downloadLink).build();
client.newCall(request).enqueue(new okhttp3.Callback() {
@Override
public void onFailure(okhttp3.Call call, IOException e) {
Log.d(TAG, "Download: " + e.getMessage());
}

@Override
public void onResponse(okhttp3.Call call, okhttp3.Response response) throws IOException {
if (!response.isSuccessful() || response.body() == null) {
setStatus("Download failed", Color.BLACK);
return;
}

runOnUiThread(() -> {
setStatus("Downloading...", Color.BLACK);
progressBar.setVisibility(View.VISIBLE);
progressPercent.setVisibility(View.VISIBLE);
});

ResponseBody body = response.body();
BufferedSource source = body.source();
long contentLength = body.contentLength();
File downloadPath = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS),
fileName + "." + query.getEncoding().toString());
BufferedSink sink = Okio.buffer(Okio.sink(downloadPath));
Buffer sinkBuffer = sink.buffer();

long totalBytesRead = 0;
int bufferSize = 8 * 1024;

for (long bytesRead; (bytesRead = source.read(sinkBuffer, bufferSize)) != -1; ) {
sink.emit();
totalBytesRead += bytesRead;
int progress = (int) Math.ceil((totalBytesRead * 100) / contentLength);
runOnUiThread(() -> {
progressBar.setProgress(progress);
progressPercent.setText(String.format("%d%%", progress));
});
}
sink.flush();
sink.close();
source.close();
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(downloadPath)));

runOnUiThread(() -> {
setStatus("Download completed", Color.BLACK);
progressPercent.setVisibility(View.GONE);
progressBar.setVisibility(View.GONE);
});
Log.d(TAG, "Download completed");
}
});
}

@Override
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/res/drawable/progress_bar.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<!-- Define the progress properties like start color, end color etc -->
<item android:id="@android:id/progress">
<clip>
<shape>
<gradient
android:angle="270"
android:centerColor="@color/colorPrimary"
android:centerY="1.0"
android:endColor="@color/colorPrimary"
android:startColor="@color/colorPrimary" />
</shape>
</clip>
</item>
</layer-list>
39 changes: 27 additions & 12 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,35 @@
android:textSize="18sp"
android:textColor="@android:color/holo_red_light"/>

<LinearLayout
android:id="@+id/progressLayout"

<ProgressBar
android:id="@+id/spinning_progress"
android:layout_below="@+id/status_text"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:visibility="gone" />

<ProgressBar
android:id="@+id/progress_bar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_height="8dp"
android:layout_below="@+id/status_text"
android:layout_centerHorizontal="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center"
android:visibility="gone">
android:layout_marginTop="10dp"
android:progressDrawable="@drawable/progress_bar"
android:visibility="gone" />

<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
<TextView
android:id="@+id/progress_percent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/progress_bar"
android:textColor="@android:color/black"
android:textSize="14sp"
android:visibility="gone"></TextView>

</RelativeLayout>

0 comments on commit 4b26fef

Please sign in to comment.