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
|
package de.danoeh.antennapod.activity;
import android.annotation.SuppressLint;
import android.content.Intent;
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.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import de.danoeh.antennapod.AppConfig;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.miroguide.conn.MiroGuideException;
import de.danoeh.antennapod.miroguide.conn.MiroGuideService;
import de.danoeh.antennapod.preferences.UserPreferences;
/**
* Shows a list of available categories and offers a search button. If the user
* selects a category, the MiroGuideCategoryActivity is started.
*/
public class MiroGuideMainActivity extends ActionBarActivity implements AdapterView.OnItemClickListener{
private static final String TAG = "MiroGuideMainActivity";
private static String[] categories;
private ArrayAdapter<String> listAdapter;
private TextView txtvStatus;
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(UserPreferences.getTheme());
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.miroguide_categorylist);
txtvStatus = (TextView) findViewById(android.R.id.empty);
listView = (ListView) findViewById(android.R.id.list);
listView.setOnItemClickListener(this);
}
@Override
protected void onPause() {
super.onPause();
}
@Override
protected void onResume() {
super.onResume();
if (categories != null) {
createAdapter();
} else {
loadCategories();
}
}
private void createAdapter() {
if (categories != null) {
listAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, categories);
txtvStatus.setText(R.string.no_items_label);
listView.setAdapter(listAdapter);
}
}
/**
* Launches an AsyncTask to load the available categories in the background.
*/
@SuppressLint("NewApi")
private void loadCategories() {
AsyncTask<Void, Void, Void> listLoader = new AsyncTask<Void, Void, Void>() {
private String[] c;
private MiroGuideException exception;
@Override
protected void onPostExecute(Void result) {
if (exception == null) {
if (AppConfig.DEBUG)
Log.d(TAG, "Successfully loaded categories");
categories = c;
createAdapter();
} else {
Log.e(TAG, "Error happened while trying to load categories");
txtvStatus.setText(exception.getMessage());
}
}
@Override
protected void onPreExecute() {
txtvStatus.setText(R.string.loading_categories_label);
}
@Override
protected Void doInBackground(Void... params) {
MiroGuideService service = new MiroGuideService();
try {
c = service.getCategories();
} catch (MiroGuideException e) {
e.printStackTrace();
exception = e;
} finally {
service.close();
}
return null;
}
};
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {
listLoader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
listLoader.execute();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(Menu.NONE, R.id.search_item, Menu.NONE, R.string.search_label)
.setIcon(
obtainStyledAttributes(
new int[] { R.attr.action_search })
.getDrawable(0))
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
case R.id.search_item:
onSearchRequested();
return true;
default:
return false;
}
}
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
String selection = listAdapter.getItem(position);
Intent launchIntent = new Intent(this, MiroGuideCategoryActivity.class);
launchIntent.putExtra(MiroGuideCategoryActivity.EXTRA_CATEGORY,
selection);
startActivity(launchIntent);
}
}
|