Android Open Source - callmeter Device






From Project

Back to project page callmeter.

License

The source code is released under:

GNU General Public License

If you think the Android project callmeter listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2009-2013 Cyril Jaquier, Felix Bechstein
 * /*  w  w  w  . j av a 2s .co  m*/
 * This file is part of NetCounter.
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; If not, see <http://www.gnu.org/licenses/>.
 */

package de.ub0r.android.callmeter.data;

import android.annotation.TargetApi;
import android.content.Context;
import android.net.TrafficStats;
import android.os.Build;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import de.ub0r.android.callmeter.BuildConfig;
import de.ub0r.android.callmeter.R;
import de.ub0r.android.logg0r.Log;

/**
 * Representation of a device.
 */
public abstract class Device {

    /** Tag for output. */
    private static final String TAG = "Device";

    /** Size of read buffer. */
    private static final int BUFSIZE = 8;

    /** Single instance. */
    private static Device instance = null;

    /**
     * @return single instance
     */
    public static synchronized Device getDevice() {
        Log.d(TAG, "Device: ", Build.DEVICE);
        if (instance == null) {
            Log.d(TAG, "Device: ", Build.DEVICE);
            if (Build.PRODUCT.equals("sdk")) {
                instance = new EmulatorDevice();
            } else {
                instance = new FroyoDevice();
            }
            Log.i(TAG, "Device: " + Build.DEVICE + "/ Interface: " + instance.getCell());
        }
        return instance;
    }

    /**
     * Read a {@link File} an return its name+its first line.
     *
     * @param f filename
     * @return name + \n + 1st line
     */
    private static String readFile(final String f) {
        StringBuilder sb = new StringBuilder();
        try {
            BufferedReader r = new BufferedReader(new FileReader(f), BUFSIZE);
            sb.append("read: ").append(f);
            sb.append("\t");
            sb.append(r.readLine());
            r.close();
        } catch (IOException e) {
            Log.e(TAG, "error reading file: " + f, e);
        }
        return sb.toString();
    }

    /**
     * Get a list of possible devices.
     *
     * @param context {@link Context}
     * @return some info
     */
    public static String debugDeviceList(final Context context) {
        StringBuilder sb = new StringBuilder();
        sb.append("app: ");
        sb.append(context.getString(R.string.app_name));
        sb.append("\nversion: ");
        sb.append(BuildConfig.VERSION_NAME);
        sb.append("\nsdk version: ");
        sb.append(Build.VERSION.SDK_INT);
        sb.append("\nproduct: ");
        sb.append(Build.PRODUCT);
        sb.append("\nmodel: ");
        sb.append(Build.MODEL);
        sb.append("\n");
        sb.append("\ndevice: ");
        sb.append(getDevice());
        sb.append("\ndevice.cell: ");
        sb.append(getDevice().getCell());
        try {
            sb.append("\ndevice.cell.rx: ");
            sb.append(getDevice().getCellRxBytes());
            sb.append("\ndevice.cell.tx: ");
            sb.append(getDevice().getCellTxBytes());
        } catch (IOException e) {
            Log.e(TAG, "error reading devices", e);
            sb.append(e);
        }
        sb.append("\ndevice.wifi: ");
        sb.append(getDevice().getWiFi());
        try {
            sb.append("\ndevice.wifi.rx: ");
            sb.append(getDevice().getWiFiRxBytes());
            sb.append("\ndevice.wifi.tx: ");
            sb.append(getDevice().getWiFiTxBytes());
        } catch (IOException e) {
            Log.e(TAG, "error reading devices", e);
            sb.append(e);
        }
        sb.append("\n");
        try {
            final File f = new File(SysClassNet.SYS_CLASS_NET);
            final String[] devices = f.list();
            sb.append("\n#devices: ");
            sb.append(devices.length);
            for (String d : devices) {
                final String dev = SysClassNet.SYS_CLASS_NET + d;
                try {
                    sb.append("\n\ndevice: ");
                    sb.append(dev);
                    sb.append("\n");
                    sb.append(readFile(dev + "/type"));
                    sb.append("\n");
                    sb.append(readFile(dev + SysClassNet.CARRIER));
                    sb.append("\n");
                    sb.append(readFile(dev + SysClassNet.RX_BYTES));
                    sb.append("\n");
                    sb.append(readFile(dev + SysClassNet.TX_BYTES));
                    sb.append("\n");
                } catch (Exception e) {
                    sb.append("\nERROR: ").append(e).append("\n");
                    Log.e(TAG, "ERROR reading " + dev, e);
                }
            }
        } catch (Exception e) {
            sb.append("\nERROR: ").append(e).append("\n");
            Log.e(TAG, "error reading /sys/", e);
        }
        return sb.toString();
    }

