WeatherInformation: widget error/default layout.
authorgu.martinm@gmail.com <gu.martinm@gmail.com>
Mon, 6 Oct 2014 19:49:14 +0000 (21:49 +0200)
committergu.martinm@gmail.com <gu.martinm@gmail.com>
Mon, 6 Oct 2014 19:49:14 +0000 (21:49 +0200)
res/layout/weather_current_fragment.xml
res/values/strings.xml
res/xml/appwidget_provider.xml
src/de/example/exampletdd/WidgetIntentService.java
src/de/example/exampletdd/fragment/overview/OverviewFragment.java

index 4f2ec5c..3533424 100644 (file)
         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" />
 
index a59baa2..210e158 100644 (file)
@@ -39,4 +39,5 @@
        <string name="text_units_centigrade">ÂșC</string>
        <string name="text_units_ms">m/s</string>
        <string name="text_units_hpa">hpa</string>
+       <string name="text_field_remote_error">No data available</string>
 </resources>
index 74390c6..c710a99 100644 (file)
@@ -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">
index 3f686b8..4319637 100644 (file)
@@ -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());
index b3094ea..fd746e0 100644 (file)
@@ -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);
     }