Moving pending intent response logic to HfaLogic.

Move allows the non-ui service to also return the pending intent
for setup wizard purposes.

bug:10881619
Change-Id: I3c790321f86ad74fbbb413b1bda27605e9e099d7
diff --git a/src/com/android/phone/HfaActivity.java b/src/com/android/phone/HfaActivity.java
index e3c9345..bcd3652 100644
--- a/src/com/android/phone/HfaActivity.java
+++ b/src/com/android/phone/HfaActivity.java
@@ -19,9 +19,7 @@
 import android.app.Activity;
 import android.app.AlertDialog;
 import android.app.PendingIntent;
-import android.app.PendingIntent.CanceledException;
 import android.content.DialogInterface;
-import android.content.Intent;
 import android.os.Bundle;
 import android.util.Log;
 
@@ -39,22 +37,16 @@
 public class HfaActivity extends Activity {
     private static final String TAG = HfaActivity.class.getSimpleName();
 
-    private static final boolean VERBOSE = Log.isLoggable(TAG, Log.VERBOSE);
-
-    public static final int OTASP_UNKNOWN = 0;
-    public static final int OTASP_USER_SKIPPED = 1;
-    public static final int OTASP_SUCCESS = 2;
-    public static final int OTASP_FAILURE = 3;
-
-    private boolean mCanSkip;
     private AlertDialog mDialog;
     private HfaLogic mHfaLogic;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
+        Log.i(TAG, "onCreate");
 
-        if (VERBOSE) Log.v(TAG, "onCreate");
+        final PendingIntent otaResponseIntent = getIntent().getParcelableExtra(
+                OtaUtils.EXTRA_OTASP_RESULT_CODE_PENDING_INTENT);
 
         mHfaLogic = new HfaLogic(this.getApplicationContext(), new HfaLogic.HfaLogicCallback() {
             @Override
@@ -66,7 +58,7 @@
             public void onError(String error) {
                 onHfaError(error);
             }
-        });
+        }, otaResponseIntent);
 
         startProvisioning();
     }
@@ -75,7 +67,7 @@
     protected void onDestroy() {
         super.onDestroy();
 
-        if (VERBOSE) Log.v(TAG, "onDestroy");
+        Log.i(TAG, "onDestroy");
 
         if (mDialog != null && mDialog.isShowing()) {
             mDialog.dismiss();
@@ -89,8 +81,6 @@
     }
 
     private void buildAndShowDialog() {
-        mCanSkip = true;
-
         mDialog = new AlertDialog.Builder(this)
                 .setTitle(R.string.ota_hfa_activation_title)
                 .setMessage(R.string.ota_hfa_activation_dialog_message)
@@ -98,9 +88,7 @@
                         new DialogInterface.OnClickListener() {
                             @Override
                             public void onClick(DialogInterface di, int which) {
-                                if (mCanSkip) {
-                                    sendFinalResponse(OTASP_USER_SKIPPED);
-                                }
+                                onUserSkip();
                             }})
                 /*.setOnCancelListener(new DialogInterface.OnCancelListener() {
                     @Override
@@ -113,7 +101,7 @@
         mDialog.setCanceledOnTouchOutside(false);
         mDialog.setCancelable(false);
 
-        if (VERBOSE) Log.v(TAG, "showing dialog");
+        Log.i(TAG, "showing dialog");
         mDialog.show();
     }
 
@@ -127,7 +115,7 @@
                         @Override
                         public void onClick(DialogInterface di, int which) {
                             di.dismiss();
-                            sendFinalResponse(OTASP_USER_SKIPPED);
+                            onUserSkip();
                         }
                     })
             .setNegativeButton(R.string.ota_try_again,
@@ -144,29 +132,11 @@
     }
 
     private void onHfaSuccess() {
-        // User can no longer skip after success.
-        mCanSkip = false;
-
-        sendFinalResponse(OTASP_SUCCESS);
-    }
-
-    private void sendFinalResponse(int responseCode) {
-        final PendingIntent otaResponseIntent = getIntent().getParcelableExtra(
-                OtaUtils.EXTRA_OTASP_RESULT_CODE_PENDING_INTENT);
-
-        if (otaResponseIntent != null) {
-            final Intent extraStuff = new Intent();
-            extraStuff.putExtra(OtaUtils.EXTRA_OTASP_RESULT_CODE, responseCode);
-
-            try {
-                if (VERBOSE) Log.v(TAG, "Sending OTASP confirmation with result code: "
-                        + responseCode);
-                otaResponseIntent.send(this, 0 /* resultCode (not used) */, extraStuff);
-            } catch (CanceledException e) {
-                Log.e(TAG, "Pending Intent canceled");
-            }
-        }
-
         finish();
     }
