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
b84c5143
authored
Apr 11, 2019
by
olly
Committed by
Oliver Woodman
Apr 13, 2019
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Deprecate ActionFile and simplify upgrading
PiperOrigin-RevId: 243085292
parent
112117a1
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
63 additions
and
47 deletions
demos/main/src/main/java/com/google/android/exoplayer2/demo/DemoApplication.java
demos/main/src/main/java/com/google/android/exoplayer2/demo/DownloadTracker.java
library/core/src/main/java/com/google/android/exoplayer2/offline/ActionFile.java
library/core/src/main/java/com/google/android/exoplayer2/offline/DownloadIndexUtil.java → library/core/src/main/java/com/google/android/exoplayer2/offline/ActionFileUpgradeUtil.java
library/core/src/test/java/com/google/android/exoplayer2/offline/DownloadIndexUtilTest.java → library/core/src/test/java/com/google/android/exoplayer2/offline/ActionFileUpgradeUtilTest.java
demos/main/src/main/java/com/google/android/exoplayer2/demo/DemoApplication.java
View file @
b84c5143
...
...
@@ -19,10 +19,9 @@ import android.app.Application;
import
com.google.android.exoplayer2.DefaultRenderersFactory
;
import
com.google.android.exoplayer2.RenderersFactory
;
import
com.google.android.exoplayer2.database.ExoDatabaseProvider
;
import
com.google.android.exoplayer2.offline.ActionFile
;
import
com.google.android.exoplayer2.offline.ActionFile
UpgradeUtil
;
import
com.google.android.exoplayer2.offline.DefaultDownloadIndex
;
import
com.google.android.exoplayer2.offline.DefaultDownloaderFactory
;
import
com.google.android.exoplayer2.offline.DownloadIndexUtil
;
import
com.google.android.exoplayer2.offline.DownloadManager
;
import
com.google.android.exoplayer2.offline.DownloaderConstructorHelper
;
import
com.google.android.exoplayer2.upstream.DataSource
;
...
...
@@ -132,16 +131,15 @@ public class DemoApplication extends Application {
}
}
private
void
upgradeActionFile
(
String
file
,
DefaultDownloadIndex
downloadIndex
)
{
ActionFile
actionFile
=
new
ActionFile
(
new
File
(
getDownloadDirectory
(),
file
));
if
(
actionFile
.
exists
())
{
try
{
DownloadIndexUtil
.
mergeActionFile
(
actionFile
,
/* downloadIdProvider= */
null
,
downloadIndex
);
}
catch
(
IOException
e
)
{
Log
.
e
(
TAG
,
"Upgrading action file failed"
,
e
);
}
actionFile
.
delete
();
private
void
upgradeActionFile
(
String
fileName
,
DefaultDownloadIndex
downloadIndex
)
{
try
{
ActionFileUpgradeUtil
.
upgradeAndDelete
(
new
File
(
getDownloadDirectory
(),
fileName
),
/* downloadIdProvider= */
null
,
downloadIndex
,
/* deleteOnFailure= */
true
);
}
catch
(
IOException
e
)
{
Log
.
e
(
TAG
,
"Failed to upgrade action file: "
+
fileName
,
e
);
}
}
...
...
demos/main/src/main/java/com/google/android/exoplayer2/demo/DownloadTracker.java
View file @
b84c5143
...
...
@@ -23,7 +23,6 @@ import androidx.fragment.app.FragmentManager;
import
android.widget.Toast
;
import
com.google.android.exoplayer2.C
;
import
com.google.android.exoplayer2.RenderersFactory
;
import
com.google.android.exoplayer2.offline.ActionFile
;
import
com.google.android.exoplayer2.offline.DefaultDownloadIndex
;
import
com.google.android.exoplayer2.offline.Download
;
import
com.google.android.exoplayer2.offline.DownloadAction
;
...
...
@@ -44,10 +43,6 @@ import java.util.concurrent.CopyOnWriteArraySet;
/**
* Tracks media that has been downloaded.
*
* <p>Tracked downloads are persisted using an {@link ActionFile}, however in a real application
* it's expected that state will be stored directly in the application's media database, so that it
* can be queried efficiently together with other information about the media.
*/
public
class
DownloadTracker
implements
DownloadManager
.
Listener
{
...
...
library/core/src/main/java/com/google/android/exoplayer2/offline/ActionFile.java
View file @
b84c5143
...
...
@@ -27,8 +27,14 @@ import java.io.InputStream;
import
java.util.ArrayList
;
import
java.util.List
;
/** Loads {@link DownloadAction DownloadActions} from legacy action files. */
public
final
class
ActionFile
{
/**
* Loads {@link DownloadAction DownloadActions} from legacy action files.
*
* @deprecated Legacy action files should be merged into download indices using {@link
* ActionFileUpgradeUtil}.
*/
@Deprecated
/* package */
final
class
ActionFile
{
private
static
final
int
VERSION
=
0
;
...
...
library/core/src/main/java/com/google/android/exoplayer2/offline/
DownloadIndex
Util.java
→
library/core/src/main/java/com/google/android/exoplayer2/offline/
ActionFileUpgrade
Util.java
View file @
b84c5143
...
...
@@ -16,52 +16,69 @@
package
com
.
google
.
android
.
exoplayer2
.
offline
;
import
androidx.annotation.Nullable
;
import
java.io.File
;
import
java.io.IOException
;
/**
{@link DownloadIndex} related utility methods
. */
public
final
class
DownloadIndex
Util
{
/**
Utility class for upgrading legacy action files into {@link DefaultDownloadIndex}
. */
public
final
class
ActionFileUpgrade
Util
{
/**
An interface to provide custom download ids during ActionF
ile upgrade. */
/**
Provides download IDs during action f
ile upgrade. */
public
interface
DownloadIdProvider
{
/**
* Returns a
custom
download id for given action.
* Returns a download id for given action.
*
* @param downloadAction The action
which is an id requested for
.
* @return A c
ustom download id for given action
.
* @param downloadAction The action
for which an ID is required
.
* @return A c
orresponding download ID
.
*/
String
getId
(
DownloadAction
downloadAction
);
}
private
DownloadIndex
Util
()
{}
private
ActionFileUpgrade
Util
()
{}
/**
* Merges {@link DownloadAction DownloadActions} contained in an {@link ActionFile} into a {@link
* DownloadIndex}.
* Merges {@link DownloadAction DownloadActions} contained in a legacy action file into a {@link
* DefaultDownloadIndex}, deleting the action file if the merge is successful or if {@code
* deleteOnFailure} is {@code true}.
*
* <p>This method must not be called while the {@link D
ownloadIndex} is being used by a {@link
* DownloadManager}.
* <p>This method must not be called while the {@link D
efaultDownloadIndex} is being used by a
*
{@link
DownloadManager}.
*
* @param actionFile The action file.
* @param downloadIdProvider A custom download id provider, or {@code null}.
* @param actionFilePath The action file path.
* @param downloadIdProvider A download ID provider, or {@code null}. If {@code null} then ID of
* each download will be its custom cache key if one is specified, or else its URL.
* @param downloadIndex The index into which the action will be merged.
* @param deleteOnFailure Whether to delete the action file if the merge fails.
* @throws IOException If an error occurs loading or merging the actions.
*/
public
static
void
mergeActionFile
(
ActionFile
actionFile
,
@SuppressWarnings
(
"deprecation"
)
public
static
void
upgradeAndDelete
(
File
actionFilePath
,
@Nullable
DownloadIdProvider
downloadIdProvider
,
DefaultDownloadIndex
downloadIndex
)
DefaultDownloadIndex
downloadIndex
,
boolean
deleteOnFailure
)
throws
IOException
{
for
(
DownloadAction
action
:
actionFile
.
load
())
{
if
(
downloadIdProvider
!=
null
)
{
action
=
action
.
copyWithId
(
downloadIdProvider
.
getId
(
action
));
ActionFile
actionFile
=
new
ActionFile
(
actionFilePath
);
if
(
actionFile
.
exists
())
{
boolean
success
=
false
;
try
{
for
(
DownloadAction
action
:
actionFile
.
load
())
{
if
(
downloadIdProvider
!=
null
)
{
action
=
action
.
copyWithId
(
downloadIdProvider
.
getId
(
action
));
}
mergeAction
(
action
,
downloadIndex
);
}
success
=
true
;
}
finally
{
if
(
success
||
deleteOnFailure
)
{
actionFile
.
delete
();
}
}
mergeAction
(
action
,
downloadIndex
);
}
}
/**
* Merges a {@link DownloadAction} into a {@link D
ownloadIndexUtil
}.
* Merges a {@link DownloadAction} into a {@link D
efaultDownloadIndex
}.
*
* @param action The action to be merged.
* @param downloadIndex The index into which the action will be merged.
...
...
library/core/src/test/java/com/google/android/exoplayer2/offline/
DownloadIndex
UtilTest.java
→
library/core/src/test/java/com/google/android/exoplayer2/offline/
ActionFileUpgrade
UtilTest.java
View file @
b84c5143
...
...
@@ -34,9 +34,9 @@ import org.junit.Before;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
/** Unit tests for {@link
DownloadIndex
Util}. */
/** Unit tests for {@link
ActionFileUpgrade
Util}. */
@RunWith
(
AndroidJUnit4
.
class
)
public
class
DownloadIndex
UtilTest
{
public
class
ActionFileUpgrade
UtilTest
{
private
File
tempFile
;
private
ExoDatabaseProvider
databaseProvider
;
...
...
@@ -69,7 +69,7 @@ public class DownloadIndexUtilTest {
/* customCacheKey= */
"key123"
,
data
);
DownloadIndex
Util
.
mergeAction
(
action
,
downloadIndex
);
ActionFileUpgrade
Util
.
mergeAction
(
action
,
downloadIndex
);
assertDownloadIndexContainsAction
(
action
,
Download
.
STATE_QUEUED
);
}
...
...
@@ -96,9 +96,9 @@ public class DownloadIndexUtilTest {
asList
(
streamKey2
),
/* customCacheKey= */
"key123"
,
new
byte
[]
{
5
,
4
,
3
,
2
,
1
});
DownloadIndex
Util
.
mergeAction
(
action1
,
downloadIndex
);
ActionFileUpgrade
Util
.
mergeAction
(
action1
,
downloadIndex
);
DownloadIndex
Util
.
mergeAction
(
action2
,
downloadIndex
);
ActionFileUpgrade
Util
.
mergeAction
(
action2
,
downloadIndex
);
Download
download
=
downloadIndex
.
getDownload
(
action2
.
id
);
assertThat
(
download
).
isNotNull
();
...
...
@@ -142,8 +142,8 @@ public class DownloadIndexUtilTest {
/* customCacheKey= */
"key234"
,
new
byte
[]
{
5
,
4
,
3
,
2
,
1
});
ActionFile
actionFile
=
new
ActionFile
(
tempFile
);
DownloadIndexUtil
.
mergeActionFile
(
actionFile
,
/* downloadIdProvider= */
null
,
downloadIndex
);
ActionFile
UpgradeUtil
.
upgradeAndDelete
(
tempFile
,
/* downloadIdProvider= */
null
,
downloadIndex
,
/* deleteOnFailure= */
true
);
assertDownloadIndexContainsAction
(
expectedAction1
,
Download
.
STATE_QUEUED
);
assertDownloadIndexContainsAction
(
expectedAction2
,
Download
.
STATE_QUEUED
);
}
...
...
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