    /**
     * @return device's device file: cell
     */
    protected abstract String getCell();

    /**
     * @return received bytes on cell device
     * @throws IOException IOException
     */
    public abstract long getCellRxBytes() throws IOException;

    /**
     * @return transmitted bytes on cell device
     * @throws IOException IOException
     */
    public abstract long getCellTxBytes() throws IOException;

    /**
     * @return device's device file: wifi
     */
    protected abstract String getWiFi();

    /**
     * @return received bytes on wifi device
     * @throws IOException IOException
     */
    public abstract long getWiFiRxBytes() throws IOException;

    /**
     * @return transmitted bytes on wifi device
     * @throws IOException IOException
     */
    public abstract long getWiFiTxBytes() throws IOException;
}

/**
 * Emulator Device showing all traffic on cell and wifi.
 */
final class EmulatorDevice extends Device {

    /** Tag for output. */
    private static final String TAG = "EmulatorDevice";

    /** My cell interface. */
    private final String mCell = "eth0";
    /** My wifi interface. */
    private final String mWiFi = "eth0";

    /**
     * {@inheritDoc}
     */
    @Override
    protected String getCell() {
        Log.d(TAG, "Cell interface: ", mCell);
        return mCell;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected String getWiFi() {
        Log.d(TAG, "WiFi interface: ", mCell);
        return mWiFi;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public long getCellRxBytes() throws IOException {
        String dev = getCell();
        if (dev == null) {
            return 0L;
        }
        return SysClassNet.getRxBytes(dev);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public long getCellTxBytes() throws IOException {
        String dev = getCell();
        if (dev == null) {
            return 0L;
        }
        return SysClassNet.getTxBytes(dev);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public long getWiFiRxBytes() throws IOException {
        String dev = getWiFi();
        if (dev == null) {
            return 0L;
        }
        return SysClassNet.getRxBytes(dev);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public long getWiFiTxBytes() throws IOException {
        String dev = getWiFi();
        if (dev == null) {
            return 0L;
        }
        return SysClassNet.getTxBytes(dev);
    }
}

/**
 * Emulator Device showing all traffic on cell and wifi.
 */
@SuppressWarnings("UnusedDeclaration")
final class DebugDevice extends Device {

    /** Tag for output. */
    private static final String TAG = "DebugDevice";

    /** My cell interface. */
    private final String mCell = "tiwlan0";
    /** My wifi interface. */
    private final String mWiFi = "tiwlan0";

    /**
     * {@inheritDoc}
     */
    @Override
    protected String getCell() {
        Log.d(TAG, "Cell interface: ", mCell);
        return mCell;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected String getWiFi() {
        Log.d(TAG, "WiFi interface: ", mCell);
        return mWiFi;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public long getCellRxBytes() throws IOException {
        String dev = getCell();
        if (dev == null) {
            return 0L;
        }
        return SysClassNet.getRxBytes(dev);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public long getCellTxBytes() throws IOException {
        String dev = getCell();
        if (dev == null) {
            return 0L;
        }
        return SysClassNet.getTxBytes(dev);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public long getWiFiRxBytes() throws IOException {
        String dev = getWiFi();
        if (dev == null) {
            return 0L;
        }
        return SysClassNet.getRxBytes(dev);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public long getWiFiTxBytes() throws IOException {
        String dev = getWiFi();
        if (dev == null) {
            return 0L;
        }
        return SysClassNet.getTxBytes(dev);
    }
}

/**
 * Common Device for API>=8.
 */
@TargetApi(8)
final class FroyoDevice extends Device {
    /** Tag for output. */
    // private static final String TAG = "FroyoDevice";

    /**
     * {@inheritDoc}
     */
    @Override
    protected String getCell() {
        return "TrafficStats";
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public long getCellRxBytes() throws IOException {
        final long l = TrafficStats.getMobileRxBytes();
        if (l < 0L) {
            return 0L;
        }
        return l;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public long getCellTxBytes() throws IOException {
        final long l = TrafficStats.getMobileTxBytes();
        if (l < 0L) {
            return 0L;
        }
        return l;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected String getWiFi() {
        return "TrafficStats";
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public long getWiFiRxBytes() throws IOException {
        final long l = TrafficStats.getMobileRxBytes();
        final long la = TrafficStats.getTotalRxBytes();
        if (la < 0L || la < l) {
            return 0L;
        }
        return la - l;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public long getWiFiTxBytes() throws IOException {
        final long l = TrafficStats.getMobileTxBytes();
        final long la = TrafficStats.getTotalTxBytes();
        if (la < 0L || la < l) {
            return 0L;
        }
        return la - l;
    }
}




Java Source Code List

android.preference.DatePreference.java
com.actionbarsherlock.BuildConfig.java
de.ub0r.android.callmeter.Ads.java
de.ub0r.android.callmeter.CallMeter.java
de.ub0r.android.callmeter.TrackingUtils.java
de.ub0r.android.callmeter.data.DataProvider.java
de.ub0r.android.callmeter.data.Device.java
de.ub0r.android.callmeter.data.ExportProvider.java
de.ub0r.android.callmeter.data.LogRunnerReceiver.java
de.ub0r.android.callmeter.data.LogRunnerService.java
de.ub0r.android.callmeter.data.NameCache.java
de.ub0r.android.callmeter.data.NameLoader.java
de.ub0r.android.callmeter.data.RuleMatcher.java
de.ub0r.android.callmeter.data.SysClassNet.java
de.ub0r.android.callmeter.ui.AboutActivity.java
de.ub0r.android.callmeter.ui.AddLogActivity.java
de.ub0r.android.callmeter.ui.AskForPlan.java
de.ub0r.android.callmeter.ui.Common.java
de.ub0r.android.callmeter.ui.HelpActivity.java
de.ub0r.android.callmeter.ui.IntroActivity.java
de.ub0r.android.callmeter.ui.LogsFragment.java
de.ub0r.android.callmeter.ui.PlansFragment.java
de.ub0r.android.callmeter.ui.Plans.java
de.ub0r.android.callmeter.ui.TrackingActivity.java
de.ub0r.android.callmeter.ui.TrackingSherlockActivity.java
de.ub0r.android.callmeter.ui.TrackingSherlockFragmentActivity.java
de.ub0r.android.callmeter.ui.TrackingSherlockPreferenceActivity.java
de.ub0r.android.callmeter.ui.prefs.BillModeListPreference.java
de.ub0r.android.callmeter.ui.prefs.CV2EditTextPreference.java
de.ub0r.android.callmeter.ui.prefs.CVBillModePreference.java
de.ub0r.android.callmeter.ui.prefs.CVCheckBoxPreference.java
de.ub0r.android.callmeter.ui.prefs.CVDatePreference.java
de.ub0r.android.callmeter.ui.prefs.CVEditTextPreference.java
de.ub0r.android.callmeter.ui.prefs.CVListPreference.java
de.ub0r.android.callmeter.ui.prefs.HourGroupEdit.java
de.ub0r.android.callmeter.ui.prefs.HourGroups.java
de.ub0r.android.callmeter.ui.prefs.NumberGroupEdit.java
de.ub0r.android.callmeter.ui.prefs.NumberGroups.java
de.ub0r.android.callmeter.ui.prefs.PlanEdit.java
de.ub0r.android.callmeter.ui.prefs.Plans.java
de.ub0r.android.callmeter.ui.prefs.PreferencesImport.java
de.ub0r.android.callmeter.ui.prefs.PreferencesPlain.java
de.ub0r.android.callmeter.ui.prefs.PreferencesRules.java
de.ub0r.android.callmeter.ui.prefs.Preferences.java
de.ub0r.android.callmeter.ui.prefs.RuleEdit.java
de.ub0r.android.callmeter.ui.prefs.Rules.java
de.ub0r.android.callmeter.ui.prefs.SimplePreferencesChild.java
de.ub0r.android.callmeter.ui.prefs.SimplePreferences.java
de.ub0r.android.callmeter.ui.prefs.UpDownPreference.java
de.ub0r.android.callmeter.ui.prefs.UpdateListener.java
de.ub0r.android.callmeter.widget.LogsAppWidgetConfigure.java
de.ub0r.android.callmeter.widget.LogsAppWidgetProvider.java
de.ub0r.android.callmeter.widget.StatsAppWidgetConfigure.java
de.ub0r.android.callmeter.widget.StatsAppWidgetProvider.java
yuku.ambilwarna.AmbilWarnaDialog.java
yuku.ambilwarna.AmbilWarnaKotak.java