it.tidalwave.bluebill.mobile.android.preferences.BlueBillPreferencesActivity Maven / Gradle / Ivy
The newest version!
/***********************************************************************************************************************
*
* blueBill Mobile - Android - open source birding
* Copyright (C) 2009-2011 by Tidalwave s.a.s. (http://www.tidalwave.it)
*
***********************************************************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
***********************************************************************************************************************
*
* WWW: http://bluebill.tidalwave.it/mobile
* SCM: https://java.net/hg/bluebill-mobile~android-src
*
**********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.android.preferences;
import java.util.concurrent.ExecutorService;
import it.tidalwave.role.Displayable;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import javax.annotation.Nonnull;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import it.tidalwave.bluebill.mobile.android.R;
import it.tidalwave.bluebill.mobile.android.taxonomy.TaxonomyPickerActivity;
import it.tidalwave.bluebill.mobile.preferences.TaxonomyPreferences;
import it.tidalwave.bluebill.mobile.taxonomy.spi.TaxonomyPreferencesSupport;
import it.tidalwave.bluebill.taxonomy.mobile.Taxonomy;
import it.tidalwave.ui.android.app.AndroidActivityHelper;
import it.tidalwave.netbeans.util.Locator;
import it.tidalwave.util.logging.Logger;
import static it.tidalwave.util.ui.UserNotification.notification;
/***********************************************************************************************************************
*
* @stereotype View
* @stereotype Activity
*
* @author Fabrizio Giudici
* @version $Id$
*
**********************************************************************************************************************/
public class BlueBillPreferencesActivity extends PreferenceActivity
{
private static final String CLASS = BlueBillPreferencesActivity.class.getName();
private static final Logger logger = Logger.getLogger(CLASS);
@Nonnull
private final SharedPreferences preferences = Locator.find(SharedPreferences.class);
private final AndroidActivityHelper activityHelper = new AndroidActivityHelper(this);
@Nonnull
private final ExecutorService executorService = Locator.find(ExecutorService.class);
/*******************************************************************************************************************
*
* When the selected taxonomy changes, this listener reloads it. Since it's a long operation, it's done in
* background, with a progress dialog blocking the UI. It can't be implemented in TaxonomyPreferencesSupport since
* it isn't impossible to open a ProgressDialog from there, because there's no reference Activity.
*
******************************************************************************************************************/
private final OnSharedPreferenceChangeListener listener = new OnSharedPreferenceChangeListener()
{
public void onSharedPreferenceChanged (final @Nonnull SharedPreferences sharedPreferences,
final @Nonnull String string)
{
logger.info("onSharedPreferenceChanged(%s, %s)", sharedPreferences, string);
if (TaxonomyPreferencesSupport.PREF_TAXONOMY.equals(string)) // FIXME: and only if you actually changed the value
{
final String message = getResources().getString(R.string.loadingData);
final ProgressDialog dialog = ProgressDialog.show(BlueBillPreferencesActivity.this, "", message, true);
executorService.submit(new Runnable()
{
@Override
public void run()
{
final Taxonomy taxonomy = Locator.find(TaxonomyPreferences.class).reloadTaxonomy(); // long task, pre-load now
runOnUiThread(new Runnable()
{
public void run()
{
dialog.dismiss();
final String msg = getResources().getString(R.string.currentChecklist);
final String message = String.format(msg, taxonomy.as(Displayable.class).getDisplayName());
activityHelper.showLightNotification(notification().withText(message));
}
});
}
});
}
}
};
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Override
protected void onCreate (final @Nonnull Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
preferences.registerOnSharedPreferenceChangeListener(listener);
final Preference taxonomyPreference = (Preference)findPreference("taxonomy");
taxonomyPreference.setOnPreferenceClickListener(new OnPreferenceClickListener()
{
public boolean onPreferenceClick (final @Nonnull Preference preference)
{
startActivity(new Intent(BlueBillPreferencesActivity.this, TaxonomyPickerActivity.class));
return true;
}
});
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Override
protected void onDestroy()
{
super.onDestroy();
preferences.unregisterOnSharedPreferenceChangeListener(listener);
}
}