+
+    private void onUserSkip() {
+        finish();
+    }
+
 }
diff --git a/src/com/android/phone/HfaLogic.java b/src/com/android/phone/HfaLogic.java
index 7fd37cf..5a5e4b4 100644
--- a/src/com/android/phone/HfaLogic.java
+++ b/src/com/android/phone/HfaLogic.java
@@ -16,6 +16,8 @@
 
 package com.android.phone;
 
+import android.app.PendingIntent;
+import android.app.PendingIntent.CanceledException;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
@@ -46,7 +48,6 @@
  */
 public class HfaLogic {
     private static final String TAG = HfaLogic.class.getSimpleName();
-    private static final boolean VERBOSE = Log.isLoggable(TAG, Log.VERBOSE);
 
     private static final String ACTION_START = "com.android.action.START_HFA";
     private static final String ACTION_ERROR = "com.android.action.ERROR_HFA";
@@ -59,9 +60,15 @@
     public static final int WAITING_FOR_RADIO_OFF = 1;
     public static final int WAITING_FOR_RADIO_ON = 2;
 
+    public static final int OTASP_UNKNOWN = 0;
+    public static final int OTASP_USER_SKIPPED = 1;
+    public static final int OTASP_SUCCESS = 2;
+    public static final int OTASP_FAILURE = 3;
+
     private int mPhoneMonitorState = NOT_WAITING;
     private BroadcastReceiver mReceiver;
     private HfaLogicCallback mCallback;
+    private PendingIntent mResponseIntent;
     private Context mContext;
 
     public interface HfaLogicCallback {
@@ -69,9 +76,10 @@
         public void onError(String errorMsg);
     }
 
-    public HfaLogic(Context context, HfaLogicCallback callback) {
+    public HfaLogic(Context context, HfaLogicCallback callback, PendingIntent intent) {
         mCallback = Preconditions.checkNotNull(callback);
         mContext = Preconditions.checkNotNull(context);
+        mResponseIntent = intent;
     }
 
     public void start() {
@@ -85,21 +93,25 @@
     }
 
     private void sendHfaCommand(String action) {
-        if (VERBOSE) Log.v(TAG, "Sending command: " + action);
+        Log.i(TAG, "Sending command: " + action);
         mContext.sendBroadcast(new Intent(action));
     }
 
     private void onHfaError(String errorMsg) {
+        Log.i(TAG, "HfaError");
         stopHfaIntentReceiver();
+        sendFinalResponse(OTASP_FAILURE, errorMsg);
         mCallback.onError(errorMsg);
     }
 
     private void onHfaSuccess() {
+        Log.i(TAG, "HfaSuccess");
         stopHfaIntentReceiver();
         bounceRadio();
     }
 
     private void onTotalSuccess() {
+        sendFinalResponse(OTASP_SUCCESS, null);
         mCallback.onSuccess();
     }
 
@@ -116,7 +128,7 @@
         final boolean radioIsOff = state.getVoiceRegState() == ServiceState.STATE_POWER_OFF;
         final Phone phone = PhoneGlobals.getInstance().getPhone();
 
-        if (VERBOSE) Log.v(TAG, "Radio is on: " + !radioIsOff);
+        Log.i(TAG, "Radio is on: " + !radioIsOff);
 
         if (mPhoneMonitorState == WAITING_FOR_RADIO_OFF) {
             if (radioIsOff) {
@@ -144,7 +156,7 @@
                 if (action.equals(ACTION_ERROR)) {
                     onHfaError(intent.getStringExtra("errorCode"));
                 } else if (action.equals(ACTION_COMPLETE)) {
-                    if (VERBOSE) Log.v(TAG, "Hfa Successful");
+                    Log.i(TAG, "Hfa Successful");
                     onHfaSuccess();
                 }
             }
@@ -160,6 +172,25 @@
         }
     }
 
+    private void sendFinalResponse(int responseCode, String errorCode) {
+        if (mResponseIntent != null) {
+            final Intent extraStuff = new Intent();
+            extraStuff.putExtra(OtaUtils.EXTRA_OTASP_RESULT_CODE, responseCode);
+
+            if (responseCode == OTASP_FAILURE && errorCode != null) {
+                extraStuff.putExtra(OtaUtils.EXTRA_OTASP_ERROR_CODE, errorCode);
+            }
+
+            try {
+                Log.i(TAG, "Sending OTASP confirmation with result code: "
+                        + responseCode);
+                mResponseIntent.send(mContext, 0 /* resultCode (not used) */, extraStuff);
+            } catch (CanceledException e) {
+                Log.e(TAG, "Pending Intent canceled");
+            }
+        }
+    }
+
     private Handler mHandler = new Handler() {
         @Override
         public void handleMessage(Message msg) {
diff --git a/src/com/android/phone/HfaService.java b/src/com/android/phone/HfaService.java
index a4d13f2..3aeed4d 100644
--- a/src/com/android/phone/HfaService.java
+++ b/src/com/android/phone/HfaService.java
@@ -16,6 +16,7 @@
 
 package com.android.phone;
 
+import android.app.PendingIntent;
 import android.app.Service;
 import android.content.Intent;
 import android.os.IBinder;
@@ -29,6 +30,14 @@
 
     @Override
     public void onCreate() {
+        Log.i(TAG, "service started");
+    }
+
+    @Override
+    public int onStartCommand(Intent intent, int flags, int startId) {
+        final PendingIntent otaResponseIntent = intent.getParcelableExtra(
+                OtaUtils.EXTRA_OTASP_RESULT_CODE_PENDING_INTENT);
+
         new HfaLogic(this, new HfaLogic.HfaLogicCallback() {
             @Override
             public void onSuccess() {
@@ -43,9 +52,9 @@
                 // we do the same thing...finish.
                 onComplete();
             }
-        }).start();
+        }, otaResponseIntent).start();
 
-        Log.i(TAG, "service started");
+        return START_STICKY;
     }
 
     @Override
diff --git a/src/com/android/phone/OtaUtils.java b/src/com/android/phone/OtaUtils.java
index 8b67148..fe11831 100644
--- a/src/com/android/phone/OtaUtils.java
+++ b/src/com/android/phone/OtaUtils.java
@@ -125,8 +125,11 @@
 
     // Extra attached to the above PendingIntent that indicates
     // success or failure.
-    public static final String EXTRA_OTASP_RESULT_CODE =
-            "otasp_result_code";
+    public static final String EXTRA_OTASP_RESULT_CODE = "otasp_result_code";
+
+    // Extra attached to the above PendingIntent that contains an error code.
+    public static final String EXTRA_OTASP_ERROR_CODE = "otasp_error_code";
+
     public static final int OTASP_UNKNOWN = 0;
     public static final int OTASP_USER_SKIPPED = 1;  // Only meaningful with interactive OTASP
     public static final int OTASP_SUCCESS = 2;