Search in sources :

Example 1 with DownloadManager

use of com.android.mms.util.DownloadManager in project qksms by moezbhatti.

the class MessageListAdapter method bindNotifInd.

/**
 * Binds a MessageItem that hasn't been downloaded yet
 */
private void bindNotifInd(final MessageListViewHolder holder, final MessageItem messageItem) {
    holder.showMmsView(false);
    switch(messageItem.getMmsDownloadStatus()) {
        case DownloadManager.STATE_PRE_DOWNLOADING:
        case DownloadManager.STATE_DOWNLOADING:
            showDownloadingAttachment(holder);
            break;
        case DownloadManager.STATE_UNKNOWN:
        case DownloadManager.STATE_UNSTARTED:
            DownloadManager downloadManager = DownloadManager.getInstance();
            boolean autoDownload = downloadManager.isAuto();
            boolean dataSuspended = (QKSMSApp.getApplication().getTelephonyManager().getDataState() == TelephonyManager.DATA_SUSPENDED);
            // download begins. Instead, show downloading as taking place.
            if (autoDownload && !dataSuspended) {
                showDownloadingAttachment(holder);
                break;
            }
        case DownloadManager.STATE_TRANSIENT_FAILURE:
        case DownloadManager.STATE_PERMANENT_FAILURE:
        case DownloadManager.STATE_SKIP_RETRYING:
        default:
            holder.inflateDownloadControls();
            holder.mDownloadingLabel.setVisibility(View.GONE);
            holder.mDownloadButton.setVisibility(View.VISIBLE);
            holder.mDownloadButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    holder.mDownloadingLabel.setVisibility(View.VISIBLE);
                    holder.mDownloadButton.setVisibility(View.GONE);
                    Intent intent = new Intent(mContext, TransactionService.class);
                    intent.putExtra(TransactionBundle.URI, messageItem.mMessageUri.toString());
                    intent.putExtra(TransactionBundle.TRANSACTION_TYPE, Transaction.RETRIEVE_TRANSACTION);
                    mContext.startService(intent);
                    DownloadManager.getInstance().markState(messageItem.mMessageUri, DownloadManager.STATE_PRE_DOWNLOADING);
                }
            });
            break;
    }
    // Hide the indicators.
    holder.mLockedIndicator.setVisibility(View.GONE);
    holder.mDeliveredIndicator.setVisibility(View.GONE);
    holder.mDetailsIndicator.setVisibility(View.GONE);
}
Also used : TransactionService(com.android.mms.transaction.TransactionService) Intent(android.content.Intent) DownloadManager(com.android.mms.util.DownloadManager) View(android.view.View) AvatarView(com.moez.QKSMS.ui.view.AvatarView)

Example 2 with DownloadManager

use of com.android.mms.util.DownloadManager in project qksms by moezbhatti.

the class TransactionService method onNewIntent.

