summaryrefslogtreecommitdiff
path: root/java/com/android/dialer/callrecord/CallRecordingDataStore.java
blob: 88b603b54262b4a24c17c52d327fb61447486720 (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
 * Copyright (C) 2014 The CyanogenMod 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.dialer.callrecord;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.provider.BaseColumns;
import android.util.Log;
import android.util.SparseArray;

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

/**
 * Persistent data store for call recordings.  Usage:
 * open()
 * read/write operations
 * close()
 */
public class CallRecordingDataStore {
  private static final String TAG = "CallRecordingStore";
  private SQLiteOpenHelper mOpenHelper = null;
  private SQLiteDatabase mDatabase = null;

  /**
   * Open before reading/writing.  Will not open handle if one is already open.
   */
  public void open(Context context) {
    if (mDatabase == null) {
      mOpenHelper = new CallRecordingSQLiteOpenHelper(context);
      mDatabase = mOpenHelper.getWritableDatabase();
    }
  }

  /**
   * close when finished reading/writing
   */
  public void close() {
    if (mDatabase != null) {
      mDatabase.close();
    }
    if (mOpenHelper != null) {
      mOpenHelper.close();
    }
    mDatabase = null;
    mOpenHelper = null;
  }

  /**
   * Save a recording in the data store
   *
   * @param recording the recording to store
   */
  public void putRecording(CallRecording recording) {
    final String insertSql = "INSERT INTO " +
        CallRecordingsContract.CallRecording.TABLE_NAME + " (" +
        CallRecordingsContract.CallRecording.COLUMN_NAME_PHONE_NUMBER + ", " +
        CallRecordingsContract.CallRecording.COLUMN_NAME_CALL_DATE + ", " +
        CallRecordingsContract.CallRecording.COLUMN_NAME_RECORDING_FILENAME + ", " +
        CallRecordingsContract.CallRecording.COLUMN_NAME_CREATION_DATE + ", " +
        CallRecordingsContract.CallRecording.COLUMN_NAME_MEDIA_ID + ") " +
        " VALUES (?, ?, ?, ?, ?)";

    try {
      SQLiteStatement stmt = mDatabase.compileStatement(insertSql);
      int idx = 1;
      stmt.bindString(idx++, recording.phoneNumber);
      stmt.bindLong(idx++, recording.creationTime);
      stmt.bindString(idx++, recording.fileName);
      stmt.bindLong(idx++, System.currentTimeMillis());
      stmt.bindLong(idx++, recording.mediaId);
      long id = stmt.executeInsert();
      Log.i(TAG, "Saved recording " + recording + " with id " + id);
    } catch (SQLiteException e) {
      Log.w(TAG, "Failed to save recording " + recording, e);
    }
  }

  /**
   * Get all recordings associated with a phone call
   *
   * @param phoneNumber phone number no spaces
   * @param callCreationDate time that the call was created
   * @return list of recordings
   */
  public List<CallRecording> getRecordings(String phoneNumber, long callCreationDate) {
    List<CallRecording> resultList = new ArrayList<CallRecording>();

    final String query = "SELECT " +
        CallRecordingsContract.CallRecording.COLUMN_NAME_RECORDING_FILENAME + "," +
        CallRecordingsContract.CallRecording.COLUMN_NAME_CREATION_DATE + "," +
        CallRecordingsContract.CallRecording.COLUMN_NAME_MEDIA_ID +
        " FROM " + CallRecordingsContract.CallRecording.TABLE_NAME +
        " WHERE " + CallRecordingsContract.CallRecording.COLUMN_NAME_PHONE_NUMBER + " = ?" +
        " AND " + CallRecordingsContract.CallRecording.COLUMN_NAME_CALL_DATE + " = ?" +
        " AND " + CallRecordingsContract.CallRecording.COLUMN_NAME_MEDIA_ID + " != 0" +
        " ORDER BY " + CallRecordingsContract.CallRecording.COLUMN_NAME_CREATION_DATE;

    String args[] = {
      phoneNumber, String.valueOf(callCreationDate)
    };

    try {
      Cursor cursor = mDatabase.rawQuery(query, args);
      while (cursor.moveToNext()) {
        String fileName = cursor.getString(0);
        long creationDate = cursor.getLong(1);
        long mediaId = cursor.getLong(2);
        // FIXME: need to check whether media entry still exists?
        resultList.add(
            new CallRecording(phoneNumber, callCreationDate, fileName, creationDate, mediaId));
      }
      cursor.close();
    } catch (SQLiteException e) {
      Log.w(TAG, "Failed to fetch recordings for number " + phoneNumber +
          ", date " + callCreationDate, e);
    }

    return resultList;
  }

  public SparseArray<CallRecording> getUnmigratedRecordingData() {
    final String query = "SELECT " +
        CallRecordingsContract.CallRecording._ID + "," +
        CallRecordingsContract.CallRecording.COLUMN_NAME_PHONE_NUMBER + "," +
        CallRecordingsContract.CallRecording.COLUMN_NAME_RECORDING_FILENAME + "," +
        CallRecordingsContract.CallRecording.COLUMN_NAME_CREATION_DATE +
        " FROM " + CallRecordingsContract.CallRecording.TABLE_NAME +
        " WHERE " + CallRecordingsContract.CallRecording.COLUMN_NAME_MEDIA_ID + " == 0";
    final SparseArray<CallRecording> result = new SparseArray<>();

    try {
      Cursor cursor = mDatabase.rawQuery(query, null);
      while (cursor.moveToNext()) {
        int id = cursor.getInt(0);
        String phoneNumber = cursor.getString(1);
        String fileName = cursor.getString(2);
        long creationDate = cursor.getLong(3);
        CallRecording recording = new CallRecording(
            phoneNumber, creationDate, fileName, creationDate, 0);
        result.put(id, recording);
      }
      cursor.close();
    } catch (SQLiteException e) {
      Log.w(TAG, "Failed to fetch recordings for migration", e);
    }

    return result;
  }

  public void updateMigratedRecording(int id, int mediaId) {
    ContentValues cv = new ContentValues(1);
    cv.put(CallRecordingsContract.CallRecording.COLUMN_NAME_MEDIA_ID, mediaId);
    mDatabase.update(CallRecordingsContract.CallRecording.TABLE_NAME, cv,
        CallRecordingsContract.CallRecording._ID + " = ?", new String[] { String.valueOf(id) });
  }

  static class CallRecordingsContract {
    static interface CallRecording extends BaseColumns {
      static final String TABLE_NAME = "call_recordings";
      static final String COLUMN_NAME_PHONE_NUMBER = "phone_number";
      static final String COLUMN_NAME_CALL_DATE = "call_date";
      static final String COLUMN_NAME_RECORDING_FILENAME = "recording_filename";
      static final String COLUMN_NAME_CREATION_DATE = "creation_date";
      static final String COLUMN_NAME_MEDIA_ID = "media_id";
    }
  }

  static class CallRecordingSQLiteOpenHelper extends SQLiteOpenHelper {
    private static final int VERSION = 2;
    private static final String DB_NAME = "callrecordings.db";

    public CallRecordingSQLiteOpenHelper(Context context) {
      super(context, DB_NAME, null, VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
      db.execSQL("CREATE TABLE " + CallRecordingsContract.CallRecording.TABLE_NAME + " (" +
          CallRecordingsContract.CallRecording._ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
          CallRecordingsContract.CallRecording.COLUMN_NAME_PHONE_NUMBER + " TEXT," +
          CallRecordingsContract.CallRecording.COLUMN_NAME_CALL_DATE + " LONG," +
          CallRecordingsContract.CallRecording.COLUMN_NAME_RECORDING_FILENAME + " TEXT, " +
          CallRecordingsContract.CallRecording.COLUMN_NAME_CREATION_DATE + " LONG," +
          CallRecordingsContract.CallRecording.COLUMN_NAME_MEDIA_ID + " INTEGER DEFAULT 0" +
          ");"
      );

      db.execSQL("CREATE INDEX IF NOT EXISTS phone_number_call_date_index ON " +
          CallRecordingsContract.CallRecording.TABLE_NAME + " (" +
          CallRecordingsContract.CallRecording.COLUMN_NAME_PHONE_NUMBER + ", " +
          CallRecordingsContract.CallRecording.COLUMN_NAME_CALL_DATE + ");"
      );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      if (oldVersion < 2) {
        db.execSQL("ALTER TABLE " + CallRecordingsContract.CallRecording.TABLE_NAME +
            " ADD COLUMN " + CallRecordingsContract.CallRecording.COLUMN_NAME_MEDIA_ID +
            " INTEGER DEFAULT 0;");
      }
    }
  }
}