1 /* 2 * Copyright 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.bluetooth.pbap; 18 19 import android.bluetooth.BluetoothProfile; 20 import android.bluetooth.BluetoothProtoEnums; 21 import android.util.Log; 22 23 import com.android.bluetooth.BluetoothStatsLog; 24 import com.android.bluetooth.content_profiles.ContentProfileErrorReportUtils; 25 import com.android.obex.Operation; 26 27 import java.io.IOException; 28 import java.io.OutputStream; 29 30 /** Handler to emit vCards to PCE. */ 31 // Next tag value for ContentProfileErrorReportUtils.report(): 2 32 public class HandlerForStringBuffer { 33 private static final String TAG = "HandlerForStringBuffer"; 34 35 private final Operation mOperation; 36 private final String mOwnerVCard; 37 38 private OutputStream mOutputStream; 39 HandlerForStringBuffer(Operation op, String ownerVCard)40 public HandlerForStringBuffer(Operation op, String ownerVCard) { 41 mOperation = op; 42 mOwnerVCard = ownerVCard; 43 Log.v(TAG, "ownerVCard \n " + mOwnerVCard); 44 } 45 init()46 public boolean init() { 47 try { 48 mOutputStream = mOperation.openOutputStream(); 49 if (mOwnerVCard != null) { 50 return writeVCard(mOwnerVCard); 51 } 52 return true; 53 } catch (IOException e) { 54 ContentProfileErrorReportUtils.report( 55 BluetoothProfile.PBAP, 56 BluetoothProtoEnums.BLUETOOTH_PBAP_HANDLER_FOR_STRING_BUFFER, 57 BluetoothStatsLog.BLUETOOTH_CONTENT_PROFILE_ERROR_REPORTED__TYPE__EXCEPTION, 58 0); 59 Log.e(TAG, "openOutputStream failed", e); 60 } 61 return false; 62 } 63 writeVCard(String vCard)64 public boolean writeVCard(String vCard) { 65 try { 66 if (vCard != null) { 67 mOutputStream.write(vCard.getBytes()); 68 return true; 69 } 70 } catch (IOException e) { 71 ContentProfileErrorReportUtils.report( 72 BluetoothProfile.PBAP, 73 BluetoothProtoEnums.BLUETOOTH_PBAP_HANDLER_FOR_STRING_BUFFER, 74 BluetoothStatsLog.BLUETOOTH_CONTENT_PROFILE_ERROR_REPORTED__TYPE__EXCEPTION, 75 1); 76 Log.e(TAG, "write failed", e); 77 } 78 return false; 79 } 80 terminate()81 public void terminate() { 82 boolean result = BluetoothPbapObexServer.closeStream(mOutputStream, mOperation); 83 Log.v(TAG, "closeStream " + (result ? "succeeded" : "failed") + "!"); 84 } 85 } 86