public void onNewIntent(Intent intent, int serviceId) {
    mobileDataEnabled = Utils.isMobileDataEnabled(this);
    mConnMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    if (!mobileDataEnabled) {
        Utils.setMobileDataEnabled(this, true);
    }
    if (mConnMgr == null) {
        endMmsConnectivity();
        stopSelf(serviceId);
        return;
    }
    NetworkInfo ni = mConnMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_MMS);
    boolean noNetwork = ni == null || !ni.isAvailable();
    // Make sure we are the default SMS app before processing transactions.
    final String action = intent.getAction();
    if (!Utils.isDefaultSmsApp(this)) {
        // Issue warnings / logs and quit.
        if (TRANSACTION_BUNDLE_ACTION.equals(action)) {
            // This intent only comes from the application developer when they've requested for
            // us to process a Transaction, i.e. a Notification.ind (WAP push) or an M-Send.conf
            // (the user is sending a MM). It is an error to tell us to handle a Transaction
            // when we're not the default, so issue an error log.
            Log.e(TAG, "Asked to process an MMS message when not the default SMS app");
        }
        // don't need to issue any warnings.
        if (LOCAL_LOGV)
            Log.v(TAG, "Stopping TransactionService due to not being the default" + "SMS app");
        endMmsConnectivity();
        stopSelf(serviceId);
        return;
    }
    if (LOCAL_LOGV)
        Log.v(TAG, "onNewIntent: serviceId: " + serviceId + ": " + intent.getExtras() + " intent=" + intent);
    if (LOCAL_LOGV)
        Log.v(TAG, "    networkAvailable=" + !noNetwork);
    if (HANDLE_PENDING_TRANSACTIONS_ACTION.equals(action)) {
        // Scan database to find all pending operations.
        Cursor cursor = PduPersister.getPduPersister(this).getPendingMessages(System.currentTimeMillis());
        if (cursor != null) {
            try {
                int count = cursor.getCount();
                if (LOCAL_LOGV)
                    Log.v(TAG, "onNewIntent: cursor.count=" + count + " action=" + action);
                if (count == 0) {
                    if (LOCAL_LOGV)
                        Log.v(TAG, "onNewIntent: no pending messages. Stopping service.");
                    RetryScheduler.setRetryAlarm(this);
                    stopSelfIfIdle(serviceId);
                    return;
                }
                int columnIndexOfMsgId = cursor.getColumnIndexOrThrow("msg_id");
                int columnIndexOfMsgType = cursor.getColumnIndexOrThrow("msg_type");
                while (cursor.moveToNext()) {
                    int msgType = cursor.getInt(columnIndexOfMsgType);
                    int transactionType = getTransactionType(msgType);
                    if (LOCAL_LOGV)
                        Log.v(TAG, "onNewIntent: msgType=" + msgType + " transactionType=" + transactionType);
                    if (noNetwork) {
                        onNetworkUnavailable(serviceId, transactionType);
                        return;
                    }
                    switch(transactionType) {
                        case -1:
                            break;
                        case Transaction.RETRIEVE_TRANSACTION:
                            // If it's a transiently failed transaction,
                            // we should retry it in spite of current
                            // downloading mode. If the user just turned on the auto-retrieve
                            // option, we also retry those messages that don't have any errors.
                            int failureType = cursor.getInt(cursor.getColumnIndexOrThrow("err_type"));
                            DownloadManager.init(this);
                            DownloadManager downloadManager = DownloadManager.getInstance();
                            boolean autoDownload = downloadManager.isAuto();
                            if (LOCAL_LOGV)
                                Log.v(TAG, "onNewIntent: failureType=" + failureType + " action=" + action + " isTransientFailure:" + isTransientFailure(failureType) + " autoDownload=" + autoDownload);
                            if (!autoDownload) {
                                // transaction.
                                if (LOCAL_LOGV)
                                    Log.v(TAG, "onNewIntent: skipping - autodownload off");
                                // sendBroadcast(new Intent(com.moez.QKSMS.send_message.Transaction.NOTIFY_OF_MMS));
                                break;
                            }
                            // Otherwise, break out and skip processing this transaction.
                            if (!(failureType == 0 || isTransientFailure(failureType))) {
                                if (LOCAL_LOGV)
                                    Log.v(TAG, "onNewIntent: skipping - permanent error");
                                break;
                            }
                            if (LOCAL_LOGV)
                                Log.v(TAG, "onNewIntent: falling through and processing");
                        // fall-through
                        default:
                            Uri uri = ContentUris.withAppendedId(Uri.parse("content://mms"), cursor.getLong(columnIndexOfMsgId));
                            TransactionBundle args = new TransactionBundle(transactionType, uri.toString());
                            // FIXME: We use the same startId for all MMs.
                            if (LOCAL_LOGV)
                                Log.v(TAG, "onNewIntent: launchTransaction uri=" + uri);
                            launchTransaction(serviceId, args, false);
                            break;
                    }
                }
            } finally {
                cursor.close();
            }
        } else {
            if (LOCAL_LOGV)
                Log.v(TAG, "onNewIntent: no pending messages. Stopping service.");
            RetryScheduler.setRetryAlarm(this);
            stopSelfIfIdle(serviceId);
        }
    } else if (TRANSACTION_BUNDLE_ACTION.equals(action)) {
        if (LOCAL_LOGV)
            Log.v(TAG, "onNewIntent: launch transaction...");
        // For launching NotificationTransaction and test purpose.
        TransactionBundle args = new TransactionBundle(intent.getExtras());
        launchTransaction(serviceId, args, noNetwork);
    }
}
Also used : NetworkInfo(android.net.NetworkInfo) Cursor(android.database.Cursor) DownloadManager(com.android.mms.util.DownloadManager) Uri(android.net.Uri)

Example 3 with DownloadManager

use of com.android.mms.util.DownloadManager in project qksms by moezbhatti.

the class NotificationTransaction method run.

