Twig N
5 years ago
20 changed files with 1288 additions and 660 deletions
@ -0,0 +1,150 @@
@@ -0,0 +1,150 @@
|
||||
package org.transdroid.core.gui.lists; |
||||
|
||||
import org.transdroid.R; |
||||
|
||||
import android.content.Context; |
||||
import android.view.View; |
||||
import android.graphics.Canvas; |
||||
import android.graphics.Paint; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import java.lang.Math; |
||||
|
||||
class PiecesMapView extends View { |
||||
|
||||
private final float scale = getContext().getResources().getDisplayMetrics().density; |
||||
private final int MINIMUM_HEIGHT = (int) (25 * scale); |
||||
private final int MINIMUM_PIECE_WIDTH = (int) (2 * scale); |
||||
|
||||
private ArrayList<Integer> pieces = null; |
||||
|
||||
private final Paint downloadingPaint = new Paint(); |
||||
private final Paint donePaint = new Paint(); |
||||
private final Paint partialDonePaint = new Paint(); |
||||
|
||||
public PiecesMapView(Context context) { |
||||
super(context); |
||||
initPaints(); |
||||
} |
||||
|
||||
private void initPaints() { |
||||
downloadingPaint.setColor(getResources().getColor(R.color.torrent_downloading)); |
||||
donePaint.setColor(getResources().getColor(R.color.torrent_seeding)); |
||||
partialDonePaint.setColor(getResources().getColor(R.color.file_low)); |
||||
} |
||||
|
||||
public void setPieces(List<Integer> pieces) { |
||||
this.pieces = new ArrayList<Integer>(pieces); |
||||
invalidate(); |
||||
} |
||||
|
||||
@Override |
||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
||||
int ws = MeasureSpec.getSize(widthMeasureSpec); |
||||
int hs = Math.max(getHeight(), MINIMUM_HEIGHT); |
||||
setMeasuredDimension(ws, hs); |
||||
} |
||||
|
||||
@Override |
||||
protected void onDraw(Canvas canvas) { |
||||
super.onDraw(canvas); |
||||
|
||||
if (this.pieces == null) { |
||||
return; |
||||
} |
||||
|
||||
int height = getHeight(); |
||||
int width = getWidth(); |
||||
|
||||
// downscale
|
||||
ArrayList<Integer> piecesScaled; |
||||
int pieceWidth; |
||||
|
||||
pieceWidth = MINIMUM_PIECE_WIDTH; |
||||
piecesScaled = new ArrayList<Integer>(); |
||||
|
||||
int bucketCount = (int) Math.ceil((double) width / (double) pieceWidth); |
||||
int bucketSize = (int) Math.floor((double)this.pieces.size() / (double) bucketCount); |
||||
|
||||
// loop buckets
|
||||
for (int i = 0; i < bucketCount; i++) { |
||||
|
||||
// Get segment of pieces that fall into bucket
|
||||
int start = i * bucketSize; |
||||
|
||||
// If this is the last bucket, throw the remainder of the pieces array into it
|
||||
int end = (i == bucketCount-1) ? this.pieces.size() : (i+1) * bucketSize; |
||||
|
||||
ArrayList<Integer> bucket = new ArrayList<Integer>(this.pieces.subList(start, end)); |
||||
|
||||
int doneCount = 0; |
||||
int downloadingCount = 0; |
||||
|
||||
// loop pieces in bucket
|
||||
for(int j = 0; j < bucket.size(); j++) { |
||||
// Count downloading pieces
|
||||
if (bucket.get(j) == 1) { |
||||
downloadingCount++; |
||||
} |
||||
// Count finished pieces
|
||||
else if (bucket.get(j) == 2) { |
||||
doneCount++; |
||||
} |
||||
} |
||||
|
||||
int state; |
||||
// If a piece is downloading show bucket as downloading
|
||||
if (downloadingCount > 0) { |
||||
state = 1; |
||||
} |
||||
// If all pieces are done, show bucket as done
|
||||
else if (doneCount == bucket.size()) { |
||||
state = 2; |
||||
} |
||||
// Some done pieces, show bucket as partially done
|
||||
else if (doneCount > 0) { |
||||
state = 3; |
||||
} |
||||
// bucket is not downloaded
|
||||
else { |
||||
state = 0; |
||||
} |
||||
|
||||
piecesScaled.add(state); |
||||
} |
||||
|
||||
String scaledPiecesString = ""; |
||||
for (int s : piecesScaled) |
||||
{ |
||||
scaledPiecesString += s; |
||||
} |
||||
|
||||
// Draw downscaled peices
|
||||
for (int i = 0; i < piecesScaled.size(); i++) { |
||||
int piece = piecesScaled.get(i); |
||||
|
||||
if (piece == 0) { |
||||
continue; |
||||
} |
||||
|
||||
Paint paint = new Paint(); |
||||
switch (piece) { |
||||
case 1: |
||||
paint = downloadingPaint; |
||||
break; |
||||
case 2: |
||||
paint = donePaint; |
||||
break; |
||||
case 3: |
||||
paint = partialDonePaint; |
||||
break; |
||||
} |
||||
int x = i * pieceWidth; |
||||
|
||||
canvas.drawRect(x, 0, x + pieceWidth, height, paint); |
||||
} |
||||
} |
||||
|
||||
} |
||||
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,31 @@
@@ -0,0 +1,31 @@
|
||||
/* |
||||
* This file is part of Transdroid <http://www.transdroid.org>
|
||||
* |
||||
* Transdroid is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU General Public License as published by |
||||
* the Free Software Foundation, either version 3 of the License, or |
||||
* (at your option) any later version. |
||||
* |
||||
* Transdroid is distributed in the hope that it will be useful, |
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
* GNU General Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU General Public License |
||||
* along with Transdroid. If not, see <http://www.gnu.org/licenses/>.
|
||||
* |
||||
*/ |
||||
package org.transdroid.daemon.task; |
||||
|
||||
import org.transdroid.daemon.DaemonMethod; |
||||
import org.transdroid.daemon.IDaemonAdapter; |
||||
import org.transdroid.daemon.Torrent; |
||||
|
||||
public class ToggleFirstLastPieceDownloadTask extends DaemonTask { |
||||
protected ToggleFirstLastPieceDownloadTask(IDaemonAdapter adapter, Torrent targetTorrent) { |
||||
super(adapter, DaemonMethod.ToggleFirstLastPieceDownload, targetTorrent, null); |
||||
} |
||||
public static ToggleFirstLastPieceDownloadTask create(IDaemonAdapter adapter, Torrent targetTorrent) { |
||||
return new ToggleFirstLastPieceDownloadTask(adapter, targetTorrent); |
||||
} |
||||
} |
@ -0,0 +1,31 @@
@@ -0,0 +1,31 @@
|
||||
/* |
||||
* This file is part of Transdroid <http://www.transdroid.org>
|
||||
* |
||||
* Transdroid is free software: you can redistribute it and/or modify |
||||
* it under the terms of the GNU General Public License as published by |
||||
* the Free Software Foundation, either version 3 of the License, or |
||||
* (at your option) any later version. |
||||
* |
||||
* Transdroid is distributed in the hope that it will be useful, |
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||
* GNU General Public License for more details. |
||||
* |
||||
* You should have received a copy of the GNU General Public License |
||||
* along with Transdroid. If not, see <http://www.gnu.org/licenses/>.
|
||||
* |
||||
*/ |
||||
package org.transdroid.daemon.task; |
||||
|
||||
import org.transdroid.daemon.DaemonMethod; |
||||
import org.transdroid.daemon.IDaemonAdapter; |
||||
import org.transdroid.daemon.Torrent; |
||||
|
||||
public class ToggleSequentialDownloadTask extends DaemonTask { |
||||
protected ToggleSequentialDownloadTask(IDaemonAdapter adapter, Torrent targetTorrent) { |
||||
super(adapter, DaemonMethod.ToggleSequentialDownload, targetTorrent, null); |
||||
} |
||||
public static ToggleSequentialDownloadTask create(IDaemonAdapter adapter, Torrent targetTorrent) { |
||||
return new ToggleSequentialDownloadTask(adapter, targetTorrent); |
||||
} |
||||
} |
Loading…
Reference in new issue