Merge "Ensure Telecom ServiceBinder class unlinks death recipient." into pi-dev
diff --git a/res/values-my/strings.xml b/res/values-my/strings.xml
index 286eb43..529e92a 100644
--- a/res/values-my/strings.xml
+++ b/res/values-my/strings.xml
@@ -18,7 +18,7 @@
     xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
     <string name="telecommAppLabel" product="default" msgid="382363169988504520">"ခေါ်ဆိုမှုစီမံခန့်ခွဲရေး"</string>
     <string name="userCallActivityLabel" product="default" msgid="5415173590855187131">"ဖုန်း"</string>
-    <string name="unknown" msgid="6878797917991465859">"အကြောင်းအရာ မသိရှိ"</string>
+    <string name="unknown" msgid="6878797917991465859">"မသိပါ"</string>
     <string name="notification_missedCallTitle" msgid="7554385905572364535">"လွဲသွားသော ဖုန်းခေါ်မှု"</string>
     <string name="notification_missedWorkCallTitle" msgid="6242489980390803090">"လွတ်သွားသည့် အလုပ်ဆိုင်ရာ ခ​ေါ်ဆိုမှု"</string>
     <string name="notification_missedCallsTitle" msgid="1361677948941502522">"လွဲသွားသော ဖုန်းခေါ်မှုများ"</string>
