Create a 1x1 home screen widget using static data which launches Sunshine when tapped

This commit is contained in:
Dan Galpin
2015-05-25 04:09:34 -07:00
parent e7357a9631
commit 3ce8bff71c
8 changed files with 191 additions and 0 deletions

View File

@@ -109,6 +109,17 @@
<category android:name="com.example.android.sunshine.app" />
</intent-filter>
</receiver>
<!-- Today Widget -->
<receiver
android:name=".widget.TodayWidgetProvider"
android:label="@string/title_widget_today" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/widget_info_today" />
</receiver>
</application>
</manifest>

View File

@@ -0,0 +1,69 @@
/*
* Copyright (C) 2015 The Android Open Source Project
*
* 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.
*/
package com.example.android.sunshine.app.widget;
import android.annotation.TargetApi;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.widget.RemoteViews;
import com.example.android.sunshine.app.MainActivity;
import com.example.android.sunshine.app.R;
import com.example.android.sunshine.app.Utility;
/**
* Provider for a widget showing today's weather.
*/
public class TodayWidgetProvider extends AppWidgetProvider {
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
int weatherArtResourceId = R.drawable.art_clear;
String description = "Clear";
double maxTemp = 24;
String formattedMaxTemperature = Utility.formatTemperature(context, maxTemp);
// Perform this loop procedure for each Today widget
for (int appWidgetId : appWidgetIds) {
int layoutId = R.layout.widget_today_small;
RemoteViews views = new RemoteViews(context.getPackageName(), layoutId);
// Add the data to the RemoteViews
views.setImageViewResource(R.id.widget_icon, weatherArtResourceId);
// Content Descriptions for RemoteViews were only added in ICS MR1
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
setRemoteContentDescription(views, description);
}
views.setTextViewText(R.id.widget_high_temperature, formattedMaxTemperature);
// Create an Intent to launch MainActivity
Intent launchIntent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, launchIntent, 0);
views.setOnClickPendingIntent(R.id.widget, pendingIntent);
// Tell the AppWidgetManager to perform an update on the current app widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
private void setRemoteContentDescription(RemoteViews views, String description) {
views.setContentDescription(R.id.widget_icon, description);
}
}

View File

@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2015 The Android Open Source Project
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.
-->
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/widget_margin">
<LinearLayout
android:id="@+id/widget"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/touch_selector_white"
android:orientation="vertical"
android:padding="4dp">
<ImageView
android:id="@+id/widget_icon"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
tools:src="@drawable/art_clear" />
<TextView
android:id="@+id/widget_high_temperature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:fontFamily="sans-serif"
android:textAppearance="?android:textAppearanceLarge"
android:textColor="@color/primary_text"
tools:text="48" />
</LinearLayout>
</FrameLayout>

View File

@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2015 The Android Open Source Project
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.
-->
<resources>
<dimen name="widget_margin">0dp</dimen>
</resources>

View File

@@ -52,4 +52,8 @@
<dimen name="detail_card_elevation">6dp</dimen>
<dimen name="landscape_forecast_view_width">360dp</dimen>
<!-- Today Widget -->
<dimen name="widget_today_default_width">40dp</dimen>
<dimen name="widget_today_default_height">40dp</dimen>
</resources>

View File

@@ -136,6 +136,9 @@
<!-- Strings related to Notification preference -->
<string name="pref_last_notification">last_notification</string>
<!-- Strings related to Widgets -->
<string name="title_widget_today">Sunshine Today</string>
<!-- Empty Weather Database -->
<string name="empty_forecast_list">No Weather Information Available</string>
<string name="empty_forecast_list_no_network">No weather information available. The network is not available to fetch weather data.</string>

View File

@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2015 The Android Open Source Project
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.
-->
<resources>
<dimen name="widget_margin">8dp</dimen>
</resources>

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2015 The Android Open Source Project
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.
-->
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget_today_small"
android:minHeight="@dimen/widget_today_default_height"
android:minWidth="@dimen/widget_today_default_width"
android:updatePeriodMillis="0" />