[Rlog15] ResearchLogger option to include account name in feedback

multi-project commit with I0a293f392bd605c8203dc9a62993345ad58dcfbe

Change-Id: Ief5b940a62dbaeaecdf1c55f2ffdd1e0fdaea2b9
diff --git a/java/res/layout/research_feedback_fragment_layout.xml b/java/res/layout/research_feedback_fragment_layout.xml
index cc04ced..2a90ba2 100644
--- a/java/res/layout/research_feedback_fragment_layout.xml
+++ b/java/res/layout/research_feedback_fragment_layout.xml
@@ -70,6 +70,14 @@
         android:text="@string/research_feedback_include_history_label"
     />
 
+    <CheckBox
+        android:id="@+id/research_feedback_include_account_name"
+        android:layout_height="wrap_content"
+        android:layout_width="match_parent"
+        android:layout_marginBottom="8dip"
+        android:checked="false"
+        android:text="@string/research_feedback_include_account_name_label"
+    />
     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
diff --git a/java/res/values/research_strings.xml b/java/res/values/research_strings.xml
index 2cad15e..2d9dad1 100644
--- a/java/res/values/research_strings.xml
+++ b/java/res/values/research_strings.xml
@@ -21,4 +21,6 @@
     <!-- Contents of note explaining what data is collected and how. -->
     <!-- TODO: remove translatable=false attribute once text is stable -->
     <string name="research_splash_content" translatable="false"></string>
+    <string name="research_account_type" translatable="false"></string>
+    <string name="research_allowed_account_domain" translatable="false"></string>
 </resources>
diff --git a/java/res/values/strings.xml b/java/res/values/strings.xml
index 54ff23b..fdae302 100644
--- a/java/res/values/strings.xml
+++ b/java/res/values/strings.xml
@@ -270,6 +270,9 @@
     <!-- Text for checkbox option to include user data in feedback for research purposes [CHAR LIMIT=50] -->
     <!-- TODO: remove translatable=false attribute once text is stable -->
     <string name="research_feedback_include_history_label" translatable="false">Include session history</string>
+    <!-- Text for checkbox option to include user account name in feedback for research purposes [CHAR LIMIT=50] -->
+    <!-- TODO: remove translatable=false attribute once text is stable -->
+    <string name="research_feedback_include_account_name_label" translatable="false">Include account name</string>
     <!-- Hint to user about the text entry field where they should enter research feedback [CHAR LIMIT=40] -->
     <!-- TODO: remove translatable=false attribute once text is stable -->
     <string name="research_feedback_hint" translatable="false">Enter your feedback here.</string>
diff --git a/java/src/com/android/inputmethod/research/FeedbackFragment.java b/java/src/com/android/inputmethod/research/FeedbackFragment.java
index a9adbb7..fee61a9 100644
--- a/java/src/com/android/inputmethod/research/FeedbackFragment.java
+++ b/java/src/com/android/inputmethod/research/FeedbackFragment.java
@@ -32,7 +32,8 @@
 
 public class FeedbackFragment extends Fragment {
     private EditText mEditText;
-    private CheckBox mCheckBox;
+    private CheckBox mIncludingHistoryCheckBox;
+    private CheckBox mIncludingAccountNameCheckBox;
 
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
@@ -40,7 +41,10 @@
         final View view = inflater.inflate(R.layout.research_feedback_fragment_layout, container,
                 false);
         mEditText = (EditText) view.findViewById(R.id.research_feedback_contents);
-        mCheckBox = (CheckBox) view.findViewById(R.id.research_feedback_include_history);
+        mIncludingHistoryCheckBox = (CheckBox) view.findViewById(
+                R.id.research_feedback_include_history);
+        mIncludingAccountNameCheckBox = (CheckBox) view.findViewById(
+                R.id.research_feedback_include_account_name);
 
         final Button sendButton = (Button) view.findViewById(
                 R.id.research_feedback_send_button);
@@ -49,8 +53,10 @@
             public void onClick(View v) {
                 final Editable editable = mEditText.getText();
                 final String feedbackContents = editable.toString();
-                final boolean includeHistory = mCheckBox.isChecked();
-                ResearchLogger.getInstance().sendFeedback(feedbackContents, includeHistory);
+                final boolean isIncludingHistory = mIncludingHistoryCheckBox.isChecked();
+                final boolean isIncludingAccountName = mIncludingAccountNameCheckBox.isChecked();
+                ResearchLogger.getInstance().sendFeedback(feedbackContents, isIncludingHistory,
+                        isIncludingAccountName);
                 final Activity activity = FeedbackFragment.this.getActivity();
                 activity.finish();
                 ResearchLogger.getInstance().onLeavingSendFeedbackDialog();
diff --git a/java/src/com/android/inputmethod/research/ResearchLogger.java b/java/src/com/android/inputmethod/research/ResearchLogger.java
index 0a24af6..2da571d 100644
--- a/java/src/com/android/inputmethod/research/ResearchLogger.java
+++ b/java/src/com/android/inputmethod/research/ResearchLogger.java
@@ -18,6 +18,8 @@
 
 import static com.android.inputmethod.latin.Constants.Subtype.ExtraValue.KEYBOARD_LAYOUT_SET;
 
+import android.accounts.Account;
+import android.accounts.AccountManager;
 import android.app.AlarmManager;
 import android.app.AlertDialog;
 import android.app.Dialog;
@@ -30,6 +32,7 @@
 import android.content.SharedPreferences.Editor;
 import android.content.pm.PackageInfo;
 import android.content.pm.PackageManager.NameNotFoundException;
+import android.content.res.Resources;
 import android.graphics.Canvas;
 import android.graphics.Color;
 import android.graphics.Paint;
@@ -115,6 +118,8 @@
     private static final String PREF_RESEARCH_LOGGER_UUID_STRING = "pref_research_logger_uuid";
 
     private static final ResearchLogger sInstance = new ResearchLogger();
+    private static String sAccountType = null;
+    private static String sAllowedAccountDomain = null;
     // to write to a different filename, e.g., for testing, set mFile before calling start()
     /* package */ File mFilesDir;
     /* package */ String mUUIDString;
@@ -199,6 +204,9 @@
                 e.apply();
             }
         }
