it.tidalwave.bluebill.mobile.android.preferences.BlueBillPreferenceActivity Maven / Gradle / Ivy
/***********************************************************************************************************************
*
* blueBill Mobile - open source birdwatching
* ==========================================
*
* Copyright (C) 2009, 2010 by Tidalwave s.a.s. (http://www.tidalwave.it)
* http://bluebill.tidalwave.it/mobile/
*
***********************************************************************************************************************
*
* 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.
*
***********************************************************************************************************************
*
* $Id: BlueBillPreferenceActivity.java,v 99d3e7db3adb 2010/08/25 14:34:29 fabrizio $
*
**********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.android.preferences;
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.PickTaxonomyActivity;
import it.tidalwave.bluebill.mobile.taxonomy.TaxonomyPreferences;
import it.tidalwave.bluebill.mobile.taxonomy.TaxonomyPreferencesSupport;
import it.tidalwave.bluebill.taxonomy.Taxonomy;
import it.tidalwave.mobile.android.ui.AndroidCommonUITasks;
import it.tidalwave.netbeans.util.Locator;
import it.tidalwave.util.logging.Logger;
/***********************************************************************************************************************
*
* @author Fabrizio Giudici
* @version $Id: $
*
**********************************************************************************************************************/
public class BlueBillPreferenceActivity extends PreferenceActivity
{
private static final String CLASS = BlueBillPreferenceActivity.class.getName();
private static final Logger logger = Logger.getLogger(CLASS);
@Nonnull
private final SharedPreferences preferences = Locator.find(SharedPreferences.class);
private final AndroidCommonUITasks commonUITasks = new AndroidCommonUITasks(this);
/*******************************************************************************************************************
*
* 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(BlueBillPreferenceActivity.this, "", message, true);
final Thread thread = new Thread()
{
@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);
commonUITasks.showTemporaryMessage(String.format(msg, taxonomy.getDisplayName()));
}
});
}
};
thread.start();
}
}
};
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@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(BlueBillPreferenceActivity.this, PickTaxonomyActivity.class));
return true;
}
});
}
/*******************************************************************************************************************
*
*
******************************************************************************************************************/
@Override
protected void onDestroy()
{
super.onDestroy();
preferences.unregisterOnSharedPreferenceChangeListener(listener);
}
}