Prevent passing null context to AggregationSuggestionEngine

The monkey crash suggests we're creating this object when getActivity()
returns null, meaning after the fragment is destroyed.

I couldn't really find any path that could cause it, but let's make
sure we'll never pass null.

Bug 6340106

Change-Id: I38091b46a99821065e248c811ed2f974f2f47152
diff --git a/src/com/android/contacts/editor/AggregationSuggestionEngine.java b/src/com/android/contacts/editor/AggregationSuggestionEngine.java
index 0861d92..c340f96 100644
--- a/src/com/android/contacts/editor/AggregationSuggestionEngine.java
+++ b/src/com/android/contacts/editor/AggregationSuggestionEngine.java
@@ -119,7 +119,7 @@
 
     public AggregationSuggestionEngine(Context context) {
         super("AggregationSuggestions", Process.THREAD_PRIORITY_BACKGROUND);
-        mContext = context;
+        mContext = context.getApplicationContext();
         mMainHandler = new Handler() {
             @Override
             public void handleMessage(Message msg) {
diff --git a/src/com/android/contacts/editor/ContactEditorFragment.java b/src/com/android/contacts/editor/ContactEditorFragment.java
index b3fb5e9..5a6a3b6 100644
--- a/src/com/android/contacts/editor/ContactEditorFragment.java
+++ b/src/com/android/contacts/editor/ContactEditorFragment.java
@@ -724,13 +724,17 @@
             if (bitmap != null) editor.setPhotoBitmap(bitmap);
 
             if (editor instanceof RawContactEditorView) {
+                final Activity activity = getActivity();
                 final RawContactEditorView rawContactEditor = (RawContactEditorView) editor;
                 EditorListener listener = new EditorListener() {
 
                     @Override
                     public void onRequest(int request) {
+                        if (activity.isFinishing()) { // Make sure activity is still running.
+                            return;
+                        }
                         if (request == EditorListener.FIELD_CHANGED && !isEditingUserProfile()) {
-                            acquireAggregationSuggestions(rawContactEditor);
+                            acquireAggregationSuggestions(activity, rawContactEditor);
                         }
                     }
 
@@ -752,7 +756,7 @@
                 rawContactEditor.setAutoAddToDefaultGroup(mAutoAddToDefaultGroup);
 
                 if (rawContactId == mAggregationSuggestionsRawContactId) {
-                    acquireAggregationSuggestions(rawContactEditor);
+                    acquireAggregationSuggestions(activity, rawContactEditor);
                 }
             }
         }
@@ -1351,7 +1355,8 @@
     /**
      * Triggers an asynchronous search for aggregation suggestions.
      */
-    public void acquireAggregationSuggestions(RawContactEditorView rawContactEditor) {
+    private void acquireAggregationSuggestions(Context context,
+            RawContactEditorView rawContactEditor) {
         long rawContactId = rawContactEditor.getRawContactId();
         if (mAggregationSuggestionsRawContactId != rawContactId
                 && mAggregationSuggestionView != null) {
@@ -1363,7 +1368,7 @@
         mAggregationSuggestionsRawContactId = rawContactId;
 
         if (mAggregationSuggestionEngine == null) {
-            mAggregationSuggestionEngine = new AggregationSuggestionEngine(getActivity());
+            mAggregationSuggestionEngine = new AggregationSuggestionEngine(context);
             mAggregationSuggestionEngine.setListener(this);
             mAggregationSuggestionEngine.start();
         }