From 41ad3b841515a82e67c64c2ffd6b69e5cdcc606a Mon Sep 17 00:00:00 2001 From: "gu.martinm@gmail.com" Date: Mon, 6 Oct 2014 21:49:14 +0200 Subject: [PATCH] WeatherInformation: widget error/default layout. --- res/layout/weather_current_fragment.xml | 2 +- res/values/strings.xml | 1 + res/xml/appwidget_provider.xml | 2 +- src/de/example/exampletdd/WidgetIntentService.java | 42 +++++++++++++++++++--- .../fragment/overview/OverviewFragment.java | 3 +- 5 files changed, 41 insertions(+), 9 deletions(-) diff --git a/res/layout/weather_current_fragment.xml b/res/layout/weather_current_fragment.xml index 4f2ec5c..3533424 100644 --- a/res/layout/weather_current_fragment.xml +++ b/res/layout/weather_current_fragment.xml @@ -553,7 +553,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" - android:text="No data available" + android:text="@string/text_field_remote_error" android:textAppearance="?android:attr/textAppearanceSmall" android:visibility="gone" /> diff --git a/res/values/strings.xml b/res/values/strings.xml index a59baa2..210e158 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -39,4 +39,5 @@ ºC m/s hpa + No data available diff --git a/res/xml/appwidget_provider.xml b/res/xml/appwidget_provider.xml index 74390c6..c710a99 100644 --- a/res/xml/appwidget_provider.xml +++ b/res/xml/appwidget_provider.xml @@ -6,7 +6,7 @@ android:minResizeHeight="40dp" android:resizeMode="none" android:previewImage="@drawable/ic_launcher" - android:initialLayout="@layout/appwidget" + android:initialLayout="@layout/appwidget_error" android:configure="de.example.exampletdd.widget.WeatherInformationWidgetConfigure" android:widgetCategory="home_screen|keyguard" android:updatePeriodMillis="3600000"> diff --git a/src/de/example/exampletdd/WidgetIntentService.java b/src/de/example/exampletdd/WidgetIntentService.java index 3f686b8..4319637 100644 --- a/src/de/example/exampletdd/WidgetIntentService.java +++ b/src/de/example/exampletdd/WidgetIntentService.java @@ -48,14 +48,16 @@ public class WidgetIntentService extends IntentService { @Override protected void onHandleIntent(final Intent intent) { Log.i(TAG, "onHandleIntent"); - final int appWidgetId = intent.getIntExtra("appWidgetId", -666); + final int appWidgetId = intent.getIntExtra("appWidgetId", AppWidgetManager.INVALID_APPWIDGET_ID); final boolean isUpdateByApp = intent.getBooleanExtra("updateByApp", false); final DatabaseQueries query = new DatabaseQueries(this.getApplicationContext()); final WeatherLocation weatherLocation = query.queryDataBase(); if (weatherLocation == null) { - // Nothing to do. + // Nothing to do. Show error. + final RemoteViews view = this.makeErrorView(); + this.updateWidgets(view); return; } @@ -76,8 +78,10 @@ public class WidgetIntentService extends IntentService { } private void updateByTimeout(final WeatherLocation weatherLocation, final int appWidgetId) { - if (appWidgetId == -666) { - // Nothing to do. Something went wrong. + if (appWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) { + // Nothing to do. Something went wrong. Show error. + final RemoteViews view = this.makeErrorView(); + this.updateWidgets(view); return; } @@ -86,7 +90,9 @@ public class WidgetIntentService extends IntentService { final RemoteViews view = this.makeView(current, weatherLocation); this.updateWidget(view, appWidgetId); } else { - // TODO: show layout error in Widget + // Show error. + final RemoteViews view = this.makeErrorView(); + this.updateWidgets(view); } } @@ -221,6 +227,32 @@ public class WidgetIntentService extends IntentService { return remoteView; } + private RemoteViews makeErrorView() { + final RemoteViews remoteView = new RemoteViews(this.getApplicationContext().getPackageName(), R.layout.appwidget_error); + + // 5. Activity launcher. + final Intent resultIntent = new Intent(this.getApplicationContext(), WeatherTabsActivity.class); + // The PendingIntent to launch our activity if the user selects this notification + // final PendingIntent contentIntent = PendingIntent.getActivity( + // this.getApplicationContext(), 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT); + // The stack builder object will contain an artificial back stack for the started Activity. + // This ensures that navigating backward from the Activity leads out of + // your application to the Home screen. + final TaskStackBuilder stackBuilder = TaskStackBuilder.create(this.getApplicationContext()); + // Adds the back stack for the Intent (but not the Intent itself) + stackBuilder.addParentStack(WeatherTabsActivity.class); + // Adds the Intent that starts the Activity to the top of the stack + stackBuilder.addNextIntent(resultIntent); + final PendingIntent resultPendingIntent = + stackBuilder.getPendingIntent( + 0, + PendingIntent.FLAG_UPDATE_CURRENT + ); + remoteView.setOnClickPendingIntent(R.id.weather_appwidget_error, resultPendingIntent); + + return remoteView; + } + private void updateWidget(final RemoteViews remoteView, final int appWidgetId) { final AppWidgetManager manager = AppWidgetManager.getInstance(this.getApplicationContext()); diff --git a/src/de/example/exampletdd/fragment/overview/OverviewFragment.java b/src/de/example/exampletdd/fragment/overview/OverviewFragment.java index b3094ea..fd746e0 100644 --- a/src/de/example/exampletdd/fragment/overview/OverviewFragment.java +++ b/src/de/example/exampletdd/fragment/overview/OverviewFragment.java @@ -76,8 +76,7 @@ public class OverviewFragment extends ListFragment { this.setHasOptionsMenu(false); - // TODO: string static resource - this.setEmptyText("No data available"); + this.setEmptyText(this.getString(R.string.text_field_remote_error)); this.setListShownNoAnimation(false); } -- 2.1.4