diff options
Diffstat (limited to 'src/com/cyanogenmod/lockclock/preference')
4 files changed, 193 insertions, 129 deletions
diff --git a/src/com/cyanogenmod/lockclock/preference/CalendarPreferences.java b/src/com/cyanogenmod/lockclock/preference/CalendarPreferences.java index 2d6b4a8..1a430f9 100644 --- a/src/com/cyanogenmod/lockclock/preference/CalendarPreferences.java +++ b/src/com/cyanogenmod/lockclock/preference/CalendarPreferences.java @@ -79,7 +79,6 @@ public class CalendarPreferences extends PreferenceFragment implements if (!hasCalendarPermission()) { mShowCalendar.setChecked(false); } else { - mShowCalendar.setChecked(true); updateCalendars(); } } diff --git a/src/com/cyanogenmod/lockclock/preference/CustomLocationPreference.java b/src/com/cyanogenmod/lockclock/preference/CustomLocationPreference.java index 6d0992f..c290cf5 100644 --- a/src/com/cyanogenmod/lockclock/preference/CustomLocationPreference.java +++ b/src/com/cyanogenmod/lockclock/preference/CustomLocationPreference.java @@ -17,27 +17,27 @@ package com.cyanogenmod.lockclock.preference; import android.app.AlertDialog; -import android.app.Dialog; import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; -import android.os.AsyncTask; import android.os.Bundle; +import android.os.Handler; import android.preference.EditTextPreference; import android.text.TextUtils; import android.util.AttributeSet; import android.view.View; import android.widget.Button; import android.widget.Toast; - import com.cyanogenmod.lockclock.R; import com.cyanogenmod.lockclock.misc.Preferences; -import com.cyanogenmod.lockclock.weather.WeatherProvider.LocationResult; +import cyanogenmod.weather.CMWeatherManager; +import cyanogenmod.weather.WeatherLocation; import java.util.HashSet; import java.util.List; -public class CustomLocationPreference extends EditTextPreference { +public class CustomLocationPreference extends EditTextPreference + implements CMWeatherManager.LookupCityRequestListener { public CustomLocationPreference(Context context) { super(context); } @@ -48,18 +48,35 @@ public class CustomLocationPreference extends EditTextPreference { super(context, attrs, defStyle); } + private ProgressDialog mProgressDialog; + private int mCustomLocationRequestId; + private Handler mHandler; @Override protected void showDialog(Bundle state) { super.showDialog(state); + mHandler = new Handler(getContext().getMainLooper()); final AlertDialog d = (AlertDialog) getDialog(); - Button okButton = d.getButton(DialogInterface.BUTTON_POSITIVE); - + final Button okButton = d.getButton(DialogInterface.BUTTON_POSITIVE); okButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { CustomLocationPreference.this.onClick(d, DialogInterface.BUTTON_POSITIVE); - new WeatherLocationTask(d, getEditText().getText().toString()).execute(); + final String customLocationToLookUp = getEditText().getText().toString(); + if (TextUtils.equals(customLocationToLookUp, "")) return; + final CMWeatherManager weatherManager = CMWeatherManager.getInstance(getContext()); + mProgressDialog = new ProgressDialog(getContext()); + mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); + mProgressDialog.setMessage(getContext().getString(R.string.weather_progress_title)); + mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { + @Override + public void onCancel(DialogInterface dialog) { + weatherManager.cancelRequest(mCustomLocationRequestId); + } + }); + mCustomLocationRequestId = weatherManager.lookupCity(customLocationToLookUp, + CustomLocationPreference.this); + mProgressDialog.show(); } }); } @@ -68,10 +85,12 @@ public class CustomLocationPreference extends EditTextPreference { protected void onBindDialogView(View view) { super.onBindDialogView(view); - String location = Preferences.customWeatherLocationCity(getContext()); + String location = Preferences.getCustomWeatherLocationCity(getContext()); if (location != null) { getEditText().setText(location); getEditText().setSelection(location.length()); + } else { + getEditText().setText(""); } } @@ -81,115 +100,88 @@ public class CustomLocationPreference extends EditTextPreference { super.onDialogClosed(false); } - private class WeatherLocationTask extends AsyncTask<Void, Void, List<LocationResult>> { - private Dialog mDialog; - private ProgressDialog mProgressDialog; - private String mLocation; - - public WeatherLocationTask(Dialog dialog, String location) { - mDialog = dialog; - mLocation = location; - } - - @Override - protected void onPreExecute() { - super.onPreExecute(); - - final Context context = getContext(); + private void handleResultDisambiguation(final List<WeatherLocation> results) { + CharSequence[] items = buildItemList(results); + new AlertDialog.Builder(getContext()) + .setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + applyLocation(results.get(which)); + dialog.dismiss(); + } + }) + .setNegativeButton(android.R.string.cancel, null) + .setTitle(R.string.weather_select_location) + .show(); + } - mProgressDialog = new ProgressDialog(context); - mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); - mProgressDialog.setMessage(context.getString(R.string.weather_progress_title)); - mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() { - @Override - public void onCancel(DialogInterface dialog) { - cancel(true); - } - }); - mProgressDialog.show(); - } + private CharSequence[] buildItemList(List<WeatherLocation> results) { + boolean needCountry = false, needPostal = false; + String countryId = results.get(0).getCountryId(); + HashSet<String> postalIds = new HashSet<>(); - @Override - protected List<LocationResult> doInBackground(Void... input) { - return Preferences.weatherProvider(getContext()).getLocations(mLocation); + for (WeatherLocation result : results) { + if (!TextUtils.equals(result.getCountryId(), countryId)) { + needCountry = true; + } + String postalId = result.getCountryId() + "##" + result.getCity(); + if (postalIds.contains(postalId)) { + needPostal = true; + } + postalIds.add(postalId); + if (needPostal && needCountry) { + break; + } } - @Override - protected void onPostExecute(List<LocationResult> results) { - super.onPostExecute(results); - - final Context context = getContext(); - - if (results == null || results.isEmpty()) { - Toast.makeText(context, - context.getString(R.string.weather_retrieve_location_dialog_title), - Toast.LENGTH_SHORT) - .show(); - } else if (results.size() > 1) { - handleResultDisambiguation(results); - } else { - applyLocation(results.get(0)); + int count = results.size(); + CharSequence[] items = new CharSequence[count]; + for (int i = 0; i < count; i++) { + WeatherLocation result = results.get(i); + StringBuilder builder = new StringBuilder(); + if (needPostal && result.getPostalCode() != null) { + builder.append(result.getPostalCode()).append(" "); } - mProgressDialog.dismiss(); + builder.append(result.getCity()); + if (needCountry) { + String country = result.getCountry() != null + ? result.getCountry() : result.getCountryId(); + builder.append(" (").append(country).append(")"); + } + items[i] = builder.toString(); } + return items; + } - private void handleResultDisambiguation(final List<LocationResult> results) { - CharSequence[] items = buildItemList(results); - new AlertDialog.Builder(getContext()) - .setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - applyLocation(results.get(which)); - dialog.dismiss(); - } - }) - .setNegativeButton(android.R.string.cancel, null) - .setTitle(R.string.weather_select_location) - .show(); + private void applyLocation(final WeatherLocation result) { + if (Preferences.setCustomWeatherLocation(getContext(), result)) { + String cityName = result.getCity(); + String state = result.getState(); + String country = result.getCountry(); + setText(cityName + "," + state + "/" + country); } + final AlertDialog d = (AlertDialog) getDialog(); + d.dismiss(); + } - private CharSequence[] buildItemList(List<LocationResult> results) { - boolean needCountry = false, needPostal = false; - String countryId = results.get(0).countryId; - HashSet<String> postalIds = new HashSet<String>(); - - for (LocationResult result : results) { - if (!TextUtils.equals(result.countryId, countryId)) { - needCountry = true; - } - String postalId = result.countryId + "##" + result.city; - if (postalIds.contains(postalId)) { - needPostal = true; - } - postalIds.add(postalId); - if (needPostal && needCountry) { - break; - } - } - - int count = results.size(); - CharSequence[] items = new CharSequence[count]; - for (int i = 0; i < count; i++) { - LocationResult result = results.get(i); - StringBuilder builder = new StringBuilder(); - if (needPostal && result.postal != null) { - builder.append(result.postal).append(" "); - } - builder.append(result.city); - if (needCountry) { - String country = result.country != null - ? result.country : result.countryId; - builder.append(" (").append(country).append(")"); + @Override + public void onLookupCityRequestCompleted(int status, final List<WeatherLocation> locations) { + mHandler.post(new Runnable() { + @Override + public void run() { + final Context context = getContext(); + if (locations == null || locations.isEmpty()) { + Toast.makeText(context, + context.getString(R.string.weather_retrieve_location_dialog_title), + Toast.LENGTH_SHORT) + .show(); + } else if (locations.size() > 1) { + handleResultDisambiguation(locations); + } else { + applyLocation(locations.get(0)); } - items[i] = builder.toString(); + mProgressDialog.dismiss(); } - return items; - } - - private void applyLocation(final LocationResult result) { - Preferences.setCustomWeatherLocationId(getContext(), result.id); - setText(result.city); - mDialog.dismiss(); - } + }); } } diff --git a/src/com/cyanogenmod/lockclock/preference/Preferences.java b/src/com/cyanogenmod/lockclock/preference/Preferences.java index a138c75..dc70081 100644 --- a/src/com/cyanogenmod/lockclock/preference/Preferences.java +++ b/src/com/cyanogenmod/lockclock/preference/Preferences.java @@ -25,6 +25,7 @@ import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import com.cyanogenmod.lockclock.R; +import com.cyanogenmod.lockclock.weather.Utils; import java.util.List; @@ -37,6 +38,15 @@ public class Preferences extends PreferenceActivity { public void onBuildHeaders(List<Header> target) { loadHeadersFromResource(R.xml.preferences_headers, target); + if (!Utils.isWeatherServiceAvailable(this)) { + for (Header header : target) { + if (header.titleRes == R.string.weather_category) { + target.remove(header); + break; + } + } + } + // Check if triggered from adding a new widget Intent intent = getIntent(); if (intent != null diff --git a/src/com/cyanogenmod/lockclock/preference/WeatherPreferences.java b/src/com/cyanogenmod/lockclock/preference/WeatherPreferences.java index 1a42a20..0824a4b 100644 --- a/src/com/cyanogenmod/lockclock/preference/WeatherPreferences.java +++ b/src/com/cyanogenmod/lockclock/preference/WeatherPreferences.java @@ -19,7 +19,6 @@ package com.cyanogenmod.lockclock.preference; import android.Manifest; import android.app.AlertDialog; import android.app.Dialog; -import android.content.ContentResolver; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; @@ -31,31 +30,24 @@ import android.preference.EditTextPreference; import android.preference.ListPreference; import android.preference.Preference; import android.preference.PreferenceFragment; +import android.preference.PreferenceScreen; import android.preference.SwitchPreference; import android.provider.Settings; import android.text.TextUtils; import android.util.Log; - import com.cyanogenmod.lockclock.ClockWidgetProvider; import com.cyanogenmod.lockclock.R; import com.cyanogenmod.lockclock.misc.Constants; import com.cyanogenmod.lockclock.misc.Preferences; import com.cyanogenmod.lockclock.weather.WeatherUpdateService; +import cyanogenmod.weather.CMWeatherManager; public class WeatherPreferences extends PreferenceFragment implements - SharedPreferences.OnSharedPreferenceChangeListener, Preference.OnPreferenceChangeListener { + SharedPreferences.OnSharedPreferenceChangeListener, Preference.OnPreferenceChangeListener, + CMWeatherManager.WeatherServiceProviderChangeListener { private static final String TAG = "WeatherPreferences"; private static final int LOCATION_PERMISSION_REQUEST_CODE = 1; - private static final String[] LOCATION_PREF_KEYS = new String[] { - Constants.WEATHER_USE_CUSTOM_LOCATION, - Constants.WEATHER_CUSTOM_LOCATION_CITY - }; - private static final String[] WEATHER_REFRESH_KEYS = new String[] { - Constants.SHOW_WEATHER, - Constants.WEATHER_REFRESH_INTERVAL - }; - private SwitchPreference mUseCustomLoc; private EditTextPreference mCustomWeatherLoc; private ListPreference mFontColor; @@ -65,8 +57,8 @@ public class WeatherPreferences extends PreferenceFragment implements private SwitchPreference mUseCustomlocation; private SwitchPreference mShowWeather; private Context mContext; - private ContentResolver mResolver; private Runnable mPostResumeRunnable; + private PreferenceScreen mWeatherSource; @Override public void onCreate(Bundle savedInstanceState) { @@ -74,7 +66,6 @@ public class WeatherPreferences extends PreferenceFragment implements getPreferenceManager().setSharedPreferencesName(Constants.PREF_NAME); addPreferencesFromResource(R.xml.preferences_weather); mContext = getActivity(); - mResolver = mContext.getContentResolver(); // Load items that need custom summaries etc. mUseCustomLoc = (SwitchPreference) findPreference(Constants.WEATHER_USE_CUSTOM_LOCATION); @@ -84,6 +75,18 @@ public class WeatherPreferences extends PreferenceFragment implements mIconSet = (IconSelectionPreference) findPreference(Constants.WEATHER_ICONS); mUseMetric = (SwitchPreference) findPreference(Constants.WEATHER_USE_METRIC); mUseCustomlocation = (SwitchPreference) findPreference(Constants.WEATHER_USE_CUSTOM_LOCATION); + mWeatherSource = (PreferenceScreen) findPreference(Constants.WEATHER_SOURCE); + mWeatherSource.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object o) { + if (Preferences.getWeatherSource(mContext) != null && mShowWeather.isChecked()) { + mWeatherSource.notifyDependencyChange(false); + } else { + mWeatherSource.notifyDependencyChange(true); + } + return false; + } + }); mShowWeather = (SwitchPreference) findPreference(Constants.SHOW_WEATHER); mShowWeather.setOnPreferenceChangeListener(this); @@ -116,15 +119,34 @@ public class WeatherPreferences extends PreferenceFragment implements mPostResumeRunnable = null; } + final CMWeatherManager weatherManager = CMWeatherManager.getInstance(mContext); + weatherManager.registerWeatherServiceProviderChangeListener(this); + + mWeatherSource.setEnabled(mShowWeather.isChecked()); + updateLocationSummary(); updateFontColorsSummary(); updateIconSetSummary(); + updateWeatherProviderSummary(getWeatherProviderName()); } @Override public void onPause() { super.onPause(); getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this); + final CMWeatherManager weatherManager = CMWeatherManager.getInstance(mContext); + weatherManager.unregisterWeatherServiceProviderChangeListener(this); + } + + @Override + public void onDestroy() { + super.onDestroy(); + if (mUseCustomlocation.isChecked() + && Preferences.getCustomWeatherLocationCity(mContext) == null) { + //The user decided to toggle the custom location switch, but forgot to set a custom + //location, we need to go back to geo location + Preferences.setUseCustomWeatherLocation(mContext, false); + } } @Override @@ -152,18 +174,24 @@ public class WeatherPreferences extends PreferenceFragment implements forceWeatherUpdate = true; } - // If the weather source has changes, invalidate the custom location settings and change - // back to GeoLocation to force the user to specify a new custom location if needed if (TextUtils.equals(key, Constants.WEATHER_SOURCE)) { - Preferences.setCustomWeatherLocationId(mContext, null); + // The weather source changed, invalidate the custom location settings and change + // back to GeoLocation to force the user to specify a new custom location if needed Preferences.setCustomWeatherLocationCity(mContext, null); + Preferences.setCustomWeatherLocation(mContext, null); Preferences.setUseCustomWeatherLocation(mContext, false); mUseCustomlocation.setChecked(false); updateLocationSummary(); } - if (key.equals(Constants.WEATHER_USE_CUSTOM_LOCATION) - || key.equals(Constants.WEATHER_CUSTOM_LOCATION_CITY)) { + if (key.equals(Constants.WEATHER_USE_CUSTOM_LOCATION)) { + if (!mUseCustomLoc.isChecked() || (mUseCustomLoc.isChecked() && + Preferences.getCustomWeatherLocation(mContext) != null)) { + forceWeatherUpdate = true; + } + } + + if (key.equals(Constants.WEATHER_CUSTOM_LOCATION_CITY) && mUseCustomLoc.isChecked()) { forceWeatherUpdate = true; } @@ -171,6 +199,15 @@ public class WeatherPreferences extends PreferenceFragment implements needWeatherUpdate = true; } + if (key.equals(Constants.SHOW_WEATHER)) { + mWeatherSource.setEnabled(mShowWeather.isChecked()); + if (Preferences.getWeatherSource(mContext) != null && mShowWeather.isChecked()) { + mWeatherSource.notifyDependencyChange(false); + } else { + mWeatherSource.notifyDependencyChange(true); + } + } + if (Constants.DEBUG) { Log.v(TAG, "Preference " + key + " changed, need update " + needWeatherUpdate + " force update " + forceWeatherUpdate); @@ -199,7 +236,7 @@ public class WeatherPreferences extends PreferenceFragment implements private void updateLocationSummary() { if (mUseCustomLoc.isChecked()) { - String location = Preferences.customWeatherLocationCity(mContext); + String location = Preferences.getCustomWeatherLocationCity(mContext); if (location == null) { location = getResources().getString(R.string.unknown); } @@ -274,4 +311,30 @@ public class WeatherPreferences extends PreferenceFragment implements } return true; } + + @Override + public void onWeatherServiceProviderChanged(String providerName) { + updateWeatherProviderSummary(providerName); + } + + private void updateWeatherProviderSummary(String providerName) { + if (providerName != null) { + mWeatherSource.setSummary(providerName); + Preferences.setWeatherSource(mContext, providerName); + } else { + mWeatherSource.setSummary(R.string.weather_source_not_selected); + Preferences.setWeatherSource(mContext, null); + } + + if (providerName != null && mShowWeather.isChecked()) { + mWeatherSource.notifyDependencyChange(false); + } else { + mWeatherSource.notifyDependencyChange(true); + } + } + + private String getWeatherProviderName() { + final CMWeatherManager weatherManager = CMWeatherManager.getInstance(mContext); + return weatherManager.getActiveWeatherServiceProviderLabel(); + } } |