Merge "Add ability to add test voicemails from test app"
diff --git a/tests/AndroidManifest.xml b/tests/AndroidManifest.xml
index 94aa3aa..73febff 100644
--- a/tests/AndroidManifest.xml
+++ b/tests/AndroidManifest.xml
@@ -35,6 +35,8 @@
     <uses-permission android:name="android.permission.READ_PROFILE" />
     <uses-permission android:name="android.permission.READ_SOCIAL_STREAM" />
 
+    <uses-permission android:name="com.android.voicemail.permission.ADD_VOICEMAIL" />
+
     <application>
         <uses-library android:name="android.test.runner" />
         <meta-data android:name="com.android.dialer.iconset" android:resource="@xml/iconset" />
diff --git a/tests/res/layout/fill_call_log_test.xml b/tests/res/layout/fill_call_log_test.xml
index 2359f2c..e34125c 100644
--- a/tests/res/layout/fill_call_log_test.xml
+++ b/tests/res/layout/fill_call_log_test.xml
@@ -86,6 +86,12 @@
                 android:layout_height="wrap_content"
                 android:text="@string/call_type_outgoing"
                 android:textSize="9sp" />
+            <RadioButton
+                android:id="@+id/call_type_voicemail"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:text="@string/call_type_voicemail"
+                android:textSize="9sp" />
         </RadioGroup>
         <CheckBox
             android:id="@+id/call_type_video"
diff --git a/tests/res/values/donottranslate_strings.xml b/tests/res/values/donottranslate_strings.xml
index d4b48a6..2f8017c 100644
--- a/tests/res/values/donottranslate_strings.xml
+++ b/tests/res/values/donottranslate_strings.xml
@@ -42,6 +42,7 @@
     <string name="call_type_missed">Missed</string>
     <string name="call_type_outgoing">Outgoing</string>
     <string name="call_type_video">Video</string>
+    <string name="call_type_voicemail">Voicemail</string>
     <string name="call_date">Call date</string>
     <string name="call_time">Call time</string>
     <string name="edit">Edit</string>
diff --git a/tests/src/com/android/dialer/tests/calllog/FillCallLogTestActivity.java b/tests/src/com/android/dialer/tests/calllog/FillCallLogTestActivity.java
index 2e90c4d..7157154 100644
--- a/tests/src/com/android/dialer/tests/calllog/FillCallLogTestActivity.java
+++ b/tests/src/com/android/dialer/tests/calllog/FillCallLogTestActivity.java
@@ -23,14 +23,19 @@
 import android.app.LoaderManager;
 import android.app.TimePickerDialog;
 import android.content.ContentProviderClient;
+import android.content.ContentResolver;
 import android.content.ContentValues;
 import android.content.CursorLoader;
 import android.content.Loader;
 import android.database.Cursor;
+import android.net.Uri;
 import android.os.AsyncTask;
 import android.os.Bundle;
 import android.os.RemoteException;
 import android.provider.CallLog.Calls;
+import android.provider.VoicemailContract;
+import android.provider.VoicemailContract.Status;
+import android.provider.VoicemailContract.Voicemails;
 import android.telecom.PhoneAccountHandle;
 import android.telecom.TelecomManager;
 import android.text.format.DateFormat;
@@ -72,6 +77,7 @@
     private RadioButton mCallTypeIncoming;
     private RadioButton mCallTypeMissed;
     private RadioButton mCallTypeOutgoing;
+    private RadioButton mCallTypeVoicemail;
     private CheckBox mCallTypeVideo;
     private RadioButton mPresentationAllowed;
     private RadioButton mPresentationRestricted;
@@ -125,6 +131,7 @@
         mCallTypeIncoming = (RadioButton) findViewById(R.id.call_type_incoming);
         mCallTypeMissed = (RadioButton) findViewById(R.id.call_type_missed);
         mCallTypeOutgoing = (RadioButton) findViewById(R.id.call_type_outgoing);
+        mCallTypeVoicemail = (RadioButton) findViewById(R.id.call_type_voicemail);
         mCallTypeVideo = (CheckBox) findViewById(R.id.call_type_video);
         mPresentationAllowed = (RadioButton) findViewById(R.id.presentation_allowed);
         mPresentationPayphone = (RadioButton) findViewById(R.id.presentation_payphone);
@@ -375,6 +382,8 @@
             return Calls.INCOMING_TYPE;
         } else if (mCallTypeOutgoing.isChecked()) {
             return Calls.OUTGOING_TYPE;
+        } else if (mCallTypeVoicemail.isChecked()) {
+            return Calls.VOICEMAIL_TYPE;
         } else {
             return Calls.MISSED_TYPE;
         }
@@ -497,9 +506,13 @@
             dataUsage = (long) RNG.nextInt(52428800);
         }
 
-        Calls.addCall(null, this, mPhoneNumber.getText().toString(), getManualPresentation(),
-                getManualCallType(), features, getManualAccount(),
-                dateTime.getTimeInMillis(), RNG.nextInt(60 * 60), dataUsage);
+        if (getManualCallType() == Calls.VOICEMAIL_TYPE) {
+            addManualVoicemail(dateTime.getTimeInMillis());
+        } else {
+            Calls.addCall(null, this, mPhoneNumber.getText().toString(), getManualPresentation(),
+                    getManualCallType(), features, getManualAccount(),
+                    dateTime.getTimeInMillis(), RNG.nextInt(60 * 60), dataUsage);
+        }
 
         // Subtract offset from the call date/time and store as new date/time
         int offset = Integer.parseInt(mOffset.getText().toString());
@@ -513,4 +526,35 @@
         setDisplayDate();
         setDisplayTime();
     }
+
+    private void addManualVoicemail(Long time) {
+        final ContentValues contentValues = new ContentValues();
+        contentValues.put(Voicemails.DATE, time);
+        contentValues.put(Voicemails.NUMBER, mPhoneNumber.getText().toString());
+        contentValues.put(Voicemails.DURATION, 5000);
+        contentValues.put(Voicemails.SOURCE_PACKAGE, getPackageName());
+        contentValues.put(Voicemails.SOURCE_DATA, 500);
+        contentValues.put(Voicemails.IS_READ, 0);
+
+        getContentResolver().insert(VoicemailContract.Voicemails.buildSourceUri(getPackageName()),
+                contentValues);
+
+        updateVoicemailStatus();
+    }
+
+    private void updateVoicemailStatus() {
+        ContentResolver contentResolver = getContentResolver();
+        Uri statusUri = VoicemailContract.Status.buildSourceUri(getPackageName());
+        final PhoneAccountHandle accountHandle = getManualAccount();
+
+        ContentValues values = new ContentValues();
+        values.put(Status.PHONE_ACCOUNT_COMPONENT_NAME, getPackageName());
+        values.put(Status.PHONE_ACCOUNT_ID, "ACCOUNT_ID");
+        values.put(Status.CONFIGURATION_STATE, VoicemailContract.Status.CONFIGURATION_STATE_OK);
+        values.put(Status.DATA_CHANNEL_STATE, VoicemailContract.Status.DATA_CHANNEL_STATE_OK);
+        values.put(Status.NOTIFICATION_CHANNEL_STATE,
+                VoicemailContract.Status.NOTIFICATION_CHANNEL_STATE_OK);
+
+        contentResolver.insert(statusUri, values);
+    }
 }