Skip to content

Commit 15ec7d6

Browse files
committed
Add ALLOW_METERED column to DownloadManager.
Also allow isActiveNetworkMetered() to inspect networks without requiring ACCESS_WIFI_STATE. Bug: 3001465 Change-Id: Ibc23577d4ad941e4f93db417be6b046881dcbfb1
1 parent 7054453 commit 15ec7d6

File tree

3 files changed

+53
-33
lines changed

3 files changed

+53
-33
lines changed

core/java/android/app/DownloadManager.java

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -347,9 +347,9 @@ public static class Request {
347347
private CharSequence mTitle;
348348
private CharSequence mDescription;
349349
private String mMimeType;
350-
private boolean mRoamingAllowed = true;
351350
private int mAllowedNetworkTypes = ~0; // default to all network types allowed
352-
private boolean mAllowedOverMetered = true;
351+
private boolean mRoamingAllowed = true;
352+
private boolean mMeteredAllowed = true;
353353
private boolean mIsVisibleInDownloadsUi = true;
354354
private boolean mScannable = false;
355355
private boolean mUseSystemCache = false;
@@ -623,17 +623,6 @@ public Request setAllowedNetworkTypes(int flags) {
623623
return this;
624624
}
625625

626-
/**
627-
* Set whether this download may proceed over a metered network
628-
* connection. By default, metered networks are allowed.
629-
*
630-
* @see ConnectivityManager#isActiveNetworkMetered()
631-
*/
632-
public Request setAllowedOverMetered(boolean allow) {
633-
mAllowedOverMetered = allow;
634-
return this;
635-
}
636-
637626
/**
638627
* Set whether this download may proceed over a roaming connection. By default, roaming is
639628
* allowed.
@@ -645,6 +634,17 @@ public Request setAllowedOverRoaming(boolean allowed) {
645634
return this;
646635
}
647636

637+
/**
638+
* Set whether this download may proceed over a metered network
639+
* connection. By default, metered networks are allowed.
640+
*
641+
* @see ConnectivityManager#isActiveNetworkMetered()
642+
*/
643+
public Request setAllowedOverMetered(boolean allow) {
644+
mMeteredAllowed = allow;
645+
return this;
646+
}
647+
648648
/**
649649
* Set whether this download should be displayed in the system's Downloads UI. True by
650650
* default.
@@ -687,10 +687,10 @@ ContentValues toContentValues(String packageName) {
687687
putIfNonNull(values, Downloads.Impl.COLUMN_DESCRIPTION, mDescription);
688688
putIfNonNull(values, Downloads.Impl.COLUMN_MIME_TYPE, mMimeType);
689689

690-
// TODO: add COLUMN_ALLOW_METERED and persist
691690
values.put(Downloads.Impl.COLUMN_VISIBILITY, mNotificationVisibility);
692691
values.put(Downloads.Impl.COLUMN_ALLOWED_NETWORK_TYPES, mAllowedNetworkTypes);
693692
values.put(Downloads.Impl.COLUMN_ALLOW_ROAMING, mRoamingAllowed);
693+
values.put(Downloads.Impl.COLUMN_ALLOW_METERED, mMeteredAllowed);
694694
values.put(Downloads.Impl.COLUMN_IS_VISIBLE_IN_DOWNLOADS_UI, mIsVisibleInDownloadsUi);
695695

696696
return values;
@@ -1340,9 +1340,6 @@ private long getErrorCode(int status) {
13401340
case Downloads.Impl.STATUS_FILE_ALREADY_EXISTS_ERROR:
13411341
return ERROR_FILE_ALREADY_EXISTS;
13421342

1343-
case Downloads.Impl.STATUS_BLOCKED:
1344-
return ERROR_BLOCKED;
1345-
13461343
default:
13471344
return ERROR_UNKNOWN;
13481345
}

core/java/android/provider/Downloads.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,14 @@ private Impl() {}
328328
*/
329329
public static final String COLUMN_IS_PUBLIC_API = "is_public_api";
330330

331+
/**
332+
* The name of the column holding a bitmask of allowed network types. This is only used for
333+
* public API downloads.
334+
* <P>Type: INTEGER</P>
335+
* <P>Owner can Init/Read</P>
336+
*/
337+
public static final String COLUMN_ALLOWED_NETWORK_TYPES = "allowed_network_types";
338+
331339
/**
332340
* The name of the column indicating whether roaming connections can be used. This is only
333341
* used for public API downloads.
@@ -337,12 +345,12 @@ private Impl() {}
337345
public static final String COLUMN_ALLOW_ROAMING = "allow_roaming";
338346

339347
/**
340-
* The name of the column holding a bitmask of allowed network types. This is only used for
341-
* public API downloads.
342-
* <P>Type: INTEGER</P>
348+
* The name of the column indicating whether metered connections can be used. This is only
349+
* used for public API downloads.
350+
* <P>Type: BOOLEAN</P>
343351
* <P>Owner can Init/Read</P>
344352
*/
345-
public static final String COLUMN_ALLOWED_NETWORK_TYPES = "allowed_network_types";
353+
public static final String COLUMN_ALLOW_METERED = "allow_metered";
346354

347355
/**
348356
* Whether or not this download should be displayed in the system's Downloads UI. Defaults
@@ -701,7 +709,10 @@ public static boolean isStatusCompleted(int status) {
701709
* blocked by {@link NetworkPolicyManager}.
702710
*
703711
* @hide
712+
* @deprecated since behavior now uses
713+
* {@link #STATUS_WAITING_FOR_NETWORK}
704714
*/
715+
@Deprecated
705716
public static final int STATUS_BLOCKED = 498;
706717

707718
/** {@hide} */

services/java/com/android/server/ConnectivityService.java

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -865,27 +865,39 @@ private NetworkState getNetworkStateUnchecked(int networkType) {
865865
@Override
866866
public NetworkQuotaInfo getActiveNetworkQuotaInfo() {
867867
enforceAccessPermission();
868-
final NetworkState state = getNetworkStateUnchecked(mActiveDefaultNetwork);
869-
if (state != null) {
870-
try {
871-
return mPolicyManager.getNetworkQuotaInfo(state);
872-
} catch (RemoteException e) {
868+
869+
final long token = Binder.clearCallingIdentity();
870+
try {
871+
final NetworkState state = getNetworkStateUnchecked(mActiveDefaultNetwork);
872+
if (state != null) {
873+
try {
874+
return mPolicyManager.getNetworkQuotaInfo(state);
875+
} catch (RemoteException e) {
876+
}
873877
}
878+
return null;
879+
} finally {
880+
Binder.restoreCallingIdentity(token);
874881
}
875-
return null;
876882
}
877883

878884
@Override
879885
public boolean isActiveNetworkMetered() {
880886
enforceAccessPermission();
881-
final NetworkState state = getNetworkStateUnchecked(mActiveDefaultNetwork);
882-
if (state != null) {
883-
try {
884-
return mPolicyManager.isNetworkMetered(state);
885-
} catch (RemoteException e) {
887+
888+
final long token = Binder.clearCallingIdentity();
889+
try {
890+
final NetworkState state = getNetworkStateUnchecked(mActiveDefaultNetwork);
891+
if (state != null) {
892+
try {
893+
return mPolicyManager.isNetworkMetered(state);
894+
} catch (RemoteException e) {
895+
}
886896
}
897+
return false;
898+
} finally {
899+
Binder.restoreCallingIdentity(token);
887900
}
888-
return false;
889901
}
890902

891903
public boolean setRadios(boolean turnOn) {

0 commit comments

Comments
 (0)