aboutsummaryrefslogtreecommitdiff
path: root/src/java/com/android/internal/telephony/analytics/TelephonyAnalyticsUtil.java
blob: 78b607b279502fa657f90b262bdcf225fb839332 (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
/*
 * Copyright (C) 2023 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.internal.telephony.analytics;

import static com.android.internal.telephony.analytics.TelephonyAnalyticsDatabase.DATE_FORMAT;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.android.internal.annotations.VisibleForTesting;
import com.android.telephony.Rlog;

import java.util.Calendar;

/**
 * Singleton Utility class to support TelephonyAnalytics Extends SQLiteOpenHelper class. Supports db
 * related operations which includes creating tables,insertion,updation,deletion. Supports some
 * generic functionality like getting the Cursor resulting from a query.
 */
public class TelephonyAnalyticsUtil extends SQLiteOpenHelper {
    private static TelephonyAnalyticsUtil sTelephonyAnalyticsUtil;
    private static final String DATABASE_NAME = "telephony_analytics.db";
    private static final int DATABASE_VERSION = 10;
    private static final String TAG = TelephonyAnalyticsUtil.class.getSimpleName();
    private static final int MAX_ENTRIES_LIMIT = 1000;
    private static final int CUTOFF_MONTHS = 2;

    private TelephonyAnalyticsUtil(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    /**
     * Get the instance of the TelephonyAnalyticsUtil class. Instantiates the TelephonyAnalyticsUtil
     * object sTelephonyAnalyticsUtil only once.
     *
     * @return Returns the TelephonyAnalyticsUtil object sTelephonyAnalyticsUtil.
     */
    public static synchronized TelephonyAnalyticsUtil getInstance(Context context) {
        if (sTelephonyAnalyticsUtil == null) {
            sTelephonyAnalyticsUtil = new TelephonyAnalyticsUtil(context);
        }
        return sTelephonyAnalyticsUtil;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {}

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}

    /**
     * Uses Util class functionality to create CallTable in db
     *
     * @param createTableQuery : CallTable Schema
     */
    @VisibleForTesting
    public synchronized void createTable(String createTableQuery) {
        try {
            SQLiteDatabase db = getWritableDatabase();
            db.execSQL(createTableQuery);
        } catch (Exception e) {
            Rlog.e(TAG, "Error during table creation : " + e);
        }
    }

    /** Utility function that performs insertion on the given database table */
    @VisibleForTesting
    public synchronized void insert(String tableName, ContentValues values) {
        try {
            SQLiteDatabase db = getWritableDatabase();
            db.insert(tableName, null, values);
        } catch (SQLException e) {
            Rlog.e(TAG, "error occurred during insertion");
        }
    }

    /** Utility function that performs update query on the given database table */
    @VisibleForTesting
    public synchronized int update(
            String table, ContentValues values, String whereClause, String[] whereArgs) {
        int rowsAffected = -1;
        try {
            SQLiteDatabase db = getWritableDatabase();
            rowsAffected = db.update(table, values, whereClause, whereArgs);

        } catch (SQLException e) {
            Rlog.e(TAG, "Error during update.");
        }
        return rowsAffected;
    }

    /**
     * @Return the cursor object obtained from running a query based on given parameters.
     */
    @VisibleForTesting
    public synchronized Cursor getCursor(
            String tableName,
            String[] columns,
            String selection,
            String[] selectionArgs,
            String groupBy,
            String having,
            String orderBy,
            String limit) {

        Cursor cursor = null;
        try {
            SQLiteDatabase db = getReadableDatabase();
            cursor =
                    db.query(
                            tableName,
                            columns,
                            selection,
                            selectionArgs,
                            groupBy,
                            having,
                            orderBy,
                            limit);
        } catch (SQLException e) {
            Rlog.e(TAG, "Error during querying for getCursor()" + e);
        }
        return cursor;
    }

    /** Returns the count stored in the cursor obtained from query execution. */
    @VisibleForTesting
    public synchronized long getCountFromCursor(Cursor cursor) {
        long count = 0;
        if (cursor != null && cursor.moveToFirst()) {
            count = cursor.getInt(0);
        }
        return count;
    }

    /** Deletes Old Data and Overflow data in the db. */
    @VisibleForTesting
    public void deleteOverflowAndOldData(
            String tableName, String overflowWhereClause, String oldDataWhereClause) {
        deleteOverFlowData(tableName, overflowWhereClause);
        deleteOldData(tableName, oldDataWhereClause);
    }

    protected void deleteOverFlowData(String tableName, String whereClause) {
        String[] whereArgs = {Integer.toString(MAX_ENTRIES_LIMIT)};
        delete(tableName, whereClause, whereArgs);
    }

    protected void deleteOldData(String tableName, String whereClause) {
        String[] whereArgs = {getCutoffDate()};
        delete(tableName, whereClause, whereArgs);
    }

    /** Utility function that performs deletion on the database table */
    @VisibleForTesting
    public synchronized void delete(String tableName, String whereClause, String[] whereArgs) {
        try {
            SQLiteDatabase db = getWritableDatabase();
            db.delete(tableName, whereClause, whereArgs);
        } catch (SQLException e) {
            Rlog.e(TAG, "Sqlite Operation Error during deletion of Overflow data " + e);
        }
    }

    private String getCutoffDate() {
        Calendar cutoffDate = Calendar.getInstance();
        cutoffDate.add(Calendar.MONTH, -1 * CUTOFF_MONTHS);
        return DATE_FORMAT.format(cutoffDate.toInstant());
    }
}