Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
SDK
/
exoplayer
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
82da627c
authored
Jan 18, 2019
by
olly
Committed by
Oliver Woodman
Jan 21, 2019
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Split out shared database components for reuse in caching
PiperOrigin-RevId: 229946997
parent
02dc937c
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
406 additions
and
338 deletions
library/core/src/main/java/com/google/android/exoplayer2/database/DatabaseProvider.java
library/core/src/main/java/com/google/android/exoplayer2/database/DefaultDatabaseProvider.java
library/core/src/main/java/com/google/android/exoplayer2/database/ExoDatabaseProvider.java
library/core/src/main/java/com/google/android/exoplayer2/database/VersionTable.java
library/core/src/main/java/com/google/android/exoplayer2/offline/DefaultDownloadIndex.java
library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadIndex.java
library/core/src/test/java/com/google/android/exoplayer2/database/VersionTableTest.java
library/core/src/test/java/com/google/android/exoplayer2/offline/DefaultDownloadIndexTest.java
library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadIndexUtilTest.java
library/core/src/main/java/com/google/android/exoplayer2/database/DatabaseProvider.java
0 → 100644
View file @
82da627c
/*
* Copyright (C) 2018 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
.
google
.
android
.
exoplayer2
.
database
;
import
android.database.sqlite.SQLiteDatabase
;
import
android.database.sqlite.SQLiteException
;
/**
* Provides {@link SQLiteDatabase} instances to ExoPlayer components, which may read and write
* tables prefixed with {@link #TABLE_PREFIX}.
*/
public
interface
DatabaseProvider
{
/** Prefix for tables that can be read and written by ExoPlayer components. */
String
TABLE_PREFIX
=
"ExoPlayer"
;
/**
* Creates and/or opens a database that will be used for reading and writing.
*
* <p>Once opened successfully, the database is cached, so you can call this method every time you
* need to write to the database. Errors such as bad permissions or a full disk may cause this
* method to fail, but future attempts may succeed if the problem is fixed.
*
* @throws SQLiteException If the database cannot be opened for writing.
* @return A read/write database object.
*/
SQLiteDatabase
getWritableDatabase
();
/**
* Creates and/or opens a database. This will be the same object returned by {@link
* #getWritableDatabase()} unless some problem, such as a full disk, requires the database to be
* opened read-only. In that case, a read-only database object will be returned. If the problem is
* fixed, a future call to {@link #getWritableDatabase()} may succeed, in which case the read-only
* database object will be closed and the read/write object will be returned in the future.
*
* <p>Once opened successfully, the database is cached, so you can call this method every time you
* need to read from the database.
*
* @throws SQLiteException If the database cannot be opened.
* @return A database object valid until {@link #getWritableDatabase()} is called.
*/
SQLiteDatabase
getReadableDatabase
();
}
library/core/src/main/java/com/google/android/exoplayer2/database/DefaultDatabaseProvider.java
0 → 100644
View file @
82da627c
/*
* Copyright (C) 2018 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
.
google
.
android
.
exoplayer2
.
database
;
import
android.database.sqlite.SQLiteDatabase
;
import
android.database.sqlite.SQLiteOpenHelper
;
/** A {@link DatabaseProvider} that provides instances obtained from a {@link SQLiteOpenHelper}. */
public
final
class
DefaultDatabaseProvider
implements
DatabaseProvider
{
private
final
SQLiteOpenHelper
sqliteOpenHelper
;
/**
* @param sqliteOpenHelper An {@link SQLiteOpenHelper} from which to obtain database instances.
*/
public
DefaultDatabaseProvider
(
SQLiteOpenHelper
sqliteOpenHelper
)
{
this
.
sqliteOpenHelper
=
sqliteOpenHelper
;
}
@Override
public
SQLiteDatabase
getWritableDatabase
()
{
return
sqliteOpenHelper
.
getWritableDatabase
();
}
@Override
public
SQLiteDatabase
getReadableDatabase
()
{
return
sqliteOpenHelper
.
getReadableDatabase
();
}
}
library/core/src/main/java/com/google/android/exoplayer2/database/ExoDatabaseProvider.java
0 → 100644
View file @
82da627c
/*
* Copyright (C) 2018 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
.
google
.
android
.
exoplayer2
.
database
;
import
android.content.Context
;
import
android.database.sqlite.SQLiteDatabase
;
import
android.database.sqlite.SQLiteOpenHelper
;
/**
* An {@link SQLiteOpenHelper} that provides instances of a standalone ExoPlayer database.
*
* <p>Suitable for use by applications that do not already have their own database, or which would
* prefer to keep ExoPlayer tables isolated in their own database. Other applications should prefer
* to use {@link DefaultDatabaseProvider} with their own {@link SQLiteOpenHelper}.
*/
public
final
class
ExoDatabaseProvider
extends
SQLiteOpenHelper
implements
DatabaseProvider
{
/** The file name used for the standalone ExoPlayer database. */
public
static
final
String
DATABASE_NAME
=
"exoplayer_internal.db"
;
private
static
final
int
VERSION
=
1
;
public
ExoDatabaseProvider
(
Context
context
)
{
super
(
context
.
getApplicationContext
(),
DATABASE_NAME
,
/* factory= */
null
,
VERSION
);
}
@Override
public
void
onCreate
(
SQLiteDatabase
db
)
{
// Features create their own tables.
}
@Override
public
void
onUpgrade
(
SQLiteDatabase
db
,
int
oldVersion
,
int
newVersion
)
{
// Features handle their own upgrades.
}
@Override
public
void
onDowngrade
(
SQLiteDatabase
db
,
int
oldVersion
,
int
newVersion
)
{
// TODO: Wipe the database.
super
.
onDowngrade
(
db
,
oldVersion
,
newVersion
);
}
}
library/core/src/main/java/com/google/android/exoplayer2/database/VersionTable.java
0 → 100644
View file @
82da627c
/*
* Copyright (C) 2018 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
.
google
.
android
.
exoplayer2
.
database
;
import
android.content.ContentValues
;
import
android.database.Cursor
;
import
android.database.DatabaseUtils
;
import
android.database.sqlite.SQLiteDatabase
;
import
android.support.annotation.IntDef
;
import
java.lang.annotation.Documented
;
import
java.lang.annotation.Retention
;
import
java.lang.annotation.RetentionPolicy
;
/**
* A table that holds version information about other ExoPlayer tables. This allows ExoPlayer tables
* to be versioned independently to the version of the containing database.
*/
public
final
class
VersionTable
{
/** Returned by {@link #getVersion(int)} if the version is unset. */
public
static
final
int
VERSION_UNSET
=
-
1
;
/** Version of tables used for offline functionality. */
public
static
final
int
FEATURE_OFFLINE
=
0
;
/** Version of tables used for cache functionality. */
public
static
final
int
FEATURE_CACHE
=
1
;
private
static
final
String
TABLE_NAME
=
DatabaseProvider
.
TABLE_PREFIX
+
"Versions"
;
private
static
final
String
COLUMN_FEATURE
=
"feature"
;
private
static
final
String
COLUMN_VERSION
=
"version"
;
private
static
final
String
SQL_CREATE_TABLE_IF_NOT_EXISTS
=
"CREATE TABLE IF NOT EXISTS "
+
TABLE_NAME
+
" ("
+
COLUMN_FEATURE
+
" INTEGER PRIMARY KEY NOT NULL,"
+
COLUMN_VERSION
+
" INTEGER NOT NULL)"
;
@Documented
@Retention
(
RetentionPolicy
.
SOURCE
)
@IntDef
({
FEATURE_OFFLINE
,
FEATURE_CACHE
})
private
@interface
Feature
{}
private
final
DatabaseProvider
databaseProvider
;
public
VersionTable
(
DatabaseProvider
databaseProvider
)
{
this
.
databaseProvider
=
databaseProvider
;
// Check whether the table exists to avoid getting a writable database if we don't need one.
if
(!
doesTableExist
(
databaseProvider
,
TABLE_NAME
))
{
databaseProvider
.
getWritableDatabase
().
execSQL
(
SQL_CREATE_TABLE_IF_NOT_EXISTS
);
}
}
/**
* Sets the version of tables belonging to the specified feature.
*
* @param feature The feature.
* @param version The version.
*/
public
void
setVersion
(
@Feature
int
feature
,
int
version
)
{
ContentValues
values
=
new
ContentValues
();
values
.
put
(
COLUMN_FEATURE
,
feature
);
values
.
put
(
COLUMN_VERSION
,
version
);
SQLiteDatabase
writableDatabase
=
databaseProvider
.
getWritableDatabase
();
writableDatabase
.
replace
(
TABLE_NAME
,
/* nullColumnHack= */
null
,
values
);
}
/**
* Returns the version of tables belonging to the specified feature, or {@link #VERSION_UNSET} if
* no version information is available.
*/
public
int
getVersion
(
@Feature
int
feature
)
{
String
selection
=
COLUMN_FEATURE
+
" = ?"
;
String
[]
selectionArgs
=
{
Integer
.
toString
(
feature
)};
try
(
Cursor
cursor
=
databaseProvider
.
getReadableDatabase
()
.
query
(
TABLE_NAME
,
new
String
[]
{
COLUMN_VERSION
},
selection
,
selectionArgs
,
/* groupBy= */
null
,
/* having= */
null
,
/* orderBy= */
null
))
{
if
(
cursor
.
getCount
()
==
0
)
{
return
VERSION_UNSET
;
}
cursor
.
moveToNext
();
return
cursor
.
getInt
(
/* COLUMN_VERSION index */
0
);
}
}
/* package */
static
boolean
doesTableExist
(
DatabaseProvider
databaseProvider
,
String
tableName
)
{
SQLiteDatabase
readableDatabase
=
databaseProvider
.
getReadableDatabase
();
long
count
=
DatabaseUtils
.
queryNumEntries
(
readableDatabase
,
"sqlite_master"
,
"tbl_name = ?"
,
new
String
[]
{
tableName
});
return
count
>
0
;
}
}
library/core/src/main/java/com/google/android/exoplayer2/offline/DefaultDownloadIndex.java
View file @
82da627c
...
...
@@ -16,21 +16,15 @@
package
com
.
google
.
android
.
exoplayer2
.
offline
;
import
android.content.ContentValues
;
import
android.content.Context
;
import
android.database.Cursor
;
import
android.database.DatabaseUtils
;
import
android.database.sqlite.SQLiteDatabase
;
import
android.database.sqlite.SQLiteException
;
import
android.database.sqlite.SQLiteOpenHelper
;
import
android.net.Uri
;
import
android.support.annotation.IntDef
;
import
android.support.annotation.Nullable
;
import
android.support.annotation.VisibleForTesting
;
import
com.google.android.exoplayer2.database.DatabaseProvider
;
import
com.google.android.exoplayer2.database.VersionTable
;
import
com.google.android.exoplayer2.util.Assertions
;
import
com.google.android.exoplayer2.util.Util
;
import
java.lang.annotation.Documented
;
import
java.lang.annotation.Retention
;
import
java.lang.annotation.RetentionPolicy
;
/**
* A {@link DownloadIndex} which uses SQLite to persist {@link DownloadState}s.
...
...
@@ -40,55 +34,13 @@ import java.lang.annotation.RetentionPolicy;
*/
public
final
class
DefaultDownloadIndex
implements
DownloadIndex
{
/** Provides {@link SQLiteDatabase} instances. */
public
interface
DatabaseProvider
{
/** Closes any open database object. */
void
close
();
/**
* Creates and/or opens a database that will be used for reading and writing.
*
* <p>Once opened successfully, the database is cached, so you can call this method every time
* you need to write to the database. (Make sure to call {@link #close} when you no longer need
* the database.) Errors such as bad permissions or a full disk may cause this method to fail,
* but future attempts may succeed if the problem is fixed.
*
* @throws SQLiteException If the database cannot be opened for writing.
* @return A read/write database object valid until {@link #close} is called.
*/
SQLiteDatabase
getWritableDatabase
();
/**
* Creates and/or opens a database. This will be the same object returned by {@link
* #getWritableDatabase} unless some problem, such as a full disk, requires the database to be
* opened read-only. In that case, a read-only database object will be returned. If the problem
* is fixed, a future call to {@link #getWritableDatabase} may succeed, in which case the
* read-only database object will be closed and the read/write object will be returned in the
* future.
*
* <p>Once opened successfully, the database should be cached. When the database is no longer
* needed, {@link #close} will be called.
*
* @throws SQLiteException If the database cannot be opened.
* @return A database object valid until {@link #getWritableDatabase} or {@link #close} is
* called.
*/
SQLiteDatabase
getReadableDatabase
();
}
@VisibleForTesting
/* package */
static
final
String
TABLE_NAME
=
DatabaseProvider
.
TABLE_PREFIX
+
"Downloads"
;
private
static
final
String
DATABASE_NAME
=
"exoplayer_internal.db"
;
@VisibleForTesting
/* package */
static
final
int
TABLE_VERSION
=
1
;
private
final
DatabaseProvider
databaseProvider
;
@Nullable
private
DownloadStateTable
downloadStateTable
;
/**
* Creates a DefaultDownloadIndex which stores the {@link DownloadState}s on a SQLite database.
*
* @param context A Context.
*/
public
DefaultDownloadIndex
(
Context
context
)
{
this
(
new
DefaultDatabaseProvider
(
context
));
}
@Nullable
private
DownloadsTable
downloadTable
;
/**
* Creates a DefaultDownloadIndex which stores the {@link DownloadState}s on a SQLite database
...
...
@@ -102,45 +54,31 @@ public final class DefaultDownloadIndex implements DownloadIndex {
}
@Override
public
void
release
()
{
databaseProvider
.
close
();
}
@Override
@Nullable
public
DownloadState
getDownloadState
(
String
id
)
{
return
getDownload
State
Table
().
get
(
id
);
return
getDownloadTable
().
get
(
id
);
}
@Override
public
DownloadStateCursor
getDownloadStates
(
@DownloadState
.
State
int
...
states
)
{
return
getDownload
State
Table
().
get
(
states
);
return
getDownloadTable
().
get
(
states
);
}
@Override
public
void
putDownloadState
(
DownloadState
downloadState
)
{
getDownload
State
Table
().
replace
(
downloadState
);
getDownloadTable
().
replace
(
downloadState
);
}
@Override
public
void
removeDownloadState
(
String
id
)
{
getDownload
State
Table
().
delete
(
id
);
getDownloadTable
().
delete
(
id
);
}
private
Download
StateTable
getDownloadState
Table
()
{
if
(
download
State
Table
==
null
)
{
download
StateTable
=
new
DownloadState
Table
(
databaseProvider
);
private
Download
sTable
getDownload
Table
()
{
if
(
downloadTable
==
null
)
{
download
Table
=
new
Downloads
Table
(
databaseProvider
);
}
return
downloadStateTable
;
}
@VisibleForTesting
/* package */
static
boolean
doesTableExist
(
DatabaseProvider
databaseProvider
,
String
tableName
)
{
SQLiteDatabase
readableDatabase
=
databaseProvider
.
getReadableDatabase
();
long
count
=
DatabaseUtils
.
queryNumEntries
(
readableDatabase
,
"sqlite_master"
,
"tbl_name = ?"
,
new
String
[]
{
tableName
});
return
count
>
0
;
return
downloadTable
;
}
private
static
final
class
DownloadStateCursorImpl
implements
DownloadStateCursor
{
...
...
@@ -153,7 +91,7 @@ public final class DefaultDownloadIndex implements DownloadIndex {
@Override
public
DownloadState
getDownloadState
()
{
return
Download
State
Table
.
getDownloadState
(
cursor
);
return
Download
s
Table
.
getDownloadState
(
cursor
);
}
@Override
...
...
@@ -182,10 +120,7 @@ public final class DefaultDownloadIndex implements DownloadIndex {
}
}
@VisibleForTesting
/* package */
static
final
class
DownloadStateTable
{
@VisibleForTesting
/* package */
static
final
String
TABLE_NAME
=
"ExoPlayerDownloadStates"
;
@VisibleForTesting
/* package */
static
final
int
TABLE_VERSION
=
1
;
private
static
final
class
DownloadsTable
{
private
static
final
String
COLUMN_ID
=
"id"
;
private
static
final
String
COLUMN_TYPE
=
"title"
;
...
...
@@ -237,9 +172,9 @@ public final class DefaultDownloadIndex implements DownloadIndex {
COLUMN_CUSTOM_METADATA
};
private
static
final
String
SQL_DROP_TABLE
=
"DROP TABLE IF EXISTS "
+
TABLE_NAME
;
private
static
final
String
SQL_DROP_TABLE
_IF_EXISTS
=
"DROP TABLE IF EXISTS "
+
TABLE_NAME
;
private
static
final
String
SQL_CREATE_TABLE
=
"CREATE TABLE
IF NOT EXISTS
"
"CREATE TABLE "
+
TABLE_NAME
+
" ("
+
COLUMN_ID
...
...
@@ -273,17 +208,15 @@ public final class DefaultDownloadIndex implements DownloadIndex {
private
final
DatabaseProvider
databaseProvider
;
public
Download
State
Table
(
DatabaseProvider
databaseProvider
)
{
public
Download
s
Table
(
DatabaseProvider
databaseProvider
)
{
this
.
databaseProvider
=
databaseProvider
;
VersionTable
versionTable
=
new
VersionTable
(
databaseProvider
);
int
version
=
versionTable
.
getVersion
(
VersionTable
.
FEATURE_OFFLINE
);
if
(!
doesTableExist
(
databaseProvider
,
TABLE_NAME
)
||
version
==
0
||
version
>
TABLE_VERSION
)
{
if
(
version
==
VersionTable
.
VERSION_UNSET
||
version
>
TABLE_VERSION
)
{
SQLiteDatabase
writableDatabase
=
databaseProvider
.
getWritableDatabase
();
writableDatabase
.
beginTransaction
();
try
{
writableDatabase
.
execSQL
(
SQL_DROP_TABLE
);
writableDatabase
.
execSQL
(
SQL_DROP_TABLE
_IF_EXISTS
);
writableDatabase
.
execSQL
(
SQL_CREATE_TABLE
);
versionTable
.
setVersion
(
VersionTable
.
FEATURE_OFFLINE
,
TABLE_VERSION
);
writableDatabase
.
setTransactionSuccessful
();
...
...
@@ -421,108 +354,4 @@ public final class DefaultDownloadIndex implements DownloadIndex {
return
streamKeys
;
}
}
@VisibleForTesting
/* package */
static
final
class
VersionTable
{
private
static
final
String
TABLE_NAME
=
"ExoPlayerVersions"
;
private
static
final
String
COLUMN_FEATURE
=
"feature"
;
private
static
final
String
COLUMN_VERSION
=
"version"
;
private
static
final
String
SQL_CREATE_TABLE
=
"CREATE TABLE IF NOT EXISTS "
+
TABLE_NAME
+
" ("
+
COLUMN_FEATURE
+
" INTEGER PRIMARY KEY NOT NULL,"
+
COLUMN_VERSION
+
" INTEGER NOT NULL)"
;
@Documented
@Retention
(
RetentionPolicy
.
SOURCE
)
@IntDef
({
FEATURE_OFFLINE
,
FEATURE_CACHE
})
private
@interface
Feature
{}
public
static
final
int
FEATURE_OFFLINE
=
0
;
public
static
final
int
FEATURE_CACHE
=
1
;
private
final
DatabaseProvider
databaseProvider
;
public
VersionTable
(
DatabaseProvider
databaseProvider
)
{
this
.
databaseProvider
=
databaseProvider
;
if
(!
doesTableExist
(
databaseProvider
,
TABLE_NAME
))
{
databaseProvider
.
getWritableDatabase
().
execSQL
(
SQL_CREATE_TABLE
);
}
}
public
void
setVersion
(
@Feature
int
feature
,
int
version
)
{
ContentValues
values
=
new
ContentValues
();
values
.
put
(
COLUMN_FEATURE
,
feature
);
values
.
put
(
COLUMN_VERSION
,
version
);
SQLiteDatabase
writableDatabase
=
databaseProvider
.
getWritableDatabase
();
writableDatabase
.
replace
(
TABLE_NAME
,
/* nullColumnHack= */
null
,
values
);
}
public
int
getVersion
(
@Feature
int
feature
)
{
String
selection
=
COLUMN_FEATURE
+
" = ?"
;
String
[]
selectionArgs
=
{
Integer
.
toString
(
feature
)};
try
(
Cursor
cursor
=
databaseProvider
.
getReadableDatabase
()
.
query
(
TABLE_NAME
,
new
String
[]
{
COLUMN_VERSION
},
selection
,
selectionArgs
,
/* groupBy= */
null
,
/* having= */
null
,
/* orderBy= */
null
))
{
if
(
cursor
.
getCount
()
==
0
)
{
return
0
;
}
cursor
.
moveToNext
();
return
cursor
.
getInt
(
/* COLUMN_VERSION index */
0
);
}
}
}
private
static
final
class
DefaultDatabaseProvider
extends
SQLiteOpenHelper
implements
DatabaseProvider
{
public
DefaultDatabaseProvider
(
Context
context
)
{
super
(
context
,
DATABASE_NAME
,
/* factory= */
null
,
/* version= */
1
);
}
@Override
public
void
onCreate
(
SQLiteDatabase
db
)
{
// Table creation is done in DownloadStateTable constructor.
}
@Override
public
void
onUpgrade
(
SQLiteDatabase
db
,
int
oldVersion
,
int
newVersion
)
{
// Upgrade is handled in DownloadStateTable constructor.
}
@Override
public
void
onDowngrade
(
SQLiteDatabase
db
,
int
oldVersion
,
int
newVersion
)
{
// TODO: Wipe the database.
super
.
onDowngrade
(
db
,
oldVersion
,
newVersion
);
}
// DatabaseProvider implementation.
@Override
public
synchronized
void
close
()
{
super
.
close
();
}
@Override
public
SQLiteDatabase
getWritableDatabase
()
{
return
super
.
getWritableDatabase
();
}
@Override
public
SQLiteDatabase
getReadableDatabase
()
{
return
super
.
getReadableDatabase
();
}
}
}
library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadIndex.java
View file @
82da627c
...
...
@@ -19,8 +19,6 @@ import android.support.annotation.Nullable;
/** Persists {@link DownloadState}s. */
interface
DownloadIndex
{
/** Releases the used resources. */
void
release
();
/**
* Returns the {@link DownloadState} with the given {@code id}, or null.
...
...
library/core/src/test/java/com/google/android/exoplayer2/database/VersionTableTest.java
0 → 100644
View file @
82da627c
/*
* Copyright (C) 2018 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
.
google
.
android
.
exoplayer2
.
database
;
import
static
com
.
google
.
android
.
exoplayer2
.
database
.
VersionTable
.
FEATURE_CACHE
;
import
static
com
.
google
.
android
.
exoplayer2
.
database
.
VersionTable
.
FEATURE_OFFLINE
;
import
static
com
.
google
.
common
.
truth
.
Truth
.
assertThat
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.robolectric.RobolectricTestRunner
;
import
org.robolectric.RuntimeEnvironment
;
/** Unit tests for {@link VersionTable}. */
@RunWith
(
RobolectricTestRunner
.
class
)
public
class
VersionTableTest
{
private
ExoDatabaseProvider
databaseProvider
;
@Before
public
void
setUp
()
{
databaseProvider
=
new
ExoDatabaseProvider
(
RuntimeEnvironment
.
application
);
}
@After
public
void
tearDown
()
{
databaseProvider
.
close
();
}
@Test
public
void
getVersion_nonExistingTable_returnsVersionUnset
()
{
VersionTable
versionTable
=
new
VersionTable
(
databaseProvider
);
int
version
=
versionTable
.
getVersion
(
FEATURE_OFFLINE
);
assertThat
(
version
).
isEqualTo
(
VersionTable
.
VERSION_UNSET
);
}
@Test
public
void
getVersion_returnsSetVersion
()
{
VersionTable
versionTable
=
new
VersionTable
(
databaseProvider
);
versionTable
.
setVersion
(
FEATURE_OFFLINE
,
1
);
assertThat
(
versionTable
.
getVersion
(
FEATURE_OFFLINE
)).
isEqualTo
(
1
);
versionTable
.
setVersion
(
FEATURE_OFFLINE
,
10
);
assertThat
(
versionTable
.
getVersion
(
FEATURE_OFFLINE
)).
isEqualTo
(
10
);
versionTable
.
setVersion
(
FEATURE_CACHE
,
5
);
assertThat
(
versionTable
.
getVersion
(
FEATURE_CACHE
)).
isEqualTo
(
5
);
assertThat
(
versionTable
.
getVersion
(
FEATURE_OFFLINE
)).
isEqualTo
(
10
);
}
@Test
public
void
doesTableExist_nonExistingTable_returnsFalse
()
{
assertThat
(
VersionTable
.
doesTableExist
(
databaseProvider
,
"NonExistingTable"
)).
isFalse
();
}
@Test
public
void
doesTableExist_existingTable_returnsTrue
()
{
String
table
=
"TestTable"
;
databaseProvider
.
getWritableDatabase
().
execSQL
(
"CREATE TABLE "
+
table
+
" (dummy INTEGER)"
);
assertThat
(
VersionTable
.
doesTableExist
(
databaseProvider
,
table
)).
isTrue
();
}
}
library/core/src/test/java/com/google/android/exoplayer2/offline/DefaultDownloadIndexTest.java
View file @
82da627c
...
...
@@ -15,15 +15,13 @@
*/
package
com
.
google
.
android
.
exoplayer2
.
offline
;
import
static
com
.
google
.
android
.
exoplayer2
.
offline
.
DefaultDownloadIndex
.
VersionTable
.
FEATURE_CACHE
;
import
static
com
.
google
.
android
.
exoplayer2
.
offline
.
DefaultDownloadIndex
.
VersionTable
.
FEATURE_OFFLINE
;
import
static
com
.
google
.
common
.
truth
.
Truth
.
assertThat
;
import
android.database.sqlite.SQLiteDatabase
;
import
android.database.sqlite.SQLiteOpenHelper
;
import
android.net.Uri
;
import
android.support.annotation.Nullable
;
import
com.google.android.exoplayer2.C
;
import
com.google.android.exoplayer2.database.ExoDatabaseProvider
;
import
com.google.android.exoplayer2.database.VersionTable
;
import
java.util.Arrays
;
import
org.junit.After
;
import
org.junit.Before
;
...
...
@@ -36,16 +34,18 @@ import org.robolectric.RuntimeEnvironment;
@RunWith
(
RobolectricTestRunner
.
class
)
public
class
DefaultDownloadIndexTest
{
private
ExoDatabaseProvider
databaseProvider
;
private
DefaultDownloadIndex
downloadIndex
;
@Before
public
void
setUp
()
{
downloadIndex
=
new
DefaultDownloadIndex
(
RuntimeEnvironment
.
application
);
databaseProvider
=
new
ExoDatabaseProvider
(
RuntimeEnvironment
.
application
);
downloadIndex
=
new
DefaultDownloadIndex
(
databaseProvider
);
}
@After
public
void
tearDown
()
{
d
ownloadIndex
.
relea
se
();
d
atabaseProvider
.
clo
se
();
}
@Test
...
...
@@ -99,29 +99,12 @@ public class DefaultDownloadIndexTest {
public
void
releaseAndRecreateDownloadIndex_returnsTheSameDownloadState
()
{
String
id
=
"id"
;
DownloadState
downloadState
=
new
DownloadStateBuilder
(
id
).
build
();
downloadIndex
.
putDownloadState
(
downloadState
);
downloadIndex
.
release
();
downloadIndex
=
new
DefaultDownloadIndex
(
RuntimeEnvironment
.
application
);
DownloadState
readDownloadState
=
downloadIndex
.
getDownloadState
(
id
);
assertThat
(
readDownloadState
).
isNotNull
();
assertEqual
(
readDownloadState
,
downloadState
);
}
@Test
public
void
customDatabaseProvider_getDownloadStateReturnsNull
()
{
String
id
=
"id"
;
DownloadState
downloadState
=
new
DownloadStateBuilder
(
id
).
build
();
downloadIndex
.
putDownloadState
(
downloadState
);
downloadIndex
.
release
();
DatabaseProviderImpl
databaseProvider
=
new
DatabaseProviderImpl
();
downloadIndex
=
new
DefaultDownloadIndex
(
databaseProvider
);
DownloadState
readDownloadState
=
downloadIndex
.
getDownloadState
(
id
);
assertThat
(
readDownloadState
).
isNull
();
databaseProvider
.
close
();
assertThat
(
readDownloadState
).
isNotNull
();
assertEqual
(
readDownloadState
,
downloadState
);
}
@Test
...
...
@@ -134,10 +117,9 @@ public class DefaultDownloadIndexTest {
String
id
=
"id"
;
DownloadState
downloadState
=
new
DownloadStateBuilder
(
id
).
build
();
downloadIndex
.
putDownloadState
(
downloadState
);
downloadIndex
.
removeDownloadState
(
id
);
DownloadState
readDownloadState
=
downloadIndex
.
getDownloadState
(
id
);
DownloadState
readDownloadState
=
downloadIndex
.
getDownloadState
(
id
);
assertThat
(
readDownloadState
).
isNull
();
}
...
...
@@ -160,6 +142,7 @@ public class DefaultDownloadIndexTest {
assertEqual
(
cursor
.
getDownloadState
(),
downloadState2
);
cursor
.
moveToNext
();
assertEqual
(
cursor
.
getDownloadState
(),
downloadState1
);
cursor
.
close
();
}
@Test
...
...
@@ -191,111 +174,39 @@ public class DefaultDownloadIndexTest {
assertEqual
(
cursor
.
getDownloadState
(),
downloadState1
);
cursor
.
moveToNext
();
assertEqual
(
cursor
.
getDownloadState
(),
downloadState3
);
cursor
.
close
();
}
@Test
public
void
doesTableExist_nonExistingTable_returnsFalse
()
{
DatabaseProviderImpl
databaseProvider
=
new
DatabaseProviderImpl
();
assertThat
(
DefaultDownloadIndex
.
doesTableExist
(
databaseProvider
,
"NonExistingTable"
)).
isFalse
();
databaseProvider
.
close
();
}
@Test
public
void
doesTableExist_existingTable_returnsTrue
()
{
DatabaseProviderImpl
databaseProvider
=
new
DatabaseProviderImpl
();
String
tableName
=
"ExistingTable"
;
databaseProvider
.
getWritableDatabase
().
execSQL
(
"CREATE TABLE "
+
tableName
+
"(dummy)"
);
assertThat
(
DefaultDownloadIndex
.
doesTableExist
(
databaseProvider
,
tableName
)).
isTrue
();
databaseProvider
.
close
();
}
@Test
public
void
getVersion_nonExistingTable_returnsZero
()
{
DatabaseProviderImpl
databaseProvider
=
new
DatabaseProviderImpl
();
DefaultDownloadIndex
.
VersionTable
versionTable
=
new
DefaultDownloadIndex
.
VersionTable
(
databaseProvider
);
int
version
=
versionTable
.
getVersion
(
FEATURE_OFFLINE
);
assertThat
(
version
).
isEqualTo
(
0
);
databaseProvider
.
close
();
}
@Test
public
void
getVersion_returnsSetVersion
()
{
DatabaseProviderImpl
databaseProvider
=
new
DatabaseProviderImpl
();
DefaultDownloadIndex
.
VersionTable
versionTable
=
new
DefaultDownloadIndex
.
VersionTable
(
databaseProvider
);
public
void
putDownloadState_setsVersion
()
{
VersionTable
versionTable
=
new
VersionTable
(
databaseProvider
);
assertThat
(
versionTable
.
getVersion
(
VersionTable
.
FEATURE_OFFLINE
))
.
isEqualTo
(
VersionTable
.
VERSION_UNSET
);
versionTable
.
setVersion
(
FEATURE_OFFLINE
,
1
);
assertThat
(
versionTable
.
getVersion
(
FEATURE_OFFLINE
)).
isEqualTo
(
1
);
downloadIndex
.
putDownloadState
(
new
DownloadStateBuilder
(
"id1"
).
build
());
versionTable
.
setVersion
(
FEATURE_OFFLINE
,
10
);
assertThat
(
versionTable
.
getVersion
(
FEATURE_OFFLINE
)).
isEqualTo
(
10
);
versionTable
.
setVersion
(
FEATURE_CACHE
,
5
);
assertThat
(
versionTable
.
getVersion
(
FEATURE_CACHE
)).
isEqualTo
(
5
);
assertThat
(
versionTable
.
getVersion
(
FEATURE_OFFLINE
)).
isEqualTo
(
10
);
databaseProvider
.
close
();
assertThat
(
versionTable
.
getVersion
(
VersionTable
.
FEATURE_OFFLINE
))
.
isEqualTo
(
DefaultDownloadIndex
.
TABLE_VERSION
);
}
@Test
public
void
downloadStateTableConstructor_noTable_createsTable
()
{
DatabaseProviderImpl
databaseProvider
=
new
DatabaseProviderImpl
();
assertThat
(
DefaultDownloadIndex
.
doesTableExist
(
databaseProvider
,
DefaultDownloadIndex
.
DownloadStateTable
.
TABLE_NAME
))
.
isFalse
();
new
DefaultDownloadIndex
.
DownloadStateTable
(
databaseProvider
);
assertThat
(
DefaultDownloadIndex
.
doesTableExist
(
databaseProvider
,
DefaultDownloadIndex
.
DownloadStateTable
.
TABLE_NAME
))
.
isTrue
();
databaseProvider
.
close
();
}
@Test
public
void
downloadStateTableConstructor_versionZero_versionSet
()
{
DatabaseProviderImpl
databaseProvider
=
new
DatabaseProviderImpl
();
new
DefaultDownloadIndex
.
DownloadStateTable
(
databaseProvider
);
public
void
downloadIndex_versionDowngradeWipesData
()
{
DownloadState
downloadState1
=
new
DownloadStateBuilder
(
"id1"
).
build
();
downloadIndex
.
putDownloadState
(
downloadState1
);
DownloadStateCursor
cursor
=
downloadIndex
.
getDownloadStates
();
assertThat
(
cursor
.
getCount
()).
isEqualTo
(
1
);
cursor
.
close
();
DefaultDownloadIndex
.
VersionTable
versionTable
=
new
DefaultDownloadIndex
.
VersionTable
(
databaseProvider
);
assertThat
(
versionTable
.
getVersion
(
FEATURE_OFFLINE
))
.
isEqualTo
(
DefaultDownloadIndex
.
DownloadStateTable
.
TABLE_VERSION
);
databaseProvider
.
close
();
}
VersionTable
versionTable
=
new
VersionTable
(
databaseProvider
);
versionTable
.
setVersion
(
VersionTable
.
FEATURE_OFFLINE
,
Integer
.
MAX_VALUE
);
@Test
public
void
downloadStateTableConstructor_greaterVersion_tableRecreated
()
{
DatabaseProviderImpl
databaseProvider
=
new
DatabaseProviderImpl
();
databaseProvider
.
getWritableDatabase
()
.
execSQL
(
"CREATE TABLE "
+
DefaultDownloadIndex
.
DownloadStateTable
.
TABLE_NAME
+
"(dummy)"
);
DefaultDownloadIndex
.
VersionTable
versionTable
=
new
DefaultDownloadIndex
.
VersionTable
(
databaseProvider
);
versionTable
.
setVersion
(
FEATURE_OFFLINE
,
Integer
.
MAX_VALUE
);
DefaultDownloadIndex
.
DownloadStateTable
downloadStateTable
=
new
DefaultDownloadIndex
.
DownloadStateTable
(
databaseProvider
);
String
id
=
"id"
;
DownloadState
downloadState
=
new
DownloadStateBuilder
(
id
).
build
();
downloadStateTable
.
replace
(
downloadState
);
DownloadState
readDownloadState
=
downloadStateTable
.
get
(
id
);
assertEqual
(
readDownloadState
,
downloadState
);
downloadIndex
=
new
DefaultDownloadIndex
(
databaseProvider
);
assertThat
(
versionTable
.
getVersion
(
FEATURE_OFFLINE
))
.
isEqualTo
(
DefaultDownloadIndex
.
DownloadStateTable
.
TABLE_VERSION
);
databaseProvider
.
close
();
cursor
=
downloadIndex
.
getDownloadStates
();
assertThat
(
cursor
.
getCount
()).
isEqualTo
(
0
);
cursor
.
close
();
assertThat
(
versionTable
.
getVersion
(
VersionTable
.
FEATURE_OFFLINE
))
.
isEqualTo
(
DefaultDownloadIndex
.
TABLE_VERSION
);
}
private
static
void
assertEqual
(
DownloadState
downloadState
,
DownloadState
expected
)
{
...
...
@@ -468,24 +379,4 @@ public class DefaultDownloadIndexTest {
customMetadata
);
}
}
private
static
final
class
DatabaseProviderImpl
extends
SQLiteOpenHelper
implements
DefaultDownloadIndex
.
DatabaseProvider
{
private
static
final
int
DATABASE_VERSION
=
1
;
private
static
final
String
DATABASE_NAME
=
"TestExoPlayerDownloadIndex.db"
;
public
DatabaseProviderImpl
()
{
super
(
RuntimeEnvironment
.
application
,
DATABASE_NAME
,
null
,
DATABASE_VERSION
);
}
@Override
public
void
onCreate
(
SQLiteDatabase
db
)
{
// Do nothing.
}
@Override
public
void
onUpgrade
(
SQLiteDatabase
db
,
int
oldVersion
,
int
newVersion
)
{
// Do nothing.
}
}
}
library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadIndexUtilTest.java
View file @
82da627c
...
...
@@ -19,6 +19,7 @@ import static com.google.android.exoplayer2.offline.DownloadAction.TYPE_DASH;
import
static
com
.
google
.
common
.
truth
.
Truth
.
assertThat
;
import
android.net.Uri
;
import
com.google.android.exoplayer2.database.ExoDatabaseProvider
;
import
com.google.android.exoplayer2.util.Util
;
import
java.io.File
;
import
java.util.Arrays
;
...
...
@@ -34,18 +35,20 @@ import org.robolectric.RuntimeEnvironment;
@RunWith
(
RobolectricTestRunner
.
class
)
public
class
DownloadIndexUtilTest
{
private
DefaultDownloadIndex
downloadIndex
;
private
File
tempFile
;
private
ExoDatabaseProvider
databaseProvider
;
private
DefaultDownloadIndex
downloadIndex
;
@Before
public
void
setUp
()
throws
Exception
{
tempFile
=
Util
.
createTempFile
(
RuntimeEnvironment
.
application
,
"ExoPlayerTest"
);
downloadIndex
=
new
DefaultDownloadIndex
(
RuntimeEnvironment
.
application
);
databaseProvider
=
new
ExoDatabaseProvider
(
RuntimeEnvironment
.
application
);
downloadIndex
=
new
DefaultDownloadIndex
(
databaseProvider
);
}
@After
public
void
tearDown
()
{
d
ownloadIndex
.
relea
se
();
d
atabaseProvider
.
clo
se
();
tempFile
.
delete
();
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment