Android Open Source - DeviceStatus Device Status Activity






From Project

Back to project page DeviceStatus.

License

The source code is released under:

Copyright (c) 2013, Michael Cook All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: *...

If you think the Android project DeviceStatus 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

package com.waxrat.devicestatus;
/*from   w  w w .j a va 2 s.c o m*/
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.BatteryManager;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class DeviceStatusActivity extends Activity {
    private final static String TAG = "DeviceStatusActivity";

    private TextView batteryView;
    private TextView wifiView;
    private ReceiveMessages receiver;
    private WifiManager wifi;

    class ReceiveMessages extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Log.i(TAG, "action is " + action);
            if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
                showBattery(intent);
                return;
            }
            if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
                @SuppressWarnings("deprecation")
                NetworkInfo ni = (NetworkInfo)
                    intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
                if (ni.getType() == ConnectivityManager.TYPE_WIFI)
                    showWifi();
                return;
            }
            Log.e(TAG, "Unexpected action: " + action);
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.i(TAG, "onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        receiver = new ReceiveMessages();
        registerReceiver(receiver,
                new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        registerReceiver(receiver,
                new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

        wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);

        checkSensors();

        wifiView = (TextView) findViewById(R.id.wifi);
        batteryView = (TextView) findViewById(R.id.battery);

        showWifi();
        showBattery(batteryIntent());

        try {
            Log.i(TAG, "FilesDir " + getFilesDir());
            Log.i(TAG, "ExternalFilesDir " + getExternalFilesDir(null));
            Log.i(TAG, "ExternalCacheDir " + getExternalCacheDir());
            Log.i(TAG, "ExternalFilesDir " + getExternalFilesDir(null));
            Log.i(TAG, "ExternalStorageState " + Environment.getExternalStorageState());
            Log.i(TAG, "DataDirectory " + Environment.getDataDirectory());
            Log.i(TAG, "DownloadCacheDirectory " + Environment.getDownloadCacheDirectory());
            Log.i(TAG, "ExternalStorageDirectory " + Environment.getExternalStorageDirectory());
            Log.i(TAG, "ExternalStoragePublicDirectory ALARMS " + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_ALARMS));
            Log.i(TAG, "ExternalStoragePublicDirectory DCIM " + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM));
            Log.i(TAG, "ExternalStoragePublicDirectory DOWNLOADS " + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS));
            Log.i(TAG, "ExternalStoragePublicDirectory MOVIES " + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES));
            Log.i(TAG, "ExternalStoragePublicDirectory MUSIC " + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC));
            Log.i(TAG, "ExternalStoragePublicDirectory NOTIFICATIONS " + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_NOTIFICATIONS));
            Log.i(TAG, "ExternalStoragePublicDirectory PICTURES " + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES));
            Log.i(TAG, "ExternalStoragePublicDirectory PODCASTS " + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PODCASTS));
            Log.i(TAG, "ExternalStoragePublicDirectory RINGTONES " + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_RINGTONES));
            Log.i(TAG, "Environment.getRootDirectory " + Environment.getRootDirectory());
        }
        catch (Throwable thr) {
            Log.e(TAG, "Ouch!", thr);
        }
    }

    private Intent batteryIntent() {
        IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        return registerReceiver(null, ifilter);
    }

    private String batteryInfo(Intent bi) {
        StringBuffer sb = new StringBuffer();
        sb.append(getString(R.string.battery_label));

        int plugged = bi.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        switch (plugged) {
        case 0:
            sb.append(" Unplugged");
            break;
        case BatteryManager.BATTERY_PLUGGED_AC:
            sb.append(" AC");
            break;
        case BatteryManager.BATTERY_PLUGGED_USB:
            sb.append(" USB");
            break;
        default:
            sb.append(" Plugged ");
            sb.append(plugged);
            sb.append('?');
            break;
        }

        int level = bi.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        int scale = bi.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
        sb.append(' ');
        sb.append(100 * level / scale);
        sb.append('%');

        int temp = bi.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);
        sb.append('\n');
        sb.append(temp / 10);
        sb.append('.');
        sb.append(temp % 10);
        sb.append('c');

        int voltage = bi.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1);
        sb.append(", ");
        sb.append(voltage / 1000);
        sb.append('.');
        sb.append(voltage % 1000);
        sb.append('v');

        int status = bi.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        switch (status) {
        case BatteryManager.BATTERY_STATUS_CHARGING:
            sb.append(", Charging");
            break;
        case BatteryManager.BATTERY_STATUS_DISCHARGING:
            sb.append(", Discharging");
            break;
        case BatteryManager.BATTERY_STATUS_FULL:
            sb.append(", Full");
            break;
        case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
            sb.append(", Not charging");
            break;
        case BatteryManager.BATTERY_STATUS_UNKNOWN:
            sb.append(", Unknown status");
            break;
        default:
            sb.append(", Status ");
            sb.append(status);
            sb.append('?');
            break;
        }

        int health = bi.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        switch (health) {
        case 0:
            // Health status isn't available
            break;
        case BatteryManager.BATTERY_HEALTH_COLD:
            sb.append(", Cold");
            break;
        case BatteryManager.BATTERY_HEALTH_DEAD:
            sb.append(", Dead");
            break;
        case BatteryManager.BATTERY_HEALTH_GOOD:
            sb.append(", Good");
            break;
        case BatteryManager.BATTERY_HEALTH_OVERHEAT:
            sb.append(", Overheat");
            break;
        case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
            sb.append(", Over voltage");
            break;
        case BatteryManager.BATTERY_HEALTH_UNKNOWN:
            sb.append(", Unknown health");
            break;
        case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
            sb.append(", Unspecified failure");
            break;
        default:
            sb.append(", Health ");
            sb.append(health);
            sb.append('?');
            break;
        }

        sb.append(", \"");
        String tech = bi.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY);
        if (tech != null)
            sb.append(tech);
        sb.append('"');

        return sb.toString();
    }

    private String wifiInfo() {
        WifiInfo info = wifi.getConnectionInfo();
        if (info == null)
            return "No Wi-Fi info";

        StringBuffer sb = new StringBuffer();
        sb.append(getString(R.string.wifi_label));
        sb.append(' ');

        String ssid = info.getSSID();

        // In some Android versions, the SSID is enclosed in quotes.
        if (ssid != null && ssid.startsWith("\"") && ssid.endsWith("\""))
            ssid = ssid.substring(1, ssid.length() - 1);

        String bssid = info.getBSSID();
        if (ssid == null || ssid.length() == 0)
            sb.append("No Wi-Fi network");
        else {
            sb.append(ssid);
            if (bssid != null) {
                //Log.i(TAG, "bssid=" + bssid);
                if (ssid.equals("montana")) {
                    if (bssid.endsWith("cd:4d"))        // Netgear
                        sb.append(" (Library)");
                    else if (bssid.endsWith("d6:92"))   // Verizon
                        sb.append(" (Utility Room)");
                    else if (bssid.endsWith("83:d2"))   // Dell Truemobile
                        sb.append(" (Sitting Room)");
                    else if (bssid.endsWith("cf:6a"))   // Cisco/Linksys
                        sb.append(" (Sitting Room)");
                }
                else if (bssid.equals("34:bd:c8:ad:a1:c0") ||
                         bssid.equals("34:bd:c8:ad:a1:cf"))
                    sb.append(" (Essex)");
            }
        }
        sb.append('\n');

        sb.append("BSSID: ");
        if (bssid == null)
            sb.append("none");
        else
            sb.append(bssid);
        sb.append(", ");

        String s = info.toString();
        if (s.startsWith(", "))
            s = s.substring(2);
        sb.append(s);
        return sb.toString();
    }

    public void sendWifiInfo(View view) {
        Log.i(TAG, "sendWifiInfo");
        Intent i = new Intent(Intent.ACTION_SEND);
        i.setType("text/plain");
        i.putExtra(Intent.EXTRA_SUBJECT, R.string.wifi_label);
        i.putExtra(Intent.EXTRA_TEXT, wifiInfo());
        startActivity(Intent.createChooser(i, getString(R.string.wifi_label)));
    }

    public void sendBatteryInfo(View view) {
        Log.i(TAG, "sendBatteryInfo");
        Intent i=new Intent(Intent.ACTION_SEND);
        i.setType("text/plain");
        i.putExtra(Intent.EXTRA_SUBJECT, R.string.battery_label);
        i.putExtra(Intent.EXTRA_TEXT, batteryInfo(batteryIntent()));
        startActivity(Intent.createChooser(i, getString(R.string.battery_label)));
    }

    private final void showWifi() {
        wifiView.setText(wifiInfo());
    }

    private void showBattery(Intent bi) {
        batteryView.setText(batteryInfo(bi));
    }

    @SuppressWarnings("deprecation")
    private final void checkSensors() {
        SensorManager sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        checkSensor(sm, "Accelerometer", Sensor.TYPE_ACCELEROMETER);
        checkSensor(sm, "Ambient Temperature", Sensor.TYPE_AMBIENT_TEMPERATURE);
        checkSensor(sm, "Gravity", Sensor.TYPE_GRAVITY);
        checkSensor(sm, "Gyroscope", Sensor.TYPE_GYROSCOPE);
        checkSensor(sm, "Light", Sensor.TYPE_LIGHT);
        checkSensor(sm, "Linear Acceleration", Sensor.TYPE_LINEAR_ACCELERATION);
        checkSensor(sm, "Magnetic Field", Sensor.TYPE_MAGNETIC_FIELD);
        checkSensor(sm, "Orientation", Sensor.TYPE_ORIENTATION);
        checkSensor(sm, "Pressure", Sensor.TYPE_PRESSURE);
        checkSensor(sm, "Proximity", Sensor.TYPE_PROXIMITY);
        checkSensor(sm, "Relative Humidity", Sensor.TYPE_RELATIVE_HUMIDITY);
        checkSensor(sm, "Rotation Vector", Sensor.TYPE_ROTATION_VECTOR);
        checkSensor(sm, "Temperature", Sensor.TYPE_TEMPERATURE);
    }

    private final void checkSensor(SensorManager sm, String name, int type) {
        Sensor sensor = sm.getDefaultSensor(type);
        Log.i(TAG, "Sensor type " + name + (sensor != null ? ": Yes" : ": No"));
    }

    @Override
    protected void onDestroy() {
        Log.i(TAG, "onDestroy");
        super.onDestroy();
        unregisterReceiver(receiver);
    }

    private final OnMenuItemClickListener runme =
            new OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {
            Log.i(TAG, "runme");
            try {
                Process proc = Runtime.getRuntime().exec("/system/bin/sh /mnt/extSdCard/runme.sh >/mnt/extSdCard/runme.out 2>&1");
                proc.waitFor();
                toast("Run!");
            } catch (Exception e) {
                Log.e(TAG, "Exception", e);
                toast("Exception!");
            }
            return true;
        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        Log.i(TAG, "onCreateOptionsMenu");
        menu.add("runme.sh").setOnMenuItemClickListener(runme);
        return true;
    }

    protected void toast(String msg) {
        Toast.makeText(DeviceStatusActivity.this, msg,
                Toast.LENGTH_SHORT).show();
    }
}




Java Source Code List

com.waxrat.devicestatus.DeviceStatusActivity.java