summaryrefslogtreecommitdiffstats
path: root/src/de/animux/android/andmal/ListActivity.java
blob: ec21f41812bf7386e7dd1b36d34e67d9113ad6db (plain) (blame)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package de.animux.android.andmal;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import de.animux.android.andmal.api.MalList;
import de.animux.android.andmal.api.MalObject;
import de.animux.android.andmal.api.MalState;
import de.animux.android.andmal.api.anime.AnimeList;
import de.animux.android.andmal.api.manga.MangaList;
import de.animux.android.andmal.util.SeparatedListAdapter;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class ListActivity extends Activity {

	public final static String ITEM_TITLE = "title";
	public final static String ITEM_CAPTION = "caption";

	public final static int SYNCING = 0;

	private MalList currentList;

	private UpdateUIHandler updateUIHandler = new UpdateUIHandler();
	private SharedPreferences prefs;

	public ListActivity() {
		super();
		this.currentList = null;
		this.prefs = null;
	}

	public Map<String, ?> createItem(String title, String caption) {
		Map<String, String> item = new HashMap<String, String>();
		item.put(ITEM_TITLE, title);
		item.put(ITEM_CAPTION, caption);
		return item;
	}

	private class UpdateUIHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			SeparatedListAdapter adapter = new SeparatedListAdapter(
					ListActivity.this);

			if (currentList != null) {
				Map<MalState, Collection<MalObject>> malObjects = currentList
						.getObjects();

				if (malObjects != null) {
					for (MalState s : malObjects.keySet())
						addMalObjects(adapter, malObjects.get(s), s.toString());

					ListView list = new ListView(ListActivity.this);
					list.setAdapter(adapter);
					ListActivity.this.setContentView(list);
				}
			}

			dismissDialog(SYNCING);
		}
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		PreferenceManager.setDefaultValues(this, R.menu.settings, false);

		prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

		if (prefs.getString("MAL_USERNAME", null) == null) {
			startActivity(new Intent(this, Settings.class));
			return;
		}

		currentList = new AnimeList(prefs.getString("MAL_USERNAME", null),
				prefs.getString("MAL_PASSWORD", null));
		refreshDisplay();
	}

	@Override
	protected void onResume() {
		super.onResume();

		if (currentList == null) {
			if (prefs.getString("USERNAME", null) != null) {
				currentList = new AnimeList(prefs.getString("MAL_USERNAME",
						null), prefs.getString("MAL_PASSWORD", null));
			}
		} else {
			currentList.setUsername(prefs.getString("MAL_USERNAME", null));
			currentList.setPassword(prefs.getString("MAL_PASSWORD", null));
		}

		refreshDisplay();
	}

	private synchronized void refreshDisplay() {
		if (currentList != null) {
			if (currentList.needsRefresh()) {
				showDialog(SYNCING);

				new Thread() {
					public void run() {
						try {
							currentList.refresh();
						} catch (Exception e) {
							e.printStackTrace();
						}

						updateUIHandler.sendEmptyMessage(0);
					}
				}.start();
			} else {
				updateUIHandler.sendEmptyMessage(0);
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		super.onPrepareOptionsMenu(menu);
		MenuItem item;

		item = menu.findItem(R.id.menu_switch_anime);
		item.setVisible(currentList instanceof MangaList);

		item = menu.findItem(R.id.menu_switch_manga);
		item.setVisible(currentList instanceof AnimeList);

		return true;
	}

	private void addMalObjects(SeparatedListAdapter adapter,
			Collection<MalObject> malObjects, String sectionName) {
		if (malObjects != null && malObjects.size() > 0) {
			java.util.List<Map<String, ?>> listSection = new LinkedList<Map<String, ?>>();

			for (MalObject m : malObjects) {
				listSection.add(createItem(m.getTitle(), m.getProgress()));
			}

			adapter.addSection(sectionName, new SimpleAdapter(this,
					listSection, R.layout.list_item, new String[] { ITEM_TITLE,
							ITEM_CAPTION }, new int[] { R.id.list_item_title,
							R.id.list_item_other }));
		}
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case SYNCING:
			ProgressDialog dialog = ProgressDialog.show(this, "",
					"Loading list. Please wait...", true);
			dialog.show();
			return dialog;
		default:
			return super.onCreateDialog(id);
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_settings:
			startActivity(new Intent(this, Settings.class));
			return true;
		case R.id.menu_switch_manga:
			currentList = new MangaList();
			break;
		case R.id.menu_switch_anime:
			currentList = new AnimeList();
			break;
		case R.id.menu_refresh:
			if (currentList != null)
				currentList.setNeedsRefresh(true);

			break;
		case R.id.menu_exit:
			System.exit(0);
			break;
		default:
			return false;
		}

		refreshDisplay();
		return true;
	}

}