5 package com.andrew.apollo.preferences;
\r
7 import java.util.List;
\r
9 import android.content.BroadcastReceiver;
\r
10 import android.content.ComponentName;
\r
11 import android.content.Context;
\r
12 import android.content.Intent;
\r
13 import android.content.IntentFilter;
\r
14 import android.content.ServiceConnection;
\r
15 import android.content.SharedPreferences;
\r
16 import android.content.pm.PackageManager;
\r
17 import android.content.pm.ResolveInfo;
\r
18 import android.net.Uri;
\r
19 import android.os.Bundle;
\r
20 import android.os.IBinder;
\r
21 import android.preference.ListPreference;
\r
22 import android.preference.Preference;
\r
23 import android.preference.Preference.OnPreferenceChangeListener;
\r
24 import android.preference.PreferenceActivity;
\r
25 import android.view.MenuItem;
\r
26 import android.view.View;
\r
27 import android.view.View.OnClickListener;
\r
28 import android.widget.ImageView;
\r
29 import android.widget.TextView;
\r
31 import com.andrew.apollo.IApolloService;
\r
32 import com.andrew.apollo.R;
\r
33 import com.andrew.apollo.activities.AudioPlayerHolder;
\r
34 import com.andrew.apollo.activities.MusicLibrary;
\r
35 import com.andrew.apollo.service.ApolloService;
\r
36 import com.andrew.apollo.service.ServiceToken;
\r
37 import com.andrew.apollo.utils.ApolloUtils;
\r
38 import com.andrew.apollo.utils.MusicUtils;
\r
39 import com.andrew.apollo.utils.ThemeUtils;
\r
40 import com.androidquery.AQuery;
\r
42 import static com.andrew.apollo.Constants.ALBUM_IMAGE;
\r
43 import static com.andrew.apollo.Constants.APOLLO;
\r
44 import static com.andrew.apollo.Constants.THEME_PACKAGE_NAME;
\r
45 import static com.andrew.apollo.Constants.THEME_PREVIEW;
\r
47 * @author Andrew Neal FIXME - Work on the IllegalStateException thrown when
\r
48 * using PreferenceFragment and theme chooser
\r
50 @SuppressWarnings("deprecation")
\r
51 public class SettingsHolder extends PreferenceActivity implements ServiceConnection {
\r
54 private ServiceToken mToken;
\r
57 protected void onCreate(Bundle savedInstanceState) {
\r
58 // This should be called first thing
\r
59 super.onCreate(savedInstanceState);
\r
61 // Load settings XML
\r
62 int preferencesResId = R.xml.settings;
\r
63 addPreferencesFromResource(preferencesResId);
\r
65 // Load the theme chooser
\r
73 public boolean onOptionsItemSelected(MenuItem item) {
\r
74 switch (item.getItemId()) {
\r
75 case android.R.id.home:
\r
79 return super.onOptionsItemSelected(item);
\r
83 public void onServiceConnected(ComponentName name, IBinder obj) {
\r
84 MusicUtils.mService = IApolloService.Stub.asInterface(obj);
\r
88 public void onServiceDisconnected(ComponentName name) {
\r
89 MusicUtils.mService = null;
\r
93 * Update the ActionBar as needed
\r
95 private final BroadcastReceiver mMediaStatusReceiver = new BroadcastReceiver() {
\r
98 public void onReceive(Context context, Intent intent) {
\r
99 // Update the ActionBar
\r
106 protected void onStart() {
\r
108 mToken = MusicUtils.bindToService(this, this);
\r
110 IntentFilter filter = new IntentFilter();
\r
111 filter.addAction(ApolloService.META_CHANGED);
\r
112 filter.addAction(ApolloService.QUEUE_CHANGED);
\r
113 filter.addAction(ApolloService.PLAYSTATE_CHANGED);
\r
115 registerReceiver(mMediaStatusReceiver, filter);
\r
120 protected void onStop() {
\r
122 if (MusicUtils.mService != null)
\r
123 MusicUtils.unbindFromService(mToken);
\r
125 unregisterReceiver(mMediaStatusReceiver);
\r
130 * Update the ActionBar
\r
132 public void initActionBar() {
\r
133 // Custom ActionBar layout
\r
134 View view = getLayoutInflater().inflate(R.layout.custom_action_bar, null);
\r
135 // Show the ActionBar
\r
136 getActionBar().setCustomView(view);
\r
137 getActionBar().setTitle(R.string.settings);
\r
138 getActionBar().setDisplayHomeAsUpEnabled(true);
\r
139 getActionBar().setDisplayShowHomeEnabled(true);
\r
140 getActionBar().setDisplayShowCustomEnabled(true);
\r
142 ImageView mAlbumArt = (ImageView)view.findViewById(R.id.action_bar_album_art);
\r
143 TextView mTrackName = (TextView)view.findViewById(R.id.action_bar_track_name);
\r
144 TextView mAlbumName = (TextView)view.findViewById(R.id.action_bar_album_name);
\r
146 String url = ApolloUtils.getImageURL(MusicUtils.getAlbumName(), ALBUM_IMAGE, this);
\r
147 AQuery aq = new AQuery(this);
\r
148 mAlbumArt.setImageBitmap(aq.getCachedImage(url));
\r
150 mTrackName.setText(MusicUtils.getTrackName());
\r
151 mAlbumName.setText(MusicUtils.getAlbumName());
\r
153 view.setOnClickListener(new OnClickListener() {
\r
155 public void onClick(View v) {
\r
156 Context context = v.getContext();
\r
157 context.startActivity(new Intent(context, AudioPlayerHolder.class));
\r
166 public void applyTheme(View v) {
\r
167 ThemePreview themePreview = (ThemePreview)findPreference(THEME_PREVIEW);
\r
168 String packageName = themePreview.getValue().toString();
\r
169 ThemeUtils.setThemePackageName(this, packageName);
\r
170 Intent intent = new Intent();
\r
171 intent.setClass(this, MusicLibrary.class);
\r
172 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
\r
173 startActivity(intent);
\r
180 public void getThemes(View v) {
\r
181 Uri marketUri = Uri
\r
182 .parse("https://market.android.com/search?q=ApolloThemes&c=apps&featured=APP_STORE_SEARCH");
\r
183 Intent marketIntent = new Intent(Intent.ACTION_VIEW).setData(marketUri);
\r
184 startActivity(marketIntent);
\r
189 * Set up the theme chooser
\r
191 public void initThemeChooser() {
\r
192 SharedPreferences sp = getPreferenceManager().getSharedPreferences();
\r
193 String themePackage = sp.getString(THEME_PACKAGE_NAME, APOLLO);
\r
194 ListPreference themeLp = (ListPreference)findPreference(THEME_PACKAGE_NAME);
\r
195 themeLp.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
\r
197 public boolean onPreferenceChange(Preference preference, Object newValue) {
\r
198 ThemePreview themePreview = (ThemePreview)findPreference(THEME_PREVIEW);
\r
199 themePreview.setTheme(newValue.toString());
\r
204 Intent intent = new Intent("com.andrew.apollo.THEMES");
\r
205 intent.addCategory("android.intent.category.DEFAULT");
\r
206 PackageManager pm = getPackageManager();
\r
207 List<ResolveInfo> themes = pm.queryIntentActivities(intent, 0);
\r
208 String[] entries = new String[themes.size() + 1];
\r
209 String[] values = new String[themes.size() + 1];
\r
210 entries[0] = APOLLO;
\r
211 values[0] = APOLLO;
\r
212 for (int i = 0; i < themes.size(); i++) {
\r
213 String appPackageName = (themes.get(i)).activityInfo.packageName.toString();
\r
214 String themeName = (themes.get(i)).loadLabel(pm).toString();
\r
215 entries[i + 1] = themeName;
\r
216 values[i + 1] = appPackageName;
\r
218 themeLp.setEntries(entries);
\r
219 themeLp.setEntryValues(values);
\r
220 ThemePreview themePreview = (ThemePreview)findPreference(THEME_PREVIEW);
\r
221 themePreview.setTheme(themePackage);
\r