http://www.oschina.net/code/explore/android-2.2-froyo/com/android/mms/util/DownloadManager.java
002 | * Copyright (C) 2008 Esmertec AG. |
003 | * Copyright (C) 2008 The Android Open Source Project |
005 | * Licensed under the Apache License, Version 2.0 (the "License"); |
006 | * you may not use this file except in compliance with the License. |
007 | * You may obtain a copy of the License at |
009 | * http://www.apache.org/licenses/LICENSE-2.0 |
011 | * Unless required by applicable law or agreed to in writing, software |
012 | * distributed under the License is distributed on an "AS IS" BASIS, |
013 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
014 | * See the License for the specific language governing permissions and |
015 | * limitations under the License. |
018 | package com.android.mms.util; |
020 | import com.android.mms.R; |
021 | import com.android.mms.data.Contact; |
022 | import com.android.mms.ui.MessagingPreferenceActivity; |
023 | import com.google.android.mms.MmsException; |
024 | import com.google.android.mms.pdu.EncodedStringValue; |
025 | import com.google.android.mms.pdu.NotificationInd; |
026 | import com.google.android.mms.pdu.PduPersister; |
027 | import android.database.sqlite.SqliteWrapper; |
028 | import com.android.internal.telephony.TelephonyIntents; |
029 | import com.android.internal.telephony.TelephonyProperties; |
031 | import android.content.BroadcastReceiver; |
032 | import android.content.ContentValues; |
033 | import android.content.Context; |
034 | import android.content.Intent; |
035 | import android.content.IntentFilter; |
036 | import android.content.SharedPreferences; |
037 | import android.content.SharedPreferences.OnSharedPreferenceChangeListener; |
038 | import android.database.Cursor; |
039 | import android.net.Uri; |
040 | import android.os.Handler; |
041 | import android.preference.PreferenceManager; |
042 | import android.provider.Telephony.Mms; |
043 | import android.telephony.ServiceState; |
044 | import android.util.Config; |
045 | import android.util.Log; |
046 | import android.widget.Toast; |
048 | import android.os.SystemProperties; |
050 | public class DownloadManager { |
051 | private static final String TAG = "DownloadManager" ; |
052 | private static final boolean DEBUG = false ; |
053 | private static final boolean LOCAL_LOGV = DEBUG ? Config.LOGD : Config.LOGV; |
055 | private static final int DEFERRED_MASK = 0x04 ; |
057 | public static final int STATE_UNSTARTED = 0x80 ; |
058 | public static final int STATE_DOWNLOADING = 0x81 ; |
059 | public static final int STATE_TRANSIENT_FAILURE = 0x82 ; |
060 | public static final int STATE_PERMANENT_FAILURE = 0x87 ; |
062 | private final Context mContext; |
063 | private final Handler mHandler; |
064 | private final SharedPreferences mPreferences; |
065 | private boolean mAutoDownload; |
067 | private final OnSharedPreferenceChangeListener mPreferencesChangeListener = |
068 | new OnSharedPreferenceChangeListener() { |
069 | public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { |
070 | if (MessagingPreferenceActivity.AUTO_RETRIEVAL.equals(key) |
071 | || MessagingPreferenceActivity.RETRIEVAL_DURING_ROAMING.equals(key)) { |
073 | Log.v(TAG, "Preferences updated." ); |
076 | synchronized (sInstance) { |
077 | mAutoDownload = getAutoDownloadState(prefs); |
079 | Log.v(TAG, "mAutoDownload ------> " + mAutoDownload); |
086 | private final BroadcastReceiver mRoamingStateListener = |
087 | new BroadcastReceiver() { |
089 | public void onReceive(Context context, Intent intent) { |
090 | if (TelephonyIntents.ACTION_SERVICE_STATE_CHANGED.equals(intent.getAction())) { |
092 | Log.v(TAG, "Service state changed: " + intent.getExtras()); |
095 | ServiceState state = ServiceState.newFromBundle(intent.getExtras()); |
096 | boolean isRoaming = state.getRoaming(); |
098 | Log.v(TAG, "roaming ------> " + isRoaming); |
100 | synchronized (sInstance) { |
101 | mAutoDownload = getAutoDownloadState(mPreferences, isRoaming); |
103 | Log.v(TAG, "mAutoDownload ------> " + mAutoDownload); |
110 | private static DownloadManager sInstance; |
112 | private DownloadManager(Context context) { |
114 | mHandler = new Handler(); |
115 | mPreferences = PreferenceManager.getDefaultSharedPreferences(context); |
116 | mPreferences.registerOnSharedPreferenceChangeListener(mPreferencesChangeListener); |
118 | context.registerReceiver( |
119 | mRoamingStateListener, |
120 | new IntentFilter(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED)); |
122 | mAutoDownload = getAutoDownloadState(mPreferences); |
124 | Log.v(TAG, "mAutoDownload ------> " + mAutoDownload); |
128 | public boolean isAuto() { |
132 | public static void init(Context context) { |
134 | Log.v(TAG, "DownloadManager.init()" ); |
137 | if (sInstance != null ) { |
138 | Log.w(TAG, "Already initialized." ); |
140 | sInstance = new DownloadManager(context); |
143 | public static DownloadManager getInstance() { |
144 | if (sInstance == null ) { |
145 | throw new IllegalStateException( "Uninitialized." ); |
150 | static boolean getAutoDownloadState(SharedPreferences prefs) { |
151 | return getAutoDownloadState(prefs, isRoaming()); |
154 | static boolean getAutoDownloadState(SharedPreferences prefs, boolean roaming) { |
155 | boolean autoDownload = prefs.getBoolean( |
156 | MessagingPreferenceActivity.AUTO_RETRIEVAL, true ); |
159 | Log.v(TAG, "auto download without roaming -> " + autoDownload); |
163 | boolean alwaysAuto = prefs.getBoolean( |
164 | MessagingPreferenceActivity.RETRIEVAL_DURING_ROAMING, false ); |
167 | Log.v(TAG, "auto download during roaming -> " + alwaysAuto); |
170 | if (!roaming || alwaysAuto) { |
177 | static boolean isRoaming() { |
179 | String roaming = SystemProperties.get( |
180 | TelephonyProperties.PROPERTY_OPERATOR_ISROAMING, null ); |
182 | Log.v(TAG, "roaming ------> " + roaming); |
184 | return "true" .equals(roaming); |
187 | public void markState( final Uri uri, int state) { |
190 | NotificationInd nInd = (NotificationInd) PduPersister.getPduPersister(mContext) |
192 | if ((nInd.getExpiry() < System.currentTimeMillis()/1000L) |
193 | && (state == STATE_DOWNLOADING)) { |
194 | mHandler.post( new Runnable() { |
196 | Toast.makeText(mContext, R.string.dl_expired_notification, |
197 | Toast.LENGTH_LONG).show(); |
200 | SqliteWrapper.delete(mContext, mContext.getContentResolver(), uri, null , null ); |
203 | } catch (MmsException e) { |
204 | Log.e(TAG, e.getMessage(), e); |
209 | if (state == STATE_PERMANENT_FAILURE) { |
210 | mHandler.post( new Runnable() { |
213 | Toast.makeText(mContext, getMessage(uri), |
214 | Toast.LENGTH_LONG).show(); |
215 | } catch (MmsException e) { |
216 | Log.e(TAG, e.getMessage(), e); |
220 | } else if (!mAutoDownload) { |
221 | state |= DEFERRED_MASK; |
226 | ContentValues values = new ContentValues( 1 ); |
227 | values.put(Mms.STATUS, state); |
228 | SqliteWrapper.update(mContext, mContext.getContentResolver(), |
229 | uri, values, null , null ); |
232 | public void showErrorCodeToast( int errorStr) { |
233 | final int errStr = errorStr; |
234 | mHandler.post( new Runnable() { |
237 | Toast.makeText(mContext, errStr, Toast.LENGTH_LONG).show(); |
238 | } catch (Exception e) { |
239 | Log.e(TAG, "Caught an exception in showErrorCodeToast" ); |
245 | private String getMessage(Uri uri) throws MmsException { |
246 | NotificationInd ind = (NotificationInd) PduPersister |
247 | .getPduPersister(mContext).load(uri); |
249 | EncodedStringValue v = ind.getSubject(); |
250 | String subject = (v != null ) ? v.getString() |
251 | : mContext.getString(R.string.no_subject); |
254 | String from = (v != null ) |
255 | ? Contact.get(v.getString(), false ).getName() |
256 | : mContext.getString(R.string.unknown_sender); |
258 | return mContext.getString(R.string.dl_failure_notification, subject, from); |
261 | public int getState(Uri uri) { |
262 | Cursor cursor = SqliteWrapper.query(mContext, mContext.getContentResolver(), |
263 | uri, new String[] {Mms.STATUS}, null , null , null ); |
267 | if (cursor.moveToFirst()) { |
268 | return cursor.getInt( 0 ) &~ DEFERRED_MASK; |
274 | return STATE_UNSTARTED; |
002 | * Copyright (C) 2008 Esmertec AG. |
003 | * Copyright (C) 2008 The Android Open Source Project |
005 | * Licensed under the Apache License, Version 2.0 (the "License"); |
006 | * you may not use this file except in compliance with the License. |
007 | * You may obtain a copy of the License at |
009 | * http://www.apache.org/licenses/LICENSE-2.0 |
011 | * Unless required by applicable law or agreed to in writing, software |
012 | * distributed under the License is distributed on an "AS IS" BASIS, |
013 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
014 | * See the License for the specific language governing permissions and |
015 | * limitations under the License. |
018 | package com.android.mms.util; |
020 | import com.android.mms.R; |
021 | import com.android.mms.data.Contact; |
022 | import com.android.mms.ui.MessagingPreferenceActivity; |
023 | import com.google.android.mms.MmsException; |
024 | import com.google.android.mms.pdu.EncodedStringValue; |
025 | import com.google.android.mms.pdu.NotificationInd; |
026 | import com.google.android.mms.pdu.PduPersister; |
027 | import android.database.sqlite.SqliteWrapper; |
028 | import com.android.internal.telephony.TelephonyIntents; |
029 | import com.android.internal.telephony.TelephonyProperties; |
031 | import android.content.BroadcastReceiver; |
032 | import android.content.ContentValues; |
033 | import android.content.Context; |
034 | import android.content.Intent; |
035 | import android.content.IntentFilter; |
036 | import android.content.SharedPreferences; |
037 | import android.content.SharedPreferences.OnSharedPreferenceChangeListener; |
038 | import android.database.Cursor; |
039 | import android.net.Uri; |
040 | import android.os.Handler; |
041 | import android.preference.PreferenceManager; |
042 | import android.provider.Telephony.Mms; |
043 | import android.telephony.ServiceState; |
044 | import android.util.Config; |
045 | import android.util.Log; |
046 | import android.widget.Toast; |
048 | import android.os.SystemProperties; |
050 | public class DownloadManager { |
051 | private static final String TAG = "DownloadManager" ; |
052 | private static final boolean DEBUG = false ; |
053 | private static final boolean LOCAL_LOGV = DEBUG ? Config.LOGD : Config.LOGV; |
055 | private static final int DEFERRED_MASK = 0x04 ; |
057 | public static final int STATE_UNSTARTED = 0x80 ; |
058 | public static final int STATE_DOWNLOADING = 0x81 ; |
059 | public static final int STATE_TRANSIENT_FAILURE = 0x82 ; |
060 | public static final int STATE_PERMANENT_FAILURE = 0x87 ; |
062 | private final Context mContext; |
063 | private final Handler mHandler; |
064 | private final SharedPreferences mPreferences; |
065 | private boolean mAutoDownload; |
067 | private final OnSharedPreferenceChangeListener mPreferencesChangeListener = |
068 | new OnSharedPreferenceChangeListener() { |
069 | public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { |
070 | if (MessagingPreferenceActivity.AUTO_RETRIEVAL.equals(key) |
071 | || MessagingPreferenceActivity.RETRIEVAL_DURING_ROAMING.equals(key)) { |
073 | Log.v(TAG, "Preferences updated." ); |
076 | synchronized (sInstance) { |
077 | mAutoDownload = getAutoDownloadState(prefs); |
079 | Log.v(TAG, "mAutoDownload ------> " + mAutoDownload); |
086 | private final BroadcastReceiver mRoamingStateListener = |
087 | new BroadcastReceiver() { |
089 | public void onReceive(Context context, Intent intent) { |
090 | if (TelephonyIntents.ACTION_SERVICE_STATE_CHANGED.equals(intent.getAction())) { |
092 | Log.v(TAG, "Service state changed: " + intent.getExtras()); |
095 | ServiceState state = ServiceState.newFromBundle(intent.getExtras()); |
096 | boolean isRoaming = state.getRoaming(); |
098 | Log.v(TAG, "roaming ------> " + isRoaming); |
100 | synchronized (sInstance) { |
101 | mAutoDownload = getAutoDownloadState(mPreferences, isRoaming); |
103 | Log.v(TAG, "mAutoDownload ------> " + mAutoDownload); |
110 | private static DownloadManager sInstance; |
112 | private DownloadManager(Context context) { |
114 | mHandler = new Handler(); |
115 | mPreferences = PreferenceManager.getDefaultSharedPreferences(context); |
116 | mPreferences.registerOnSharedPreferenceChangeListener(mPreferencesChangeListener); |
118 | context.registerReceiver( |
119 | mRoamingStateListener, |
120 | new IntentFilter(TelephonyIntents.ACTION_SERVICE_STATE_CHANGED)); |
122 | mAutoDownload = getAutoDownloadState(mPreferences); |
124 | Log.v(TAG, "mAutoDownload ------> " + mAutoDownload); |
128 | public boolean isAuto() { |
132 | public static void init(Context context) { |
134 | Log.v(TAG, "DownloadManager.init()" ); |
137 | if (sInstance != null ) { |
138 | Log.w(TAG, "Already initialized." ); |
140 | sInstance = new DownloadManager(context); |
143 | public static DownloadManager getInstance() { |
144 | if (sInstance == null ) { |
145 | throw new IllegalStateException( "Uninitialized." ); |
150 | static boolean getAutoDownloadState(SharedPreferences prefs) { |
151 | return getAutoDownloadState(prefs, isRoaming()); |
154 | static boolean getAutoDownloadState(SharedPreferences prefs, boolean roaming) { |
155 | boolean autoDownload = prefs.getBoolean( |
156 | MessagingPreferenceActivity.AUTO_RETRIEVAL, true ); |
159 | Log.v(TAG, "auto download without roaming -> " + autoDownload); |
163 | boolean alwaysAuto = prefs.getBoolean( |
164 | MessagingPreferenceActivity.RETRIEVAL_DURING_ROAMING, false ); |
167 | Log.v(TAG, "auto download during roaming -> " + alwaysAuto); |
170 | if (!roaming || alwaysAuto) { |
177 | static boolean isRoaming() { |
179 | String roaming = SystemProperties.get( |
180 | TelephonyProperties.PROPERTY_OPERATOR_ISROAMING, null ); |
182 | Log.v(TAG, "roaming ------> " + roaming); |
184 | return "true" .equals(roaming); |
187 | public void markState( final Uri uri, int state) { |
190 | NotificationInd nInd = (NotificationInd) PduPersister.getPduPersister(mContext) |
192 | if ((nInd.getExpiry() < System.currentTimeMillis()/1000L) |
193 | && (state == STATE_DOWNLOADING)) { |
194 | mHandler.post( new Runnable() { |
196 | Toast.makeText(mContext, R.string.dl_expired_notification, |
197 | Toast.LENGTH_LONG).show(); |
200 | SqliteWrapper.delete(mContext, mContext.getContentResolver(), uri, null , null ); |
203 | } catch (MmsException e) { |
204 | Log.e(TAG, e.getMessage(), e); |
209 | if (state == STATE_PERMANENT_FAILURE) { |
210 | mHandler.post( new Runnable() { |
213 | Toast.makeText(mContext, getMessage(uri), |
214 | Toast.LENGTH_LONG).show(); |
215 | } catch (MmsException e) { |
216 | Log.e(TAG, e.getMessage(), e); |
220 | } else if (!mAutoDownload) { |
221 | state |= DEFERRED_MASK; |
226 | ContentValues values = new ContentValues( 1 ); |
227 | values.put(Mms.STATUS, state); |
228 | SqliteWrapper.update(mContext, mContext.getContentResolver(), |
229 | uri, values, null , null ); |
232 | public void showErrorCodeToast( int errorStr) { |
233 | final int errStr = errorStr; |
234 | mHandler.post( new Runnable() { |
237 | Toast.makeText(mContext, errStr, Toast.LENGTH_LONG).show(); |
238 | } catch (Exception e) { |
239 | Log.e(TAG, "Caught an exception in showErrorCodeToast" ); |
245 | private String getMessage(Uri uri) throws MmsException { |
246 | NotificationInd ind = (NotificationInd) PduPersister |
247 | .getPduPersister(mContext).load(uri); |
249 | EncodedStringValue v = ind.getSubject(); |
250 | String subject = (v != null ) ? v.getString() |
251 | : mContext.getString(R.string.no_subject); |
254 | String from = (v != null ) |
255 | ? Contact.get(v.getString(), false ).getName() |
256 | : mContext.getString(R.string.unknown_sender); |
258 | return mContext.getString(R.string.dl_failure_notification, subject, from); |
261 | public int getState(Uri uri) { |
262 | Cursor cursor = SqliteWrapper.query(mContext, mContext.getContentResolver(), |
263 | uri, new String[] {Mms.STATUS}, null , null , null ); |
267 | if (cursor.moveToFirst()) { |
268 | return cursor.getInt( 0 ) &~ DEFERRED_MASK; |
274 | return STATE_UNSTARTED; |