NetworkPolicyManager.java revision 1b861278a2051f53ce7955fb7992fa536dc975d9
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 22import java.io.PrintWriter; 23 24/** 25 * Manager for creating and modifying network policy rules. 26 * 27 * {@hide} 28 */ 29public class NetworkPolicyManager { 30 31 /** No specific network policy, use system default. */ 32 public static final int POLICY_NONE = 0x0; 33 /** Reject network usage on paid networks when application in background. */ 34 public static final int POLICY_REJECT_PAID_BACKGROUND = 0x1; 35 36 /** All network traffic should be allowed. */ 37 public static final int RULE_ALLOW_ALL = 0x0; 38 /** Reject traffic on paid networks. */ 39 public static final int RULE_REJECT_PAID = 0x1; 40 41 private INetworkPolicyManager mService; 42 43 public NetworkPolicyManager(INetworkPolicyManager service) { 44 if (service == null) { 45 throw new IllegalArgumentException("missing INetworkPolicyManager"); 46 } 47 mService = service; 48 } 49 50 public static NetworkPolicyManager getSystemService(Context context) { 51 return (NetworkPolicyManager) context.getSystemService(Context.NETWORK_POLICY_SERVICE); 52 } 53 54 /** 55 * Set policy flags for specific UID. 56 * 57 * @param policy {@link #POLICY_NONE} or combination of flags like 58 * {@link #POLICY_REJECT_PAID_BACKGROUND}. 59 */ 60 public void setUidPolicy(int uid, int policy) { 61 try { 62 mService.setUidPolicy(uid, policy); 63 } catch (RemoteException e) { 64 } 65 } 66 67 public int getUidPolicy(int uid) { 68 try { 69 return mService.getUidPolicy(uid); 70 } catch (RemoteException e) { 71 return POLICY_NONE; 72 } 73 } 74 75 /** {@hide} */ 76 public static void dumpPolicy(PrintWriter fout, int policy) { 77 fout.write("["); 78 if ((policy & POLICY_REJECT_PAID_BACKGROUND) != 0) { 79 fout.write("REJECT_PAID_BACKGROUND"); 80 } 81 fout.write("]"); 82 } 83 84 /** {@hide} */ 85 public static void dumpRules(PrintWriter fout, int rules) { 86 fout.write("["); 87 if ((rules & RULE_REJECT_PAID) != 0) { 88 fout.write("REJECT_PAID"); 89 } 90 fout.write("]"); 91 } 92 93} 94