diff options
Diffstat (limited to 'src/com')
6 files changed, 240 insertions, 25 deletions
diff --git a/src/com/android/providers/downloads/DownloadInfo.java b/src/com/android/providers/downloads/DownloadInfo.java index eb9ac4bd..467af836 100644 --- a/src/com/android/providers/downloads/DownloadInfo.java +++ b/src/com/android/providers/downloads/DownloadInfo.java @@ -87,6 +87,8 @@ public class DownloadInfo { info.mAllowRoaming = getInt(Downloads.Impl.COLUMN_ALLOW_ROAMING) != 0; info.mTitle = getString(info.mTitle, Downloads.Impl.COLUMN_TITLE); info.mDescription = getString(info.mDescription, Downloads.Impl.COLUMN_DESCRIPTION); + info.mBypassRecommendedSizeLimit = + getInt(Downloads.Impl.COLUMN_BYPASS_RECOMMENDED_SIZE_LIMIT); synchronized (this) { info.mControl = getInt(Downloads.Impl.COLUMN_CONTROL); @@ -159,6 +161,37 @@ public class DownloadInfo { } } + // the following NETWORK_* constants are used to indicates specfic reasons for disallowing a + // download from using a network, since specific causes can require special handling + + /** + * The network is usable for the given download. + */ + public static final int NETWORK_OK = 1; + + /** + * The network is unusuable for some unspecified reason. + */ + public static final int NETWORK_UNUSABLE_GENERIC = 2; + + /** + * The download exceeds the maximum size for this network. + */ + public static final int NETWORK_UNUSABLE_DUE_TO_SIZE = 3; + + /** + * The download exceeds the recommended maximum size for this network, the user must confirm for + * this download to proceed without WiFi. + */ + public static final int NETWORK_RECOMMENDED_UNUSABLE_DUE_TO_SIZE = 4; + + /** + * For intents used to notify the user that a download exceeds a size threshold, if this extra + * is true, WiFi is required for this download size; otherwise, it is only recommended. + */ + public static final String EXTRA_IS_WIFI_REQUIRED = "isWifiRequired"; + + public long mId; public String mUri; public boolean mNoIntegrity; @@ -188,6 +221,7 @@ public class DownloadInfo { public boolean mAllowRoaming; public String mTitle; public String mDescription; + public int mBypassRecommendedSizeLimit; public String mPausedReason; public int mFuzz; @@ -307,7 +341,7 @@ public class DownloadInfo { if (mStatus == Downloads.Impl.STATUS_RUNNING_PAUSED) { if (mNumFailed == 0) { // download is waiting for network connectivity to return before it can resume - return canUseNetwork(); + return checkCanUseNetwork() == NETWORK_OK; } if (restartTime() < now) { // download was waiting for a delayed restart, and the delay has expired @@ -333,19 +367,17 @@ public class DownloadInfo { /** * Returns whether this download is allowed to use the network. + * @return one of the NETWORK_* constants */ - public boolean canUseNetwork() { + public int checkCanUseNetwork() { Integer networkType = mSystemFacade.getActiveNetworkType(); if (networkType == null) { - return false; - } - if (!isNetworkTypeAllowed(networkType)) { - return false; + return NETWORK_UNUSABLE_GENERIC; } if (!isRoamingAllowed() && mSystemFacade.isNetworkRoaming()) { - return false; + return NETWORK_UNUSABLE_GENERIC; } - return true; + return checkIsNetworkTypeAllowed(networkType); } private boolean isRoamingAllowed() { @@ -359,20 +391,16 @@ public class DownloadInfo { /** * Check if this download can proceed over the given network type. * @param networkType a constant from ConnectivityManager.TYPE_*. + * @return one of the NETWORK_* constants */ - private boolean isNetworkTypeAllowed(int networkType) { + private int checkIsNetworkTypeAllowed(int networkType) { if (mIsPublicApi) { int flag = translateNetworkTypeToApiFlag(networkType); if ((flag & mAllowedNetworkTypes) == 0) { - return false; + return NETWORK_UNUSABLE_GENERIC; } } - if (!isSizeAllowedForNetwork(networkType)) { - mPausedReason = mContext.getResources().getString( - R.string.notification_need_wifi_for_size); - return false; - } - return true; + return checkSizeAllowedForNetwork(networkType); } /** @@ -397,19 +425,27 @@ public class DownloadInfo { /** * Check if the download's size prohibits it from running over the current network. + * @return one of the NETWORK_* constants */ - private boolean isSizeAllowedForNetwork(int networkType) { + private int checkSizeAllowedForNetwork(int networkType) { if (mTotalBytes <= 0) { - return true; // we don't know the size yet + return NETWORK_OK; // we don't know the size yet } if (networkType == ConnectivityManager.TYPE_WIFI) { - return true; // anything goes over wifi + return NETWORK_OK; // anything goes over wifi } Long maxBytesOverMobile = mSystemFacade.getMaxBytesOverMobile(); - if (maxBytesOverMobile == null) { - return true; // no limit + if (maxBytesOverMobile != null && mTotalBytes > maxBytesOverMobile) { + return NETWORK_UNUSABLE_DUE_TO_SIZE; + } + if (mBypassRecommendedSizeLimit == 0) { + Long recommendedMaxBytesOverMobile = mSystemFacade.getRecommendedMaxBytesOverMobile(); + if (recommendedMaxBytesOverMobile != null + && mTotalBytes > recommendedMaxBytesOverMobile) { + return NETWORK_RECOMMENDED_UNUSABLE_DUE_TO_SIZE; + } } - return mTotalBytes <= maxBytesOverMobile; + return NETWORK_OK; } void start(long now) { @@ -505,4 +541,16 @@ public class DownloadInfo { && Downloads.Impl.isStatusSuccess(mStatus) && !DrmRawContent.DRM_MIMETYPE_MESSAGE_STRING.equalsIgnoreCase(mMimeType); } + + void notifyPauseDueToSize(boolean isWifiRequired) { + mPausedReason = mContext.getResources().getString( + R.string.notification_need_wifi_for_size); + Intent intent = new Intent(Intent.ACTION_VIEW); + intent.setData(getAllDownloadsUri()); + intent.setClassName(SizeLimitActivity.class.getPackage().getName(), + SizeLimitActivity.class.getName()); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + intent.putExtra(EXTRA_IS_WIFI_REQUIRED, isWifiRequired); + mContext.startActivity(intent); + } } diff --git a/src/com/android/providers/downloads/DownloadProvider.java b/src/com/android/providers/downloads/DownloadProvider.java index 102c611d..26065015 100644 --- a/src/com/android/providers/downloads/DownloadProvider.java +++ b/src/com/android/providers/downloads/DownloadProvider.java @@ -57,7 +57,7 @@ public final class DownloadProvider extends ContentProvider { /** Database filename */ private static final String DB_NAME = "downloads.db"; /** Current database version */ - private static final int DB_VERSION = 103; + private static final int DB_VERSION = 104; /** Name of table in the database */ private static final String DB_TABLE = "downloads"; @@ -223,6 +223,11 @@ public final class DownloadProvider extends ContentProvider { makeCacheDownloadsInvisible(db); break; + case 104: + addColumn(db, DB_TABLE, Downloads.Impl.COLUMN_BYPASS_RECOMMENDED_SIZE_LIMIT, + "INTEGER NOT NULL DEFAULT 0"); + break; + default: throw new IllegalStateException("Don't know how to upgrade to " + version); } @@ -839,7 +844,9 @@ public final class DownloadProvider extends ContentProvider { Integer status = values.getAsInteger(Downloads.Impl.COLUMN_STATUS); boolean isRestart = status != null && status == Downloads.Impl.STATUS_PENDING; - if (isRestart) { + boolean isUserBypassingSizeLimit = + values.containsKey(Downloads.Impl.COLUMN_BYPASS_RECOMMENDED_SIZE_LIMIT); + if (isRestart || isUserBypassingSizeLimit) { startService = true; } } diff --git a/src/com/android/providers/downloads/DownloadThread.java b/src/com/android/providers/downloads/DownloadThread.java index 57007f49..79778b0c 100644 --- a/src/com/android/providers/downloads/DownloadThread.java +++ b/src/com/android/providers/downloads/DownloadThread.java @@ -240,7 +240,13 @@ public class DownloadThread extends Thread { * Check if current connectivity is valid for this request. */ private void checkConnectivity(State state) throws StopRequest { - if (!mInfo.canUseNetwork()) { + int networkUsable = mInfo.checkCanUseNetwork(); + if (networkUsable != DownloadInfo.NETWORK_OK) { + if (networkUsable == DownloadInfo.NETWORK_UNUSABLE_DUE_TO_SIZE) { + mInfo.notifyPauseDueToSize(true); + } else if (networkUsable == DownloadInfo.NETWORK_RECOMMENDED_UNUSABLE_DUE_TO_SIZE) { + mInfo.notifyPauseDueToSize(false); + } throw new StopRequest(Downloads.Impl.STATUS_RUNNING_PAUSED); } } diff --git a/src/com/android/providers/downloads/RealSystemFacade.java b/src/com/android/providers/downloads/RealSystemFacade.java index 421fc2be..ce86f739 100644 --- a/src/com/android/providers/downloads/RealSystemFacade.java +++ b/src/com/android/providers/downloads/RealSystemFacade.java @@ -71,6 +71,16 @@ class RealSystemFacade implements SystemFacade { } @Override + public Long getRecommendedMaxBytesOverMobile() { + try { + return Settings.Secure.getLong(mContext.getContentResolver(), + Settings.Secure.DOWNLOAD_RECOMMENDED_MAX_BYTES_OVER_MOBILE); + } catch (SettingNotFoundException exc) { + return null; + } + } + + @Override public void sendBroadcast(Intent intent) { mContext.sendBroadcast(intent); } diff --git a/src/com/android/providers/downloads/SizeLimitActivity.java b/src/com/android/providers/downloads/SizeLimitActivity.java new file mode 100644 index 00000000..53e70de7 --- /dev/null +++ b/src/com/android/providers/downloads/SizeLimitActivity.java @@ -0,0 +1,137 @@ +/* + * Copyright (C) 2010 The Android Open Source Project + * + * 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.android.providers.downloads; + +import android.app.Activity; +import android.app.AlertDialog; +import android.app.Dialog; +import android.content.ContentValues; +import android.content.DialogInterface; +import android.content.Intent; +import android.database.Cursor; +import android.net.Uri; +import android.provider.Downloads; +import android.text.format.Formatter; +import android.util.Log; + +import java.util.LinkedList; +import java.util.Queue; + +/** + * Activity to show dialogs to the user when a download exceeds a limit on download sizes for + * mobile networks. This activity gets started by the background download service when a download's + * size is discovered to be exceeded one of these thresholds. + */ +public class SizeLimitActivity extends Activity + implements DialogInterface.OnCancelListener, DialogInterface.OnClickListener { + private Dialog mDialog; + private Queue<Intent> mDownloadsToShow = new LinkedList<Intent>(); + private Uri mCurrentUri; + private Intent mCurrentIntent; + + @Override + protected void onNewIntent(Intent intent) { + super.onNewIntent(intent); + setIntent(intent); + } + + @Override + protected void onResume() { + super.onResume(); + Intent intent = getIntent(); + if (intent != null) { + mDownloadsToShow.add(intent); + setIntent(null); + showNextDialog(); + } + if (mDialog != null && !mDialog.isShowing()) { + mDialog.show(); + } + } + + private void showNextDialog() { + if (mDialog != null) { + return; + } + + if (mDownloadsToShow.isEmpty()) { + finish(); + return; + } + + mCurrentIntent = mDownloadsToShow.poll(); + mCurrentUri = mCurrentIntent.getData(); + Cursor cursor = getContentResolver().query(mCurrentUri, null, null, null, null); + try { + if (!cursor.moveToFirst()) { + Log.e(Constants.TAG, "Empty cursor for URI " + mCurrentUri); + dialogClosed(); + return; + } + showDialog(cursor); + } finally { + cursor.close(); + } + } + + private void showDialog(Cursor cursor) { + int size = cursor.getInt(cursor.getColumnIndexOrThrow(Downloads.Impl.COLUMN_TOTAL_BYTES)); + String sizeString = Formatter.formatFileSize(this, size); + String queueText = getString(R.string.button_queue_for_wifi); + boolean isWifiRequired = + mCurrentIntent.getExtras().getBoolean(DownloadInfo.EXTRA_IS_WIFI_REQUIRED); + + AlertDialog.Builder builder = new AlertDialog.Builder(this); + if (isWifiRequired) { + builder.setTitle(R.string.wifi_required_title) + .setMessage(getString(R.string.wifi_required_body, sizeString, queueText)) + .setPositiveButton(R.string.button_queue_for_wifi, this) + .setNegativeButton(R.string.button_cancel_download, this); + } else { + builder.setTitle(R.string.wifi_recommended_title) + .setMessage(getString(R.string.wifi_recommended_body, sizeString, queueText)) + .setPositiveButton(R.string.button_start_now, this) + .setNegativeButton(R.string.button_queue_for_wifi, this); + } + mDialog = builder.setOnCancelListener(this).show(); + } + + @Override + public void onCancel(DialogInterface dialog) { + dialogClosed(); + } + + private void dialogClosed() { + mDialog = null; + mCurrentUri = null; + showNextDialog(); + } + + @Override + public void onClick(DialogInterface dialog, int which) { + boolean isRequired = + mCurrentIntent.getExtras().getBoolean(DownloadInfo.EXTRA_IS_WIFI_REQUIRED); + if (isRequired && which == AlertDialog.BUTTON_NEGATIVE) { + getContentResolver().delete(mCurrentUri, null, null); + } else if (!isRequired && which == AlertDialog.BUTTON_POSITIVE) { + ContentValues values = new ContentValues(); + values.put(Downloads.Impl.COLUMN_BYPASS_RECOMMENDED_SIZE_LIMIT, true); + getContentResolver().update(mCurrentUri, values , null, null); + } + dialogClosed(); + } +} diff --git a/src/com/android/providers/downloads/SystemFacade.java b/src/com/android/providers/downloads/SystemFacade.java index 50624c3a..ed0d3306 100644 --- a/src/com/android/providers/downloads/SystemFacade.java +++ b/src/com/android/providers/downloads/SystemFacade.java @@ -30,6 +30,13 @@ interface SystemFacade { public Long getMaxBytesOverMobile(); /** + * @return recommended maximum size, in bytes, of downloads that may go over a mobile + * connection; or null if there's no recommended limit. The user will have the option to bypass + * this limit. + */ + public Long getRecommendedMaxBytesOverMobile(); + + /** * Send a broadcast intent. */ public void sendBroadcast(Intent intent); |