Skip to content

Commit 9cacc28

Browse files
committed
feat(SQLite): LP-4345 adding SQLite.
1 parent 9b63cc8 commit 9cacc28

File tree

3 files changed

+302
-116
lines changed

3 files changed

+302
-116
lines changed

AndroidSDK/src/com/leanplum/Leanplum.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.leanplum.callbacks.StartCallback;
3939
import com.leanplum.callbacks.VariablesChangedCallback;
4040
import com.leanplum.internal.Constants;
41+
import com.leanplum.internal.LeanplumEventDataManager;
4142
import com.leanplum.internal.FileManager;
4243
import com.leanplum.internal.JsonConverter;
4344
import com.leanplum.internal.LeanplumInternal;
@@ -591,6 +592,7 @@ protected Void doInBackground(Void... params) {
591592

592593
private static void startHelper(
593594
String userId, final Map<String, ?> attributes, final boolean isBackground) {
595+
LeanplumEventDataManager.init(context);
594596
LeanplumPushService.onStart();
595597

596598
Boolean limitAdTracking = null;
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
/*
2+
* Copyright 2017, Leanplum, Inc. All rights reserved.
3+
*
4+
* Licensed to the Apache Software Foundation (ASF) under one
5+
* or more contributor license agreements. See the NOTICE file
6+
* distributed with this work for additional information
7+
* regarding copyright ownership. The ASF licenses this file
8+
* to you under the Apache License, Version 2.0 (the
9+
* "License"); you may not use this file except in compliance
10+
* with the License. You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing,
15+
* software distributed under the License is distributed on an
16+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
* KIND, either express or implied. See the License for the
18+
* specific language governing permissions and limitations
19+
* under the License.
20+
*/
21+
22+
package com.leanplum.internal;
23+
24+
import android.content.ContentValues;
25+
import android.content.Context;
26+
import android.database.Cursor;
27+
28+
import android.database.DatabaseUtils;
29+
import android.database.sqlite.SQLiteDatabase;
30+
import android.database.sqlite.SQLiteDatabaseCorruptException;
31+
32+
import org.json.JSONObject;
33+
34+
import java.io.File;
35+
import java.util.ArrayList;
36+
import java.util.List;
37+
import java.util.Map;
38+
39+
/**
40+
* LeanplumEventDataManager class to work with SQLite.
41+
*
42+
* @author Anna Orlova
43+
*/
44+
public class LeanplumEventDataManager {
45+
private static final String DATABASE_NAME = "__leanplum.db";
46+
private static final String EVENT_TABLE_NAME = "event";
47+
private static final String COLUMN_DATA = "data";
48+
private static final String KEY_ROWID = "rowid";
49+
50+
private static SQLiteDatabase database;
51+
private static ContentValues contentValues = new ContentValues();
52+
53+
public static void init(Context context) {
54+
if (database != null) {
55+
Log.e("Database is already initialized.");
56+
return;
57+
}
58+
File dbFile = new File(context.getFilesDir(), DATABASE_NAME);
59+
boolean needMigration = false;
60+
if (!dbFile.exists()) {
61+
needMigration = true;
62+
}
63+
// Create database if needed.
64+
try {
65+
database = SQLiteDatabase.openDatabase(dbFile.getAbsolutePath(), null,
66+
SQLiteDatabase.CREATE_IF_NECESSARY);
67+
} catch (SQLiteDatabaseCorruptException e) {
68+
Log.e("Database is corrupted. Recreate.");
69+
try {
70+
if (!dbFile.exists() || dbFile.delete()) {
71+
database = SQLiteDatabase.openDatabase(dbFile.getAbsolutePath(), null,
72+
SQLiteDatabase.CREATE_IF_NECESSARY);
73+
}
74+
} catch (Throwable t) {
75+
Log.e("Cannot create database. Retry failed.", t);
76+
Util.handleException(t);
77+
}
78+
} catch (Throwable t) {
79+
Log.e("Cannot create database.", t);
80+
Util.handleException(t);
81+
}
82+
if (database != null) {
83+
// Create table.
84+
try {
85+
database.execSQL("CREATE TABLE IF NOT EXISTS " + EVENT_TABLE_NAME + "(" + COLUMN_DATA +
86+
" TEXT)");
87+
if (needMigration) {
88+
Request.moveOldDataFromSharedPreferences();
89+
}
90+
} catch (Throwable t) {
91+
Log.e("Cannot create table.", t);
92+
Util.handleException(t);
93+
}
94+
}
95+
}
96+
97+
/**
98+
* Inserts event to event table.
99+
*
100+
* @param event String with json of event.
101+
*/
102+
static void insertEvent(String event) {
103+
if (database == null) {
104+
return;
105+
}
106+
contentValues.put(COLUMN_DATA, event);
107+
try {
108+
database.insert(EVENT_TABLE_NAME, null, contentValues);
109+
} catch (Throwable t) {
110+
Log.e("Unable to insert event to database.", t);
111+
Util.handleException(t);
112+
}
113+
}
114+
115+
/**
116+
* Gets first count events from event table.
117+
*
118+
* @param count Number of events.
119+
* @return List of events.
120+
*/
121+
static List<Map<String, Object>> getEvents(int count) {
122+
List<Map<String, Object>> events = new ArrayList<>();
123+
if (database == null) {
124+
return events;
125+
}
126+
Cursor cursor = null;
127+
try {
128+
cursor = database.query(EVENT_TABLE_NAME, new String[] {COLUMN_DATA}, null, null, null,
129+
null, KEY_ROWID + " ASC", "" + count);
130+
if (cursor.moveToFirst()) {
131+
while (!cursor.isAfterLast()) {
132+
Map<String, Object> requestArgs = JsonConverter.mapFromJson(new JSONObject(
133+
cursor.getString(cursor.getColumnIndex(COLUMN_DATA))));
134+
events.add(requestArgs);
135+
cursor.moveToNext();
136+
}
137+
}
138+
} catch (Throwable t) {
139+
Log.e("Unable to get events from the table.", t);
140+
Util.handleException(t);
141+
} finally {
142+
if (cursor != null) {
143+
cursor.close();
144+
}
145+
}
146+
return events;
147+
}
148+
149+
/**
150+
* Deletes first count elements from event table.
151+
*
152+
* @param count Number of event that need to be deleted.
153+
*/
154+
static void deleteEvents(int count) {
155+
if (database == null) {
156+
return;
157+
}
158+
try {
159+
database.delete(EVENT_TABLE_NAME, KEY_ROWID + " in (select " +
160+
KEY_ROWID + " from " + EVENT_TABLE_NAME + " LIMIT " + count + ")", null);
161+
} catch (Throwable t) {
162+
Log.e("Unable to delete events from the table.", t);
163+
Util.handleException(t);
164+
}
165+
}
166+
167+
/**
168+
* Gets number of rows in the event table.
169+
*
170+
* @return Number of rows in the event table.
171+
*/
172+
static long getEventsCount() {
173+
long count = 0;
174+
if (database == null) {
175+
return count;
176+
}
177+
try {
178+
count = DatabaseUtils.queryNumEntries(database, EVENT_TABLE_NAME);
179+
} catch (Throwable t) {
180+
Log.e("Unable to get a number of rows in the table.", t);
181+
Util.handleException(t);
182+
}
183+
return count;
184+
}
185+
}

0 commit comments

Comments
 (0)