Home | History | Annotate | Download | only in cellbroadcastreceiver
      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.android.cellbroadcastreceiver;
     18 
     19 import android.content.Context;
     20 import android.database.Cursor;
     21 import android.database.sqlite.SQLiteDatabase;
     22 import android.database.sqlite.SQLiteOpenHelper;
     23 import android.provider.BaseColumns;
     24 
     25 public class CellBroadcastDatabase {
     26     private static final String TAG = "CellBroadcastDatabase";
     27 
     28     private CellBroadcastDatabase() {}
     29 
     30     static final String DATABASE_NAME = "cell_broadcasts.db";
     31     static final String TABLE_NAME = "broadcasts";
     32 
     33     static final int DATABASE_VERSION = 1;
     34 
     35     static final class Columns implements BaseColumns {
     36 
     37         private Columns() {}
     38 
     39         /**
     40          * Message geographical scope.
     41          * <P>Type: INTEGER</P>
     42          */
     43         public static final String GEOGRAPHICAL_SCOPE = "geo_scope";
     44 
     45         /**
     46          * Message serial number.
     47          * <P>Type: INTEGER</P>
     48          */
     49         public static final String SERIAL_NUMBER = "serial_number";
     50 
     51         /**
     52          * Message code.
     53          * <P>Type: INTEGER</P>
     54          */
     55         public static final String MESSAGE_CODE = "message_code";
     56 
     57         /**
     58          * Message identifier.
     59          * <P>Type: INTEGER</P>
     60          */
     61         public static final String MESSAGE_IDENTIFIER = "message_id";
     62 
     63         /**
     64          * Message language code.
     65          * <P>Type: TEXT</P>
     66          */
     67         public static final String LANGUAGE_CODE = "language";
     68 
     69         /**
     70          * Message body.
     71          * <P>Type: TEXT</P>
     72          */
     73         public static final String MESSAGE_BODY = "body";
     74 
     75         /**
     76          * Message delivery time.
     77          * <P>Type: INTEGER (long)</P>
     78          */
     79         public static final String DELIVERY_TIME = "date";
     80 
     81         /**
     82          * Has the message been viewed?
     83          * <P>Type: INTEGER (boolean)</P>
     84          */
     85         public static final String MESSAGE_READ = "read";
     86 
     87         /**
     88          * Query for list view adapter.
     89          */
     90         static final String[] QUERY_COLUMNS = {
     91                 _ID,
     92                 GEOGRAPHICAL_SCOPE,
     93                 SERIAL_NUMBER,
     94                 MESSAGE_CODE,
     95                 MESSAGE_IDENTIFIER,
     96                 LANGUAGE_CODE,
     97                 MESSAGE_BODY,
     98                 DELIVERY_TIME,
     99                 MESSAGE_READ,
    100         };
    101     }
    102 
    103     /* Column indexes for reading from cursor. */
    104 
    105     static final int COLUMN_ID                  = 0;
    106     static final int COLUMN_GEOGRAPHICAL_SCOPE  = 1;
    107     static final int COLUMN_SERIAL_NUMBER       = 2;
    108     static final int COLUMN_MESSAGE_CODE        = 3;
    109     static final int COLUMN_MESSAGE_IDENTIFIER  = 4;
    110     static final int COLUMN_LANGUAGE_CODE       = 5;
    111     static final int COLUMN_MESSAGE_BODY        = 6;
    112     static final int COLUMN_DELIVERY_TIME       = 7;
    113     static final int COLUMN_MESSAGE_READ        = 8;
    114 
    115     static class DatabaseHelper extends SQLiteOpenHelper {
    116 
    117         DatabaseHelper(Context context) {
    118             super(context, DATABASE_NAME, null, DATABASE_VERSION);
    119         }
    120 
    121         @Override
    122         public void onCreate(SQLiteDatabase db) {
    123             db.execSQL("CREATE TABLE " + TABLE_NAME + " ("
    124                     + Columns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
    125                     + Columns.GEOGRAPHICAL_SCOPE + " INTEGER,"
    126                     + Columns.SERIAL_NUMBER + " INTEGER,"
    127                     + Columns.MESSAGE_CODE + " INTEGER,"
    128                     + Columns.MESSAGE_IDENTIFIER + " INTEGER,"
    129                     + Columns.LANGUAGE_CODE + " TEXT,"
    130                     + Columns.MESSAGE_BODY + " TEXT,"
    131                     + Columns.DELIVERY_TIME + " INTEGER,"
    132                     + Columns.MESSAGE_READ + " INTEGER);");
    133         }
    134 
    135         @Override
    136         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    137             // ignored for now
    138         }
    139     }
    140 
    141     /**
    142      * Returns a Cursor for the list view adapter, in reverse chronological order.
    143      * @param db an open readable database
    144      * @return the cursor for the list view adapter
    145      */
    146     static Cursor getCursor(SQLiteDatabase db) {
    147         return db.query(false, TABLE_NAME, Columns.QUERY_COLUMNS,
    148                 null, null, null, null, Columns.DELIVERY_TIME + " DESC", null);
    149     }
    150 }