Pull in changes from smsmms, remove unused classes

This commit is contained in:
Moez Bhatti
2018-05-19 20:28:21 -04:00
parent 71deb2dfd0
commit 3799724506
8 changed files with 94 additions and 360 deletions
@@ -21,14 +21,11 @@ import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SqliteWrapper;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.PowerManager;
import android.preference.PreferenceManager;
import android.provider.Telephony.Mms;
import android.provider.Telephony.Mms.Inbox;
import com.android.mms.MmsConfig;
@@ -61,23 +58,21 @@ import static com.google.android.mms.pdu_alt.PduHeaders.MESSAGE_TYPE_READ_ORIG_I
*/
public class PushReceiver extends BroadcastReceiver {
private static final String TAG = LogTag.TAG;
private static final boolean DEBUG = false;
private static final boolean LOCAL_LOGV = true;
static final String[] PROJECTION = new String[] {
Mms.CONTENT_LOCATION,
Mms.LOCKED
};
static final String[] PROJECTION = new String[]{Mms.CONTENT_LOCATION, Mms.LOCKED};
static final int COLUMN_CONTENT_LOCATION = 0;
static final int COLUMN_CONTENT_LOCATION = 0;
private static Set<String> downloadedUrls = new HashSet<String>();
private static final ExecutorService PUSH_RECEIVER_EXECUTOR = Executors.newSingleThreadExecutor();
private class ReceivePushTask extends AsyncTask<Intent,Void,Void> {
private class ReceivePushTask extends AsyncTask<Intent, Void, Void> {
private Context mContext;
public ReceivePushTask(Context context) {
private PendingResult pendingResult;
private ReceivePushTask(Context context, PendingResult pendingResult) {
mContext = context;
this.pendingResult = pendingResult;
}
@Override
@@ -90,7 +85,7 @@ public class PushReceiver extends BroadcastReceiver {
PduParser parser = new PduParser(pushData);
GenericPdu pdu = parser.parse();
if (null == pdu) {
if (pdu == null) {
Log.e(TAG, "Invalid PUSH data");
return null;
}
@@ -98,7 +93,7 @@ public class PushReceiver extends BroadcastReceiver {
PduPersister p = PduPersister.getPduPersister(mContext);
ContentResolver cr = mContext.getContentResolver();
int type = pdu.getMessageType();
long threadId = -1;
long threadId;
try {
switch (type) {
@@ -123,11 +118,11 @@ public class PushReceiver extends BroadcastReceiver {
NotificationInd nInd = (NotificationInd) pdu;
if (MmsConfig.getTransIdEnabled()) {
byte [] contentLocation = nInd.getContentLocation();
byte[] contentLocation = nInd.getContentLocation();
if ('=' == contentLocation[contentLocation.length - 1]) {
byte [] transactionId = nInd.getTransactionId();
byte [] contentLocationWithId = new byte [contentLocation.length
+ transactionId.length];
byte[] transactionId = nInd.getTransactionId();
byte[] contentLocationWithId = new byte[contentLocation.length
+ transactionId.length];
System.arraycopy(contentLocation, 0, contentLocationWithId,
0, contentLocation.length);
System.arraycopy(transactionId, 0, contentLocationWithId,
@@ -156,9 +151,8 @@ public class PushReceiver extends BroadcastReceiver {
Log.v(TAG, "receiving on a lollipop+ device");
DownloadManager.getInstance().downloadMultimediaMessage(mContext, location, uri, true);
} else if (LOCAL_LOGV) {
Log.v(TAG, "Skip downloading duplicate message: "
+ new String(nInd.getContentLocation()));
} else {
Log.v(TAG, "Skip downloading duplicate message: " + new String(nInd.getContentLocation()));
}
break;
}
@@ -171,12 +165,15 @@ public class PushReceiver extends BroadcastReceiver {
Log.e(TAG, "Unexpected RuntimeException.", e);
}
if (LOCAL_LOGV) {
Log.v(TAG, "PUSH Intent processed.");
}
Log.v(TAG, "PUSH Intent processed.");
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
pendingResult.finish();
}
}
@Override
@@ -184,28 +181,16 @@ public class PushReceiver extends BroadcastReceiver {
Log.v(TAG, intent.getAction() + " " + intent.getType());
if ((intent.getAction().equals(WAP_PUSH_DELIVER_ACTION) || intent.getAction().equals(WAP_PUSH_RECEIVED_ACTION))
&& ContentType.MMS_MESSAGE.equals(intent.getType())) {
if (LOCAL_LOGV) {
Log.v(TAG, "Received PUSH Intent: " + intent);
}
Log.v(TAG, "Received PUSH Intent: " + intent);
SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
// Hold a wake lock for 5 seconds, enough to give any
// services we start time to take their own wake locks.
PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"MMS PushReceiver");
wl.acquire(5000);
MmsConfig.init(context);
new ReceivePushTask(context).executeOnExecutor(PUSH_RECEIVER_EXECUTOR, intent);
new ReceivePushTask(context, goAsync()).executeOnExecutor(PUSH_RECEIVER_EXECUTOR, intent);
Log.v("mms_receiver", context.getPackageName() + " received and aborted");
abortBroadcast();
}
}
public static String getContentLocation(Context context, Uri uri)
throws MmsException {
public static String getContentLocation(Context context, Uri uri) throws MmsException {
Cursor cursor = SqliteWrapper.query(context, context.getContentResolver(),
uri, PROJECTION, null, null, null);
@@ -245,8 +230,8 @@ public class PushReceiver extends BroadcastReceiver {
// sb.append(')');
Cursor cursor = SqliteWrapper.query(context, context.getContentResolver(),
Mms.CONTENT_URI, new String[] { Mms.THREAD_ID },
sb.toString(), null, null);
Mms.CONTENT_URI, new String[]{Mms.THREAD_ID},
sb.toString(), null, null);
if (cursor != null) {
try {
if ((cursor.getCount() == 1) && cursor.moveToFirst()) {
@@ -262,16 +247,15 @@ public class PushReceiver extends BroadcastReceiver {
return -1;
}
private static boolean isDuplicateNotification(
Context context, NotificationInd nInd) {
private static boolean isDuplicateNotification(Context context, NotificationInd nInd) {
byte[] rawLocation = nInd.getContentLocation();
if (rawLocation != null) {
String location = new String(rawLocation);
String selection = Mms.CONTENT_LOCATION + " = ?";
String[] selectionArgs = new String[] { location };
String[] selectionArgs = new String[]{location};
Cursor cursor = SqliteWrapper.query(
context, context.getContentResolver(),
Mms.CONTENT_URI, new String[] { Mms._ID },
Mms.CONTENT_URI, new String[]{Mms._ID},
selection, selectionArgs, null);
if (cursor != null) {
try {
@@ -253,7 +253,11 @@ public class TransactionService extends Service implements Observer {
}
public void onNewIntent(Intent intent, int serviceId) {
mobileDataEnabled = Utils.isMobileDataEnabled(this);
try {
mobileDataEnabled = Utils.isMobileDataEnabled(this);
} catch (Exception e) {
mobileDataEnabled = true;
}
mConnMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (!mobileDataEnabled) {
Utils.setMobileDataEnabled(this, true);
@@ -1,114 +0,0 @@
/*
* Copyright 2013 Jacob Klinker
*
* 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.klinker.android.send_message;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import com.klinker.android.logger.Log;
import java.util.Calendar;
public class DeliveredReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.v("delivery_receiver", "marking message as delivered");
Uri uri;
try {
uri = Uri.parse(intent.getStringExtra("message_uri"));
if (uri.equals("")) {
uri = null;
}
} catch (Exception e) {
uri = null;
}
switch (getResultCode()) {
case Activity.RESULT_OK:
// notify user that message was delivered
Intent delivered = new Intent(Transaction.NOTIFY_OF_DELIVERY);
delivered.putExtra("result", true);
delivered.putExtra("message_uri", uri == null ? "" : uri.toString());
BroadcastUtils.sendExplicitBroadcast(
context, delivered, Transaction.NOTIFY_OF_DELIVERY);
if (uri != null) {
ContentValues values = new ContentValues();
values.put("status", "0");
values.put("date_sent", Calendar.getInstance().getTimeInMillis());
values.put("read", true);
context.getContentResolver().update(uri, values, null, null);
} else {
Cursor query = context.getContentResolver().query(Uri.parse("content://sms/sent"), null, null, null, "date desc");
// mark message as delivered in database
if (query.moveToFirst()) {
String id = query.getString(query.getColumnIndex("_id"));
ContentValues values = new ContentValues();
values.put("status", "0");
values.put("date_sent", Calendar.getInstance().getTimeInMillis());
values.put("read", true);
context.getContentResolver().update(Uri.parse("content://sms/sent"), values, "_id=" + id, null);
}
query.close();
}
break;
case Activity.RESULT_CANCELED:
// notify user that message failed to be delivered
Intent notDelivered = new Intent(Transaction.NOTIFY_OF_DELIVERY);
notDelivered.putExtra("result", false);
notDelivered.putExtra("message_uri", uri == null ? "" : uri.toString());
BroadcastUtils.sendExplicitBroadcast(
context, notDelivered, Transaction.NOTIFY_OF_DELIVERY);
if (uri != null) {
ContentValues values = new ContentValues();
values.put("status", "64");
values.put("date_sent", Calendar.getInstance().getTimeInMillis());
values.put("read", true);
values.put("error_code", getResultCode());
context.getContentResolver().update(uri, values, null, null);
} else {
Cursor query2 = context.getContentResolver().query(Uri.parse("content://sms/sent"), null, null, null, "date desc");
// mark failed in database
if (query2.moveToFirst()) {
String id = query2.getString(query2.getColumnIndex("_id"));
ContentValues values = new ContentValues();
values.put("status", "64");
values.put("read", true);
values.put("error_code", getResultCode());
context.getContentResolver().update(Uri.parse("content://sms/sent"), values, "_id=" + id, null);
}
query2.close();
}
break;
}
BroadcastUtils.sendExplicitBroadcast(context, new Intent(), Transaction.REFRESH);
}
}
@@ -1,78 +0,0 @@
/*
* Copyright (C) 2015 Jacob Klinker
*
* 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.klinker.android.send_message
import android.app.Activity
import android.content.*
import android.net.Uri
import android.provider.Telephony
import android.util.Log
import com.google.android.mms.MmsException
import com.google.android.mms.util_alt.SqliteWrapper
import java.io.File
open class MmsSentReceiver : BroadcastReceiver() {
companion object {
private val TAG = "MmsSentReceiver"
const val MMS_SENT = "com.klinker.android.messaging.MMS_SENT"
const val EXTRA_CONTENT_URI = "content_uri"
const val EXTRA_FILE_PATH = "file_path"
}
override fun onReceive(context: Context, intent: Intent) {
Log.v(TAG, "MMS sending result: " + resultCode)
val uri = Uri.parse(intent.getStringExtra(EXTRA_CONTENT_URI))
Log.v(TAG, uri.toString())
if (resultCode == Activity.RESULT_OK) {
Log.v(TAG, "MMS has finished sending, marking it as so in the database")
val values = ContentValues(1)
values.put(Telephony.Mms.MESSAGE_BOX, Telephony.Mms.MESSAGE_BOX_SENT)
SqliteWrapper.update(context, context.contentResolver, uri, values, null, null)
} else {
Log.v(TAG, "MMS has failed to send, marking it as so in the database")
try {
val messageId = ContentUris.parseId(uri)
val values = ContentValues(1)
values.put(Telephony.Mms.MESSAGE_BOX, Telephony.Mms.MESSAGE_BOX_FAILED)
SqliteWrapper.update(context, context.contentResolver, Telephony.Mms.CONTENT_URI, values,
"${Telephony.Mms._ID} = ?", arrayOf(messageId.toString()))
// TODO this query isn't able to find any results
// Need to figure out why the message isn't appearing in the PendingMessages Uri,
// so that we can properly assign the error type
val errorTypeValues = ContentValues(1)
errorTypeValues.put(Telephony.MmsSms.PendingMessages.ERROR_TYPE, Telephony.MmsSms.ERR_TYPE_GENERIC_PERMANENT)
SqliteWrapper.update(context, context.contentResolver, Telephony.MmsSms.PendingMessages.CONTENT_URI,
errorTypeValues, "${Telephony.MmsSms.PendingMessages.MSG_ID} = ?", arrayOf(messageId.toString()))
} catch (e: MmsException) {
e.printStackTrace()
}
}
val filePath = intent.getStringExtra(EXTRA_FILE_PATH)
Log.v(TAG, filePath)
File(filePath).delete()
}
}
@@ -1,114 +0,0 @@
/*
* Copyright 2013 Jacob Klinker
*
* 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.klinker.android.send_message;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.telephony.SmsManager;
import com.klinker.android.logger.Log;
public class SentReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.v("sent_receiver", "marking message as sent");
Uri uri;
try {
uri = Uri.parse(intent.getStringExtra("message_uri"));
if (uri.equals("")) {
uri = null;
}
} catch (Exception e) {
uri = null;
}
switch (getResultCode()) {
case Activity.RESULT_OK:
if (uri != null) {
try {
Log.v("sent_receiver", "using supplied uri");
ContentValues values = new ContentValues();
values.put("type", 2);
values.put("read", 1);
context.getContentResolver().update(uri, values, null, null);
} catch (NullPointerException e) {
markFirstAsSent(context);
}
} else {
markFirstAsSent(context);
}
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
case SmsManager.RESULT_ERROR_NO_SERVICE:
case SmsManager.RESULT_ERROR_NULL_PDU:
case SmsManager.RESULT_ERROR_RADIO_OFF:
if (uri != null) {
Log.v("sent_receiver", "using supplied uri");
ContentValues values = new ContentValues();
values.put("type", 5);
values.put("read", true);
values.put("error_code", getResultCode());
context.getContentResolver().update(uri, values, null, null);
} else {
Log.v("sent_receiver", "using first message");
Cursor query = context.getContentResolver().query(Uri.parse("content://sms/outbox"), null, null, null, null);
// mark message failed
if (query != null && query.moveToFirst()) {
String id = query.getString(query.getColumnIndex("_id"));
ContentValues values = new ContentValues();
values.put("type", 5);
values.put("read", 1);
values.put("error_code", getResultCode());
context.getContentResolver().update(Uri.parse("content://sms/outbox"), values, "_id=" + id, null);
query.close();
}
}
BroadcastUtils.sendExplicitBroadcast(
context, new Intent(), Transaction.NOTIFY_SMS_FAILURE);
break;
}
BroadcastUtils.sendExplicitBroadcast(context, new Intent(), Transaction.REFRESH);
}
private void markFirstAsSent(Context context) {
Log.v("sent_receiver", "using first message");
Cursor query = context.getContentResolver().query(Uri.parse("content://sms/outbox"), null, null, null, null);
// mark message as sent successfully
if (query != null && query.moveToFirst()) {
String id = query.getString(query.getColumnIndex("_id"));
ContentValues values = new ContentValues();
values.put("type", 2);
values.put("read", 1);
context.getContentResolver().update(Uri.parse("content://sms/outbox"), values, "_id=" + id, null);
query.close();
}
}
}
@@ -80,6 +80,10 @@ public class Transaction {
public String SMS_SENT = ".SMS_SENT";
public String SMS_DELIVERED = ".SMS_DELIVERED";
public static final String MMS_SENT = "com.moez.QKSMS.MMS_SENT";
public static final String EXTRA_CONTENT_URI = "content_uri";
public static final String EXTRA_FILE_PATH = "file_path";
public static String NOTIFY_SMS_FAILURE = ".NOTIFY_SMS_FAILURE";
public static final String MMS_UPDATED = "com.moez.QKSMS.MMS_UPDATED";
public static final String MMS_ERROR = "com.klinker.android.send_message.MMS_ERROR";
@@ -384,11 +388,11 @@ public class Transaction {
PduPersister persister = PduPersister.getPduPersister(context);
Uri messageUri = persister.persist(sendReq, Uri.parse("content://mms/outbox"), true, true, null);
Intent sentIntent = new Intent(MmsSentReceiver.MMS_SENT);
BroadcastUtils.addClassName(context, sentIntent, MmsSentReceiver.MMS_SENT);
Intent sentIntent = new Intent(MMS_SENT);
BroadcastUtils.addClassName(context, sentIntent, MMS_SENT);
sentIntent.putExtra(MmsSentReceiver.EXTRA_CONTENT_URI, messageUri.toString());
sentIntent.putExtra(MmsSentReceiver.EXTRA_FILE_PATH, mSendFile.getPath());
sentIntent.putExtra(EXTRA_CONTENT_URI, messageUri.toString());
sentIntent.putExtra(EXTRA_FILE_PATH, mSendFile.getPath());
final PendingIntent sentPI = PendingIntent.getBroadcast(
context, 0, sentIntent, PendingIntent.FLAG_CANCEL_CURRENT);