summaryrefslogtreecommitdiff
path: root/src/de/danoeh/antennapod/activity/DownloadLogActivity.java
blob: 65b6a7a9e71313c6acb5b718f03a35fa67945ac9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package de.danoeh.antennapod.activity;

import android.os.AsyncTask;
import android.os.Bundle;

import android.support.v7.app.ActionBarActivity;

import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.adapter.DownloadLogAdapter;
import de.danoeh.antennapod.feed.EventDistributor;
import de.danoeh.antennapod.preferences.UserPreferences;
import de.danoeh.antennapod.service.download.DownloadStatus;
import de.danoeh.antennapod.storage.DBReader;

import java.util.List;

/**
 * Displays completed and failed downloads in a list.
 */
public class DownloadLogActivity extends ActionBarActivity {
	private static final String TAG = "DownloadLogActivity";

    private List<DownloadStatus> downloadLog;
	private DownloadLogAdapter dla;

    private ListView listview;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setTheme(UserPreferences.getTheme());
		super.onCreate(savedInstanceState);
        setContentView(R.layout.listview_activity);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);


        listview = (ListView) findViewById(R.id.listview);

		dla = new DownloadLogAdapter(this, itemAccess);
		listview.setAdapter(dla);
        loadData();
	}

	@Override
	protected void onPause() {
		super.onPause();
		EventDistributor.getInstance().unregister(contentUpdate);
	}

	@Override
	protected void onResume() {
		super.onResume();
		EventDistributor.getInstance().register(contentUpdate);
		dla.notifyDataSetChanged();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			break;
		default:
			return false;
		}
		return true;
	}

    private void loadData() {
        AsyncTask<Void, Void, List<DownloadStatus>> loadTask = new AsyncTask<Void, Void, List<DownloadStatus>>() {
            @Override
            protected List<DownloadStatus> doInBackground(Void... voids) {
                return DBReader.getDownloadLog(DownloadLogActivity.this);
            }

            @Override
            protected void onPostExecute(List<DownloadStatus> downloadStatuses) {
                super.onPostExecute(downloadStatuses);
                if (downloadStatuses != null) {
                    downloadLog = downloadStatuses;
                    if (dla != null) {
                        dla.notifyDataSetChanged();
                    }
                } else {
                    Log.e(TAG, "Could not load download log");
                }
            }
        };
        loadTask.execute();
    }

    private DownloadLogAdapter.ItemAccess itemAccess = new DownloadLogAdapter.ItemAccess() {

        @Override
        public int getCount() {
            return (downloadLog != null) ? downloadLog.size() : 0;
        }

        @Override
        public DownloadStatus getItem(int position) {
            return (downloadLog != null) ? downloadLog.get(position) : null;
        }
    };

	private EventDistributor.EventListener contentUpdate = new EventDistributor.EventListener() {
		
		@Override
		public void update(EventDistributor eventDistributor, Integer arg) {
			if ((arg & EventDistributor.DOWNLOADLOG_UPDATE) != 0) {
				loadData();
			}
		}
	};

}