You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
393 lines
15 KiB
393 lines
15 KiB
11 years ago
|
/*
|
||
|
* Copyright 2010-2013 Eric Kok et al.
|
||
|
*
|
||
|
* 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/>.
|
||
|
*/
|
||
11 years ago
|
package org.transdroid.core.gui;
|
||
|
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.List;
|
||
|
|
||
|
import org.androidannotations.annotations.AfterViews;
|
||
|
import org.androidannotations.annotations.Background;
|
||
|
import org.androidannotations.annotations.Bean;
|
||
|
import org.androidannotations.annotations.EActivity;
|
||
|
import org.androidannotations.annotations.Extra;
|
||
|
import org.androidannotations.annotations.FragmentById;
|
||
|
import org.androidannotations.annotations.InstanceState;
|
||
|
import org.androidannotations.annotations.OptionsItem;
|
||
|
import org.androidannotations.annotations.OptionsMenu;
|
||
|
import org.androidannotations.annotations.UiThread;
|
||
10 years ago
|
import org.transdroid.R;
|
||
11 years ago
|
import org.transdroid.core.app.settings.*;
|
||
11 years ago
|
import org.transdroid.core.gui.lists.LocalTorrent;
|
||
11 years ago
|
import org.transdroid.core.gui.lists.NoProgressHeaderTransformer;
|
||
11 years ago
|
import org.transdroid.core.gui.log.Log;
|
||
11 years ago
|
import org.transdroid.core.gui.navigation.Label;
|
||
11 years ago
|
import org.transdroid.core.gui.navigation.NavigationHelper;
|
||
11 years ago
|
import org.transdroid.core.gui.navigation.RefreshableActivity;
|
||
11 years ago
|
import org.transdroid.core.service.ConnectivityHelper;
|
||
11 years ago
|
import org.transdroid.daemon.Daemon;
|
||
11 years ago
|
import org.transdroid.daemon.IDaemonAdapter;
|
||
11 years ago
|
import org.transdroid.daemon.Priority;
|
||
11 years ago
|
import org.transdroid.daemon.Torrent;
|
||
|
import org.transdroid.daemon.TorrentDetails;
|
||
|
import org.transdroid.daemon.TorrentFile;
|
||
|
import org.transdroid.daemon.task.DaemonTaskFailureResult;
|
||
|
import org.transdroid.daemon.task.DaemonTaskResult;
|
||
11 years ago
|
import org.transdroid.daemon.task.DaemonTaskSuccessResult;
|
||
11 years ago
|
import org.transdroid.daemon.task.ForceRecheckTask;
|
||
11 years ago
|
import org.transdroid.daemon.task.GetFileListTask;
|
||
|
import org.transdroid.daemon.task.GetFileListTaskSuccessResult;
|
||
|
import org.transdroid.daemon.task.GetTorrentDetailsTask;
|
||
|
import org.transdroid.daemon.task.GetTorrentDetailsTaskSuccessResult;
|
||
11 years ago
|
import org.transdroid.daemon.task.PauseTask;
|
||
|
import org.transdroid.daemon.task.RemoveTask;
|
||
|
import org.transdroid.daemon.task.ResumeTask;
|
||
11 years ago
|
import org.transdroid.daemon.task.RetrieveTask;
|
||
|
import org.transdroid.daemon.task.RetrieveTaskSuccessResult;
|
||
11 years ago
|
import org.transdroid.daemon.task.SetDownloadLocationTask;
|
||
11 years ago
|
import org.transdroid.daemon.task.SetFilePriorityTask;
|
||
11 years ago
|
import org.transdroid.daemon.task.SetLabelTask;
|
||
|
import org.transdroid.daemon.task.SetTrackersTask;
|
||
|
import org.transdroid.daemon.task.StartTask;
|
||
|
import org.transdroid.daemon.task.StopTask;
|
||
11 years ago
|
|
||
11 years ago
|
import uk.co.senab.actionbarpulltorefresh.library.PullToRefreshAttacher;
|
||
|
import uk.co.senab.actionbarpulltorefresh.library.PullToRefreshAttacher.OnRefreshListener;
|
||
|
import uk.co.senab.actionbarpulltorefresh.library.PullToRefreshAttacher.Options;
|
||
11 years ago
|
import android.annotation.TargetApi;
|
||
10 years ago
|
import android.app.Activity;
|
||
11 years ago
|
import android.content.Intent;
|
||
|
import android.os.Build;
|
||
11 years ago
|
import android.os.Bundle;
|
||
11 years ago
|
import android.view.View;
|
||
11 years ago
|
import android.widget.Toast;
|
||
11 years ago
|
import de.keyboardsurfer.android.widget.crouton.Crouton;
|
||
|
|
||
11 years ago
|
/**
|
||
|
* An activity that holds a single torrents details fragment. It is used on devices (i.e. phones) where there is no room
|
||
|
* to show details in the {@link TorrentsActivity} directly. Task execution, such as loading of more details and
|
||
|
* updating file priorities, is performed in this activity via background methods.
|
||
|
* @author Eric Kok
|
||
|
*/
|
||
11 years ago
|
@EActivity(resName = "activity_details")
|
||
|
@OptionsMenu(resName = "activity_details")
|
||
10 years ago
|
public class DetailsActivity extends Activity implements TorrentTasksExecutor, RefreshableActivity {
|
||
11 years ago
|
|
||
|
@Extra
|
||
|
@InstanceState
|
||
|
protected Torrent torrent;
|
||
11 years ago
|
@Extra
|
||
|
@InstanceState
|
||
|
protected ArrayList<Label> currentLabels;
|
||
11 years ago
|
|
||
|
// Settings
|
||
|
@Bean
|
||
11 years ago
|
protected NavigationHelper navigationHelper;
|
||
|
@Bean
|
||
11 years ago
|
protected ConnectivityHelper connectivityHelper;
|
||
|
@Bean
|
||
11 years ago
|
protected ApplicationSettings applicationSettings;
|
||
|
private IDaemonAdapter currentConnection = null;
|
||
11 years ago
|
private PullToRefreshAttacher pullToRefreshAttacher = null;
|
||
11 years ago
|
|
||
11 years ago
|
// Details view components
|
||
10 years ago
|
@FragmentById(resName = "torrentdetails_fragment")
|
||
11 years ago
|
protected DetailsFragment fragmentDetails;
|
||
11 years ago
|
|
||
11 years ago
|
@Override
|
||
|
public void onCreate(Bundle savedInstanceState) {
|
||
|
// Set the theme according to the user preference
|
||
|
if (SystemSettings_.getInstance_(this).useDarkTheme()) {
|
||
|
setTheme(R.style.TransdroidTheme_Dark);
|
||
10 years ago
|
getActionBar().setIcon(R.drawable.ic_activity_torrents);
|
||
11 years ago
|
}
|
||
|
super.onCreate(savedInstanceState);
|
||
|
}
|
||
|
|
||
11 years ago
|
@AfterViews
|
||
|
protected void init() {
|
||
|
|
||
11 years ago
|
// We require a torrent to be specified; otherwise close the activity
|
||
11 years ago
|
if (torrent == null) {
|
||
11 years ago
|
finish();
|
||
|
return;
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
// Simple action bar with up, torrent name as title and refresh button
|
||
10 years ago
|
getActionBar().setDisplayHomeAsUpEnabled(true);
|
||
|
getActionBar().setTitle(NavigationHelper.buildCondensedFontString(torrent.getName()));
|
||
11 years ago
|
|
||
|
// Connect to the last used server
|
||
|
ServerSetting lastUsed = applicationSettings.getLastUsedServer();
|
||
11 years ago
|
fragmentDetails.setCurrentServerSettings(lastUsed);
|
||
11 years ago
|
currentConnection = lastUsed.createServerAdapter(connectivityHelper.getConnectedNetworkName(), this);
|
||
11 years ago
|
|
||
11 years ago
|
// Show details and load fine stats and torrent files
|
||
|
fragmentDetails.updateTorrent(torrent);
|
||
11 years ago
|
fragmentDetails.updateLabels(currentLabels);
|
||
11 years ago
|
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
11 years ago
|
@Override
|
||
|
protected void onDestroy() {
|
||
|
Crouton.cancelAllCroutons();
|
||
|
super.onDestroy();
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
|
||
|
@OptionsItem(android.R.id.home)
|
||
|
protected void navigateUp() {
|
||
|
TorrentsActivity_.intent(this).flags(Intent.FLAG_ACTIVITY_CLEAR_TOP).start();
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
/**
|
||
|
* Attaches some view (perhaps contained in a fragment) to this activity's pull to refresh support
|
||
|
* @param view The view to attach
|
||
|
*/
|
||
|
@Override
|
||
|
public void addRefreshableView(View view) {
|
||
|
if (pullToRefreshAttacher == null) {
|
||
|
// Still need to initialise the PullToRefreshAttacher
|
||
|
Options options = new PullToRefreshAttacher.Options();
|
||
|
options.headerTransformer = new NoProgressHeaderTransformer();
|
||
|
pullToRefreshAttacher = PullToRefreshAttacher.get(this, options);
|
||
|
}
|
||
|
pullToRefreshAttacher.addRefreshableView(view, new OnRefreshListener() {
|
||
|
@Override
|
||
|
public void onRefreshStarted(View view) {
|
||
|
// Just refresh the full screen, now that the user has pulled to refresh
|
||
|
pullToRefreshAttacher.setRefreshComplete();
|
||
|
refreshScreen();
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
11 years ago
|
@OptionsItem(resName = "action_refresh")
|
||
11 years ago
|
public void refreshScreen() {
|
||
|
fragmentDetails.updateIsLoading(true, null);
|
||
11 years ago
|
refreshTorrent();
|
||
11 years ago
|
refreshTorrentDetails(torrent);
|
||
|
refreshTorrentFiles(torrent);
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
11 years ago
|
@Background
|
||
|
protected void refreshTorrent() {
|
||
|
DaemonTaskResult result = RetrieveTask.create(currentConnection).execute();
|
||
|
if (result instanceof RetrieveTaskSuccessResult) {
|
||
11 years ago
|
onTorrentsRetrieved(((RetrieveTaskSuccessResult) result).getTorrents(),
|
||
|
((RetrieveTaskSuccessResult) result).getLabels());
|
||
11 years ago
|
} else {
|
||
11 years ago
|
onCommunicationError((DaemonTaskFailureResult) result, true);
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
|
@Background
|
||
11 years ago
|
public void refreshTorrentDetails(Torrent torrent) {
|
||
11 years ago
|
if (!Daemon.supportsFineDetails(torrent.getDaemon()))
|
||
|
return;
|
||
11 years ago
|
DaemonTaskResult result = GetTorrentDetailsTask.create(currentConnection, torrent).execute();
|
||
|
if (result instanceof GetTorrentDetailsTaskSuccessResult) {
|
||
11 years ago
|
onTorrentDetailsRetrieved(torrent, ((GetTorrentDetailsTaskSuccessResult) result).getTorrentDetails());
|
||
11 years ago
|
} else {
|
||
11 years ago
|
onCommunicationError((DaemonTaskFailureResult) result, false);
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
|
@Background
|
||
11 years ago
|
public void refreshTorrentFiles(Torrent torrent) {
|
||
11 years ago
|
if (!Daemon.supportsFileListing(torrent.getDaemon()))
|
||
|
return;
|
||
11 years ago
|
DaemonTaskResult result = GetFileListTask.create(currentConnection, torrent).execute();
|
||
|
if (result instanceof GetFileListTaskSuccessResult) {
|
||
11 years ago
|
onTorrentFilesRetrieved(torrent, ((GetFileListTaskSuccessResult) result).getFiles());
|
||
11 years ago
|
} else {
|
||
11 years ago
|
onCommunicationError((DaemonTaskFailureResult) result, false);
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
@Background
|
||
|
@Override
|
||
|
public void resumeTorrent(Torrent torrent) {
|
||
|
torrent.mimicResume();
|
||
|
DaemonTaskResult result = ResumeTask.create(currentConnection, torrent).execute();
|
||
11 years ago
|
if (result instanceof DaemonTaskSuccessResult) {
|
||
11 years ago
|
onTaskSucceeded((DaemonTaskSuccessResult) result, getString(R.string.result_resumed, torrent.getName()));
|
||
11 years ago
|
} else {
|
||
11 years ago
|
onCommunicationError((DaemonTaskFailureResult) result, false);
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
|
@Background
|
||
|
@Override
|
||
|
public void pauseTorrent(Torrent torrent) {
|
||
|
torrent.mimicPause();
|
||
|
DaemonTaskResult result = PauseTask.create(currentConnection, torrent).execute();
|
||
11 years ago
|
if (result instanceof DaemonTaskSuccessResult) {
|
||
11 years ago
|
onTaskSucceeded((DaemonTaskSuccessResult) result, getString(R.string.result_paused, torrent.getName()));
|
||
11 years ago
|
} else {
|
||
11 years ago
|
onCommunicationError((DaemonTaskFailureResult) result, false);
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
|
@Background
|
||
|
@Override
|
||
|
public void startTorrent(Torrent torrent, boolean forced) {
|
||
|
torrent.mimicStart();
|
||
|
DaemonTaskResult result = StartTask.create(currentConnection, torrent, forced).execute();
|
||
11 years ago
|
if (result instanceof DaemonTaskSuccessResult) {
|
||
11 years ago
|
onTaskSucceeded((DaemonTaskSuccessResult) result, getString(R.string.result_started, torrent.getName()));
|
||
11 years ago
|
} else {
|
||
11 years ago
|
onCommunicationError((DaemonTaskFailureResult) result, false);
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
|
@Background
|
||
|
@Override
|
||
|
public void stopTorrent(Torrent torrent) {
|
||
|
torrent.mimicStop();
|
||
|
DaemonTaskResult result = StopTask.create(currentConnection, torrent).execute();
|
||
11 years ago
|
if (result instanceof DaemonTaskSuccessResult) {
|
||
11 years ago
|
onTaskSucceeded((DaemonTaskSuccessResult) result, getString(R.string.result_stopped, torrent.getName()));
|
||
11 years ago
|
} else {
|
||
11 years ago
|
onCommunicationError((DaemonTaskFailureResult) result, false);
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
|
@Background
|
||
|
@Override
|
||
|
public void removeTorrent(Torrent torrent, boolean withData) {
|
||
|
DaemonTaskResult result = RemoveTask.create(currentConnection, torrent, withData).execute();
|
||
11 years ago
|
if (result instanceof DaemonTaskSuccessResult) {
|
||
11 years ago
|
// Close the details activity (as the torrent is now removed)
|
||
|
closeActivity(getString(withData ? R.string.result_removed_with_data : R.string.result_removed,
|
||
|
torrent.getName()));
|
||
11 years ago
|
} else {
|
||
11 years ago
|
onCommunicationError((DaemonTaskFailureResult) result, false);
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
@UiThread
|
||
|
protected void closeActivity(String closeText) {
|
||
11 years ago
|
setResult(RESULT_OK,
|
||
|
new Intent().putExtra("torrent_removed", true).putExtra("affected_torrent", torrent));
|
||
11 years ago
|
finish();
|
||
|
if (closeText != null)
|
||
|
Toast.makeText(this, closeText, Toast.LENGTH_LONG).show();
|
||
|
}
|
||
|
|
||
11 years ago
|
@Background
|
||
|
@Override
|
||
|
public void updateLabel(Torrent torrent, String newLabel) {
|
||
|
torrent.mimicNewLabel(newLabel);
|
||
11 years ago
|
DaemonTaskResult result = SetLabelTask.create(currentConnection, torrent, newLabel == null ? "" : newLabel)
|
||
|
.execute();
|
||
11 years ago
|
if (result instanceof DaemonTaskSuccessResult) {
|
||
11 years ago
|
onTaskSucceeded((DaemonTaskSuccessResult) result, getString(R.string.result_labelset, newLabel));
|
||
11 years ago
|
} else {
|
||
11 years ago
|
onCommunicationError((DaemonTaskFailureResult) result, false);
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
@Background
|
||
|
@Override
|
||
|
public void forceRecheckTorrent(Torrent torrent) {
|
||
|
torrent.mimicCheckingStatus();
|
||
|
DaemonTaskResult result = ForceRecheckTask.create(currentConnection, torrent).execute();
|
||
|
if (result instanceof DaemonTaskSuccessResult) {
|
||
|
onTaskSucceeded((DaemonTaskSuccessResult) result,
|
||
|
getString(R.string.result_recheckedstarted, torrent.getName()));
|
||
|
} else {
|
||
|
onCommunicationError((DaemonTaskFailureResult) result, false);
|
||
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
@Background
|
||
|
@Override
|
||
|
public void updateTrackers(Torrent torrent, List<String> newTrackers) {
|
||
|
DaemonTaskResult result = SetTrackersTask.create(currentConnection, torrent, newTrackers).execute();
|
||
11 years ago
|
if (result instanceof DaemonTaskSuccessResult) {
|
||
11 years ago
|
onTaskSucceeded((DaemonTaskSuccessResult) result, getString(R.string.result_trackersupdated));
|
||
11 years ago
|
} else {
|
||
11 years ago
|
onCommunicationError((DaemonTaskFailureResult) result, false);
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
|
@Background
|
||
|
@Override
|
||
|
public void updateLocation(Torrent torrent, String newLocation) {
|
||
|
DaemonTaskResult result = SetDownloadLocationTask.create(currentConnection, torrent, newLocation).execute();
|
||
11 years ago
|
if (result instanceof DaemonTaskSuccessResult) {
|
||
11 years ago
|
onTaskSucceeded((DaemonTaskSuccessResult) result, getString(R.string.result_locationset, newLocation));
|
||
11 years ago
|
} else {
|
||
11 years ago
|
onCommunicationError((DaemonTaskFailureResult) result, false);
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
@Background
|
||
|
@Override
|
||
|
public void updatePriority(Torrent torrent, List<TorrentFile> files, Priority priority) {
|
||
|
DaemonTaskResult result = SetFilePriorityTask.create(currentConnection, torrent, priority,
|
||
|
new ArrayList<TorrentFile>(files)).execute();
|
||
11 years ago
|
if (result instanceof DaemonTaskSuccessResult) {
|
||
11 years ago
|
onTaskSucceeded((DaemonTaskSuccessResult) result, getString(R.string.result_priotitiesset));
|
||
|
} else {
|
||
11 years ago
|
onCommunicationError((DaemonTaskFailureResult) result, false);
|
||
11 years ago
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
@UiThread
|
||
11 years ago
|
protected void onTaskSucceeded(DaemonTaskSuccessResult result, String successMessage) {
|
||
11 years ago
|
// Set the activity result so the calling activity knows it needs to update its view
|
||
|
setResult(RESULT_OK,
|
||
|
new Intent().putExtra("torrent_updated", true).putExtra("affected_torrent", torrent));
|
||
11 years ago
|
// Refresh the screen as well
|
||
|
refreshTorrent();
|
||
|
refreshTorrentDetails(torrent);
|
||
11 years ago
|
Crouton.showText(this, successMessage, NavigationHelper.CROUTON_INFO_STYLE);
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
11 years ago
|
@UiThread
|
||
|
protected void onTorrentDetailsRetrieved(Torrent torrent, TorrentDetails torrentDetails) {
|
||
|
// Update the details fragment with the new fine details for the shown torrent
|
||
|
fragmentDetails.updateTorrentDetails(torrent, torrentDetails);
|
||
|
}
|
||
|
|
||
|
@UiThread
|
||
|
protected void onTorrentFilesRetrieved(Torrent torrent, List<TorrentFile> torrentFiles) {
|
||
|
// Update the details fragment with the newly retrieved list of files
|
||
|
fragmentDetails.updateTorrentFiles(torrent, new ArrayList<TorrentFile>(torrentFiles));
|
||
|
}
|
||
|
|
||
11 years ago
|
@UiThread
|
||
11 years ago
|
protected void onCommunicationError(DaemonTaskFailureResult result, boolean isCritical) {
|
||
11 years ago
|
Log.i(this, result.getException().toString());
|
||
11 years ago
|
String error = getString(LocalTorrent.getResourceForDaemonException(result.getException()));
|
||
11 years ago
|
fragmentDetails.updateIsLoading(false, isCritical ? error : null);
|
||
11 years ago
|
Crouton.showText(this, getString(LocalTorrent.getResourceForDaemonException(result.getException())),
|
||
11 years ago
|
NavigationHelper.CROUTON_ERROR_STYLE);
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
@UiThread
|
||
|
protected void onTorrentsRetrieved(List<Torrent> torrents, List<org.transdroid.daemon.Label> labels) {
|
||
11 years ago
|
// Update the details fragment accordingly
|
||
11 years ago
|
fragmentDetails.updateIsLoading(false, null);
|
||
11 years ago
|
fragmentDetails.perhapsUpdateTorrent(torrents);
|
||
11 years ago
|
fragmentDetails.updateLabels(Label.convertToNavigationLabels(labels,
|
||
|
getResources().getString(R.string.labels_unlabeled)));
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
}
|