diff --git a/src/com/android/server/telecom/Call.java b/src/com/android/server/telecom/Call.java
index b5e2958..d08ff39 100644
--- a/src/com/android/server/telecom/Call.java
+++ b/src/com/android/server/telecom/Call.java
@@ -1451,6 +1451,7 @@
             if ((mConnectionProperties & Connection.PROPERTY_IS_RTT) ==
                     Connection.PROPERTY_IS_RTT) {
                 createRttStreams();
+                mWasEverRtt = true;
                 if (isEmergencyCall()) {
                     mCallsManager.setAudioRoute(CallAudioState.ROUTE_SPEAKER, null);
                     mCallsManager.mute(false);
@@ -2593,7 +2594,6 @@
         if (!areRttStreamsInitialized()) {
             Log.i(this, "Initializing RTT streams");
             try {
-                mWasEverRtt = true;
                 mInCallToConnectionServiceStreams = ParcelFileDescriptor.createReliablePipe();
                 mConnectionServiceToInCallStreams = ParcelFileDescriptor.createReliablePipe();
             } catch (IOException e) {
diff --git a/src/com/android/server/telecom/CallsManager.java b/src/com/android/server/telecom/CallsManager.java
index 099e6d3..34baa41 100644
--- a/src/com/android/server/telecom/CallsManager.java
+++ b/src/com/android/server/telecom/CallsManager.java
@@ -17,12 +17,14 @@
 package com.android.server.telecom;
 
 import android.app.ActivityManager;
+import android.app.KeyguardManager;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.pm.UserInfo;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.media.AudioManager;
+import android.media.AudioSystem;
 import android.net.Uri;
 import android.os.Bundle;
 import android.os.Handler;
@@ -516,20 +518,6 @@
             return;
         }
 
-        // Check DISALLOW_OUTGOING_CALLS restriction.
-        // Only ecbm calls are allowed through when users with the DISALLOW_OUTGOING_CALLS
-        // restriction are the current user.
-        final UserManager userManager = (UserManager) mContext.getSystemService(
-                Context.USER_SERVICE);
-        if (userManager.hasUserRestriction(UserManager.DISALLOW_OUTGOING_CALLS,
-                mCurrentUserHandle)) {
-            Log.w(this, "Rejecting non-ecbm phone call due to DISALLOW_INCOMING_CALLS "
-                    + "restriction");
-            incomingCall.reject(false, null);
-            mCallLogManager.logCall(incomingCall, Calls.MISSED_TYPE, false /* showNotification */);
-            return;
-        }
-
         List<IncomingCallFilter.CallFilter> filters = new ArrayList<>();
         filters.add(new DirectToVoicemailCallFilter(mCallerInfoLookupHelper));
         filters.add(new AsyncBlockCheckFilter(mContext, new BlockCheckerAdapter(),
@@ -1909,6 +1897,7 @@
         setCallState(call, CallState.DIALING, "dialing set explicitly");
         maybeMoveToSpeakerPhone(call);
         maybeTurnOffMute(call);
+        ensureCallAudible();
     }
 
     void markCallAsPulling(Call call) {
@@ -1962,6 +1951,7 @@
         } else {
             setCallState(call, CallState.ACTIVE, "active set explicitly");
             maybeMoveToSpeakerPhone(call);
+            ensureCallAudible();
         }
     }
 
@@ -2958,6 +2948,19 @@
         }
     }
 
+    private void ensureCallAudible() {
+        AudioManager am = mContext.getSystemService(AudioManager.class);
+        if (am == null) {
+            Log.w(this, "ensureCallAudible: audio manager is null");
+            return;
+        }
+        if (am.getStreamVolume(AudioManager.STREAM_VOICE_CALL) == 0) {
+            Log.i(this, "ensureCallAudible: voice call stream has volume 0. Adjusting to default.");
+            am.setStreamVolume(AudioManager.STREAM_VOICE_CALL,
+                    AudioSystem.getDefaultStreamVolume(AudioManager.STREAM_VOICE_CALL), 0);
+        }
+    }
+
     /**
      * Creates a new call for an existing connection.
      *
@@ -3170,6 +3173,21 @@
         }
     }
 
+    public boolean isReplyWithSmsAllowed(int uid) {
+        UserHandle callingUser = UserHandle.of(UserHandle.getUserId(uid));
+        UserManager userManager = mContext.getSystemService(UserManager.class);
+        KeyguardManager keyguardManager = mContext.getSystemService(KeyguardManager.class);
+
+        boolean isUserRestricted = userManager != null
+                && userManager.hasUserRestriction(UserManager.DISALLOW_SMS, callingUser);
+        boolean isLockscreenRestricted = keyguardManager != null
+                && keyguardManager.isDeviceLocked();
+        Log.d(this, "isReplyWithSmsAllowed: isUserRestricted: %s, isLockscreenRestricted: %s",
+                isUserRestricted, isLockscreenRestricted);
+
+        // TODO(hallliu): actually check the lockscreen once b/77731473 is fixed
+        return !isUserRestricted;
+    }
     /**
      * Blocks execution until all Telecom handlers have completed their current work.
      */
diff --git a/src/com/android/server/telecom/InCallAdapter.java b/src/com/android/server/telecom/InCallAdapter.java
index 02692c5..f3afc9a 100644
--- a/src/com/android/server/telecom/InCallAdapter.java
+++ b/src/com/android/server/telecom/InCallAdapter.java
@@ -16,12 +16,9 @@
 
 package com.android.server.telecom;
 
-import android.content.Context;
 import android.net.Uri;
 import android.os.Binder;
 import android.os.Bundle;
-import android.os.UserHandle;
-import android.os.UserManager;
 import android.telecom.Log;
 import android.telecom.PhoneAccountHandle;
 
@@ -35,16 +32,14 @@
  * binding to it. This adapter can receive commands and updates until the in-call app is unbound.
  */
 class InCallAdapter extends IInCallAdapter.Stub {
-    private final Context mContext;
     private final CallsManager mCallsManager;
     private final CallIdMapper mCallIdMapper;
     private final TelecomSystem.SyncRoot mLock;
     private final String mOwnerComponentName;
 
     /** Persists the specified parameters. */
-    public InCallAdapter(Context context, CallsManager callsManager, CallIdMapper callIdMapper,
+    public InCallAdapter(CallsManager callsManager, CallIdMapper callIdMapper,
             TelecomSystem.SyncRoot lock, String ownerComponentName) {
-        mContext = context;
         mCallsManager = callsManager;
         mCallIdMapper = callIdMapper;
         mLock = lock;
@@ -101,17 +96,12 @@
     public void rejectCall(String callId, boolean rejectWithMessage, String textMessage) {
         try {
             Log.startSession(LogUtils.Sessions.ICA_REJECT_CALL, mOwnerComponentName);
-            UserHandle callingUser = UserHandle.of(UserHandle.getUserId(Binder.getCallingUid()));
-            UserManager userManager = mContext.getSystemService(UserManager.class);
-
             // Check to make sure the in-call app's user isn't restricted from sending SMS. If so,
-            // silently drop the outgoing message.
-            if (rejectWithMessage && userManager.hasUserRestriction(
-                    UserManager.DISALLOW_SMS, callingUser)) {
+            // silently drop the outgoing message. Also drop message if the screen is locked.
+            if (!mCallsManager.isReplyWithSmsAllowed(Binder.getCallingUid())) {
                 rejectWithMessage = false;
                 textMessage = null;
             }
-
             long token = Binder.clearCallingIdentity();
             try {
                 synchronized (mLock) {
diff --git a/src/com/android/server/telecom/InCallController.java b/src/com/android/server/telecom/InCallController.java
index 7aaa770..9d20d4a 100644
--- a/src/com/android/server/telecom/InCallController.java
+++ b/src/com/android/server/telecom/InCallController.java
@@ -1309,7 +1309,6 @@
         try {
             inCallService.setInCallAdapter(
                     new InCallAdapter(
-                            mContext,
                             mCallsManager,
                             mCallIdMapper,
                             mLock,
diff --git a/src/com/android/server/telecom/LogUtils.java b/src/com/android/server/telecom/LogUtils.java
index fcf914e..c4ccd8b 100644
--- a/src/com/android/server/telecom/LogUtils.java
+++ b/src/com/android/server/telecom/LogUtils.java
@@ -134,6 +134,8 @@
         public static final String ACCEPT_HANDOVER = "ACCEPT_HANDOVER";
         public static final String HANDOVER_COMPLETE = "HANDOVER_COMPLETE";
         public static final String HANDOVER_FAILED = "HANDOVER_FAILED";
+        public static final String START_RINBACK = "START_RINGBACK";
+        public static final String STOP_RINGBACK = "STOP_RINGBACK";
 
         public static class Timings {
             public static final String ACCEPT_TIMING = "accept";
diff --git a/src/com/android/server/telecom/RingbackPlayer.java b/src/com/android/server/telecom/RingbackPlayer.java
index 47b6dfe..a8af3ac 100644
--- a/src/com/android/server/telecom/RingbackPlayer.java
+++ b/src/com/android/server/telecom/RingbackPlayer.java
@@ -16,6 +16,9 @@
 
 package com.android.server.telecom;
 
+import static com.android.server.telecom.LogUtils.Events.START_RINBACK;
+import static com.android.server.telecom.LogUtils.Events.STOP_RINGBACK;
+
 import com.android.internal.util.Preconditions;
 import android.telecom.Log;
 
@@ -64,7 +67,8 @@
 
         mCall = call;
         if (mTonePlayer == null) {
-            Log.d(this, "Playing the ringback tone for %s.", call);
+            Log.i(this, "Playing the ringback tone for %s.", call);
+            Log.addEvent(call, START_RINBACK);
             mTonePlayer = mPlayerFactory.createPlayer(InCallTonePlayer.TONE_RING_BACK);
             mTonePlayer.startTone();
         }
@@ -85,6 +89,7 @@
                 Log.w(this, "No player found to stop.");
             } else {
                 Log.i(this, "Stopping the ringback tone for %s.", call);
+                Log.addEvent(call, STOP_RINGBACK);
                 mTonePlayer.stopTone();
                 mTonePlayer = null;
             }