1/*
2 * Copyright (C) 2008 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.annotation.SdkConstant;
20import android.annotation.SdkConstant.SdkConstantType;
21import android.os.Binder;
22import android.os.RemoteException;
23
24/**
25 * Class that handles throttling.  It provides read/write numbers per interface
26 * and methods to apply throttled rates.
27 * {@hide}
28 */
29public class ThrottleManager
30{
31    /**
32     * Broadcast each polling period to indicate new data counts.
33     *
34     * Includes four extras:
35     * EXTRA_CYCLE_READ - a long of the read bytecount for the current cycle
36     * EXTRA_CYCLE_WRITE -a long of the write bytecount for the current cycle
37     * EXTRA_CYLCE_START -a long of MS for the cycle start time
38     * EXTRA_CYCLE_END   -a long of MS for the cycle stop time
39     * {@hide}
40     */
41    public static final String THROTTLE_POLL_ACTION = "android.net.thrott.POLL_ACTION";
42    /**
43     * The lookup key for a long for the read bytecount for this period.  Retrieve with
44     * {@link android.content.Intent#getLongExtra(String)}.
45     * {@hide}
46     */
47    public static final String EXTRA_CYCLE_READ = "cycleRead";
48    /**
49     * contains a long of the number of bytes written in the cycle
50     * {@hide}
51     */
52    public static final String EXTRA_CYCLE_WRITE = "cycleWrite";
53    /**
54     * contains a long of the number of bytes read in the cycle
55     * {@hide}
56     */
57    public static final String EXTRA_CYCLE_START = "cycleStart";
58    /**
59     * contains a long of the ms since 1970 used to init a calendar, etc for the end
60     * of the cycle
61     * {@hide}
62     */
63    public static final String EXTRA_CYCLE_END = "cycleEnd";
64
65    /**
66     * Broadcast when the thottle level changes.
67     * {@hide}
68     */
69    public static final String THROTTLE_ACTION = "android.net.thrott.THROTTLE_ACTION";
70    /**
71     * int of the current bandwidth in TODO
72     * {@hide}
73     */
74    public static final String EXTRA_THROTTLE_LEVEL = "level";
75
76    /**
77     * Broadcast on boot and whenever the settings change.
78     * {@hide}
79     */
80    public static final String POLICY_CHANGED_ACTION = "android.net.thrott.POLICY_CHANGED_ACTION";
81
82    // {@hide}
83    public static final int DIRECTION_TX = 0;
84    // {@hide}
85    public static final int DIRECTION_RX = 1;
86
87    // {@hide}
88    public static final int PERIOD_CYCLE  = 0;
89    // {@hide}
90    public static final int PERIOD_YEAR   = 1;
91    // {@hide}
92    public static final int PERIOD_MONTH  = 2;
93    // {@hide}
94    public static final int PERIOD_WEEK   = 3;
95    // @hide
96    public static final int PERIOD_7DAY   = 4;
97    // @hide
98    public static final int PERIOD_DAY    = 5;
99    // @hide
100    public static final int PERIOD_24HOUR = 6;
101    // @hide
102    public static final int PERIOD_HOUR   = 7;
103    // @hide
104    public static final int PERIOD_60MIN  = 8;
105    // @hide
106    public static final int PERIOD_MINUTE = 9;
107    // @hide
108    public static final int PERIOD_60SEC  = 10;
109    // @hide
110    public static final int PERIOD_SECOND = 11;
111
112
113
114    /**
115     * returns a long of the ms from the epoch to the time the current cycle ends for the
116     * named interface
117     * {@hide}
118     */
119    public long getResetTime(String iface) {
120        try {
121            return mService.getResetTime(iface);
122        } catch (RemoteException e) {
123            return -1;
124        }
125    }
126
127    /**
128     * returns a long of the ms from the epoch to the time the current cycle started for the
129     * named interface
130     * {@hide}
131     */
132    public long getPeriodStartTime(String iface) {
133        try {
134            return mService.getPeriodStartTime(iface);
135        } catch (RemoteException e) {
136            return -1;
137        }
138    }
139
140    /**
141     * returns a long of the byte count either read or written on the named interface
142     * for the period described.  Direction is either DIRECTION_RX or DIRECTION_TX and
143     * period may only be PERIOD_CYCLE for the current cycle (other periods may be supported
144     * in the future).  Ago indicates the number of periods in the past to lookup - 0 means
145     * the current period, 1 is the last one, 2 was two periods ago..
146     * {@hide}
147     */
148    public long getByteCount(String iface, int direction, int period, int ago) {
149        try {
150            return mService.getByteCount(iface, direction, period, ago);
151        } catch (RemoteException e) {
152            return -1;
153        }
154    }
155
156    /**
157     * returns the number of bytes read+written after which a particular cliff
158     * takes effect on the named iface.  Currently only cliff #1 is supported (1 step)
159     * {@hide}
160     */
161    public long getCliffThreshold(String iface, int cliff) {
162        try {
163            return mService.getCliffThreshold(iface, cliff);
164        } catch (RemoteException e) {
165            return -1;
166        }
167    }
168
169    /**
170     * returns the thottling bandwidth (bps) for a given cliff # on the named iface.
171     * only cliff #1 is currently supported.
172     * {@hide}
173     */
174    public int getCliffLevel(String iface, int cliff) {
175        try {
176            return mService.getCliffLevel(iface, cliff);
177        } catch (RemoteException e) {
178            return -1;
179        }
180    }
181
182    /**
183     * returns the help URI for throttling
184     * {@hide}
185     */
186    public String getHelpUri() {
187        try {
188            return mService.getHelpUri();
189        } catch (RemoteException e) {
190            return null;
191        }
192    }
193
194
195    private IThrottleManager mService;
196
197    /**
198     * Don't allow use of default constructor.
199     */
200    @SuppressWarnings({"UnusedDeclaration"})
201    private ThrottleManager() {
202    }
203
204    /**
205     * {@hide}
206     */
207    public ThrottleManager(IThrottleManager service) {
208        if (service == null) {
209            throw new IllegalArgumentException(
210                "ThrottleManager() cannot be constructed with null service");
211        }
212        mService = service;
213    }
214}
215