Merge "Reduce log spam from PbapClient."
diff --git a/src/com/android/bluetooth/pbapclient/Authenticator.java b/src/com/android/bluetooth/pbapclient/Authenticator.java
index 0811499..3a096f1 100644
--- a/src/com/android/bluetooth/pbapclient/Authenticator.java
+++ b/src/com/android/bluetooth/pbapclient/Authenticator.java
@@ -26,6 +26,7 @@
 
 public class Authenticator extends AbstractAccountAuthenticator {
     private static final String TAG = "PbapAuthenticator";
+    private static final boolean DBG = Utils.DBG;
 
     public Authenticator(Context context) {
         super(context);
@@ -34,7 +35,7 @@
     // Editing properties is not supported
     @Override
     public Bundle editProperties(AccountAuthenticatorResponse r, String s) {
-        Log.d(TAG, "got call", new Exception());
+        if (DBG) Log.d(TAG, "got call", new Exception());
         throw new UnsupportedOperationException();
     }
 
@@ -42,7 +43,7 @@
     @Override
     public Bundle addAccount(AccountAuthenticatorResponse r, String s, String s2, String[] strings,
             Bundle bundle) throws NetworkErrorException {
-        Log.d(TAG, "got call", new Exception());
+        if (DBG) Log.d(TAG, "got call", new Exception());
         // Don't allow accounts to be added.
         throw new UnsupportedOperationException();
     }
@@ -51,7 +52,7 @@
     @Override
     public Bundle confirmCredentials(AccountAuthenticatorResponse r, Account account, Bundle bundle)
             throws NetworkErrorException {
-        Log.d(TAG, "got call", new Exception());
+        if (DBG) Log.d(TAG, "got call", new Exception());
         return null;
     }
 
@@ -59,14 +60,14 @@
     @Override
     public Bundle getAuthToken(AccountAuthenticatorResponse r, Account account, String s,
             Bundle bundle) throws NetworkErrorException {
-        Log.d(TAG, "got call", new Exception());
+        if (DBG) Log.d(TAG, "got call", new Exception());
         throw new UnsupportedOperationException();
     }
 
     // Getting a label for the auth token is not supported
     @Override
     public String getAuthTokenLabel(String s) {
-        Log.d(TAG, "got call", new Exception());
+        if (DBG) Log.d(TAG, "got call", new Exception());
         return null;
     }
 
@@ -74,7 +75,7 @@
     @Override
     public Bundle updateCredentials(AccountAuthenticatorResponse r, Account account, String s,
             Bundle bundle) throws NetworkErrorException {
-        Log.d(TAG, "got call", new Exception());
+        if (DBG) Log.d(TAG, "got call", new Exception());
         return null;
     }
 
@@ -82,7 +83,7 @@
     @Override
     public Bundle hasFeatures(AccountAuthenticatorResponse r, Account account, String[] strings)
             throws NetworkErrorException {
-        Log.d(TAG, "got call", new Exception());
+        if (DBG) Log.d(TAG, "got call", new Exception());
 
         final Bundle result = new Bundle();
         result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false);
diff --git a/src/com/android/bluetooth/pbapclient/BluetoothPbapObexAuthenticator.java b/src/com/android/bluetooth/pbapclient/BluetoothPbapObexAuthenticator.java
index ab8091a..f14a805 100644
--- a/src/com/android/bluetooth/pbapclient/BluetoothPbapObexAuthenticator.java
+++ b/src/com/android/bluetooth/pbapclient/BluetoothPbapObexAuthenticator.java
@@ -30,7 +30,8 @@
 
 class BluetoothPbapObexAuthenticator implements Authenticator {
 
-    private static final String TAG = "BluetoothPbapObexAuthenticator";
+    private static final String TAG = "BtPbapObexAuthenticator";
+    private static final boolean DBG = Utils.DBG;
 
     //Default session key for legacy devices is 0000
     private String mSessionKey = "0000";
@@ -45,13 +46,16 @@
     public PasswordAuthentication onAuthenticationChallenge(String description,
             boolean isUserIdRequired, boolean isFullAccess) {
         PasswordAuthentication pa = null;
-        Log.v(TAG, "onAuthenticationChallenge: starting");
+        if (DBG) Log.v(TAG, "onAuthenticationChallenge: starting");
 
         if (mSessionKey != null && mSessionKey.length() != 0) {
-            Log.v(TAG, "onAuthenticationChallenge: mSessionKey=" + mSessionKey);
+            if (DBG) Log.v(TAG, "onAuthenticationChallenge: mSessionKey=" + mSessionKey);
             pa = new PasswordAuthentication(null, mSessionKey.getBytes());
         } else {
-            Log.v(TAG, "onAuthenticationChallenge: mSessionKey is empty, timeout/cancel occured");
+            if (DBG) {
+                Log.v(TAG,
+                        "onAuthenticationChallenge: mSessionKey is empty, timeout/cancel occured");
+            }
         }
 
         return pa;
@@ -59,7 +63,7 @@
 
     @Override
     public byte[] onAuthenticationResponse(byte[] userName) {
-        Log.v(TAG, "onAuthenticationResponse: " + userName);
+        if (DBG) Log.v(TAG, "onAuthenticationResponse: " + userName);
         /* required only in case PCE challenges PSE which we don't do now */
         return null;
     }
diff --git a/src/com/android/bluetooth/pbapclient/BluetoothPbapRequest.java b/src/com/android/bluetooth/pbapclient/BluetoothPbapRequest.java
index 1bd71ce..8b8db79 100644
--- a/src/com/android/bluetooth/pbapclient/BluetoothPbapRequest.java
+++ b/src/com/android/bluetooth/pbapclient/BluetoothPbapRequest.java
@@ -29,6 +29,7 @@
 abstract class BluetoothPbapRequest {
 
     private static final String TAG = "BluetoothPbapRequest";
+    private static final boolean DBG = Utils.DBG;
 
     protected static final byte OAP_TAGID_ORDER = 0x01;
     protected static final byte OAP_TAGID_SEARCH_VALUE = 0x02;
@@ -58,7 +59,7 @@
     }
 
     public void execute(ClientSession session) throws IOException {
-        Log.v(TAG, "execute");
+        if (DBG) Log.v(TAG, "execute");
 
         /* in case request is aborted before can be executed */
         if (mAborted) {
@@ -88,7 +89,7 @@
 
             mResponseCode = mOp.getResponseCode();
 
-            Log.d(TAG, "mResponseCode=" + mResponseCode);
+            if (DBG) Log.d(TAG, "mResponseCode=" + mResponseCode);
 
             checkResponseCode(mResponseCode);
         } catch (IOException e) {
@@ -112,19 +113,19 @@
     }
 
     protected void readResponse(InputStream stream) throws IOException {
-        Log.v(TAG, "readResponse");
+        if (DBG) Log.v(TAG, "readResponse");
 
         /* nothing here by default */
     }
 
     protected void readResponseHeaders(HeaderSet headerset) {
-        Log.v(TAG, "readResponseHeaders");
+        if (DBG) Log.v(TAG, "readResponseHeaders");
 
         /* nothing here by dafault */
     }
 
     protected void checkResponseCode(int responseCode) throws IOException {
-        Log.v(TAG, "checkResponseCode");
+        if (DBG) Log.v(TAG, "checkResponseCode");
 
         /* nothing here by dafault */
     }
diff --git a/src/com/android/bluetooth/pbapclient/BluetoothPbapRequestPullPhoneBook.java b/src/com/android/bluetooth/pbapclient/BluetoothPbapRequestPullPhoneBook.java
index ddbf215..34ab7d5 100644
--- a/src/com/android/bluetooth/pbapclient/BluetoothPbapRequestPullPhoneBook.java
+++ b/src/com/android/bluetooth/pbapclient/BluetoothPbapRequestPullPhoneBook.java
@@ -29,9 +29,9 @@
 
 final class BluetoothPbapRequestPullPhoneBook extends BluetoothPbapRequest {
 
-    private static final boolean VDBG = false;
+    private static final boolean VDBG = Utils.VDBG;
 
-    private static final String TAG = "BluetoothPbapRequestPullPhoneBook";
+    private static final String TAG = "BtPbapReqPullPhoneBook";
 
     private static final String TYPE = "x-bt/phonebook";
 
@@ -93,7 +93,7 @@
 
     @Override
     protected void readResponse(InputStream stream) throws IOException {
-        Log.v(TAG, "readResponse");
+        if (VDBG) Log.v(TAG, "readResponse");
 
         mResponse = new BluetoothPbapVcardList(mAccount, stream, mFormat);
         if (VDBG) {
@@ -103,7 +103,7 @@
 
     @Override
     protected void readResponseHeaders(HeaderSet headerset) {
-        Log.v(TAG, "readResponseHeaders");
+        if (VDBG) Log.v(TAG, "readResponseHeaders");
 
         ObexAppParameters oap = ObexAppParameters.fromHeaderSet(headerset);
 
diff --git a/src/com/android/bluetooth/pbapclient/CallLogPullRequest.java b/src/com/android/bluetooth/pbapclient/CallLogPullRequest.java
index 8024224..9498e7e 100644
--- a/src/com/android/bluetooth/pbapclient/CallLogPullRequest.java
+++ b/src/com/android/bluetooth/pbapclient/CallLogPullRequest.java
@@ -37,8 +37,8 @@
 import java.util.List;
 
 public class CallLogPullRequest extends PullRequest {
-    private static final boolean DBG = true;
-    private static final boolean VDBG = false;
+    private static final boolean DBG = Utils.DBG;
+    private static final boolean VDBG = Utils.VDBG;
     private static final String TAG = "PbapCallLogPullRequest";
     private static final String TIMESTAMP_PROPERTY = "X-IRMC-CALL-DATETIME";
     private static final String TIMESTAMP_FORMAT = "yyyyMMdd'T'HHmmss";
diff --git a/src/com/android/bluetooth/pbapclient/PbapClientConnectionHandler.java b/src/com/android/bluetooth/pbapclient/PbapClientConnectionHandler.java
index f4f9e41..fb62ec6 100644
--- a/src/com/android/bluetooth/pbapclient/PbapClientConnectionHandler.java
+++ b/src/com/android/bluetooth/pbapclient/PbapClientConnectionHandler.java
@@ -45,8 +45,9 @@
  * controlling state machine.
  */
 class PbapClientConnectionHandler extends Handler {
-    static final String TAG = "PBAP PCE handler";
-    static final boolean DBG = true;
+    static final String TAG = "PbapClientConnHandler";
+    static final boolean DBG = Utils.DBG;
+    static final boolean VDBG = Utils.VDBG;
     static final int MSG_CONNECT = 1;
     static final int MSG_DISCONNECT = 2;
     static final int MSG_DOWNLOAD = 3;
@@ -275,14 +276,14 @@
             /* Use BluetoothSocket to connect */
             if (mPseRec == null) {
                 // BackWardCompatability: Fall back to create RFCOMM through UUID.
-                Log.v(TAG, "connectSocket: UUID: " + BluetoothUuid.PBAP_PSE.getUuid());
+                if (VDBG) Log.v(TAG, "connectSocket: UUID: " + BluetoothUuid.PBAP_PSE.getUuid());
                 mSocket =
                         mDevice.createRfcommSocketToServiceRecord(BluetoothUuid.PBAP_PSE.getUuid());
             } else if (mPseRec.getL2capPsm() != L2CAP_INVALID_PSM) {
-                Log.v(TAG, "connectSocket: PSM: " + mPseRec.getL2capPsm());
+                if (VDBG) Log.v(TAG, "connectSocket: PSM: " + mPseRec.getL2capPsm());
                 mSocket = mDevice.createL2capSocket(mPseRec.getL2capPsm());
             } else {
-                Log.v(TAG, "connectSocket: channel: " + mPseRec.getRfcommChannelNumber());
+                if (VDBG) Log.v(TAG, "connectSocket: channel: " + mPseRec.getRfcommChannelNumber());
                 mSocket = mDevice.createRfcommSocket(mPseRec.getRfcommChannelNumber());
             }
 
@@ -304,7 +305,7 @@
         boolean connectionSuccessful = false;
 
         try {
-            if (DBG) {
+            if (VDBG) {
                 Log.v(TAG, "Start Obex Client Session");
             }
             BluetoothObexTransport transport = new BluetoothObexTransport(mSocket);
diff --git a/src/com/android/bluetooth/pbapclient/PbapClientService.java b/src/com/android/bluetooth/pbapclient/PbapClientService.java
index 0a9541c..5920119 100644
--- a/src/com/android/bluetooth/pbapclient/PbapClientService.java
+++ b/src/com/android/bluetooth/pbapclient/PbapClientService.java
@@ -30,7 +30,6 @@
 import android.util.Log;
 
 import com.android.bluetooth.R;
-import com.android.bluetooth.Utils;
 import com.android.bluetooth.btservice.ProfileService;
 
 import java.util.ArrayList;
@@ -44,7 +43,9 @@
  * @hide
  */
 public class PbapClientService extends ProfileService {
-    private static final boolean DBG = false;
+    private static final boolean DBG = Utils.DBG;
+    private static final boolean VDBG = Utils.VDBG;
+
     private static final String TAG = "PbapClientService";
     // MAXIMUM_DEVICES set to 10 to prevent an excessive number of simultaneous devices.
     private static final int MAXIMUM_DEVICES = 10;
@@ -60,8 +61,8 @@
 
     @Override
     protected boolean start() {
-        if (DBG) {
-            Log.d(TAG, "onStart");
+        if (VDBG) {
+            Log.v(TAG, "onStart");
         }
         IntentFilter filter = new IntentFilter();
         filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED);
@@ -98,7 +99,7 @@
     }
 
     void cleanupDevice(BluetoothDevice device) {
-        Log.w(TAG, "Cleanup device: " + device);
+        if (DBG) Log.d(TAG, "Cleanup device: " + device);
         synchronized (mPbapClientStateMachineMap) {
             PbapClientStateMachine pbapClientStateMachine = mPbapClientStateMachineMap.get(device);
             if (pbapClientStateMachine != null) {
@@ -112,7 +113,7 @@
         AccountManager accountManager = AccountManager.get(this);
         Account[] accounts =
                 accountManager.getAccountsByType(getString(R.string.pbap_account_type));
-        Log.w(TAG, "Found " + accounts.length + " unclean accounts");
+        if (VDBG) Log.v(TAG, "Found " + accounts.length + " unclean accounts");
         for (Account acc : accounts) {
             Log.w(TAG, "Deleting " + acc);
             // The device ID is the name of the account.
@@ -129,7 +130,7 @@
         @Override
         public void onReceive(Context context, Intent intent) {
             String action = intent.getAction();
-            Log.v(TAG, "onReceive" + action);
+            if (DBG) Log.v(TAG, "onReceive" + action);
             if (action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) {
                 BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                 if (getConnectionState(device) == BluetoothProfile.STATE_CONNECTED) {
@@ -160,7 +161,7 @@
         }
 
         private PbapClientService getService() {
-            if (!Utils.checkCaller()) {
+            if (!com.android.bluetooth.Utils.checkCaller()) {
                 Log.w(TAG, "PbapClient call not allowed for non-active user");
                 return null;
             }
@@ -255,8 +256,8 @@
     }
 
     private static synchronized void setPbapClientService(PbapClientService instance) {
-        if (DBG) {
-            Log.d(TAG, "setPbapClientService(): set to: " + instance);
+        if (VDBG) {
+            Log.v(TAG, "setPbapClientService(): set to: " + instance);
         }
         sPbapClientService = instance;
     }
@@ -266,7 +267,7 @@
             throw new IllegalArgumentException("Null device");
         }
         enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, "Need BLUETOOTH ADMIN permission");
-        Log.d(TAG, "Received request to ConnectPBAPPhonebook " + device.getAddress());
+        if (DBG) Log.d(TAG, "Received request to ConnectPBAPPhonebook " + device.getAddress());
         if (getPriority(device) <= BluetoothProfile.PRIORITY_OFF) {
             return false;
         }
diff --git a/src/com/android/bluetooth/pbapclient/PbapClientStateMachine.java b/src/com/android/bluetooth/pbapclient/PbapClientStateMachine.java
index a33d3d3..255a78f 100644
--- a/src/com/android/bluetooth/pbapclient/PbapClientStateMachine.java
+++ b/src/com/android/bluetooth/pbapclient/PbapClientStateMachine.java
@@ -65,7 +65,7 @@
 import java.util.List;
 
 final class PbapClientStateMachine extends StateMachine {
-    private static final boolean DBG = true;
+    private static final boolean DBG = Utils.DBG;
     private static final String TAG = "PbapClientStateMachine";
 
     // Messages for handling connect/disconnect requests.
@@ -124,7 +124,7 @@
     class Disconnected extends State {
         @Override
         public void enter() {
-            Log.d(TAG, "Enter Disconnected: " + getCurrentMessage().what);
+            if (DBG) Log.d(TAG, "Enter Disconnected: " + getCurrentMessage().what);
             onConnectionStateChanged(mCurrentDevice, mMostRecentState,
                     BluetoothProfile.STATE_DISCONNECTED);
             mMostRecentState = BluetoothProfile.STATE_DISCONNECTED;
@@ -222,8 +222,6 @@
                     ParcelUuid uuid = intent.getParcelableExtra(BluetoothDevice.EXTRA_UUID);
                     if (DBG) {
                         Log.v(TAG, "Received UUID: " + uuid.toString());
-                    }
-                    if (DBG) {
                         Log.v(TAG, "expected UUID: " + BluetoothUuid.PBAP_PSE.toString());
                     }
                     if (uuid.equals(BluetoothUuid.PBAP_PSE)) {
@@ -248,7 +246,7 @@
     class Disconnecting extends State {
         @Override
         public void enter() {
-            Log.d(TAG, "Enter Disconnecting: " + getCurrentMessage().what);
+            if (DBG) Log.d(TAG, "Enter Disconnecting: " + getCurrentMessage().what);
             onConnectionStateChanged(mCurrentDevice, mMostRecentState,
                     BluetoothProfile.STATE_DISCONNECTING);
             mMostRecentState = BluetoothProfile.STATE_DISCONNECTING;
@@ -293,7 +291,7 @@
     class Connected extends State {
         @Override
         public void enter() {
-            Log.d(TAG, "Enter Connected: " + getCurrentMessage().what);
+            if (DBG) Log.d(TAG, "Enter Connected: " + getCurrentMessage().what);
             onConnectionStateChanged(mCurrentDevice, mMostRecentState,
                     BluetoothProfile.STATE_CONNECTED);
             mMostRecentState = BluetoothProfile.STATE_CONNECTED;
@@ -344,7 +342,7 @@
     }
 
     public void disconnect(BluetoothDevice device) {
-        Log.d(TAG, "Disconnect Request " + device);
+        if (DBG) Log.d(TAG, "Disconnect Request " + device);
         sendMessage(MSG_DISCONNECT, device);
     }
 
diff --git a/src/com/android/bluetooth/pbapclient/PhonebookPullRequest.java b/src/com/android/bluetooth/pbapclient/PhonebookPullRequest.java
index 49fbd44..48ccf37 100644
--- a/src/com/android/bluetooth/pbapclient/PhonebookPullRequest.java
+++ b/src/com/android/bluetooth/pbapclient/PhonebookPullRequest.java
@@ -30,8 +30,8 @@
 
 public class PhonebookPullRequest extends PullRequest {
     private static final int MAX_OPS = 250;
-    private static final boolean VDBG = false;
-    private static final String TAG = "PbapPhonebookPullRequest";
+    private static final boolean VDBG = Utils.VDBG;
+    private static final String TAG = "PbapPbPullRequest";
 
     private final Account mAccount;
     private final Context mContext;
diff --git a/src/com/android/bluetooth/pbapclient/Utils.java b/src/com/android/bluetooth/pbapclient/Utils.java
new file mode 100644
index 0000000..c59d8d5
--- /dev/null
+++ b/src/com/android/bluetooth/pbapclient/Utils.java
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.bluetooth.pbapclient;
+
+class Utils {
+    static final boolean DBG = false;
+    static final boolean VDBG = false;
+}