public void run() {
    try {
        Looper.prepare();
    } catch (Exception e) {
        Log.e(TAG, "exception thrown", e);
    }
    DownloadManager.init(mContext);
    DownloadManager downloadManager = DownloadManager.getInstance();
    boolean autoDownload = allowAutoDownload(mContext);
    try {
        if (LOCAL_LOGV)
            Log.v(TAG, "Notification transaction launched: " + this);
        // By default, we set status to STATUS_DEFERRED because we
        // should response MMSC with STATUS_DEFERRED when we cannot
        // download a MM immediately.
        int status = STATUS_DEFERRED;
        // Don't try to download when data is suspended, as it will fail, so defer download
        if (!autoDownload) {
            downloadManager.markState(mUri, DownloadManager.STATE_UNSTARTED);
            sendNotifyRespInd(status);
            return;
        }
        downloadManager.markState(mUri, DownloadManager.STATE_DOWNLOADING);
        if (LOCAL_LOGV)
            Log.v(TAG, "Content-Location: " + mContentLocation);
        byte[] retrieveConfData = null;
        // with STATUS_DEFERRED.
        try {
            retrieveConfData = getPdu(mContentLocation);
        } catch (IOException e) {
            mTransactionState.setState(FAILED);
        }
        if (retrieveConfData != null) {
            GenericPdu pdu = new PduParser(retrieveConfData).parse();
            if ((pdu == null) || (pdu.getMessageType() != MESSAGE_TYPE_RETRIEVE_CONF)) {
                Log.e(TAG, "Invalid M-RETRIEVE.CONF PDU. " + (pdu != null ? "message type: " + pdu.getMessageType() : "null pdu"));
                mTransactionState.setState(FAILED);
                status = STATUS_UNRECOGNIZED;
            } else {
                // Save the received PDU (must be a M-RETRIEVE.CONF).
                PduPersister p = PduPersister.getPduPersister(mContext);
                Uri uri = p.persist(pdu, Inbox.CONTENT_URI, true, com.moez.QKSMS.mmssms.Transaction.settings.getGroup(), null);
                // Use local time instead of PDU time
                ContentValues values = new ContentValues(1);
                values.put(Mms.DATE, System.currentTimeMillis() / 1000L);
                SqliteWrapper.update(mContext, mContext.getContentResolver(), uri, values, null, null);
                // We have successfully downloaded the new MM. Delete the
                // M-NotifyResp.ind from Inbox.
                SqliteWrapper.delete(mContext, mContext.getContentResolver(), mUri, null, null);
                Log.v(TAG, "NotificationTransaction received new mms message: " + uri);
                // Delete obsolete threads
                SqliteWrapper.delete(mContext, mContext.getContentResolver(), Threads.OBSOLETE_THREADS_URI, null, null);
                // Notify observers with newly received MM.
                mUri = uri;
                status = STATUS_RETRIEVED;
                mContext.sendBroadcast(new Intent(com.moez.QKSMS.mmssms.Transaction.NOTIFY_OF_MMS));
            }
        }
        if (LOCAL_LOGV)
            Log.v(TAG, "status=0x" + Integer.toHexString(status));
        // Check the status and update the result state of this Transaction.
        switch(status) {
            case STATUS_RETRIEVED:
                mTransactionState.setState(SUCCESS);
                break;
            case STATUS_DEFERRED:
                // STATUS_DEFERRED, may be a failed immediate retrieval.
                if (mTransactionState.getState() == INITIALIZED) {
                    mTransactionState.setState(SUCCESS);
                }
                break;
        }
        sendNotifyRespInd(status);
    } catch (Throwable t) {
        Log.e(TAG, Log.getStackTraceString(t));
    } finally {
        mTransactionState.setContentUri(mUri);
        if (!autoDownload) {
            // Always mark the transaction successful for deferred
            // download since any error here doesn't make sense.
            mTransactionState.setState(SUCCESS);
        }
        if (mTransactionState.getState() != SUCCESS) {
            mTransactionState.setState(FAILED);
            Log.e(TAG, "NotificationTransaction failed.");
        }
        notifyObservers();
    }
}
Also used : ContentValues(android.content.ContentValues) PduParser(com.google.android.mms.pdu_alt.PduParser) PduPersister(com.google.android.mms.pdu_alt.PduPersister) Intent(android.content.Intent) IOException(java.io.IOException) DownloadManager(com.android.mms.util.DownloadManager) Uri(android.net.Uri) MmsException(com.google.android.mms.MmsException) IOException(java.io.IOException) GenericPdu(com.google.android.mms.pdu_alt.GenericPdu)

Aggregations

DownloadManager (com.android.mms.util.DownloadManager)3 Intent (android.content.Intent)2 Uri (android.net.Uri)2 ContentValues (android.content.ContentValues)1 Cursor (android.database.Cursor)1 NetworkInfo (android.net.NetworkInfo)1 View (android.view.View)1 TransactionService (com.android.mms.transaction.TransactionService)1 MmsException (com.google.android.mms.MmsException)1 GenericPdu (com.google.android.mms.pdu_alt.GenericPdu)1 PduParser (com.google.android.mms.pdu_alt.PduParser)1 PduPersister (com.google.android.mms.pdu_alt.PduPersister)1 AvatarView (com.moez.QKSMS.ui.view.AvatarView)1 IOException (java.io.IOException)1