summaryrefslogtreecommitdiff
path: root/src/de/podfetcher/activity/PodfetcherActivity.java
blob: d892df3f4761248dfa4b889686d9fd376c6edec1 (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
package de.podfetcher.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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.FeedItemlistFragment;
import de.podfetcher.fragment.FeedlistFragment;
import de.podfetcher.fragment.UnreadItemlistFragment;

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

	private FeedManager manager;
	
	private FeedlistFragment feedlist;
	FeedItemlistFragment unreadList;
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		manager = FeedManager.getInstance();
		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);
	}
	
	@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
		}
	}
}