Eric Kok
11 years ago
26 changed files with 592 additions and 114 deletions
@ -0,0 +1,66 @@
@@ -0,0 +1,66 @@
|
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<!-- |
||||
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/>. |
||||
--> |
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
android:id="@+id/widget_line_layout" |
||||
android:layout_width="fill_parent" |
||||
android:layout_height="wrap_content" |
||||
android:background="@drawable/selectable_background_transdroid" |
||||
android:paddingRight="@dimen/margin_default" > |
||||
|
||||
<View |
||||
android:id="@+id/status_view" |
||||
android:layout_height="match_parent" |
||||
android:layout_width="6dp" /> |
||||
|
||||
<TextView |
||||
android:id="@+id/name_text" |
||||
android:layout_width="fill_parent" |
||||
android:layout_height="wrap_content" |
||||
android:layout_marginLeft="@dimen/margin_torrentlistleft" |
||||
android:fontFamily="sans-serif-condensed" |
||||
android:paddingTop="@dimen/margin_half" |
||||
android:textColor="@color/text_bright_dark" |
||||
android:textIsSelectable="false" |
||||
android:maxLines="1" |
||||
android:ellipsize="end" |
||||
android:textSize="@dimen/text_enlarged" /> |
||||
|
||||
<TextView |
||||
android:id="@+id/ratio_text" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:layout_alignParentRight="true" |
||||
android:layout_below="@id/name_text" |
||||
android:layout_marginLeft="@dimen/margin_half" |
||||
android:layout_marginTop="4.0dip" |
||||
android:paddingBottom="@dimen/margin_half" |
||||
android:textIsSelectable="false" |
||||
android:textSize="@dimen/text_small" /> |
||||
|
||||
<TextView |
||||
android:id="@+id/progress_text" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:layout_alignBaseline="@id/ratio_text" |
||||
android:layout_alignParentLeft="true" |
||||
android:layout_marginLeft="@dimen/margin_torrentlistleft" |
||||
android:layout_toLeftOf="@id/priority_image" |
||||
android:textIsSelectable="false" |
||||
android:textSize="@dimen/text_small" /> |
||||
|
||||
</RelativeLayout> |
@ -0,0 +1,66 @@
@@ -0,0 +1,66 @@
|
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<!-- |
||||
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/>. |
||||
--> |
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
android:id="@+id/widget_line_layout" |
||||
android:layout_width="fill_parent" |
||||
android:layout_height="wrap_content" |
||||
android:background="@drawable/selectable_background_transdroid2" |
||||
android:paddingRight="@dimen/margin_default" > |
||||
|
||||
<View |
||||
android:id="@+id/status_view" |
||||
android:layout_height="match_parent" |
||||
android:layout_width="6dp" /> |
||||
|
||||
<TextView |
||||
android:id="@+id/name_text" |
||||
android:layout_width="fill_parent" |
||||
android:layout_height="wrap_content" |
||||
android:layout_marginLeft="@dimen/margin_torrentlistleft" |
||||
android:fontFamily="sans-serif-condensed" |
||||
android:paddingTop="@dimen/margin_half" |
||||
android:textColor="@color/text_bright_light" |
||||
android:textIsSelectable="false" |
||||
android:maxLines="1" |
||||
android:ellipsize="end" |
||||
android:textSize="@dimen/text_enlarged" /> |
||||
|
||||
<TextView |
||||
android:id="@+id/ratio_text" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:layout_alignParentRight="true" |
||||
android:layout_below="@id/name_text" |
||||
android:layout_marginLeft="@dimen/margin_half" |
||||
android:layout_marginTop="4.0dip" |
||||
android:paddingBottom="@dimen/margin_half" |
||||
android:textIsSelectable="false" |
||||
android:textSize="@dimen/text_small" /> |
||||
|
||||
<TextView |
||||
android:id="@+id/progress_text" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:layout_alignBaseline="@id/ratio_text" |
||||
android:layout_alignParentLeft="true" |
||||
android:layout_marginLeft="@dimen/margin_torrentlistleft" |
||||
android:layout_toLeftOf="@id/priority_image" |
||||
android:textIsSelectable="false" |
||||
android:textSize="@dimen/text_small" /> |
||||
|
||||
</RelativeLayout> |
@ -1,40 +0,0 @@
@@ -1,40 +0,0 @@
|
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<!-- |
||||
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/>. |
||||
--> |
||||
<FrameLayout |
||||
xmlns:android="http://schemas.android.com/apk/res/android" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="?attr/actionBarSize"> |
||||
|
||||
<!-- This is a wrapper to be able to set the size of the progress item (since the first FrameLayout will be merged) --> |
||||
<FrameLayout |
||||
android:layout_width="26dip" |
||||
android:layout_height="match_parent" |
||||
android:paddingLeft="3dip"> |
||||
|
||||
<ProgressBar |
||||
android:id="@+id/progress" |
||||
android:layout_width="20dip" |
||||
android:layout_height="20dip" |
||||
android:layout_gravity="center_vertical" |
||||
style="@android:style/Widget.ProgressBar.Small.Inverse" |
||||
android:indeterminate="true" |
||||
android:indeterminateOnly="true" /> |
||||
|
||||
</FrameLayout> |
||||
|
||||
</FrameLayout> |
@ -0,0 +1,198 @@
@@ -0,0 +1,198 @@
|
||||
/* |
||||
* 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/>.
|
||||
*/ |
||||
package org.transdroid.core.widget; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
|
||||
import org.androidannotations.annotations.EService; |
||||
import org.transdroid.core.R; |
||||
import org.transdroid.core.app.settings.*; |
||||
import org.transdroid.core.app.settings.ServerSetting; |
||||
import org.transdroid.core.gui.lists.LocalTorrent; |
||||
import org.transdroid.core.gui.log.Log; |
||||
import org.transdroid.core.service.*; |
||||
import org.transdroid.daemon.Daemon; |
||||
import org.transdroid.daemon.IDaemonAdapter; |
||||
import org.transdroid.daemon.Torrent; |
||||
import org.transdroid.daemon.TorrentsComparator; |
||||
import org.transdroid.daemon.task.DaemonTaskResult; |
||||
import org.transdroid.daemon.task.RetrieveTask; |
||||
import org.transdroid.daemon.task.RetrieveTaskSuccessResult; |
||||
|
||||
import android.annotation.TargetApi; |
||||
import android.appwidget.AppWidgetManager; |
||||
import android.content.Context; |
||||
import android.content.Intent; |
||||
import android.content.res.Resources; |
||||
import android.os.Build; |
||||
import android.widget.RemoteViews; |
||||
import android.widget.RemoteViewsService; |
||||
|
||||
@TargetApi(Build.VERSION_CODES.HONEYCOMB) |
||||
@EService |
||||
public class WidgetService extends RemoteViewsService { |
||||
|
||||
@Override |
||||
public RemoteViewsFactory onGetViewFactory(Intent intent) { |
||||
return new WidgetViewsFactory(this.getApplicationContext(), intent); |
||||
} |
||||
|
||||
} |
||||
|
||||
@TargetApi(Build.VERSION_CODES.HONEYCOMB) |
||||
class WidgetViewsFactory implements RemoteViewsService.RemoteViewsFactory { |
||||
|
||||
private final Context context; |
||||
private final int appWidgetId; |
||||
private List<Torrent> torrents = null; |
||||
private WidgetConfig config = null; |
||||
|
||||
public WidgetViewsFactory(Context applicationContext, Intent intent) { |
||||
this.context = applicationContext; |
||||
this.appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, |
||||
AppWidgetManager.INVALID_APPWIDGET_ID); |
||||
} |
||||
|
||||
@Override |
||||
public void onCreate() { |
||||
// Nothing to do here (wait for onDataSetChanged)
|
||||
} |
||||
|
||||
@Override |
||||
public void onDataSetChanged() { |
||||
|
||||
// Load the widget settings
|
||||
ApplicationSettings settings = ApplicationSettings_.getInstance_(context); |
||||
config = settings.getWidgetConfig(appWidgetId); |
||||
if (config == null || config.getServerId() < 0) { |
||||
Log.e(context, "Looking for widget data while the widget wasn't yet configured"); |
||||
return; |
||||
} |
||||
ServerSetting server = settings.getServerSetting(config.getServerId()); |
||||
if (server == null) { |
||||
// TODO: Show error text some how in the remote view, perhaps via the EmptyView's text?
|
||||
Log.e(context, "The server for which this widget was created no longer exists"); |
||||
if (torrents != null) |
||||
torrents.clear(); |
||||
return; |
||||
} |
||||
|
||||
// Load the torrents; synchronously
|
||||
IDaemonAdapter connection = server.createServerAdapter(ConnectivityHelper_.getInstance_(context) |
||||
.getConnectedNetworkName()); |
||||
DaemonTaskResult result = RetrieveTask.create(connection).execute(); |
||||
if (!(result instanceof RetrieveTaskSuccessResult)) { |
||||
// TODO: Show error text somehow in the remote view, perhaps via the EmptyView's text?
|
||||
Log.e(context, "The torrents could not be retrieved at this time; probably a connection issue"); |
||||
if (torrents != null) |
||||
torrents.clear(); |
||||
return; |
||||
} |
||||
|
||||
// We have data; filter, sort and store it to use later when getViewAt gets called
|
||||
ArrayList<Torrent> filteredTorrents = new ArrayList<Torrent>(); |
||||
for (Torrent torrent : ((RetrieveTaskSuccessResult) result).getTorrents()) { |
||||
if (config.getStatusType().getFilterItem(context).matches(torrent)) |
||||
filteredTorrents.add(torrent); |
||||
} |
||||
if (filteredTorrents.size() > 0) { |
||||
// Only sort when there are actually torrents left after filtering
|
||||
Daemon serverType = filteredTorrents.get(0).getDaemon(); |
||||
Collections.sort(filteredTorrents, |
||||
new TorrentsComparator(serverType, config.getSortBy(), config.shouldReserveSort())); |
||||
} |
||||
torrents = filteredTorrents; |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public RemoteViews getViewAt(int position) { |
||||
|
||||
// Load the dark or light widget list item layout xml
|
||||
RemoteViews rv = new RemoteViews(context.getPackageName(), |
||||
config.shouldUseDarkTheme() ? R.layout.list_item_widget_dark : R.layout.list_item_widget_light); |
||||
|
||||
// Bind the torrent details texts and status colour
|
||||
Torrent torrent = torrents.get(position); |
||||
LocalTorrent local = LocalTorrent.fromTorrent(torrent); |
||||
Resources r = context.getResources(); |
||||
rv.setTextViewText(R.id.name_text, torrent.getName()); |
||||
rv.setTextViewText(R.id.progress_text, local.getProgressSizeText(r, false)); |
||||
rv.setTextViewText(R.id.ratio_text, local.getProgressEtaRatioText(r)); |
||||
int statusColour; |
||||
switch (torrent.getStatusCode()) { |
||||
case Downloading: |
||||
statusColour = r.getColor(r.getColor(R.color.torrent_downloading)); |
||||
break; |
||||
case Paused: |
||||
statusColour = r.getColor(r.getColor(R.color.torrent_paused)); |
||||
break; |
||||
case Seeding: |
||||
statusColour = r.getColor(r.getColor(R.color.torrent_seeding)); |
||||
break; |
||||
case Error: |
||||
statusColour = r.getColor(r.getColor(R.color.torrent_error)); |
||||
break; |
||||
default: // Checking, Waiting, Queued, Unknown
|
||||
statusColour = r.getColor(r.getColor(R.color.torrent_other)); |
||||
break; |
||||
} |
||||
rv.setInt(R.id.status_view, "setBackgroundColor", r.getColor(statusColour)); |
||||
Intent startIntent = new Intent(); |
||||
startIntent.putExtra(WidgetProvider.EXTRA_SERVER, config.getServerId()); |
||||
startIntent.putExtra(WidgetProvider.EXTRA_TORRENT, torrent); |
||||
rv.setOnClickFillInIntent(R.id.widget_line_layout, startIntent); |
||||
|
||||
return rv; |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public RemoteViews getLoadingView() { |
||||
return null; |
||||
} |
||||
|
||||
@Override |
||||
public void onDestroy() { |
||||
torrents.clear(); |
||||
} |
||||
|
||||
@Override |
||||
public int getViewTypeCount() { |
||||
return 1; |
||||
} |
||||
|
||||
@Override |
||||
public boolean hasStableIds() { |
||||
return true; |
||||
} |
||||
|
||||
@Override |
||||
public int getCount() { |
||||
if (torrents == null) |
||||
return 0; |
||||
return torrents.size(); |
||||
} |
||||
|
||||
@Override |
||||
public long getItemId(int position) { |
||||
return position; |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue