Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

widgets not crashing again, but no data yet

  • Loading branch information...
commit d9d9bc1b21a5234b66eb6ce169aa4292639c77e5 1 parent 4164dad
@trajano authored
View
2  AndroidManifest.xml
@@ -58,7 +58,6 @@
android:name=".GasPricesUpdateService"
android:label="Gas Prices Update Service" >
</service>
- <!--
<receiver
android:name="GasPricesWidgetProvider"
android:icon="@drawable/ic_launcher"
@@ -72,7 +71,6 @@
android:name="android.appwidget.provider"
android:resource="@xml/widget_info" />
</receiver>
- -->
</application>
</manifest>
View
21 src/main/java/net/trajano/gasprices/CitySelectionActivity.java
@@ -2,8 +2,6 @@
import android.app.ListActivity;
import android.content.Intent;
-import android.content.SharedPreferences;
-import android.content.SharedPreferences.Editor;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
@@ -21,7 +19,7 @@
/**
* Preference data, stored in memory until destruction.
*/
- private SharedPreferences preferences;
+ private PreferenceAdaptor preferences;
/**
* Called when the activity is first created.
@@ -45,24 +43,11 @@ public void onCreate(final Bundle savedInstanceState) {
@Override
protected void onListItemClick(final ListView l, final View v,
final int position, final long id) {
- final Editor editor = preferences.edit();
- editor.putLong("selected_city_id", id);
- editor.putString("selected_city_name", (String) getListAdapter()
- .getItem(position));
+ final PreferenceAdaptorEditor editor = preferences.edit();
+ editor.setSelectedCityId(id);
editor.apply();
final Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
-
- @Override
- protected void onPause() {
- super.onPause();
- }
-
- @Override
- protected void onResume() {
- super.onResume();
- }
-
}
View
46 src/main/java/net/trajano/gasprices/GasPricesWidgetConfigurationActivity.java
@@ -1,6 +1,6 @@
package net.trajano.gasprices;
-import android.app.Activity;
+import android.app.ListActivity;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
@@ -11,12 +11,13 @@
import android.view.View;
import android.widget.ListView;
-public class GasPricesWidgetConfigurationActivity extends Activity {
+public class GasPricesWidgetConfigurationActivity extends ListActivity {
public static final String PREF_PREFIX_CITY_ID = "widget.city.id_";
private int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
private final View.OnClickListener mOnClickListener = new View.OnClickListener() {
+ @Override
public void onClick(final View v) {
final Context context = GasPricesWidgetConfigurationActivity.this;
@@ -37,21 +38,23 @@ public void onClick(final View v) {
}
};
+ /**
+ * Preference data, stored in memory until destruction.
+ */
+ private PreferenceAdaptor preferences;
+
/** Called when the activity is first created. */
@Override
public void onCreate(final Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
if (Build.PRODUCT.endsWith("sdk")
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
StrictMode.enableDefaults();
}
- super.onCreate(savedInstanceState);
setResult(RESULT_CANCELED);
- setContentView(R.layout.widget_configuration);
- final ListView listView = (ListView) findViewById(R.id.CityList);
- findViewById(R.id.MyLocationButton)
- .setOnClickListener(mOnClickListener);
- findViewById(R.id.TorontoButton).setOnClickListener(mOnClickListener);
+ preferences = new PreferenceAdaptor(this);
+ setListAdapter(new CityListAdapter(this));
// listView.set
final Intent intent = getIntent();
@@ -68,6 +71,33 @@ public void onCreate(final Bundle savedInstanceState) {
}
+ /**
+ * When the item is selected, it updates the selected city preference rather
+ * than sending the data back. It is this activity that is setting what it
+ * wants
+ */
+ @Override
+ protected void onListItemClick(final ListView l, final View v,
+ final int position, final long id) {
+
+ // Push widget update to surface with newly set prefix
+ final AppWidgetManager appWidgetManager = AppWidgetManager
+ .getInstance(this);
+ // GasPricesWidgetProvider.updateAppWidget(context,
+ // appWidgetManager,
+ // mAppWidgetId, 0);
+
+ // Make sure we pass back the original appWidgetId
+ final Intent intent = new Intent();
+ intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);
+ final PreferenceAdaptorEditor editor = preferences.edit();
+ editor.saveWidgetCityId(mAppWidgetId, id);
+ editor.apply();
+ setResult(RESULT_OK, intent);
+ finish();
+
+ }
+
private void saveWidgetPref(final Context context, final int appWidgetId,
final int cityId) {
final SharedPreferences.Editor prefs = context.getSharedPreferences(
View
28 src/main/java/net/trajano/gasprices/GasPricesWidgetProvider.java
@@ -6,8 +6,6 @@
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
-import android.content.SharedPreferences;
-import android.content.SharedPreferences.Editor;
import android.content.pm.PackageManager;
import android.util.Log;
import android.widget.RemoteViews;
@@ -15,18 +13,16 @@
public class GasPricesWidgetProvider extends AppWidgetProvider {
@Override
public void onDeleted(final Context context, final int[] appWidgetIds) {
- final SharedPreferences preferences = context.getSharedPreferences(
- ApplicationProperties.FILE_NAME, Context.MODE_PRIVATE);
- final Editor e = preferences.edit();
- for (final int widgetId : appWidgetIds) {
- e.remove(GasPricesWidgetConfigurationActivity.PREF_PREFIX_CITY_ID + widgetId);
- }
+ final PreferenceAdaptor preferences = new PreferenceAdaptor(context);
+ final PreferenceAdaptorEditor editor = preferences.edit();
+ editor.removeWidgetCityId(appWidgetIds);
+ editor.apply();
}
@Override
public void onUpdate(final Context context,
final AppWidgetManager appWidgetManager, final int[] appWidgetIds) {
- Log.v("GasPrices", "onUpdate() received");
+ Log.d("GasPrices", "onUpdate() received");
final ComponentName thisWidget = new ComponentName(context,
GasPricesWidgetProvider.class);
@@ -34,7 +30,7 @@ public void onUpdate(final Context context,
context.getPackageName(), R.layout.widget_layout);
{
- Log.v("GasPrices", "Set up intent to launch on widget click");
+ Log.d("GasPrices", "Set up intent to launch on widget click");
final PackageManager manager = context.getPackageManager();
final Intent lintent = manager
.getLaunchIntentForPackage("net.trajano.gasprices");
@@ -45,11 +41,11 @@ public void onUpdate(final Context context,
remoteViews.setOnClickPendingIntent(R.id.thelayout, pendingIntent);
}
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
- {
- // Build the intent to call the service
- final Intent intent = new Intent(context.getApplicationContext(),
- GasPricesWidgetUpdateService.class);
- context.startService(intent);
- }
+ // {
+ // // Build the intent to call the service
+ // final Intent intent = new Intent(context.getApplicationContext(),
+ // GasPricesWidgetUpdateService.class);
+ // context.startService(intent);
+ // }
}
}
View
6 src/main/java/net/trajano/gasprices/PreferenceAdaptor.java
@@ -54,12 +54,12 @@
/**
* This is the selected city ID.
*/
- private static final String SELECTED_CITY_ID_KEY = "selected_city_id";
+ static final String SELECTED_CITY_ID_KEY = "selected_city_id";
/**
* This is the selected city name.
*/
- private static final String SELECTED_CITY_NAME_KEY = "selected_city_name";
+ static final String SELECTED_CITY_NAME_KEY = "selected_city_name";
/**
* {@link SharedPreferences} file name.
@@ -69,7 +69,7 @@
/**
* Widget preference key prefix.
*/
- private static final String WIDGET_PREFERENCE_KEY_PREFIX = "widget_"; // $NON-NLS-1$
+ static final String WIDGET_PREFERENCE_KEY_PREFIX = "widget_"; // $NON-NLS-1$
/**
* This returns true if the Gas Prices view should change if the key was
View
30 src/main/java/net/trajano/gasprices/PreferenceAdaptorEditor.java
@@ -132,6 +132,36 @@ public boolean commit() {
}
/**
+ * This removes the selected city id for the widgets.
+ *
+ * @param appWidgetIds
+ * appwidget ID.
+ * @return itself
+ */
+ public PreferenceAdaptorEditor removeWidgetCityId(final int... appWidgetIds) {
+ for (final int appWidgetId : appWidgetIds) {
+ remove(PreferenceAdaptor.WIDGET_PREFERENCE_KEY_PREFIX + appWidgetId);
+ }
+ return this;
+ }
+
+ /**
+ * This updates the selected city id for the widget
+ *
+ * @param appWidgetId
+ * appwidget ID.
+ * @param cityId
+ * city id
+ * @return itself
+ */
+ public PreferenceAdaptorEditor saveWidgetCityId(final int appWidgetId,
+ final long cityId) {
+ putLong(PreferenceAdaptor.WIDGET_PREFERENCE_KEY_PREFIX + appWidgetId,
+ cityId);
+ return this;
+ }
+
+ /**
* This will store the JSON data into the preferences as well as break it
* apart for each city. Only the main JSON feed is formatted, the individual
* city data is kept small.
Please sign in to comment.
Something went wrong with that request. Please try again.