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