summaryrefslogtreecommitdiff
path: root/src/de/podfetcher/activity/PodfetcherActivity.java
blob: 24264c64bbe5695838ce2316d8aeaaf01b645f78 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package de.podfetcher.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.actionbarsherlock.view.Window;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

import de.podfetcher.R;
import de.podfetcher.feed.FeedManager;
import de.podfetcher.fragment.ItemlistFragment;
import de.podfetcher.fragment.FeedlistFragment;
import de.podfetcher.fragment.QueueFragment;
import de.podfetcher.fragment.UnreadItemlistFragment;
import de.podfetcher.service.DownloadService;
import de.podfetcher.storage.DownloadRequester;

public class PodfetcherActivity extends SherlockFragmentActivity {
	private static final String TAG = "PodfetcherActivity";

	private FeedManager manager;
	
	private FeedlistFragment feedlist;
	ItemlistFragment unreadList;
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		manager = FeedManager.getInstance();
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setContentView(R.layout.main);
		// Set up tabs
		ActionBar actionBar = getSupportActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		actionBar.setDisplayShowTitleEnabled(false);

		Tab tab = actionBar
				.newTab()
				.setText(getText(R.string.feeds_label).toString())
				.setTabListener(
						new TabListener<FeedlistFragment>(this, getText(
								R.string.feeds_label).toString(),
								FeedlistFragment.class));

		actionBar.addTab(tab);
		
		tab = actionBar
				.newTab()
				.setText(getText(R.string.new_label).toString())
				.setTabListener(
						new TabListener<UnreadItemlistFragment>(this, getText(
								R.string.new_label).toString(),
								UnreadItemlistFragment.class));
		actionBar.addTab(tab);
		
		tab = actionBar
				.newTab()
				.setText(getText(R.string.queue_label).toString())
				.setTabListener(
						new TabListener<QueueFragment>(this, getText(
								R.string.queue_label).toString(),
								QueueFragment.class));
		actionBar.addTab(tab);
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		unregisterReceiver(contentUpdate);
	}

	@Override
	protected void onResume() {
		super.onResume();
		updateProgressBarVisibility();
		IntentFilter filter = new IntentFilter();
		filter.addAction(DownloadService.ACTION_DOWNLOAD_HANDLED);
		filter.addAction(DownloadRequester.ACTION_DOWNLOAD_QUEUED);
		registerReceiver(contentUpdate, filter);
	}
	
	private BroadcastReceiver contentUpdate = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			Log.d(TAG, "Received contentUpdate Intent.");
			updateProgressBarVisibility();
		}
	};
	
	private void updateProgressBarVisibility() {
		if (DownloadService.isRunning && DownloadRequester.getInstance().isDownloadingFeeds()) {
			setSupportProgressBarIndeterminateVisibility(true);
		} else {
			setSupportProgressBarIndeterminateVisibility(false);
		}
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch(item.getItemId()) {
	        case R.id.add_feed:
	            startActivity(new Intent(this, AddFeedActivity.class));
				return true;
			case R.id.all_feed_refresh:
				manager.refreshAllFeeds(this);
				return true;
            case R.id.show_downloads:
                startActivity(new Intent(this, DownloadActivity.class));
                return true;
            case R.id.show_preferences:
            	startActivity(new Intent(this, PreferenceActivity.class));
            	return true;
            case R.id.show_player:
            	startActivity(new Intent(this, MediaplayerActivity.class));
            	return true;
			default:
			    return super.onOptionsItemSelected(item);
	    }
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = new MenuInflater(this);
	    inflater.inflate(R.menu.podfetcher, menu);
	    return true;
	}

	/** TabListener for navigating between the main lists. */
	private class TabListener<T extends Fragment> implements
			ActionBar.TabListener {

		private final Activity activity;
		private final String tag;
		private final Class<T> fClass;
		private Fragment fragment;

		public TabListener(Activity activity, String tag, Class<T> fClass) {
			this.activity = activity;
			this.tag = tag;
			this.fClass = fClass;
		}

		@SuppressWarnings("unused")
		public TabListener(Activity activity, String tag, Fragment fragment,
				Class<T> fClass) {
			super();
			this.activity = activity;
			this.tag = tag;
			this.fragment = fragment;
			this.fClass = fClass;
		}

		public void onTabSelected(Tab tab, FragmentTransaction ft) {
			if (fragment == null) {
				fragment = Fragment.instantiate(activity, fClass.getName());
				ft.replace(R.id.main_fragment, fragment);
			} else {
				ft.attach(fragment);
			}
		}

		public void onTabUnselected(Tab tab, FragmentTransaction ft) {
			if (fragment != null) {
				ft.detach(fragment);
			}
		}

		public void onTabReselected(Tab tab, FragmentTransaction ft) {
			// Do nothing
		}
	}
}