2 * Copyright (C) 2012 Andrew Neal Licensed under the Apache License, Version 2.0
3 * (the "License"); you may not use this file except in compliance with the
4 * License. You may obtain a copy of the License at
5 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
6 * or agreed to in writing, software distributed under the License is
7 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
8 * KIND, either express or implied. See the License for the specific language
9 * governing permissions and limitations under the License.
12 package com.andrew.apollo.ui.activities;
14 import android.app.AlertDialog;
15 import android.content.DialogInterface;
16 import android.content.DialogInterface.OnClickListener;
17 import android.content.Intent;
18 import android.content.pm.PackageInfo;
19 import android.content.pm.PackageManager.NameNotFoundException;
20 import android.os.Bundle;
21 import android.preference.CheckBoxPreference;
22 import android.preference.Preference;
23 import android.preference.Preference.OnPreferenceChangeListener;
24 import android.preference.Preference.OnPreferenceClickListener;
25 import android.preference.PreferenceActivity;
26 import android.view.MenuItem;
28 import com.andrew.apollo.MusicPlaybackService;
29 import com.andrew.apollo.R;
30 import com.andrew.apollo.cache.ImageCache;
31 import com.andrew.apollo.ui.fragments.ThemeFragment;
32 import com.andrew.apollo.utils.ApolloUtils;
33 import com.andrew.apollo.utils.MusicUtils;
34 import com.andrew.apollo.utils.PreferenceUtils;
35 import com.andrew.apollo.widgets.ColorSchemeDialog;
40 * @author Andrew Neal (andrewdneal@gmail.com)
42 @SuppressWarnings("deprecation")
43 public class SettingsActivity extends PreferenceActivity {
48 private ImageCache mImageCache;
50 private PreferenceUtils mPreferences;
56 protected void onCreate(final Bundle savedInstanceState) {
57 super.onCreate(savedInstanceState);
59 overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
61 // Get the preferences
62 mPreferences = PreferenceUtils.getInstance(this);
64 // Initialze the image cache
65 mImageCache = ImageCache.getInstance(this);
68 getActionBar().setDisplayHomeAsUpEnabled(true);
70 // Add the preferences
71 addPreferencesFromResource(R.xml.settings);
75 // Removes the cache entries
78 showOpenSourceLicenses();
79 // Update the version number
81 final PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
82 findPreference("version").setSummary(packageInfo.versionName);
83 } catch (final NameNotFoundException e) {
84 findPreference("version").setSummary("?");
92 public boolean onOptionsItemSelected(final MenuItem item) {
93 switch (item.getItemId()) {
94 case android.R.id.home:
101 return super.onOptionsItemSelected(item);
108 protected void onStart() {
110 MusicUtils.notifyForegroundStateChanged(this, true);
117 protected void onStop() {
119 MusicUtils.notifyForegroundStateChanged(this, false);
123 * Initializes the preferences under the "Interface" category
125 private void initInterface() {
126 // Color scheme picker
128 // Open the theme chooser
133 * Shows the {@link ColorSchemeDialog} and then saves the changes.
135 private void updateColorScheme() {
136 final Preference colorScheme = findPreference("color_scheme");
137 colorScheme.setOnPreferenceClickListener(new OnPreferenceClickListener() {
139 public boolean onPreferenceClick(final Preference preference) {
140 ApolloUtils.showColorPicker(SettingsActivity.this);
147 * Opens the {@link ThemeFragment}.
149 private void openThemeChooser() {
150 final Preference themeChooser = findPreference("theme_chooser");
151 themeChooser.setOnPreferenceClickListener(new OnPreferenceClickListener() {
153 public boolean onPreferenceClick(final Preference preference) {
154 final Intent themeChooserIntent = new Intent(SettingsActivity.this,
155 ThemesActivity.class);
156 startActivity(themeChooserIntent);
163 * Removes all of the cache entries.
165 private void deleteCache() {
166 final Preference deleteCache = findPreference("delete_cache");
167 deleteCache.setOnPreferenceClickListener(new OnPreferenceClickListener() {
169 public boolean onPreferenceClick(final Preference preference) {
170 new AlertDialog.Builder(SettingsActivity.this).setMessage(R.string.delete_warning)
171 .setPositiveButton(android.R.string.ok, new OnClickListener() {
174 public void onClick(final DialogInterface dialog, final int which) {
175 mImageCache.clearCaches();
177 }).setNegativeButton(R.string.cancel, new OnClickListener() {
180 public void onClick(final DialogInterface dialog, final int which) {
190 * Show the open source licenses
192 private void showOpenSourceLicenses() {
193 final Preference mOpenSourceLicenses = findPreference("open_source");
194 mOpenSourceLicenses.setOnPreferenceClickListener(new OnPreferenceClickListener() {
197 public boolean onPreferenceClick(final Preference preference) {
198 ApolloUtils.createOpenSourceDialog(SettingsActivity.this).show();