From 211d135dc0843bdaf69d9d9d4f7d55c1c8c73849 Mon Sep 17 00:00:00 2001 From: fd Date: Tue, 24 Feb 2015 19:48:20 -0500 Subject: [PATCH] Cancels the download async for a more responsive experience. And other fixes and bugs. --- src/fluddokt/opsu/fake/File.java | 32 ++++++++--- src/fluddokt/opsu/fake/GameContainer.java | 5 +- src/fluddokt/opsu/fake/Music.java | 2 +- src/fluddokt/opsu/fake/MusicGdx.java | 28 --------- src/itdelatrisu/opsu/downloads/Download.java | 60 +++++++++++--------- 5 files changed, 61 insertions(+), 66 deletions(-) diff --git a/src/fluddokt/opsu/fake/File.java b/src/fluddokt/opsu/fake/File.java index 455247f5..c2afe043 100644 --- a/src/fluddokt/opsu/fake/File.java +++ b/src/fluddokt/opsu/fake/File.java @@ -35,19 +35,30 @@ public File(String name) { else fh = Gdx.files.external(name); System.out.println(fh+" "+fh.exists()+" "+fh.type()); - if (fh.exists()) + if (fh.exists()) { + System.out.println("new file:"+info()); return; - - fh = Gdx.files.internal(name); - if (fh.exists()) - return; - fh = Gdx.files.local(name); - if (fh.exists()) + } + if (Gdx.app.getType() == ApplicationType.Desktop) + fh = Gdx.files.external(name); + else + fh = Gdx.files.local(name); + if (fh.exists()) { + System.out.println("new file:"+info()); return; + } fh = Gdx.files.absolute(name); - if (fh.exists()) + if (fh.exists()) { + System.out.println("new file:"+info()); + return; + } + fh = Gdx.files.internal(name); + if (fh.exists()) { + System.out.println("new file:"+info()); return; + } fh = Gdx.files.external(name); + System.out.println("new nonexist file:"+info()); } @@ -57,9 +68,14 @@ private File(FileHandle nfh) { } public File(File parent, String child) { + System.out.println("new child file:"+parent.info()+" "+child); fh = parent.fh.child(child); } + private String info() { + return fh.path()+"["+fh.type(); + } + public File(String parent, String child) { this(new File(parent), child); } diff --git a/src/fluddokt/opsu/fake/GameContainer.java b/src/fluddokt/opsu/fake/GameContainer.java index 053d30c8..8af8c84a 100644 --- a/src/fluddokt/opsu/fake/GameContainer.java +++ b/src/fluddokt/opsu/fake/GameContainer.java @@ -34,8 +34,9 @@ public int getHeight() { public void exit() { close_sub(); - if(music != null) - music.stop(); + if(music != null){ + music.dispose(); + } Gdx.app.exit(); } diff --git a/src/fluddokt/opsu/fake/Music.java b/src/fluddokt/opsu/fake/Music.java index 310e39af..4bddbc2f 100644 --- a/src/fluddokt/opsu/fake/Music.java +++ b/src/fluddokt/opsu/fake/Music.java @@ -112,7 +112,7 @@ public float getPosition() { } public void dispose() { - System.out.println("Music pause " + music.getName()); + System.out.println("Music dispose " + music.getName()); music.dispose(); } diff --git a/src/fluddokt/opsu/fake/MusicGdx.java b/src/fluddokt/opsu/fake/MusicGdx.java index 95a19d8c..2d5beeb2 100644 --- a/src/fluddokt/opsu/fake/MusicGdx.java +++ b/src/fluddokt/opsu/fake/MusicGdx.java @@ -83,34 +83,6 @@ public void run() { music.stop(); } - /* - * public float getPosition2() { float thisPosition = music.getPosition(); - * // 1/8 //System.out.println("getPosition: mpos:"+thisPosition); long - * thisTime = TimeUtils.millis(); //float dxPosition = thisPosition - - * lastPosition; float dxPosition2 = thisPosition - lastUpdatePosition; - * - * float syncPosition = (lastPosition);//; long dxTime = thisTime - - * lastTime; //if(dxTime>1) // lastTime = thisTime; //Whenever the time - * changes check the difference between that and our current time //sync our - * time to song time if((int)(dxPosition2*1000)!=0 && Math.abs(syncPosition - * - dxTime/1000f)>0){ - * //System.out.println("Time Reset"+" "+syncPosition+" "+(dxTime/1000f) - * +" " +(syncPosition-(dxTime/1000f)) ); //dxTime = (pos*1000+dxtime)/2 - * //dxTime = (pos*1000+dxtime)/2 //lastTime = thisTime - - * (pos*1000+dxtime)/2 lastTime = thisTime - - * ((long)(syncPosition*1000)+dxTime)/2; //System.out.println( - * "ASDF:"+(syncPosition*1000)+" "+(syncPosition-lastTime)+" " - * //+((long)(syncPosition*1000)-(syncPosition-lastTime))); //lastTime = - * thisTime; lastPosition = thisPosition; //setPosition(thisPosition); - * //dxTime = 0; } lastUpdatePosition = thisPosition; //if(Gdx.) return - * dxTime/1000f; - * - * //long thisTime = TimeUtils.millis(); //long dxTime = thisTime - - * lastTime; //return dxTime/1000f; //Gdx.graphics.getDeltaTime(); //return - * music.getPosition(); - * - * } - */ @Override public float getPosition() { return music.getPosition(); diff --git a/src/itdelatrisu/opsu/downloads/Download.java b/src/itdelatrisu/opsu/downloads/Download.java index fb873e66..d8d56529 100644 --- a/src/itdelatrisu/opsu/downloads/Download.java +++ b/src/itdelatrisu/opsu/downloads/Download.java @@ -23,7 +23,6 @@ import itdelatrisu.opsu.ErrorHandler; import itdelatrisu.opsu.Utils; - import java.io.FileInputStream; //import java.io.File; import java.io.FileOutputStream; @@ -178,12 +177,20 @@ public void run() { updateReadSoFar(); int total = 0; - while(status == Status.DOWNLOADING && total < contentLength){ - long readed = foschannel.transferFrom(rbc, total, Math.min(0x10000, contentLength-total)); - total += readed; + try { + while(status == Status.DOWNLOADING && total < contentLength){ + long readed = foschannel.transferFrom(rbc, total, Math.min(8192, contentLength-total)); + total += readed; + } + } catch (Exception e) { + //e.printStackTrace(); + if(status != Status.CANCELLED){ + status = Status.ERROR; + ErrorHandler.error("Error while downloading.", e, true); + } } - //*/ - if (status == Status.DOWNLOADING) { // not interrupted + + if (status == Status.DOWNLOADING) { status = Status.COMPLETE; rbc.close(); fos.close(); @@ -356,27 +363,26 @@ else if (lastReadSoFarTime <= 0) { * Cancels the download, if running. */ public void cancel() { - try { - this.status = Status.CANCELLED; - boolean transferring = isTransferring(); - dlThread.interrupt(); - try { - dlThread.join(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - if (rbc != null && rbc.isOpen()) - rbc.close(); - if (fos != null && fos.getChannel().isOpen()) - fos.close(); - if (transferring) { - File f = localFile; - if (f.isFile()) - f.delete(); + //dlThread.interrupt(); + new Thread(){ + public void run(){ + try { + status = Status.CANCELLED; + boolean transferring = isTransferring(); + if (rbc != null && rbc.isOpen()) + rbc.close(); + if (fos != null && fos.getChannel().isOpen()) + fos.close(); + if (transferring) { + File f = localFile; + if (f.isFile()) + f.delete(); + } + } catch (Exception e) { + status = Status.ERROR; + ErrorHandler.error("Failed to cancel download.", e, true); + } } - } catch (IOException e) { - this.status = Status.ERROR; - ErrorHandler.error("Failed to cancel download.", e, true); - } + }.start(); } }