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.
533 lines
20 KiB
533 lines
20 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;
|
||
11 years ago
|
import java.util.Collections;
|
||
11 years ago
|
import java.util.List;
|
||
|
|
||
|
import org.androidannotations.annotations.AfterViews;
|
||
11 years ago
|
import org.androidannotations.annotations.Click;
|
||
11 years ago
|
import org.androidannotations.annotations.EFragment;
|
||
|
import org.androidannotations.annotations.InstanceState;
|
||
11 years ago
|
import org.androidannotations.annotations.ItemClick;
|
||
11 years ago
|
import org.androidannotations.annotations.OptionsItem;
|
||
|
import org.androidannotations.annotations.OptionsMenu;
|
||
11 years ago
|
import org.androidannotations.annotations.ViewById;
|
||
10 years ago
|
import org.transdroid.R;
|
||
11 years ago
|
import org.transdroid.core.app.settings.ServerSetting;
|
||
11 years ago
|
import org.transdroid.core.app.settings.SystemSettings_;
|
||
11 years ago
|
import org.transdroid.core.gui.lists.DetailsAdapter;
|
||
|
import org.transdroid.core.gui.lists.SimpleListItemAdapter;
|
||
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.NavigationHelper_;
|
||
11 years ago
|
import org.transdroid.core.gui.navigation.RefreshableActivity;
|
||
11 years ago
|
import org.transdroid.core.gui.navigation.SelectionManagerMode;
|
||
11 years ago
|
import org.transdroid.core.gui.navigation.SetLabelDialog;
|
||
|
import org.transdroid.core.gui.navigation.SetLabelDialog.OnLabelPickedListener;
|
||
11 years ago
|
import org.transdroid.core.gui.navigation.SetStorageLocationDialog;
|
||
|
import org.transdroid.core.gui.navigation.SetStorageLocationDialog.OnStorageLocationUpdatedListener;
|
||
11 years ago
|
import org.transdroid.core.gui.navigation.SetTrackersDialog;
|
||
|
import org.transdroid.core.gui.navigation.SetTrackersDialog.OnTrackersUpdatedListener;
|
||
11 years ago
|
import org.transdroid.daemon.Daemon;
|
||
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;
|
||
|
|
||
11 years ago
|
import android.annotation.SuppressLint;
|
||
10 years ago
|
import android.app.Fragment;
|
||
10 years ago
|
import android.content.ClipData;
|
||
|
import android.content.ClipboardManager;
|
||
|
import android.content.Context;
|
||
11 years ago
|
import android.content.Intent;
|
||
|
import android.net.Uri;
|
||
10 years ago
|
import android.view.ActionMode;
|
||
|
import android.view.Menu;
|
||
|
import android.view.MenuItem;
|
||
11 years ago
|
import android.view.View;
|
||
10 years ago
|
import android.widget.AbsListView.MultiChoiceModeListener;
|
||
|
import android.widget.ListView;
|
||
11 years ago
|
import android.widget.ProgressBar;
|
||
11 years ago
|
import android.widget.TextView;
|
||
11 years ago
|
import de.keyboardsurfer.android.widget.crouton.Crouton;
|
||
11 years ago
|
|
||
|
/**
|
||
11 years ago
|
* Fragment that shows detailed statistics about some torrent. These come from some already fetched {@link Torrent}
|
||
11 years ago
|
* object, but it also retrieves further detailed statistics. The actual execution of tasks is performed by the activity
|
||
|
* that contains this fragment, as per the {@link TorrentTasksExecutor} interface.
|
||
11 years ago
|
* @author Eric Kok
|
||
|
*/
|
||
11 years ago
|
@EFragment(resName = "fragment_details")
|
||
|
@OptionsMenu(resName = "fragment_details")
|
||
10 years ago
|
public class DetailsFragment extends Fragment implements OnTrackersUpdatedListener, OnLabelPickedListener,
|
||
11 years ago
|
OnStorageLocationUpdatedListener {
|
||
11 years ago
|
|
||
|
// Local data
|
||
|
@InstanceState
|
||
|
protected Torrent torrent = null;
|
||
|
@InstanceState
|
||
|
protected TorrentDetails torrentDetails = null;
|
||
|
@InstanceState
|
||
|
protected ArrayList<TorrentFile> torrentFiles = null;
|
||
11 years ago
|
@InstanceState
|
||
11 years ago
|
protected ArrayList<Label> currentLabels = null;
|
||
|
@InstanceState
|
||
11 years ago
|
protected boolean isLoadingTorrent = false;
|
||
11 years ago
|
@InstanceState
|
||
|
protected boolean hasCriticalError = false;
|
||
11 years ago
|
private ServerSetting currentServerSettings = null;
|
||
11 years ago
|
|
||
|
// Views
|
||
11 years ago
|
@ViewById(resName = "details_container")
|
||
|
protected View detailsContainer;
|
||
11 years ago
|
@ViewById(resName = "details_list")
|
||
10 years ago
|
protected ListView detailsList;
|
||
11 years ago
|
@ViewById
|
||
11 years ago
|
protected TextView emptyText, errorText;
|
||
11 years ago
|
@ViewById
|
||
|
protected ProgressBar loadingProgress;
|
||
11 years ago
|
|
||
|
@AfterViews
|
||
|
protected void init() {
|
||
|
|
||
11 years ago
|
// On large screens where this fragment is shown next to the torrents list, we show a continues grey vertical
|
||
|
// line to separate the lists visually
|
||
|
if (!NavigationHelper_.getInstance_(getActivity()).isSmallScreen()) {
|
||
|
if (SystemSettings_.getInstance_(getActivity()).useDarkTheme()) {
|
||
11 years ago
|
detailsContainer.setBackgroundResource(R.drawable.details_list_background_dark);
|
||
11 years ago
|
} else {
|
||
11 years ago
|
detailsContainer.setBackgroundResource(R.drawable.details_list_background_light);
|
||
11 years ago
|
}
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
// Set up details adapter (itself containing the actual lists to show), which allows multi-select and fast
|
||
|
// scrolling
|
||
11 years ago
|
detailsList.setAdapter(new DetailsAdapter(getActivity()));
|
||
11 years ago
|
detailsList.setMultiChoiceModeListener(onDetailsSelected);
|
||
11 years ago
|
detailsList.setFastScrollEnabled(true);
|
||
11 years ago
|
if (getActivity() != null && getActivity() instanceof RefreshableActivity) {
|
||
|
((RefreshableActivity) getActivity()).addRefreshableView(detailsList);
|
||
|
}
|
||
|
|
||
|
// Restore the fragment state (on orientation changes et al.)
|
||
11 years ago
|
if (torrent != null)
|
||
|
updateTorrent(torrent);
|
||
|
if (torrentDetails != null)
|
||
11 years ago
|
updateTorrentDetails(torrent, torrentDetails);
|
||
11 years ago
|
if (torrentFiles != null)
|
||
11 years ago
|
updateTorrentFiles(torrent, torrentFiles);
|
||
11 years ago
|
|
||
|
}
|
||
|
|
||
11 years ago
|
public void setCurrentServerSettings(ServerSetting serverSettings) {
|
||
|
currentServerSettings = serverSettings;
|
||
|
}
|
||
|
|
||
11 years ago
|
/**
|
||
11 years ago
|
* Updates the details adapter header to show the new torrent data.
|
||
11 years ago
|
* @param newTorrent The new, non-null torrent object
|
||
11 years ago
|
*/
|
||
|
public void updateTorrent(Torrent newTorrent) {
|
||
|
this.torrent = newTorrent;
|
||
11 years ago
|
this.hasCriticalError = false;
|
||
11 years ago
|
((DetailsAdapter) detailsList.getAdapter()).updateTorrent(newTorrent);
|
||
11 years ago
|
// Make the list (with details header) visible
|
||
11 years ago
|
detailsList.setVisibility(View.VISIBLE);
|
||
11 years ago
|
emptyText.setVisibility(View.GONE);
|
||
11 years ago
|
errorText.setVisibility(View.GONE);
|
||
11 years ago
|
loadingProgress.setVisibility(View.GONE);
|
||
11 years ago
|
// Also update the available actions in the action bar
|
||
10 years ago
|
getActivity().invalidateOptionsMenu();
|
||
11 years ago
|
// Refresh the detailed statistics (errors) and list of files
|
||
11 years ago
|
torrentDetails = null;
|
||
|
torrentFiles = null;
|
||
11 years ago
|
getTasksExecutor().refreshTorrentDetails(torrent);
|
||
|
getTasksExecutor().refreshTorrentFiles(torrent);
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* Updates the details adapter to show the list of trackers and tracker errors.
|
||
|
* @param checkTorrent The torrent for which the details were retrieved
|
||
11 years ago
|
* @param newTorrentDetails The new fine details object of some torrent
|
||
|
*/
|
||
11 years ago
|
public void updateTorrentDetails(Torrent checkTorrent, TorrentDetails newTorrentDetails) {
|
||
|
// Check if these are actually the details of the torrent we are now showing
|
||
|
if (!torrent.getUniqueID().equals(checkTorrent.getUniqueID()))
|
||
|
return;
|
||
11 years ago
|
this.torrentDetails = newTorrentDetails;
|
||
|
((DetailsAdapter) detailsList.getAdapter()).updateTrackers(SimpleListItemAdapter.SimpleStringItem
|
||
|
.wrapStringsList(newTorrentDetails.getTrackers()));
|
||
|
((DetailsAdapter) detailsList.getAdapter()).updateErrors(SimpleListItemAdapter.SimpleStringItem
|
||
|
.wrapStringsList(newTorrentDetails.getErrors()));
|
||
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* Updates the list adapter to show a new list of torrent files, replacing the old files list.
|
||
|
* @param checkTorrent The torrent for which the details were retrieved
|
||
11 years ago
|
* @param newTorrents The new, updated list of torrent file objects
|
||
|
*/
|
||
11 years ago
|
public void updateTorrentFiles(Torrent checkTorrent, ArrayList<TorrentFile> newTorrentFiles) {
|
||
|
// Check if these are actually the details of the torrent we are now showing
|
||
|
if (!torrent.getUniqueID().equals(checkTorrent.getUniqueID()))
|
||
|
return;
|
||
11 years ago
|
Collections.sort(newTorrentFiles);
|
||
11 years ago
|
this.torrentFiles = newTorrentFiles;
|
||
|
((DetailsAdapter) detailsList.getAdapter()).updateTorrentFiles(newTorrentFiles);
|
||
|
}
|
||
|
|
||
|
/**
|
||
11 years ago
|
* Can be called if some outside activity returned new torrents, so we can perhaps piggyback on this by update our
|
||
11 years ago
|
* data as well.
|
||
11 years ago
|
* @param torrents The last of retrieved torrents
|
||
|
*/
|
||
|
public void perhapsUpdateTorrent(List<Torrent> torrents) {
|
||
11 years ago
|
// Only try to update if we actually were showing a torrent
|
||
|
if (this.torrent == null || torrents == null)
|
||
|
return;
|
||
11 years ago
|
for (Torrent newTorrent : torrents) {
|
||
|
if (newTorrent.getUniqueID().equals(this.torrent.getUniqueID())) {
|
||
|
// Found, so we can update our data as well
|
||
|
updateTorrent(newTorrent);
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Updates the locally maintained list of labels that are active on the server. Used in the label picking dialog and
|
||
|
* should be updated every time after the list of torrents was retrieved to keep it updated.
|
||
|
* @param currentLabels The list of known server labels
|
||
|
*/
|
||
|
public void updateLabels(ArrayList<Label> currentLabels) {
|
||
11 years ago
|
this.currentLabels = currentLabels == null ? null : new ArrayList<Label>(currentLabels);
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Clear the screen by fully clearing the internal merge list (with header and other lists)
|
||
|
*/
|
||
|
public void clear() {
|
||
11 years ago
|
detailsList.setAdapter(new DetailsAdapter(getActivity()));
|
||
11 years ago
|
detailsList.setVisibility(View.GONE);
|
||
11 years ago
|
emptyText.setVisibility(!isLoadingTorrent && !hasCriticalError ? View.VISIBLE : View.GONE);
|
||
|
errorText.setVisibility(!isLoadingTorrent && hasCriticalError ? View.VISIBLE : View.GONE);
|
||
11 years ago
|
loadingProgress.setVisibility(isLoadingTorrent ? View.VISIBLE : View.GONE);
|
||
11 years ago
|
// Note: this.torrent is not cleared as we need to know later what the fragment was originally bound to
|
||
11 years ago
|
torrentDetails = null;
|
||
|
torrentFiles = null;
|
||
|
}
|
||
11 years ago
|
|
||
|
/**
|
||
|
* Updates the shown screen depending on whether the torrent is loading
|
||
|
* @param isLoading True if the torrent is (re)loading, false otherwise
|
||
11 years ago
|
* @param connectionErrorMessage The error message text to show to the user, or null if there was no error
|
||
11 years ago
|
*/
|
||
11 years ago
|
public void updateIsLoading(boolean isLoading, String connectionErrorMessage) {
|
||
11 years ago
|
this.isLoadingTorrent = isLoading;
|
||
11 years ago
|
this.hasCriticalError = connectionErrorMessage != null;
|
||
|
errorText.setText(connectionErrorMessage);
|
||
|
if (isLoading || hasCriticalError)
|
||
11 years ago
|
clear();
|
||
|
}
|
||
|
|
||
11 years ago
|
@ItemClick(resName = "details_list")
|
||
|
protected void detailsListClicked(int position) {
|
||
|
detailsList.setItemChecked(position, false);
|
||
|
}
|
||
10 years ago
|
|
||
11 years ago
|
@Override
|
||
|
public void onPrepareOptionsMenu(Menu menu) {
|
||
|
super.onPrepareOptionsMenu(menu);
|
||
11 years ago
|
|
||
11 years ago
|
if (torrent == null) {
|
||
|
menu.findItem(R.id.action_resume).setVisible(false);
|
||
|
menu.findItem(R.id.action_pause).setVisible(false);
|
||
|
menu.findItem(R.id.action_start).setVisible(false);
|
||
|
menu.findItem(R.id.action_stop).setVisible(false);
|
||
|
menu.findItem(R.id.action_remove).setVisible(false);
|
||
|
menu.findItem(R.id.action_remove_withdata).setVisible(false);
|
||
|
menu.findItem(R.id.action_setlabel).setVisible(false);
|
||
|
menu.findItem(R.id.action_updatetrackers).setVisible(false);
|
||
11 years ago
|
menu.findItem(R.id.action_changelocation).setVisible(false);
|
||
11 years ago
|
return;
|
||
|
}
|
||
11 years ago
|
// Update action availability
|
||
|
boolean startStop = Daemon.supportsStoppingStarting(torrent.getDaemon());
|
||
|
menu.findItem(R.id.action_resume).setVisible(torrent.canResume());
|
||
|
menu.findItem(R.id.action_pause).setVisible(torrent.canPause());
|
||
|
menu.findItem(R.id.action_start).setVisible(startStop && torrent.canStart());
|
||
|
menu.findItem(R.id.action_stop).setVisible(startStop && torrent.canStop());
|
||
11 years ago
|
menu.findItem(R.id.action_remove).setVisible(true);
|
||
11 years ago
|
boolean removeWithData = Daemon.supportsRemoveWithData(torrent.getDaemon());
|
||
|
menu.findItem(R.id.action_remove_withdata).setVisible(removeWithData);
|
||
|
boolean setLabel = Daemon.supportsSetLabel(torrent.getDaemon());
|
||
|
menu.findItem(R.id.action_setlabel).setVisible(setLabel);
|
||
|
boolean setTrackers = Daemon.supportsSetTrackers(torrent.getDaemon());
|
||
|
menu.findItem(R.id.action_updatetrackers).setVisible(setTrackers);
|
||
11 years ago
|
boolean setLocation = Daemon.supportsSetDownloadLocation(torrent.getDaemon());
|
||
|
menu.findItem(R.id.action_changelocation).setVisible(setLocation);
|
||
11 years ago
|
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
11 years ago
|
@OptionsItem(resName = "action_resume")
|
||
11 years ago
|
protected void resumeTorrent() {
|
||
|
getTasksExecutor().resumeTorrent(torrent);
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
11 years ago
|
@OptionsItem(resName = "action_pause")
|
||
11 years ago
|
protected void pauseTorrent() {
|
||
|
getTasksExecutor().pauseTorrent(torrent);
|
||
|
}
|
||
|
|
||
11 years ago
|
@OptionsItem(resName = "action_start_default")
|
||
11 years ago
|
protected void startTorrentDefault() {
|
||
|
getTasksExecutor().startTorrent(torrent, false);
|
||
|
}
|
||
|
|
||
11 years ago
|
@OptionsItem(resName = "action_start_forced")
|
||
11 years ago
|
protected void startTorrentForced() {
|
||
|
getTasksExecutor().startTorrent(torrent, true);
|
||
|
}
|
||
|
|
||
11 years ago
|
@OptionsItem(resName = "action_stop")
|
||
11 years ago
|
protected void stopTorrent() {
|
||
|
getTasksExecutor().stopTorrent(torrent);
|
||
|
}
|
||
|
|
||
11 years ago
|
@OptionsItem(resName = "action_remove_default")
|
||
11 years ago
|
protected void removeTorrentDefault() {
|
||
|
getTasksExecutor().removeTorrent(torrent, false);
|
||
|
}
|
||
|
|
||
11 years ago
|
@OptionsItem(resName = "action_remove_withdata")
|
||
11 years ago
|
protected void removeTorrentWithData() {
|
||
|
getTasksExecutor().removeTorrent(torrent, true);
|
||
|
}
|
||
|
|
||
11 years ago
|
@OptionsItem(resName = "action_setlabel")
|
||
11 years ago
|
protected void setLabel() {
|
||
10 years ago
|
if (currentLabels != null)
|
||
10 years ago
|
new SetLabelDialog().setOnLabelPickedListener(this).setCurrentLabels(currentLabels)
|
||
|
.show(getFragmentManager(), "SetLabelDialog");
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
@OptionsItem(resName = "action_forcerecheck")
|
||
|
protected void setForceRecheck() {
|
||
|
getTasksExecutor().forceRecheckTorrent(torrent);
|
||
|
}
|
||
|
|
||
11 years ago
|
@OptionsItem(resName = "action_updatetrackers")
|
||
11 years ago
|
protected void updateTrackers() {
|
||
11 years ago
|
if (torrentDetails == null) {
|
||
|
Crouton.showText(getActivity(), R.string.error_stillloadingdetails, NavigationHelper.CROUTON_INFO_STYLE);
|
||
|
return;
|
||
|
}
|
||
11 years ago
|
new SetTrackersDialog().setOnTrackersUpdated(this).setCurrentTrackers(torrentDetails.getTrackersText())
|
||
11 years ago
|
.show(getFragmentManager(), "SetTrackersDialog");
|
||
|
}
|
||
|
|
||
11 years ago
|
@OptionsItem(resName = "action_changelocation")
|
||
|
protected void changeStorageLocation() {
|
||
|
new SetStorageLocationDialog().setOnStorageLocationUpdated(this).setCurrentLocation(torrent.getLocationDir())
|
||
|
.show(getFragmentManager(), "SetStorageLocationDialog");
|
||
|
}
|
||
|
|
||
11 years ago
|
@Override
|
||
|
public void onLabelPicked(String newLabel) {
|
||
|
getTasksExecutor().updateLabel(torrent, newLabel);
|
||
11 years ago
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onTrackersUpdated(List<String> updatedTrackers) {
|
||
|
getTasksExecutor().updateTrackers(torrent, updatedTrackers);
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
@Override
|
||
|
public void onStorageLocationUpdated(String newLocation) {
|
||
|
getTasksExecutor().updateLocation(torrent, newLocation);
|
||
|
}
|
||
|
|
||
11 years ago
|
@Click
|
||
|
protected void emptyTextClicked() {
|
||
|
// Refresh the activity (that contains this fragment) when the empty view gear is clicked
|
||
|
if (getActivity() != null && getActivity() instanceof RefreshableActivity) {
|
||
|
((RefreshableActivity) getActivity()).refreshScreen();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@Click
|
||
|
protected void errorTextClicked() {
|
||
|
// Refresh the activity (that contains this fragment) when the error view gear is clicked
|
||
|
if (getActivity() != null && getActivity() instanceof RefreshableActivity) {
|
||
|
((RefreshableActivity) getActivity()).refreshScreen();
|
||
|
}
|
||
|
}
|
||
|
|
||
10 years ago
|
private MultiChoiceModeListener onDetailsSelected = new MultiChoiceModeListener() {
|
||
11 years ago
|
|
||
11 years ago
|
SelectionManagerMode selectionManagerMode;
|
||
11 years ago
|
|
||
11 years ago
|
@Override
|
||
|
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
|
||
|
// Show contextual action bar to start/stop/remove/etc. torrents in batch mode
|
||
11 years ago
|
mode.getMenuInflater().inflate(R.menu.fragment_details_cab, menu);
|
||
11 years ago
|
selectionManagerMode = new SelectionManagerMode(detailsList, R.plurals.navigation_filesselected);
|
||
|
selectionManagerMode.setOnlyCheckClass(TorrentFile.class);
|
||
|
selectionManagerMode.onCreateActionMode(mode, menu);
|
||
11 years ago
|
return true;
|
||
|
}
|
||
|
|
||
11 years ago
|
@Override
|
||
|
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
|
||
10 years ago
|
// Pause autorefresh
|
||
|
if (getActivity() != null && getActivity() instanceof TorrentsActivity) {
|
||
|
((TorrentsActivity) getActivity()).stopRefresh = true;
|
||
|
((TorrentsActivity) getActivity()).stopAutoRefresh();
|
||
|
}
|
||
10 years ago
|
menu.findItem(R.id.action_download).setVisible(
|
||
|
currentServerSettings != null && Daemon.supportsFilePaths(currentServerSettings.getType()));
|
||
11 years ago
|
return selectionManagerMode.onPrepareActionMode(mode, menu);
|
||
|
}
|
||
|
|
||
11 years ago
|
@SuppressLint("SdCardPath")
|
||
11 years ago
|
@Override
|
||
|
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
|
||
|
|
||
|
// Get checked torrents
|
||
|
List<TorrentFile> checked = new ArrayList<TorrentFile>();
|
||
|
for (int i = 0; i < detailsList.getCheckedItemPositions().size(); i++) {
|
||
|
if (detailsList.getCheckedItemPositions().valueAt(i)
|
||
11 years ago
|
&& i < detailsList.getAdapter().getCount()
|
||
11 years ago
|
&& detailsList.getAdapter().getItem(detailsList.getCheckedItemPositions().keyAt(i)) instanceof TorrentFile)
|
||
|
checked.add((TorrentFile) detailsList.getAdapter().getItem(
|
||
|
detailsList.getCheckedItemPositions().keyAt(i)));
|
||
|
}
|
||
|
|
||
|
int itemId = item.getItemId();
|
||
|
if (itemId == R.id.action_download) {
|
||
11 years ago
|
|
||
|
if (checked.size() < 1 || currentServerSettings == null)
|
||
|
return true;
|
||
|
String urlBase = currentServerSettings.getFtpUrl();
|
||
|
if (urlBase == null || urlBase.equals(""))
|
||
|
urlBase = "ftp://" + currentServerSettings.getAddress();
|
||
|
|
||
|
// Try using AndFTP intents
|
||
|
Intent andftpStart = new Intent(Intent.ACTION_PICK);
|
||
|
andftpStart.setDataAndType(Uri.parse(urlBase), "vnd.android.cursor.dir/lysesoft.andftp.uri");
|
||
|
andftpStart.putExtra("command_type", "download");
|
||
|
andftpStart.putExtra("ftp_pasv", "true");
|
||
|
if (Uri.parse(urlBase).getUserInfo() != null)
|
||
|
andftpStart.putExtra("ftp_username", Uri.parse(urlBase).getUserInfo());
|
||
|
else
|
||
|
andftpStart.putExtra("ftp_username", currentServerSettings.getUsername());
|
||
|
if (currentServerSettings.getFtpPassword() != null
|
||
|
&& !currentServerSettings.getFtpPassword().equals("")) {
|
||
|
andftpStart.putExtra("ftp_password", currentServerSettings.getFtpPassword());
|
||
|
} else {
|
||
|
andftpStart.putExtra("ftp_password", currentServerSettings.getPassword());
|
||
|
}
|
||
|
// Note: AndFTP doesn't understand the directory that Environment.getExternalStoragePublicDirectory()
|
||
|
// uses :(
|
||
|
andftpStart.putExtra("local_folder", "/sdcard/Download");
|
||
|
for (int f = 0; f < checked.size(); f++) {
|
||
|
String file = checked.get(f).getRelativePath();
|
||
|
// If the file is directly in the root, AndFTP fails if we supply the proper path (like /file.pdf)
|
||
|
// Work around this bug by removing the leading / if no further directories are used in the path
|
||
|
if (file.startsWith("/") && file.indexOf("/", 1) < 0)
|
||
|
file = file.substring(1);
|
||
|
andftpStart.putExtra("remote_file" + (f + 1), file);
|
||
|
}
|
||
|
if (andftpStart.resolveActivity(getActivity().getPackageManager()) != null) {
|
||
|
startActivity(andftpStart);
|
||
|
mode.finish();
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
// Try using a VIEW intent given an ftp:// scheme URI
|
||
|
String url = urlBase + checked.get(0).getFullPath();
|
||
|
Intent simpleStart = new Intent(Intent.ACTION_VIEW, Uri.parse(url))
|
||
|
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||
|
if (simpleStart.resolveActivity(getActivity().getPackageManager()) != null) {
|
||
|
startActivity(simpleStart);
|
||
|
mode.finish();
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
// No app is available that can handle FTP downloads
|
||
|
Crouton.showText(getActivity(), getString(R.string.error_noftpapp, url),
|
||
|
NavigationHelper.CROUTON_ERROR_STYLE);
|
||
11 years ago
|
mode.finish();
|
||
|
return true;
|
||
11 years ago
|
|
||
10 years ago
|
} else if (itemId == R.id.action_copytoclipboard) {
|
||
|
|
||
|
StringBuilder names = new StringBuilder();
|
||
|
for (int f = 0; f < checked.size(); f++) {
|
||
|
if (f != 0)
|
||
|
names.append("\n");
|
||
|
names.append(checked.get(f).getName());
|
||
|
}
|
||
|
ClipboardManager clipboardManager = (ClipboardManager) getActivity().getSystemService(
|
||
|
Context.CLIPBOARD_SERVICE);
|
||
|
clipboardManager.setPrimaryClip(ClipData.newPlainText("Transdroid", names.toString()));
|
||
|
mode.finish();
|
||
|
return true;
|
||
|
|
||
11 years ago
|
} else {
|
||
11 years ago
|
Priority priority = Priority.Off;
|
||
11 years ago
|
if (itemId == R.id.action_priority_low)
|
||
|
priority = Priority.Low;
|
||
11 years ago
|
if (itemId == R.id.action_priority_normal)
|
||
|
priority = Priority.Normal;
|
||
|
if (itemId == R.id.action_priority_high)
|
||
|
priority = Priority.High;
|
||
11 years ago
|
getTasksExecutor().updatePriority(torrent, checked, priority);
|
||
|
mode.finish();
|
||
|
return true;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
|
||
11 years ago
|
selectionManagerMode.onItemCheckedStateChanged(mode, position, id, checked);
|
||
11 years ago
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onDestroyActionMode(ActionMode mode) {
|
||
10 years ago
|
// Resume autorefresh
|
||
|
if (getActivity() != null && getActivity() instanceof TorrentsActivity) {
|
||
|
((TorrentsActivity) getActivity()).stopRefresh = false;
|
||
|
((TorrentsActivity) getActivity()).startAutoRefresh();
|
||
|
}
|
||
11 years ago
|
selectionManagerMode.onDestroyActionMode(mode);
|
||
11 years ago
|
}
|
||
|
|
||
|
};
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Returns the object responsible for executing torrent tasks against a connected server
|
||
|
* @return The executor for tasks on some torrent
|
||
|
*/
|
||
|
private TorrentTasksExecutor getTasksExecutor() {
|
||
|
// NOTE: Assumes the activity implements all the required torrent tasks
|
||
|
return (TorrentTasksExecutor) getActivity();
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
11 years ago
|
}
|