NetworkPolicyManager.java revision eedcb9525ba5befee2ba6ebb7a9ee3f13395c2a3
1/* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.net; 18 19import android.content.Context; 20import android.os.RemoteException; 21 22/** 23 * Manager for creating and modifying network policy rules. 24 * 25 * {@hide} 26 */ 27public class NetworkPolicyManager { 28 29 /** No specific network policy, use system default. */ 30 public static final int POLICY_NONE = 0x0; 31 /** Reject network usage when application in background. */ 32 public static final int POLICY_REJECT_BACKGROUND = 0x1; 33 /** Reject network usage on paid network connections. */ 34 public static final int POLICY_REJECT_PAID = 0x2; 35 /** Application should conserve data. */ 36 public static final int POLICY_CONSERVE_DATA = 0x4; 37 38 private INetworkPolicyManager mService; 39 40 public NetworkPolicyManager(INetworkPolicyManager service) { 41 if (service == null) { 42 throw new IllegalArgumentException("missing INetworkPolicyManager"); 43 } 44 mService = service; 45 } 46 47 public static NetworkPolicyManager getSystemService(Context context) { 48 return (NetworkPolicyManager) context.getSystemService(Context.NETWORK_POLICY_SERVICE); 49 } 50 51 /** 52 * Set policy flags for specific UID. 53 * 54 * @param policy {@link #POLICY_NONE} or combination of 55 * {@link #POLICY_REJECT_BACKGROUND}, {@link #POLICY_REJECT_PAID}, 56 * or {@link #POLICY_CONSERVE_DATA}. 57 */ 58 public void setUidPolicy(int uid, int policy) { 59 try { 60 mService.setUidPolicy(uid, policy); 61 } catch (RemoteException e) { 62 } 63 } 64 65 public int getUidPolicy(int uid) { 66 try { 67 return mService.getUidPolicy(uid); 68 } catch (RemoteException e) { 69 return POLICY_NONE; 70 } 71 } 72 73} 74