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.widgetdata; 18 19 import android.content.ContentProvider; 20 import android.content.ContentValues; 21 import android.database.Cursor; 22 import android.database.MatrixCursor; 23 import android.net.Uri; 24 25 import java.util.ArrayList; 26 27 /** 28 * A dummy class that we are going to use internally to store weather data. Generally, this data 29 * will be stored in an external and persistent location (ie. File, Database, SharedPreferences) so 30 * that the data can persist if the process is ever killed. For simplicity, in this sample the 31 * data will only be stored in memory. 32 */ 33 class WeatherDataPoint { 34 String day; 35 int degrees; 36 37 WeatherDataPoint(String d, int deg) { 38 day = d; 39 degrees = deg; 40 } 41 } 42 43 /** 44 * The AppWidgetProvider for our sample weather widget. 45 */ 46 public class WeatherDataProvider extends ContentProvider { 47 public static final Uri CONTENT_URI = 48 Uri.parse("content://com.example.android.widgetdata.provider"); 49 public static class Columns { 50 public static final String ID = "_id"; 51 public static final String DAY = "day"; 52 public static final String TEMPERATURE = "temperature"; 53 } 54 55 /** 56 * Generally, this data will be stored in an external and persistent location (ie. File, 57 * Database, SharedPreferences) so that the data can persist if the process is ever killed. 58 * For simplicity, in this sample the data will only be stored in memory. 59 */ 60 private static final ArrayList<WeatherDataPoint> sData = new ArrayList<WeatherDataPoint>(); 61 62 @Override 63 public boolean onCreate() { 64 // We are going to initialize the data provider with some default values 65 sData.add(new WeatherDataPoint("Monday", 13)); 66 sData.add(new WeatherDataPoint("Tuesday", 1)); 67 sData.add(new WeatherDataPoint("Wednesday", 7)); 68 sData.add(new WeatherDataPoint("Thursday", 4)); 69 sData.add(new WeatherDataPoint("Friday", 22)); 70 sData.add(new WeatherDataPoint("Saturday", -10)); 71 sData.add(new WeatherDataPoint("Sunday", -13)); 72 sData.add(new WeatherDataPoint("Monday", 8)); 73 sData.add(new WeatherDataPoint("Tuesday", 11)); 74 sData.add(new WeatherDataPoint("Wednesday", -1)); 75 sData.add(new WeatherDataPoint("Thursday", 27)); 76 sData.add(new WeatherDataPoint("Friday", 27)); 77 sData.add(new WeatherDataPoint("Saturday", 27)); 78 sData.add(new WeatherDataPoint("Sunday", 27)); 79 return true; 80 } 81 82 @Override 83 public synchronized Cursor query(Uri uri, String[] projection, String selection, 84 String[] selectionArgs, String sortOrder) { 85 assert(uri.getPathSegments().isEmpty()); 86 87 // In this sample, we only query without any parameters, so we can just return a cursor to 88 // all the weather data. 89 final MatrixCursor c = new MatrixCursor( 90 new String[]{ Columns.ID, Columns.DAY, Columns.TEMPERATURE }); 91 for (int i = 0; i < sData.size(); ++i) { 92 final WeatherDataPoint data = sData.get(i); 93 c.addRow(new Object[]{ new Integer(i), data.day, new Integer(data.degrees) }); 94 } 95 return c; 96 } 97 98 @Override 99 public String getType(Uri uri) { 100 return "vnd.android.cursor.dir/vnd.widgetdata.temperature"; 101 } 102 103 @Override 104 public Uri insert(Uri uri, ContentValues values) { 105 // This example code does not support inserting 106 return null; 107 } 108 109 @Override 110 public int delete(Uri uri, String selection, String[] selectionArgs) { 111 // This example code does not support deleting 112 return 0; 113 } 114 115 @Override 116 public synchronized int update(Uri uri, ContentValues values, String selection, 117 String[] selectionArgs) { 118 assert(uri.getPathSegments().size() == 1); 119 120 // In this sample, we only update the content provider individually for each row with new 121 // temperature values. 122 final int index = Integer.parseInt(uri.getPathSegments().get(0)); 123 final MatrixCursor c = new MatrixCursor( 124 new String[]{ Columns.ID, Columns.DAY, Columns.TEMPERATURE }); 125 assert(0 <= index && index < sData.size()); 126 final WeatherDataPoint data = sData.get(index); 127 data.degrees = values.getAsInteger(Columns.TEMPERATURE); 128 129 // Notify any listeners that the data backing the content provider has changed, and return 130 // the number of rows affected. 131 getContext().getContentResolver().notifyChange(uri, null); 132 return 1; 133 } 134 135 } 136