-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAxOfflineManager.java
120 lines (102 loc) · 4.58 KB
/
AxOfflineManager.java
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
package com.axinom.drm.sample.offline;
import android.content.Context;
import android.os.Environment;
import android.util.Log;
import com.google.android.exoplayer2.database.DatabaseProvider;
import com.google.android.exoplayer2.database.StandaloneDatabaseProvider;
import com.google.android.exoplayer2.offline.DownloadManager;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultDataSource;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSource;
import com.google.android.exoplayer2.upstream.HttpDataSource;
import com.google.android.exoplayer2.upstream.cache.Cache;
import com.google.android.exoplayer2.upstream.cache.CacheDataSource;
import com.google.android.exoplayer2.upstream.cache.NoOpCacheEvictor;
import com.google.android.exoplayer2.upstream.cache.SimpleCache;
import java.io.File;
import java.util.concurrent.Executors;
/**
* A class that manages the initialization of DownloadManager and data source factory objects.
*/
public class AxOfflineManager {
private static final String TAG = AxOfflineManager.class.getSimpleName();
private static AxOfflineManager sAxOfflineManager;
private DatabaseProvider databaseProvider;
private File mDownloadDirectory;
private DownloadManager mDownloadManager;
private AxDownloadTracker mDownloadTracker;
private Cache mDownloadCache;
private static String DEFAULT_DOWNLOADS_FOLDER;
static {
DEFAULT_DOWNLOADS_FOLDER = Environment.getExternalStorageDirectory().getAbsolutePath();
DEFAULT_DOWNLOADS_FOLDER = DEFAULT_DOWNLOADS_FOLDER.endsWith("/") ? DEFAULT_DOWNLOADS_FOLDER : (DEFAULT_DOWNLOADS_FOLDER + "/");
DEFAULT_DOWNLOADS_FOLDER += "downloads";
}
// Return and create the AxOfflineManager instance if necessary
public static AxOfflineManager getInstance() {
if (sAxOfflineManager == null) {
sAxOfflineManager = new AxOfflineManager();
}
return sAxOfflineManager;
}
public AxDownloadTracker getDownloadTracker() {
return mDownloadTracker;
}
public DownloadManager getDownloadManager() {
return mDownloadManager;
}
public void init(Context context) {
init (context, new File(DEFAULT_DOWNLOADS_FOLDER));
}
// Initializing of AxOfflineManager
public synchronized void init(Context context, File folder) {
Log.d(TAG, "init() called with: context = [" + context + "], folder = [" + folder + "]");
if (mDownloadManager == null) {
mDownloadDirectory = folder;
mDownloadManager = new DownloadManager(
context.getApplicationContext(),
getDatabaseProvider(context),
getDownloadCache(context),
buildHttpDataSourceFactory(),
Executors.newFixedThreadPool(6));
mDownloadTracker = new AxDownloadTracker(context, buildDataSourceFactory(context),
mDownloadManager);
}
}
private File getDownloadDirectory() {
if (mDownloadDirectory == null) {
mDownloadDirectory = new File(DEFAULT_DOWNLOADS_FOLDER);
Log.d(TAG, "Setting value to mDownloadDirectory: " + mDownloadDirectory);
}
return mDownloadDirectory;
}
private synchronized Cache getDownloadCache(Context context) {
if (mDownloadCache == null) {
mDownloadCache = new SimpleCache(getDownloadDirectory(), new NoOpCacheEvictor(),
getDatabaseProvider(context));
}
return mDownloadCache;
}
// Returns a {@link DataSource.Factory}
public DataSource.Factory buildDataSourceFactory(Context context) {
DefaultDataSource.Factory upstreamFactory =
new DefaultDataSource.Factory(context, buildHttpDataSourceFactory());
return buildReadOnlyCacheDataSource(upstreamFactory, getDownloadCache(context));
}
// Returns a {@link HttpDataSource.Factory}
private HttpDataSource.Factory buildHttpDataSourceFactory() {
return new DefaultHttpDataSource.Factory();
}
private static CacheDataSource.Factory buildReadOnlyCacheDataSource(
DataSource.Factory upstreamFactory, Cache cache) {
return new CacheDataSource.Factory()
.setCache(cache)
.setUpstreamDataSourceFactory(upstreamFactory);
}
private DatabaseProvider getDatabaseProvider(Context context) {
if (databaseProvider == null) {
databaseProvider = new StandaloneDatabaseProvider(context.getApplicationContext());
}
return databaseProvider;
}
}