-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathDownloadManager.java
More file actions
executable file
·149 lines (124 loc) · 5.62 KB
/
DownloadManager.java
File metadata and controls
executable file
·149 lines (124 loc) · 5.62 KB
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
package com.android.mms.transaction;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import androidx.core.content.ContextCompat;
import android.database.sqlite.SqliteWrapper;
import android.net.Uri;
import android.os.Bundle;
import android.os.PowerManager;
import android.provider.Telephony;
import android.telephony.SmsManager;
import android.text.TextUtils;
import com.android.mms.MmsConfig;
import com.klinker.android.send_message.BroadcastUtils;
import com.klinker.android.send_message.MmsReceivedReceiver;
import com.klinker.android.send_message.SmsManagerFactory;
import timber.log.Timber;
import java.io.File;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
/**
* In order to avoid downloading duplicate MMS.
* We should manage to call SMSManager.downloadMultimediaMessage().
*/
public class DownloadManager {
private static DownloadManager ourInstance = new DownloadManager();
private static final ConcurrentHashMap<String, MmsDownloadReceiver> mMap = new ConcurrentHashMap<>();
public static DownloadManager getInstance() {
return ourInstance;
}
private DownloadManager() {
}
public void downloadMultimediaMessage(final Context context, final String location, Uri uri, boolean byPush, int subscriptionId) {
if (location == null || mMap.get(location) != null) {
return;
}
// TransactionService can keep uri and location in memory while SmsManager download Mms.
if (!isNotificationExist(context, location)) {
return;
}
MmsDownloadReceiver receiver = new MmsDownloadReceiver();
mMap.put(location, receiver);
// Use unique action in order to avoid cancellation of notifying download result.
ContextCompat.registerReceiver(
context.getApplicationContext(),
receiver,
new IntentFilter(receiver.mAction),
ContextCompat.RECEIVER_NOT_EXPORTED
);
Timber.v("receiving with system method");
final String fileName = "download." + String.valueOf(Math.abs(new Random().nextLong())) + ".dat";
File mDownloadFile = new File(context.getCacheDir(), fileName);
Uri contentUri = (new Uri.Builder())
.authority(context.getPackageName() + ".MmsFileProvider")
.path(fileName)
.scheme(ContentResolver.SCHEME_CONTENT)
.build();
Intent download = new Intent(receiver.mAction);
download.putExtra(MmsReceivedReceiver.EXTRA_FILE_PATH, mDownloadFile.getPath());
download.putExtra(MmsReceivedReceiver.EXTRA_LOCATION_URL, location);
download.putExtra(MmsReceivedReceiver.EXTRA_TRIGGER_PUSH, byPush);
download.putExtra(MmsReceivedReceiver.EXTRA_URI, uri);
final PendingIntent pendingIntent = PendingIntent.getBroadcast(
context, 0, download, PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_IMMUTABLE);
final SmsManager smsManager = SmsManagerFactory.INSTANCE.createSmsManager(subscriptionId);
Bundle configOverrides = new Bundle();
String httpParams = MmsConfig.getHttpParams();
if (!TextUtils.isEmpty(httpParams)) {
configOverrides.putString(SmsManager.MMS_CONFIG_HTTP_PARAMS, httpParams);
}
grantUriPermission(context, contentUri);
smsManager.downloadMultimediaMessage(context, location, contentUri, configOverrides, pendingIntent);
}
private void grantUriPermission(Context context, Uri contentUri) {
context.grantUriPermission(context.getPackageName() + ".MmsFileProvider",
contentUri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
private static class MmsDownloadReceiver extends BroadcastReceiver {
private static final String ACTION_PREFIX = "com.android.mms.transaction.DownloadManager$MmsDownloadReceiver.";
private final String mAction;
MmsDownloadReceiver() {
mAction = ACTION_PREFIX + UUID.randomUUID().toString();
}
@Override
public void onReceive(Context context, Intent intent) {
context.unregisterReceiver(this);
PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MMS DownloadReceiver");
wakeLock.acquire(60 * 1000);
Intent newIntent = (Intent) intent.clone();
newIntent.setAction(MmsReceivedReceiver.MMS_RECEIVED);
BroadcastUtils.sendExplicitBroadcast(context, newIntent, MmsReceivedReceiver.MMS_RECEIVED);
}
}
public static void finishDownload(String location) {
if (location != null) {
mMap.remove(location);
}
}
private static boolean isNotificationExist(Context context, String location) {
String selection = Telephony.Mms.CONTENT_LOCATION + " = ?";
String[] selectionArgs = new String[] { location };
Cursor c = SqliteWrapper.query(
context, context.getContentResolver(),
Telephony.Mms.CONTENT_URI, new String[] { Telephony.Mms._ID },
selection, selectionArgs, null);
if (c != null) {
try {
if (c.getCount() > 0) {
return true;
}
} finally {
c.close();
}
}
return false;
}
}