Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 37997245 authored by Moez Bhatti's avatar Moez Bhatti
Browse files

Pull in changes from smsmms, remove unused classes

parent 71deb2df
Loading
Loading
Loading
Loading
+29 −45
Original line number Diff line number Diff line
@@ -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,13 +58,8 @@ 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;

@@ -76,8 +68,11 @@ public class PushReceiver extends BroadcastReceiver {

    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) {
@@ -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.");
            }

            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);
            }

            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);

@@ -262,8 +247,7 @@ 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);
+5 −1
Original line number Diff line number Diff line
@@ -253,7 +253,11 @@ public class TransactionService extends Service implements Observer {
    }

    public void onNewIntent(Intent intent, int serviceId) {
        try {
            mobileDataEnabled = Utils.isMobileDataEnabled(this);
        } catch (Exception e) {
            mobileDataEnabled = true;
        }
        mConnMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        if (!mobileDataEnabled) {
            Utils.setMobileDataEnabled(this, true);
+0 −114
Original line number Diff line number Diff line
/*
 * 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);
    }
}
+0 −78
Original line number Diff line number Diff line
/*
 * 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()
    }

}
+0 −114
Original line number Diff line number Diff line
/*
 * 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();
        }
    }
}
Loading