FileDocCategorySizeDatePackage
DownloadManager.javaAPI DocAndroid 1.5 API8629Wed May 06 22:42:46 BST 2009com.android.mms.util

DownloadManager

public class DownloadManager extends Object

Fields Summary
private static final String
TAG
private static final boolean
DEBUG
private static final boolean
LOCAL_LOGV
private static final int
DEFERRED_MASK
public static final int
STATE_UNSTARTED
public static final int
STATE_DOWNLOADING
public static final int
STATE_TRANSIENT_FAILURE
public static final int
STATE_PERMANENT_FAILURE
private final android.content.Context
mContext
private final android.os.Handler
mHandler
private final android.content.SharedPreferences
mPreferences
private boolean
mAutoDownload
private final android.content.SharedPreferences.OnSharedPreferenceChangeListener
mPreferencesChangeListener
private final android.content.BroadcastReceiver
mRoamingStateListener
private static DownloadManager
sInstance
Constructors Summary
private DownloadManager(android.content.Context context)


       
        mContext = context;
        mHandler = new Handler();
        mPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        mPreferences.registerOnSharedPreferenceChangeListener(mPreferencesChangeListener);

        context.registerReceiver(
                mRoamingStateListener,
                new IntentFilter(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED));

        mAutoDownload = getAutoDownloadState(mPreferences);
        if (LOCAL_LOGV) {
            Log.v(TAG, "mAutoDownload ------> " + mAutoDownload);
        }
    
Methods Summary
static booleangetAutoDownloadState(android.content.SharedPreferences prefs)

        return getAutoDownloadState(prefs, isRoaming());
    
static booleangetAutoDownloadState(android.content.SharedPreferences prefs, boolean roaming)

        boolean autoDownload = prefs.getBoolean(
                MessagingPreferenceActivity.AUTO_RETRIEVAL, true);

        if (LOCAL_LOGV) {
            Log.v(TAG, "auto download without roaming -> " + autoDownload);
        }

        if (autoDownload) {
            boolean alwaysAuto = prefs.getBoolean(
                    MessagingPreferenceActivity.RETRIEVAL_DURING_ROAMING, false);

            if (LOCAL_LOGV) {
                Log.v(TAG, "auto download during roaming -> " + alwaysAuto);
            }

            if (!roaming || alwaysAuto) {
                return true;
            }
        }
        return false;
    
public static com.android.mms.util.DownloadManagergetInstance()

        if (sInstance == null) {
            throw new IllegalStateException("Uninitialized.");
        }
        return sInstance;
    
private java.lang.StringgetMessage(android.net.Uri uri)

        NotificationInd ind = (NotificationInd) PduPersister
                .getPduPersister(mContext).load(uri);

        EncodedStringValue v = ind.getSubject();
        String subject = (v != null) ? v.getString()
                : mContext.getString(R.string.no_subject);

        v = ind.getFrom();
        String from = (v != null)
                ? ContactInfoCache.getInstance().getContactName(mContext, v.getString())
                : mContext.getString(R.string.unknown_sender);

        return mContext.getString(R.string.dl_failure_notification, subject, from);
    
public intgetState(android.net.Uri uri)

        Cursor cursor = SqliteWrapper.query(mContext, mContext.getContentResolver(),
                            uri, new String[] {Mms.STATUS}, null, null, null);

        if (cursor != null) {
            try {
                if (cursor.moveToFirst()) {
                    return cursor.getInt(0) &~ DEFERRED_MASK;
                }
            } finally {
                cursor.close();
            }
        }
        return STATE_UNSTARTED;
    
public static voidinit(android.content.Context context)

        if (LOCAL_LOGV) {
            Log.v(TAG, "DownloadManager.init()");
        }

        if (sInstance != null) {
            Log.w(TAG, "Already initialized.");
        }
        sInstance = new DownloadManager(context);
    
public booleanisAuto()

        return mAutoDownload;
    
static booleanisRoaming()

        String roaming = SystemProperties.get(
                TelephonyProperties.PROPERTY_OPERATOR_ISROAMING, null);
        if (LOCAL_LOGV) {
            Log.v(TAG, "roaming ------> " + roaming);
        }
        return "true".equals(roaming);
    
public voidmarkState(android.net.Uri uri, int state)

        // Notify user if downloading permanently failed.
        if (state == STATE_PERMANENT_FAILURE) {
            mHandler.post(new Runnable() {
                public void run() {
                    try {
                        Toast.makeText(mContext, getMessage(uri),
                                Toast.LENGTH_LONG).show();
                    } catch (MmsException e) {
                        Log.e(TAG, e.getMessage(), e);
                    }
                }
            });
        } else if (!mAutoDownload) {
            state |= DEFERRED_MASK;
        }

        // Use the STATUS field to store the state of downloading process
        // because it's useless for M-Notification.ind.
        ContentValues values = new ContentValues(1);
        values.put(Mms.STATUS, state);
        SqliteWrapper.update(mContext, mContext.getContentResolver(),
                    uri, values, null, null);