1/*
2 *
3 * Copyright 2001-2011 Texas Instruments, Inc. - http://www.ti.com/
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *    http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package com.ti.fmrxapp;
19
20
21import java.util.HashMap;
22
23import android.content.ContentValues;
24import android.content.Context;
25import android.database.Cursor;
26import android.database.SQLException;
27import android.database.sqlite.SQLiteDatabase;
28import android.database.sqlite.SQLiteOpenHelper;
29import android.util.Log;
30
31public class DBAdapter
32{
33    public static final String KEY_ROWID = "_id";
34    public static final String ITEM_KEY = "id";
35    public static final String ITEM_VALUE = "channel";
36    public static final String ITEM_NAME = "name";
37    private static final String TAG = "DBAdapter";
38    private static final String DATABASE_NAME = "presets.db";
39    private static final String DATABASE_TABLE = "presets";
40    private static final int DATABASE_VERSION = 1;
41
42    private static final String DATABASE_CREATE =
43       "create table presets (_id integer primary key autoincrement, "
44       + "id text not null, channel text not null, "
45       + "name text not null);";
46
47    private final Context context;
48
49    private DatabaseHelper DBHelper;
50    private SQLiteDatabase db;
51
52    public DBAdapter(Context ctx)
53    {
54       this.context = ctx;
55       DBHelper = new DatabaseHelper(context);
56        Log.d(TAG," DBAdapter const");
57    }
58
59    private static class DatabaseHelper extends SQLiteOpenHelper
60    {
61       DatabaseHelper(Context context)
62       {
63
64          super(context, DATABASE_NAME, null, DATABASE_VERSION);
65           Log.d(TAG," DatabaseHelper con");
66       }
67
68       @Override
69       public void onCreate(SQLiteDatabase db)
70       {
71           Log.d(TAG," DatabaseHelper onCreate");
72          db.execSQL(DATABASE_CREATE);
73       }
74
75       @Override
76       public void onUpgrade(SQLiteDatabase db, int oldVersion,
77       int newVersion)
78       {
79          Log.w(TAG, "Upgrading database from version " + oldVersion
80                + " to "
81                + newVersion + ", which will destroy all old data");
82          db.execSQL("DROP TABLE IF EXISTS presets");
83          onCreate(db);
84       }
85    }
86
87    //---opens the database---
88    public DBAdapter open() throws SQLException
89    {
90       db = DBHelper.getWritableDatabase();
91        Log.d(TAG," DatabaseHelper open");
92       return this;
93    }
94
95    //---closes the database---
96    public void close()
97    {
98       DBHelper.close();
99        Log.d(TAG," DatabaseHelper close");
100    }
101
102    //---insert a station into the database---
103    public long insertStation(String itemkey, String itemValue, String itemName)
104    {
105
106        Log.d(TAG," DatabaseHelper insertStation");
107       ContentValues initialValues = new ContentValues();
108       initialValues.put(ITEM_KEY, itemkey);
109       initialValues.put(ITEM_VALUE, itemValue);
110       initialValues.put(ITEM_NAME, itemName);
111       return db.insert(DATABASE_TABLE, null, initialValues);
112    }
113
114    //---deletes a particular station---
115    public boolean deleteStation(long rowId)
116    {
117        Log.d(TAG," DatabaseHelper deleteStation");
118       return db.delete(DATABASE_TABLE, KEY_ROWID +
119               "=" + rowId, null) > 0;
120    }
121
122    //---retrieves all the Stations---
123    public Cursor getAllStations()
124    {
125        Log.d(TAG," DatabaseHelper getAllStations");
126       return db.query(DATABASE_TABLE, new String[] {
127               KEY_ROWID,
128               ITEM_KEY,
129               ITEM_VALUE,
130               ITEM_NAME},
131             null,
132             null,
133             null,
134             null,
135             null);
136    }
137
138    //---retrieves a particular station---
139    public Cursor getStation(long rowId) throws SQLException
140    {
141
142        Log.d(TAG," DatabaseHelper getStation");
143       Cursor mCursor =
144             db.query(true, DATABASE_TABLE, new String[] {
145                     KEY_ROWID,
146                     ITEM_KEY,
147                     ITEM_VALUE,
148                     ITEM_NAME},
149                     KEY_ROWID + "=" + rowId,
150                     null,
151                     null,
152                     null,
153                     null,
154                     null);
155       if (mCursor != null) {
156          mCursor.moveToFirst();
157       }
158       return mCursor;
159    }
160
161    //---updates a station---
162    public boolean updateStation(int index, String itemKey,
163    String itemValue, String itemName)
164    {
165        Log.d(TAG," DatabaseHelper updateStation");
166       ContentValues args = new ContentValues();
167       args.put(ITEM_KEY, itemKey);
168       args.put(ITEM_VALUE, itemValue);
169       args.put(ITEM_NAME, itemName);
170       return db.update(DATABASE_TABLE, args,
171               KEY_ROWID + "=" + index, null) > 0;
172    }
173
174
175}
176