+        final Resources res = latinIME.getResources();
+        sAccountType = res.getString(R.string.research_account_type);
+        sAllowedAccountDomain = res.getString(R.string.research_allowed_account_domain);
         mLatinIME = latinIME;
         mPrefs = prefs;
         mUploadIntent = new Intent(mLatinIME, UploaderService.class);
@@ -593,6 +601,36 @@
     }
     */
 
+    /**
+     * Get the name of the first allowed account on the device.
+     *
+     * Allowed accounts must be in the domain given by ALLOWED_ACCOUNT_DOMAIN.
+     *
+     * @return The user's account name.
+     */
+    public String getAccountName() {
+        if (sAccountType == null || sAccountType.isEmpty()) {
+            return null;
+        }
+        if (sAllowedAccountDomain == null || sAllowedAccountDomain.isEmpty()) {
+            return null;
+        }
+        final AccountManager manager = AccountManager.get(mLatinIME);
+        // Filter first by account type.
+        final Account[] accounts = manager.getAccountsByType(sAccountType);
+
+        for (final Account account : accounts) {
+            if (DEBUG) {
+                Log.d(TAG, account.name);
+            }
+            final String[] parts = account.name.split("@");
+            if (parts.length > 1 && parts[1].equals(sAllowedAccountDomain)) {
+                return parts[0];
+            }
+        }
+        return null;
+    }
+
     static class LogStatement {
         final String mName;
 
@@ -626,8 +664,9 @@
     }
 
     private static final LogStatement LOGSTATEMENT_FEEDBACK =
-            new LogStatement("UserFeedback", false, false, "contents");
-    public void sendFeedback(final String feedbackContents, final boolean includeHistory) {
+            new LogStatement("UserFeedback", false, false, "contents", "accountName");
+    public void sendFeedback(final String feedbackContents, final boolean includeHistory,
+            final boolean isIncludingAccountName) {
         if (mSavedFeedbackLogBuffer == null) {
             return;
         }
@@ -635,8 +674,9 @@
             mSavedFeedbackLogBuffer.clear();
         }
         final LogUnit feedbackLogUnit = new LogUnit();
+        final String accountName = isIncludingAccountName ? getAccountName() : "";
         feedbackLogUnit.addLogStatement(LOGSTATEMENT_FEEDBACK, SystemClock.uptimeMillis(),
-                feedbackContents);
+                feedbackContents, accountName);
         mFeedbackLogBuffer.shiftIn(feedbackLogUnit);
         publishLogBuffer(mFeedbackLogBuffer, mSavedFeedbackLog, true /* isIncludingPrivateData */);
         mSavedFeedbackLog.close(new Runnable() {