fix downloader

This commit is contained in:
Page Asgardius 2022-11-28 11:40:14 -07:00
parent c1c9aee8e3
commit 29ebb3d85b
3 changed files with 136 additions and 119 deletions

View file

@ -87,113 +87,117 @@ public class Downloader extends AppCompatActivity {
downloadFile.interrupt();
//simpleProgressBar.setVisibility(View.INVISIBLE);
} else {
started = true;
simpleProgressBar.setVisibility(View.VISIBLE);
//fileDownload.setEnabled(false);
fileDownload.setText(getResources().getString(R.string.cancel_download));
//Acquiring WakeLock and WifiLock if not held
if (!mWifiLock.isHeld()) {
mWifiLock.acquire();
//System.out.println("WifiLock acquired");
}
if (!mWakeLock.isHeld()) {
mWakeLock.acquire();
//System.out.println("WakeLock acquired");
}
downloadFile = new Thread(new Runnable() {
@Override
public void run() {
simpleProgressBar.setVisibility(View.VISIBLE);
try {
//Your code goes here
//s3client.createBucket(bucket, location);
//System.out.println(fkey);
object = s3client.getObject(bucket, prefix+filename);
filesize = (object.getObjectMetadata().getContentLength())/1024;
writeContentToFile(fileuri);
runOnUiThread(new Runnable() {
@Override
public void run() {
//simpleProgressBar.setProgress(100);
//Releasing WifiLock and WakeLock if held
if (mWifiLock.isHeld()) {
mWifiLock.release();
//System.out.println("WifiLock released");
}
if (mWakeLock.isHeld()) {
mWakeLock.release();
//System.out.println("WakeLock released");
}
simpleProgressBar.setProgress(100);
fileDownload.setText(getResources().getString(R.string.download_success));
fileDownload.setEnabled(false);
//simpleProgressBar.setVisibility(View.INVISIBLE);
}
});
//System.out.println("tree "+treelevel);
//System.out.println("prefix "+prefix);
} catch (Exception e) {
e.printStackTrace();
runOnUiThread(new Runnable() {
@Override
public void run() {
//Releasing WifiLock and WakeLock if held
if (mWifiLock.isHeld()) {
mWifiLock.release();
//System.out.println("WifiLock released");
}
if (mWakeLock.isHeld()) {
mWakeLock.release();
//System.out.println("WakeLock released");
}
if (cancel) {
fileDownload.setText(getResources().getString(R.string.download_canceled));
} else {
fileDownload.setText(getResources().getString(R.string.download_failed));
}
fileDownload.setEnabled(false);
}
});
//Toast.makeText(getApplicationContext(),getResources().getString(R.string.media_list_fail), Toast.LENGTH_SHORT).show();
//finish();
}
}
});
downloadProgress = new Thread(new Runnable() {
@Override
public void run() {
try {
//Your code goes here
while (fileDownload.isEnabled()) {
try {
if (filesize != 0) {
simpleProgressBar.setProgress((int)((transfered*100)/filesize));
}
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
//Toast.makeText(getApplicationContext(),getResources().getString(R.string.media_list_fail), Toast.LENGTH_SHORT).show();
//finish();
}
}
});
downloadFile.start();
downloadProgress.start();
downloadFile();
}
}
});
}
private void downloadFile () {
started = true;
simpleProgressBar.setVisibility(View.VISIBLE);
//fileDownload.setEnabled(false);
fileDownload.setText(getResources().getString(R.string.cancel_download));
//Acquiring WakeLock and WifiLock if not held
if (!mWifiLock.isHeld()) {
mWifiLock.acquire();
//System.out.println("WifiLock acquired");
}
if (!mWakeLock.isHeld()) {
mWakeLock.acquire();
//System.out.println("WakeLock acquired");
}
downloadFile = new Thread(new Runnable() {
@Override
public void run() {
simpleProgressBar.setVisibility(View.VISIBLE);
try {
//Your code goes here
//s3client.createBucket(bucket, location);
//System.out.println(fkey);
object = s3client.getObject(bucket, prefix+filename);
filesize = (object.getObjectMetadata().getContentLength())/1024;
writeContentToFile(fileuri);
runOnUiThread(new Runnable() {
@Override
public void run() {
//simpleProgressBar.setProgress(100);
//Releasing WifiLock and WakeLock if held
if (mWifiLock.isHeld()) {
mWifiLock.release();
//System.out.println("WifiLock released");
}
if (mWakeLock.isHeld()) {
mWakeLock.release();
//System.out.println("WakeLock released");
}
simpleProgressBar.setProgress(100);
fileDownload.setText(getResources().getString(R.string.download_success));
fileDownload.setEnabled(false);
//simpleProgressBar.setVisibility(View.INVISIBLE);
}
});
//System.out.println("tree "+treelevel);
//System.out.println("prefix "+prefix);
} catch (Exception e) {
e.printStackTrace();
runOnUiThread(new Runnable() {
@Override
public void run() {
//Releasing WifiLock and WakeLock if held
if (mWifiLock.isHeld()) {
mWifiLock.release();
//System.out.println("WifiLock released");
}
if (mWakeLock.isHeld()) {
mWakeLock.release();
//System.out.println("WakeLock released");
}
if (cancel) {
fileDownload.setText(getResources().getString(R.string.download_canceled));
} else {
fileDownload.setText(getResources().getString(R.string.download_failed));
}
fileDownload.setEnabled(false);
}
});
//Toast.makeText(getApplicationContext(),getResources().getString(R.string.media_list_fail), Toast.LENGTH_SHORT).show();
//finish();
}
}
});
downloadProgress = new Thread(new Runnable() {
@Override
public void run() {
try {
//Your code goes here
while (fileDownload.isEnabled()) {
try {
if (filesize != 0) {
simpleProgressBar.setProgress((int)((transfered*100)/filesize));
}
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
//Toast.makeText(getApplicationContext(),getResources().getString(R.string.media_list_fail), Toast.LENGTH_SHORT).show();
//finish();
}
}
});
downloadFile.start();
downloadProgress.start();
}
private void performFileSearch(String messageTitle) {
//uri = Uri.parse("content://com.android.externalstorage.documents/document/home");
intent = new Intent();
@ -220,6 +224,7 @@ public class Downloader extends AppCompatActivity {
if (resultData != null && resultData.getData() != null) {
fileuri = resultData.getData();
System.out.println(fileuri.toString());
downloadFile();
//System.out.println("File selected successfully");
//System.out.println("content://com.android.externalstorage.documents"+file.getPath());
} else {

View file

@ -69,6 +69,7 @@ public class VideoPlayer extends AppCompatActivity {
private PlayerNotificationManager playerNotificationManager;
private int notificationId = 1234;
boolean hls;
boolean success = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -159,17 +160,21 @@ public class VideoPlayer extends AppCompatActivity {
public void onPlayerError(PlaybackException error) {
Throwable cause = error.getCause();
if (cause instanceof HttpDataSource.HttpDataSourceException) {
// An HTTP error occurred.
//System.out.println("Playback error F");
Toast.makeText(getApplicationContext(), getResources().getString(R.string.media_conn_fail), Toast.LENGTH_SHORT).show();
if(success) {
player.pause();
} else {
// An HTTP error occurred.
//System.out.println("Playback error F");
Toast.makeText(getApplicationContext(), getResources().getString(R.string.media_wrong_type), Toast.LENGTH_SHORT).show();
if (cause instanceof HttpDataSource.HttpDataSourceException) {
// An HTTP error occurred.
//System.out.println("Playback error F");
Toast.makeText(getApplicationContext(), getResources().getString(R.string.media_conn_fail), Toast.LENGTH_SHORT).show();
} else {
// An HTTP error occurred.
//System.out.println("Playback error F");
Toast.makeText(getApplicationContext(), getResources().getString(R.string.media_wrong_type), Toast.LENGTH_SHORT).show();
}
player.release();
finish();
}
player.release();
finish();
}
@ -180,6 +185,7 @@ public class VideoPlayer extends AppCompatActivity {
public void onPlaybackStateChanged(@Player.State int state) {
if (state == 3) {
// Active playback.
success = true;
//Acquiring WakeLock and WifiLock if not held
if (!mWifiLock.isHeld()) {
mWifiLock.acquire();

View file

@ -69,6 +69,7 @@ public class VideoPlayerPip extends AppCompatActivity {
private PlayerNotificationManager playerNotificationManager;
private int notificationId = 1234;
boolean hls;
boolean success = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -159,17 +160,21 @@ public class VideoPlayerPip extends AppCompatActivity {
public void onPlayerError(PlaybackException error) {
Throwable cause = error.getCause();
if (cause instanceof HttpDataSource.HttpDataSourceException) {
// An HTTP error occurred.
//System.out.println("Playback error F");
Toast.makeText(getApplicationContext(), getResources().getString(R.string.media_conn_fail), Toast.LENGTH_SHORT).show();
if(success) {
player.pause();
} else {
// An HTTP error occurred.
//System.out.println("Playback error F");
Toast.makeText(getApplicationContext(), getResources().getString(R.string.media_wrong_type), Toast.LENGTH_SHORT).show();
if (cause instanceof HttpDataSource.HttpDataSourceException) {
// An HTTP error occurred.
//System.out.println("Playback error F");
Toast.makeText(getApplicationContext(), getResources().getString(R.string.media_conn_fail), Toast.LENGTH_SHORT).show();
} else {
// An HTTP error occurred.
//System.out.println("Playback error F");
Toast.makeText(getApplicationContext(), getResources().getString(R.string.media_wrong_type), Toast.LENGTH_SHORT).show();
}
player.release();
finish();
}
player.release();
finish();
}
@ -180,6 +185,7 @@ public class VideoPlayerPip extends AppCompatActivity {
public void onPlaybackStateChanged(@Player.State int state) {
if (state == 3) {
// Active playback.
success = true;
//Acquiring WakeLock and WifiLock if not held
if (!mWifiLock.isHeld()) {
mWifiLock.acquire();