PageRenderTime 30ms CodeModel.GetById 2ms app.highlight 21ms RepoModel.GetById 2ms app.codeStats 0ms

/actionslib/src/com/google/android/marvin/commands/impls/ConnectivityCommand.java

http://eyes-free.googlecode.com/
Java | 100 lines | 75 code | 10 blank | 15 comment | 18 complexity | d0415ce5a0b2d6fa8a493a198895de9a MD5 | raw file
  1// Copyright 2010 Google Inc. All Rights Reserved.
  2
  3package com.google.android.marvin.commands.impls;
  4
  5import com.google.android.marvin.actionslib.R;
  6import com.google.android.marvin.commands.CommandExecutor;
  7
  8import android.content.ContentResolver;
  9import android.content.Context;
 10import android.net.ConnectivityManager;
 11import android.net.NetworkInfo;
 12import android.net.wifi.WifiInfo;
 13import android.net.wifi.WifiManager;
 14import android.provider.Settings.SettingNotFoundException;
 15import android.provider.Settings.System;
 16import android.telephony.TelephonyManager;
 17
 18/**
 19 * A command that provides the current connectivity, including phone, data, gps and bluetooth.
 20 *
 21 * Note: This command assumes that someone else (who is persistent) will be listening for changes to
 22 * the phone state and saving the updates into a shared preference via the
 23 * {@link SavingPhoneStateListener}.
 24 *
 25 * TODO(clsimon): Find a better way to get this async data.
 26 *
 27 * @author clsimon@google.com (Cheryl Simon)
 28 *
 29 */
 30public class ConnectivityCommand implements CommandExecutor {
 31
 32    @Override
 33    public String executeCommand(Context context) {
 34        String bluetooth = "";
 35        String gps = "";
 36        try {
 37            ContentResolver cr = context.getContentResolver();
 38            if (System.getInt(cr, System.BLUETOOTH_ON) == 1) {
 39                bluetooth = context.getString(R.string.bluetooth);
 40            }
 41            String locationProviders = System.getString(cr, System.LOCATION_PROVIDERS_ALLOWED);
 42            if ((locationProviders.length() > 0) && locationProviders.contains("gps")) {
 43                gps = context.getString(R.string.gps);
 44            }
 45
 46        } catch (SettingNotFoundException e) {
 47            e.printStackTrace();
 48        }
 49        StringBuilder builder = new StringBuilder();
 50        builder.append(getVoiceNetworkInfo(context));
 51        builder.append(getDataNetworkInfo(context));
 52        builder.append(bluetooth);
 53        builder.append(gps);
 54        return builder.toString();
 55    }
 56
 57
 58    private String getDataNetworkInfo(Context context) {
 59        String info = context.getString(R.string.no_data_network);
 60        ConnectivityManager cManager = (ConnectivityManager) context
 61                .getSystemService(Context.CONNECTIVITY_SERVICE);
 62        NetworkInfo networkInfo = cManager.getActiveNetworkInfo();
 63        if (networkInfo == null) {
 64            return context.getString(R.string.no_data_network);
 65        }
 66        if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
 67            info = context.getString(R.string.mobile_data_network);
 68            TelephonyManager tm = (TelephonyManager) context
 69                    .getSystemService(Context.TELEPHONY_SERVICE);
 70            if (tm.getNetworkType() == TelephonyManager.NETWORK_TYPE_UMTS) {
 71                info = context.getString(R.string.threeg_data_network);
 72            } else if (tm.getNetworkType() == TelephonyManager.NETWORK_TYPE_EDGE) {
 73                info = context.getString(R.string.edge_data_network);
 74            }
 75        } else if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
 76            WifiManager wManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
 77            WifiInfo wInfo = wManager.getConnectionInfo();
 78            int wifiSignalStrength = WifiManager.calculateSignalLevel(wInfo.getRssi(), 4);
 79            info = context.getString(R.string.wifi) + wInfo.getSSID() + " " + wifiSignalStrength
 80                    + " " + context.getString(R.string.bars);
 81        }
 82        return info;
 83    }
 84
 85    private String getVoiceNetworkInfo(Context context) {
 86        TelephonyManager tm =
 87            (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
 88        String voiceNetworkOperator = tm.getNetworkOperatorName();
 89        // get the voice signal strength from a shared preference that should be updated with the
 90        // latest.
 91        int voiceSignalStrength = SavingPhoneStateListener.getVoiceLevel();
 92
 93        String voiceNetworkStrength = voiceSignalStrength + " " + context.getString(R.string.bars);
 94        if (voiceSignalStrength == -1) {
 95            voiceNetworkStrength = "";
 96        }
 97        String voiceNetworkInfo = voiceNetworkOperator + ", " + voiceNetworkStrength;
 98        return voiceNetworkInfo;
 99    }
100}