Home | History | Annotate | Download | only in weatherlistwidget
      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 dummy 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 city;
     40     int degrees;
     41 
     42     WeatherDataPoint(String c, int d) {
     43         city = c;
     44         degrees = d;
     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 CITY = "city";
     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
     68     public boolean onCreate() {
     69         // We are going to initialize the data provider with some default values
     70         sData.add(new WeatherDataPoint("San Francisco", 13));
     71         sData.add(new WeatherDataPoint("New York", 1));
     72         sData.add(new WeatherDataPoint("Seattle", 7));
     73         sData.add(new WeatherDataPoint("Boston", 4));
     74         sData.add(new WeatherDataPoint("Miami", 22));
     75         sData.add(new WeatherDataPoint("Toronto", -10));
     76         sData.add(new WeatherDataPoint("Calgary", -13));
     77         sData.add(new WeatherDataPoint("Tokyo", 8));
     78         sData.add(new WeatherDataPoint("Kyoto", 11));
     79         sData.add(new WeatherDataPoint("London", -1));
     80         sData.add(new WeatherDataPoint("Nomanisan", 27));
     81         return true;
     82     }
     83 
     84     @Override
     85     public synchronized Cursor query(Uri uri, String[] projection, String selection,
     86             String[] selectionArgs, String sortOrder) {
     87         assert(uri.getPathSegments().isEmpty());
     88 
     89         // In this sample, we only query without any parameters, so we can just return a cursor to
     90         // all the weather data.
     91         final MatrixCursor c = new MatrixCursor(
     92                 new String[]{ Columns.ID, Columns.CITY, Columns.TEMPERATURE });
     93         for (int i = 0; i < sData.size(); ++i) {
     94             final WeatherDataPoint data = sData.get(i);
     95             c.addRow(new Object[]{ new Integer(i), data.city, new Integer(data.degrees) });
     96         }
     97         return c;
     98     }
     99 
    100     @Override
    101     public String getType(Uri uri) {
    102         return "vnd.android.cursor.dir/vnd.weatherlistwidget.citytemperature";
    103     }
    104 
    105     @Override
    106     public Uri insert(Uri uri, ContentValues values) {
    107         // This example code does not support inserting
    108         return null;
    109     }
    110 
    111     @Override
    112     public int delete(Uri uri, String selection, String[] selectionArgs) {
    113         // This example code does not support deleting
    114         return 0;
    115     }
    116 
    117     @Override
    118     public synchronized int update(Uri uri, ContentValues values, String selection,
    119             String[] selectionArgs) {
    120         assert(uri.getPathSegments().size() == 1);
    121 
    122         // In this sample, we only update the content provider individually for each row with new
    123         // temperature values.
    124         final int index = Integer.parseInt(uri.getPathSegments().get(0));
    125         final MatrixCursor c = new MatrixCursor(
    126                 new String[]{ Columns.ID, Columns.CITY, Columns.TEMPERATURE });
    127         assert(0 <= index && index < sData.size());
    128         final WeatherDataPoint data = sData.get(index);
    129         data.degrees = values.getAsInteger(Columns.TEMPERATURE);
    130 
    131         // Notify any listeners that the data backing the content provider has changed, and return
    132         // the number of rows affected.
    133         getContext().getContentResolver().notifyChange(uri, null);
    134         return 1;
    135     }
    136 
    137 }