ThrottleManager.java revision 9e696c29f06d45d2891e1d38fd8d9033a9e21bb9
19e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt/*
29e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt * Copyright (C) 2008 The Android Open Source Project
39e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt *
49e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt * Licensed under the Apache License, Version 2.0 (the "License");
59e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt * you may not use this file except in compliance with the License.
69e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt * You may obtain a copy of the License at
79e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt *
89e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt *      http://www.apache.org/licenses/LICENSE-2.0
99e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt *
109e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt * Unless required by applicable law or agreed to in writing, software
119e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt * distributed under the License is distributed on an "AS IS" BASIS,
129e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt * See the License for the specific language governing permissions and
149e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt * limitations under the License.
159e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt */
169e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt
179e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwaltpackage android.net;
189e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt
199e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwaltimport android.annotation.SdkConstant;
209e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwaltimport android.annotation.SdkConstant.SdkConstantType;
219e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwaltimport android.os.Binder;
229e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwaltimport android.os.RemoteException;
239e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt
249e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt/**
259e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt * Class that handles throttling.  It provides read/write numbers per interface
269e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt * and methods to apply throttled rates.
279e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt * {@hide}
289e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt */
299e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwaltpublic class ThrottleManager
309e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt{
319e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    /**
329e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * Broadcast each polling period to indicate new data counts.
339e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     *
349e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * Includes four extras:
359e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * EXTRA_CYCLE_READ - a long of the read bytecount for the current cycle
369e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * EXTRA_CYCLE_WRITE -a long of the write bytecount for the current cycle
379e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * EXTRA_CYLCE_START -a long of MS for the cycle start time
389e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * EXTRA_CYCLE_END   -a long of MS for the cycle stop time
399e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * {@hide}
409e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     */
419e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    public static final String THROTTLE_POLL_ACTION = "android.net.thrott.POLL_ACTION";
429e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    /**
439e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * The lookup key for a long for the read bytecount for this period.  Retrieve with
449e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * {@link android.content.Intent#getLongExtra(String)}.
459e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * {@hide}
469e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     */
479e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    public static final String EXTRA_CYCLE_READ = "cycleRead";
489e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    /**
499e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * contains a long of the number of bytes written in the cycle
509e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * {@hide}
519e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     */
529e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    public static final String EXTRA_CYCLE_WRITE = "cycleWrite";
539e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    /**
549e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * contains a long of the number of bytes read in the cycle
559e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * {@hide}
569e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     */
579e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    public static final String EXTRA_CYCLE_START = "cycleStart";
589e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    /**
599e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * contains a long of the ms since 1970 used to init a calendar, etc for the end
609e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * of the cycle
619e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * {@hide}
629e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     */
639e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    public static final String EXTRA_CYCLE_END = "cycleEnd";
649e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt
659e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    /**
669e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * Broadcast when the thottle level changes.
679e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * {@hide}
689e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     */
699e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    public static final String THROTTLE_ACTION = "android.net.thrott.THROTTLE_ACTION";
709e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    /**
719e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * int of the current bandwidth in TODO
729e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * {@hide}
739e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     */
749e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    public static final String EXTRA_THROTTLE_LEVEL = "level";
759e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt
769e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    // {@hide}
779e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    public static final int DIRECTION_TX = 0;
789e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    // {@hide}
799e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    public static final int DIRECTION_RX = 1;
809e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt
819e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    // {@hide}
829e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    public static final int PERIOD_CYCLE  = 0;
839e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    // {@hide}
849e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    public static final int PERIOD_YEAR   = 1;
859e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    // {@hide}
869e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    public static final int PERIOD_MONTH  = 2;
879e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    // {@hide}
889e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    public static final int PERIOD_WEEK   = 3;
899e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    // @hide
909e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    public static final int PERIOD_7DAY   = 4;
919e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    // @hide
929e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    public static final int PERIOD_DAY    = 5;
939e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    // @hide
949e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    public static final int PERIOD_24HOUR = 6;
959e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    // @hide
969e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    public static final int PERIOD_HOUR   = 7;
979e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    // @hide
989e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    public static final int PERIOD_60MIN  = 8;
999e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    // @hide
1009e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    public static final int PERIOD_MINUTE = 9;
1019e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    // @hide
1029e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    public static final int PERIOD_60SEC  = 10;
1039e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    // @hide
1049e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    public static final int PERIOD_SECOND = 11;
1059e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt
1069e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    /**
1079e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * returns a long of the ms from the epoch to the time the current cycle ends for the
1089e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * named interface
1099e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * {@hide}
1109e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     */
1119e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    public long getResetTime(String iface) {
1129e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt        try {
1139e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt            return mService.getResetTime(iface);
1149e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt        } catch (RemoteException e) {
1159e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt            return -1;
1169e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt        }
1179e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    }
1189e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt
1199e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    /**
1209e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * returns a long of the ms from the epoch to the time the current cycle started for the
1219e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * named interface
1229e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * {@hide}
1239e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     */
1249e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    public long getPeriodStartTime(String iface) {
1259e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt        try {
1269e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt            return mService.getPeriodStartTime(iface);
1279e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt        } catch (RemoteException e) {
1289e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt            return -1;
1299e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt        }
1309e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    }
1319e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt
1329e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    /**
1339e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * returns a long of the byte count either read or written on the named interface
1349e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * for the period described.  Direction is either DIRECTION_RX or DIRECTION_TX and
1359e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * period may only be PERIOD_CYCLE for the current cycle (other periods may be supported
1369e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * in the future).  Ago indicates the number of periods in the past to lookup - 0 means
1379e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * the current period, 1 is the last one, 2 was two periods ago..
1389e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * {@hide}
1399e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     */
1409e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    public long getByteCount(String iface, int direction, int period, int ago) {
1419e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt        try {
1429e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt            return mService.getByteCount(iface, direction, period, ago);
1439e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt        } catch (RemoteException e) {
1449e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt            return -1;
1459e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt        }
1469e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    }
1479e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt
1489e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    /**
1499e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * returns the number of bytes read+written after which a particular cliff
1509e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * takes effect on the named iface.  Currently only cliff #0 is supported (1 step)
1519e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * {@hide}
1529e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     */
1539e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    public long getCliffThreshold(String iface, int cliff) {
1549e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt        try {
1559e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt            return mService.getCliffThreshold(iface, cliff);
1569e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt        } catch (RemoteException e) {
1579e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt            return -1;
1589e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt        }
1599e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    }
1609e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt
1619e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    /**
1629e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * returns the thottling bandwidth (bps) for a given cliff # on the named iface.
1639e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * only cliff #0 is currently supported.
1649e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * {@hide}
1659e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     */
1669e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    public int getCliffLevel(String iface, int cliff) {
1679e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt        try {
1689e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt            return mService.getCliffLevel(iface, cliff);
1699e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt        } catch (RemoteException e) {
1709e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt            return -1;
1719e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt        }
1729e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    }
1739e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt
1749e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    private IThrottleManager mService;
1759e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt
1769e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    /**
1779e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * Don't allow use of default constructor.
1789e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     */
1799e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    @SuppressWarnings({"UnusedDeclaration"})
1809e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    private ThrottleManager() {
1819e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    }
1829e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt
1839e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    /**
1849e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     * {@hide}
1859e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt     */
1869e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    public ThrottleManager(IThrottleManager service) {
1879e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt        if (service == null) {
1889e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt            throw new IllegalArgumentException(
1899e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt                "ThrottleManager() cannot be constructed with null service");
1909e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt        }
1919e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt        mService = service;
1929e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt    }
1939e696c29f06d45d2891e1d38fd8d9033a9e21bb9Robert Greenwalt}
194