aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/dvr/provider/DvrDatabaseHelper.java
blob: bdba8ac317e71e2f0ab03ce029035d22b870998c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.tv.dvr.provider;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.util.Log;

import com.android.tv.dvr.ScheduledRecording;
import com.android.tv.dvr.provider.DvrContract.Recordings;

import java.util.ArrayList;
import java.util.List;

/**
 * A data class for one recorded contents.
 */
public class DvrDatabaseHelper extends SQLiteOpenHelper {
    private static final String TAG = "DvrDatabaseHelper";
    private static final boolean DEBUG = true;

    private static final int DATABASE_VERSION = 4;
    private static final String DB_NAME = "dvr.db";

    private static final String SQL_CREATE_RECORDINGS =
            "CREATE TABLE " + Recordings.TABLE_NAME + "("
            + Recordings._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + Recordings.COLUMN_PRIORITY + " INTEGER DEFAULT " + Long.MAX_VALUE + ","
            + Recordings.COLUMN_TYPE + " TEXT NOT NULL,"
            + Recordings.COLUMN_CHANNEL_ID + " INTEGER NOT NULL,"
            + Recordings.COLUMN_PROGRAM_ID + " INTEGER ,"
            + Recordings.COLUMN_START_TIME_UTC_MILLIS + " INTEGER NOT NULL,"
            + Recordings.COLUMN_END_TIME_UTC_MILLIS + " INTEGER NOT NULL,"
            + Recordings.COLUMN_STATE + " TEXT NOT NULL)";

    private static final String SQL_DROP_RECORDINGS = "DROP TABLE IF EXISTS "
            + Recordings.TABLE_NAME;
    public static final String WHERE_RECORDING_ID_EQUALS = Recordings._ID + " = ?";

    public DvrDatabaseHelper(Context context) {
        super(context.getApplicationContext(), DB_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onConfigure(SQLiteDatabase db) {
        db.setForeignKeyConstraintsEnabled(true);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        if (DEBUG) Log.d(TAG, "Executing SQL: " + SQL_CREATE_RECORDINGS);
        db.execSQL(SQL_CREATE_RECORDINGS);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (DEBUG) Log.d(TAG, "Executing SQL: " + SQL_DROP_RECORDINGS);
        db.execSQL(SQL_DROP_RECORDINGS);
        onCreate(db);
    }

    /**
     * Handles the query request and returns a {@link Cursor}.
     */
    public Cursor query(String tableName, String[] projections) {
        SQLiteDatabase db = getReadableDatabase();
        SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
        builder.setTables(tableName);
        return builder.query(db, projections, null, null, null, null, null);
    }

    /**
     * Inserts recordings.
     *
     * @return The list of recordings with id set.  The id will be -1 if there was an error.
     */
    public List<ScheduledRecording> insertRecordings(ScheduledRecording... scheduledRecordings) {
        updateChannelsFromRecordings(scheduledRecordings);

        SQLiteDatabase db = getReadableDatabase();
        List<ScheduledRecording> results = new ArrayList<>();
        for (ScheduledRecording r : scheduledRecordings) {
            ContentValues values = ScheduledRecording.toContentValues(r);
            long id = db.insert(Recordings.TABLE_NAME, null, values);
            results.add(ScheduledRecording.buildFrom(r).setId(id).build());
        }
        return results;
    }

    /**
     * Update recordings.
     *
     * @return The list of row update counts.  The count will be -1 if there was an error or 0
     * if no match was found.  The count is expected to be exactly 1 for each recording.
     */
    public List<Integer> updateRecordings(ScheduledRecording[] scheduledRecordings) {
        updateChannelsFromRecordings(scheduledRecordings);
        SQLiteDatabase db = getWritableDatabase();
        List<Integer> results = new ArrayList<>();
        for (ScheduledRecording r : scheduledRecordings) {
            ContentValues values = ScheduledRecording.toContentValues(r);
            int updated = db.update(Recordings.TABLE_NAME, values, Recordings._ID + " = ?",
                    new String[] {String.valueOf(r.getId())});
            results.add(updated);
        }
        return results;
    }

    private void updateChannelsFromRecordings(ScheduledRecording[] scheduledRecordings) {
       // TODO(DVR) implement/
       // TODO(DVR) consider not deleting channels instead of keeping a separate table.
    }

    /**
     * Delete recordings.
     *
     * @return The list of row update counts.  The count will be -1 if there was an error or 0
     * if no match was found.  The count is expected to be exactly 1 for each recording.
     */
    public List<Integer> deleteRecordings(ScheduledRecording[] scheduledRecordings) {
        SQLiteDatabase db = getWritableDatabase();
        List<Integer> results = new ArrayList<>();
        for (ScheduledRecording r : scheduledRecordings) {
            int deleted = db.delete(Recordings.TABLE_NAME, WHERE_RECORDING_ID_EQUALS,
                    new String[] {String.valueOf(r.getId())});
            results.add(deleted);
        }
        return results;
    }
}