Merge "Fix auto-join and wifiscanner test bed for key change" into m-wireless-dev
diff --git a/Common/src/com/googlecode/android_scripting/facade/AndroidFacade.java b/Common/src/com/googlecode/android_scripting/facade/AndroidFacade.java
index 791c9eb..64a3e59 100644
--- a/Common/src/com/googlecode/android_scripting/facade/AndroidFacade.java
+++ b/Common/src/com/googlecode/android_scripting/facade/AndroidFacade.java
@@ -16,6 +16,7 @@
 
 package com.googlecode.android_scripting.facade;
 
+import android.app.Activity;
 import android.app.AlertDialog;
 import android.app.Notification;
 import android.app.NotificationManager;
@@ -63,6 +64,7 @@
 import java.util.List;
 import java.util.Map;
 import java.util.TimeZone;
+import java.util.concurrent.TimeUnit;
 
 import org.json.JSONArray;
 import org.json.JSONException;
@@ -169,6 +171,37 @@
     }
   }
 
+  public int startActivityForResultCodeWithTimeout(final Intent intent,
+    final int request, final int timeout) {
+    FutureActivityTask<Integer> task = new FutureActivityTask<Integer>() {
+      @Override
+      public void onCreate() {
+        super.onCreate();
+        try {
+          startActivityForResult(intent, request);
+        } catch (Exception e) {
+          intent.putExtra("EXCEPTION", e.getMessage());
+        }
+      }
+
+      @Override
+      public void onActivityResult(int requestCode, int resultCode, Intent data) {
+        if (request == requestCode){
+            setResult(resultCode);
+        }
+      }
+    };
+    mTaskQueue.execute(task);
+
+    try {
+      return task.getResult(timeout, TimeUnit.SECONDS);
+    } catch (Exception e) {
+      throw new RuntimeException(e);
+    } finally {
+      task.finish();
+    }
+  }
+
   // TODO(damonkohler): Pull this out into proper argument deserialization and support
   // complex/nested types being passed in.
   public static void putExtrasFromJsonObject(JSONObject extras,
diff --git a/Common/src/com/googlecode/android_scripting/facade/SignalStrengthFacade.java b/Common/src/com/googlecode/android_scripting/facade/SignalStrengthFacade.java
index 02703d7..2fed019 100644
--- a/Common/src/com/googlecode/android_scripting/facade/SignalStrengthFacade.java
+++ b/Common/src/com/googlecode/android_scripting/facade/SignalStrengthFacade.java
@@ -18,7 +18,7 @@
 
 /**
  * Exposes SignalStrength functionality.
- * 
+ *
  * @author Joerg Zieren (joerg.zieren@gmail.com)
  */
 @RpcMinSdk(7)
@@ -28,9 +28,11 @@
   private final EventFacade mEventFacade;
   private final PhoneStateListener mPhoneStateListener;
   private Bundle mSignalStrengths;
+  private final int INVALID_VALUE = Integer.MIN_VALUE;
 
   public SignalStrengthFacade(FacadeManager manager) {
     super(manager);
+    mSignalStrengths = new Bundle();
     mService = manager.getService();
     mEventFacade = manager.getReceiver(EventFacade.class);
     mTelephonyManager =
@@ -41,13 +43,24 @@
         return new PhoneStateListener() {
           @Override
           public void onSignalStrengthsChanged(SignalStrength signalStrength) {
-            mSignalStrengths = new Bundle();
-            mSignalStrengths.putInt("gsm_signal_strength", signalStrength.getGsmSignalStrength());
-            mSignalStrengths.putInt("gsm_bit_error_rate", signalStrength.getGsmBitErrorRate());
-            mSignalStrengths.putInt("cdma_dbm", signalStrength.getCdmaDbm());
-            mSignalStrengths.putInt("cdma_ecio", signalStrength.getCdmaEcio());
-            mSignalStrengths.putInt("evdo_dbm", signalStrength.getEvdoDbm());
-            mSignalStrengths.putInt("evdo_ecio", signalStrength.getEvdoEcio());
+            mSignalStrengths.putInt("GsmSignalStrength", signalStrength.getGsmSignalStrength());
+            mSignalStrengths.putInt("GsmBitErrorRate", signalStrength.getGsmBitErrorRate());
+            mSignalStrengths.putInt("CdmaEcio", signalStrength.getCdmaEcio());
+            mSignalStrengths.putInt("EvdoDbm", signalStrength.getEvdoDbm());
+            mSignalStrengths.putInt("EvdoEcio", signalStrength.getEvdoEcio());
+            mSignalStrengths.putInt("LteSignalStrength", signalStrength.getLteSignalStrength());
+            mSignalStrengths.putInt("LteDbm", signalStrength.getLteDbm());
+            mSignalStrengths.putInt("LteLevel", signalStrength.getLteLevel());
+            mSignalStrengths.putInt("LteAsuLevel", signalStrength.getLteAsuLevel());
+            mSignalStrengths.putInt("Level", signalStrength.getLevel());
+            mSignalStrengths.putInt("AsuLevel", signalStrength.getAsuLevel());
+            mSignalStrengths.putInt("Dbm", signalStrength.getDbm());
+            mSignalStrengths.putInt("GsmDbm", signalStrength.getGsmDbm());
+            mSignalStrengths.putInt("GsmLevel", signalStrength.getGsmLevel());
+            mSignalStrengths.putInt("GsmAsuLevel", signalStrength.getGsmAsuLevel());
+            mSignalStrengths.putInt("CdmaLevel", signalStrength.getCdmaLevel());
+            mSignalStrengths.putInt("CdmaAsuLevel", signalStrength.getCdmaAsuLevel());
+            mSignalStrengths.putInt("CdmaDbm", signalStrength.getCdmaDbm());
             mEventFacade.postEvent("signal_strengths", mSignalStrengths.clone());
           }
         };
@@ -62,10 +75,28 @@
   }
 
   @Rpc(description = "Returns the current signal strengths.", returns = "A map of \"gsm_signal_strength\"")
-  public Bundle readSignalStrengths() {
+  public Bundle phoneGetSignalStrengthInfo() {
     return mSignalStrengths;
   }
 
+  @Rpc(description = "Returns current signal strength in dBm.")
+  public Integer phoneGetSignalStrengthDbm() {
+    Integer result = mSignalStrengths.getInt("Dbm", INVALID_VALUE);
+    return (result.equals(INVALID_VALUE)?null:result);
+  }
+
+  @Rpc(description = "Returns current signal strength in AsuLevel.")
+  public Integer phoneGetSignalStrengthAsu() {
+    Integer result = mSignalStrengths.getInt("AsuLevel", INVALID_VALUE);
+    return (result.equals(INVALID_VALUE)?null:result);
+  }
+
+  @Rpc(description = "Returns current signal strength in Level.")
+  public Integer phoneGetSignalStrengthLevel() {
+    Integer result = mSignalStrengths.getInt("Level", INVALID_VALUE);
+    return (result.equals(INVALID_VALUE)?null:result);
+  }
+
   @Rpc(description = "Stops tracking signal strength.")
   @RpcStopEvent("signal_strengths")
   public void stopTrackingSignalStrengths() {
diff --git a/Common/src/com/googlecode/android_scripting/facade/tele/PhoneFacade.java b/Common/src/com/googlecode/android_scripting/facade/tele/PhoneFacade.java
old mode 100755
new mode 100644
index 26395d2..ff63dea
--- a/Common/src/com/googlecode/android_scripting/facade/tele/PhoneFacade.java
+++ b/Common/src/com/googlecode/android_scripting/facade/tele/PhoneFacade.java
@@ -16,6 +16,7 @@
 
 package com.googlecode.android_scripting.facade.tele;
 
+import android.app.Activity;
 import android.app.Service;
 import android.content.ContentResolver;
 import android.content.Context;
@@ -77,7 +78,6 @@
     private final AndroidFacade mAndroidFacade;
     private final EventFacade mEventFacade;
     private final TelephonyManager mTelephonyManager;
-
     private ITelephony mITelephony;
     private final SubscriptionManager mSubscriptionManager;
     private List<SubscriptionInfo> mSubInfos;
@@ -151,6 +151,62 @@
                                 SubscriptionManager.getDefaultSubId());
     }
 
+    @Rpc(description = "Tethering Entitlement Check")
+    public boolean phoneIsTetheringModeAllowed(String mode, Integer timeout) {
+        String[] mProvisionApp = mService.getResources().getStringArray(
+                com.android.internal.R.array.config_mobile_hotspot_provision_app);
+        /* following check defined in
+            packages/apps/Settings/src/com/android/settings/TetherSettings.java
+            isProvisioningNeeded
+        */
+        if ((mProvisionApp == null) || (mProvisionApp.length != 2)){
+            Log.d("phoneIsTetheringModeAllowed: no check is present.");
+            return true;
+        }
+        Log.d("phoneIsTetheringModeAllowed mProvisionApp 0 " + mProvisionApp[0]);
+        Log.d("phoneIsTetheringModeAllowed mProvisionApp 1 " + mProvisionApp[1]);
+
+        // FIXME: Need to use TetherSettings.xxx to replace the following private definitions.
+        /* defined in packages/apps/Settings/src/com/android/settings/TetherSettings.java
+        public static final int INVALID             = -1;
+        public static final int WIFI_TETHERING      = 0;
+        public static final int USB_TETHERING       = 1;
+        public static final int BLUETOOTH_TETHERING = 2;
+        private static final int PROVISION_REQUEST = 0;
+        */
+        final int INVALID             = -1;
+        final int WIFI_TETHERING      = 0;
+        final int USB_TETHERING       = 1;
+        final int BLUETOOTH_TETHERING = 2;
+        final int PROVISION_REQUEST = 0;
+
+        int mTetherChoice = INVALID;
+        if (mode.equals("wifi")){
+            mTetherChoice = WIFI_TETHERING;
+        } else if (mode.equals("usb")) {
+            mTetherChoice = USB_TETHERING;
+        } else if (mode.equals("bluetooth")) {
+            mTetherChoice = BLUETOOTH_TETHERING;
+        }
+        Intent intent = new Intent(Intent.ACTION_MAIN);
+        intent.setClassName(mProvisionApp[0], mProvisionApp[1]);
+        intent.putExtra("TETHER_TYPE", mTetherChoice);
+        int result;
+        try{
+            result = mAndroidFacade.startActivityForResultCodeWithTimeout(
+                intent, PROVISION_REQUEST, timeout);
+        } catch (Exception e) {
+            Log.d("phoneTetherCheck exception" + e.toString());
+            return false;
+        }
+
+        if (result == Activity.RESULT_OK) {
+            return true;
+        } else {
+            return false;
+        }
+    }
+
     @Rpc(description = "Set preferred network setting " +
                        "for specified subscription ID")
     public boolean phoneSetPreferredNetworkTypeForSubscription(String mode,
diff --git a/Common/src/com/googlecode/android_scripting/future/FutureActivityTask.java b/Common/src/com/googlecode/android_scripting/future/FutureActivityTask.java
index 7ecc2f2..3804604 100644
--- a/Common/src/com/googlecode/android_scripting/future/FutureActivityTask.java
+++ b/Common/src/com/googlecode/android_scripting/future/FutureActivityTask.java
@@ -24,10 +24,11 @@
 import android.view.Menu;
 import android.view.View;
 import android.view.Window;
+import java.util.concurrent.TimeUnit;
 
 /**
  * Encapsulates an {@link Activity} and a {@link FutureObject}.
- * 
+ *
  * @author Damon Kohler (damonkohler@gmail.com)
  */
 public abstract class FutureActivityTask<T> {
@@ -80,6 +81,10 @@
     return mResult.get();
   }
 
+  public T getResult(long timeout, TimeUnit unit) throws InterruptedException {
+    return mResult.get(timeout, unit);
+  }
+
   public void finish() {
     mActivity.finish();
   }