1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.weatherlistwidget;
18 
19 import android.appwidget.AppWidgetManager;
20 import android.appwidget.AppWidgetProvider;
21 import android.content.ContentProvider;
22 import android.content.ContentValues;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.content.res.Resources;
26 import android.database.Cursor;
27 import android.database.MatrixCursor;
28 import android.net.Uri;
29 
30 import java.util.ArrayList;
31 
32 /**
33  * A placeholder class that we are going to use internally to store weather data.  Generally, this data
34  * will be stored in an external and persistent location (ie. File, Database, SharedPreferences) so
35  * that the data can persist if the process is ever killed.  For simplicity, in this sample the
36  * data will only be stored in memory.
37  */
38 class WeatherDataPoint {
39     String day;
40     int degrees;
41 
WeatherDataPoint(String d, int deg)42     WeatherDataPoint(String d, int deg) {
43         day = d;
44         degrees = deg;
45     }
46 }
47 
48 /**
49  * The AppWidgetProvider for our sample weather widget.
50  */
51 public class WeatherDataProvider extends ContentProvider {
52     public static final Uri CONTENT_URI =
53         Uri.parse("content://com.example.android.weatherlistwidget.provider");
54     public static class Columns {
55         public static final String ID = "_id";
56         public static final String DAY = "day";
57         public static final String TEMPERATURE = "temperature";
58     }
59 
60     /**
61      * Generally, this data will be stored in an external and persistent location (ie. File,
62      * Database, SharedPreferences) so that the data can persist if the process is ever killed.
63      * For simplicity, in this sample the data will only be stored in memory.
64      */
65     private static final ArrayList<WeatherDataPoint> sData = new ArrayList<WeatherDataPoint>();
66 
67     @Override
onCreate()68     public boolean onCreate() {
69         // We are going to initialize the data provider with some default values
70         sData.add(new WeatherDataPoint("Monday", 13));
71         sData.add(new WeatherDataPoint("Tuesday", 1));
72         sData.add(new WeatherDataPoint("Wednesday", 7));
73         sData.add(new WeatherDataPoint("Thursday", 4));
74         sData.add(new WeatherDataPoint("Friday", 22));
75         sData.add(new WeatherDataPoint("Saturday", -10));
76         sData.add(new WeatherDataPoint("Sunday", -13));
77         sData.add(new WeatherDataPoint("Monday", 8));
78         sData.add(new WeatherDataPoint("Tuesday", 11));
79         sData.add(new WeatherDataPoint("Wednesday", -1));
80         sData.add(new WeatherDataPoint("Thursday", 27));
81         sData.add(new WeatherDataPoint("Friday", 27));
82         sData.add(new WeatherDataPoint("Saturday", 27));
83         sData.add(new WeatherDataPoint("Sunday", 27));
84         return true;
85     }
86 
87     @Override
query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)88     public synchronized Cursor query(Uri uri, String[] projection, String selection,
89             String[] selectionArgs, String sortOrder) {
90         assert(uri.getPathSegments().isEmpty());
91 
92         // In this sample, we only query without any parameters, so we can just return a cursor to
93         // all the weather data.
94         final MatrixCursor c = new MatrixCursor(
95                 new String[]{ Columns.ID, Columns.DAY, Columns.TEMPERATURE });
96         for (int i = 0; i < sData.size(); ++i) {
97             final WeatherDataPoint data = sData.get(i);
98             c.addRow(new Object[]{ new Integer(i), data.day, new Integer(data.degrees) });
99         }
100         return c;
101     }
102 
103     @Override
getType(Uri uri)104     public String getType(Uri uri) {
105         return "vnd.android.cursor.dir/vnd.weatherlistwidget.temperature";
106     }
107 
108     @Override
insert(Uri uri, ContentValues values)109     public Uri insert(Uri uri, ContentValues values) {
110         // This example code does not support inserting
111         return null;
112     }
113 
114     @Override
delete(Uri uri, String selection, String[] selectionArgs)115     public int delete(Uri uri, String selection, String[] selectionArgs) {
116         // This example code does not support deleting
117         return 0;
118     }
119 
120     @Override
update(Uri uri, ContentValues values, String selection, String[] selectionArgs)121     public synchronized int update(Uri uri, ContentValues values, String selection,
122             String[] selectionArgs) {
123         assert(uri.getPathSegments().size() == 1);
124 
125         // In this sample, we only update the content provider individually for each row with new
126         // temperature values.
127         final int index = Integer.parseInt(uri.getPathSegments().get(0));
128         final MatrixCursor c = new MatrixCursor(
129                 new String[]{ Columns.ID, Columns.DAY, Columns.TEMPERATURE });
130         assert(0 <= index && index < sData.size());
131         final WeatherDataPoint data = sData.get(index);
132         data.degrees = values.getAsInteger(Columns.TEMPERATURE);
133 
134         // Notify any listeners that the data backing the content provider has changed, and return
135         // the number of rows affected.
136         getContext().getContentResolver().notifyChange(uri, null);
137         return 1;
138     }
139 
140 }
141