1 /* 2 * Copyright (C) 2021 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.ims.rcs.uce.eab; 18 19 import android.content.Context; 20 import android.database.Cursor; 21 import android.text.TextUtils; 22 import android.util.Log; 23 24 import com.android.ims.rcs.uce.eab.EabProvider.ContactColumns; 25 import com.android.ims.rcs.uce.eab.EabProvider.EabCommonColumns; 26 import com.android.ims.rcs.uce.eab.EabProvider.OptionsColumns; 27 import com.android.ims.rcs.uce.eab.EabProvider.PresenceTupleColumns; 28 import com.android.ims.rcs.uce.util.UceUtils; 29 30 import java.util.Arrays; 31 import java.util.List; 32 import java.util.stream.Collectors; 33 34 /** 35 * The util to modify the EAB database. 36 */ 37 public class EabUtil { 38 39 private static final String LOG_TAG = UceUtils.getLogPrefix() + "EabUtil"; 40 41 /** 42 * Get the given EAB contacts from the EAB database. 43 * 44 * Output format: 45 * [PHONE_NUMBER], [RAW_CONTACT_ID], [CONTACT_ID], [DATA_ID] 46 */ getContactFromEab(Context context, String contact)47 public static String getContactFromEab(Context context, String contact) { 48 StringBuilder result = new StringBuilder(); 49 try (Cursor cursor = context.getContentResolver().query( 50 EabProvider.CONTACT_URI, 51 new String[]{ContactColumns.PHONE_NUMBER, 52 ContactColumns.RAW_CONTACT_ID, 53 ContactColumns.CONTACT_ID, 54 ContactColumns.DATA_ID}, 55 ContactColumns.PHONE_NUMBER + "=?", 56 new String[]{contact}, null)) { 57 if (cursor != null && cursor.moveToFirst()) { 58 result.append(cursor.getString(cursor.getColumnIndex( 59 ContactColumns.PHONE_NUMBER))); 60 result.append(","); 61 result.append(cursor.getString(cursor.getColumnIndex( 62 ContactColumns.RAW_CONTACT_ID))); 63 result.append(","); 64 result.append(cursor.getString(cursor.getColumnIndex( 65 ContactColumns.CONTACT_ID))); 66 result.append(","); 67 result.append(cursor.getString(cursor.getColumnIndex( 68 ContactColumns.DATA_ID))); 69 } 70 } catch (Exception e) { 71 Log.w(LOG_TAG, "getEabContactId exception " + e); 72 } 73 Log.d(LOG_TAG, "getContactFromEab() result: " + result); 74 return result.toString(); 75 } 76 77 /** 78 * Get the given EAB capability from the EAB database. 79 * 80 * Output format: 81 * [PHONE_NUMBER], [RAW_PRESENCE_ID], [PRESENCE_TIMESTAMP], [RAW_OPTION_ID], [OPTION_TIMESTAMP] 82 */ getCapabilityFromEab(Context context, String contact)83 public static String getCapabilityFromEab(Context context, String contact) { 84 StringBuilder result = new StringBuilder(); 85 try (Cursor cursor = context.getContentResolver().query( 86 EabProvider.ALL_DATA_URI, 87 new String[]{ContactColumns.PHONE_NUMBER, 88 PresenceTupleColumns._ID, 89 PresenceTupleColumns.REQUEST_TIMESTAMP, 90 OptionsColumns._ID, 91 OptionsColumns.REQUEST_TIMESTAMP}, 92 ContactColumns.PHONE_NUMBER + "=?", 93 new String[]{contact}, null)) { 94 if (cursor != null && cursor.moveToFirst()) { 95 result.append(cursor.getString(cursor.getColumnIndex( 96 ContactColumns.PHONE_NUMBER))); 97 result.append(","); 98 result.append(cursor.getString(cursor.getColumnIndex( 99 PresenceTupleColumns._ID))); 100 result.append(","); 101 result.append(cursor.getString(cursor.getColumnIndex( 102 PresenceTupleColumns.REQUEST_TIMESTAMP))); 103 result.append(","); 104 result.append(cursor.getString(cursor.getColumnIndex( 105 OptionsColumns._ID))); 106 result.append(","); 107 result.append(cursor.getString(cursor.getColumnIndex( 108 OptionsColumns.REQUEST_TIMESTAMP))); 109 } 110 } catch (Exception e) { 111 Log.w(LOG_TAG, "getCapability exception " + e); 112 } 113 Log.d(LOG_TAG, "getCapabilityFromEab() result: " + result); 114 return result.toString(); 115 } 116 117 /** 118 * Remove the given EAB contacts from the EAB database. 119 */ removeContactFromEab(int subId, String contacts, Context context)120 public static int removeContactFromEab(int subId, String contacts, Context context) { 121 if (TextUtils.isEmpty(contacts)) { 122 return -1; 123 } 124 List<String> contactList = Arrays.stream(contacts.split(",")).collect(Collectors.toList()); 125 if (contactList == null || contactList.isEmpty()) { 126 return -1; 127 } 128 int count = 0; 129 for (String contact : contactList) { 130 int contactId = getEabContactId(contact, context); 131 if (contactId == -1) { 132 continue; 133 } 134 int commonId = getEabCommonId(contactId, context); 135 count += removeContactCapabilities(contactId, commonId, context); 136 } 137 return count; 138 } 139 getEabContactId(String contactNumber, Context context)140 private static int getEabContactId(String contactNumber, Context context) { 141 int contactId = -1; 142 Cursor cursor = null; 143 String formattedNumber = EabControllerImpl.formatNumber(context, contactNumber); 144 try { 145 cursor = context.getContentResolver().query( 146 EabProvider.CONTACT_URI, 147 new String[] { EabProvider.EabCommonColumns._ID }, 148 EabProvider.ContactColumns.PHONE_NUMBER + "=?", 149 new String[] { formattedNumber }, null); 150 if (cursor != null && cursor.moveToFirst()) { 151 contactId = cursor.getInt(cursor.getColumnIndex(EabProvider.ContactColumns._ID)); 152 } 153 } catch (Exception e) { 154 Log.w(LOG_TAG, "getEabContactId exception " + e); 155 } finally { 156 if (cursor != null) { 157 cursor.close(); 158 } 159 } 160 return contactId; 161 } 162 getEabCommonId(int contactId, Context context)163 private static int getEabCommonId(int contactId, Context context) { 164 int commonId = -1; 165 Cursor cursor = null; 166 try { 167 cursor = context.getContentResolver().query( 168 EabProvider.COMMON_URI, 169 new String[] { EabProvider.EabCommonColumns._ID }, 170 EabProvider.EabCommonColumns.EAB_CONTACT_ID + "=?", 171 new String[] { String.valueOf(contactId) }, null); 172 if (cursor != null && cursor.moveToFirst()) { 173 commonId = cursor.getInt(cursor.getColumnIndex(EabProvider.EabCommonColumns._ID)); 174 } 175 } catch (Exception e) { 176 Log.w(LOG_TAG, "getEabCommonId exception " + e); 177 } finally { 178 if (cursor != null) { 179 cursor.close(); 180 } 181 } 182 return commonId; 183 } 184 removeContactCapabilities(int contactId, int commonId, Context context)185 private static int removeContactCapabilities(int contactId, int commonId, Context context) { 186 int count = 0; 187 count = context.getContentResolver().delete(EabProvider.PRESENCE_URI, 188 PresenceTupleColumns.EAB_COMMON_ID + "=?", new String[]{String.valueOf(commonId)}); 189 context.getContentResolver().delete(EabProvider.OPTIONS_URI, 190 OptionsColumns.EAB_COMMON_ID + "=?", new String[]{String.valueOf(commonId)}); 191 context.getContentResolver().delete(EabProvider.COMMON_URI, 192 EabCommonColumns.EAB_CONTACT_ID + "=?", new String[]{String.valueOf(contactId)}); 193 context.getContentResolver().delete(EabProvider.CONTACT_URI, 194 ContactColumns._ID + "=?", new String[]{String.valueOf(contactId)}); 195 return count; 196 } 197 } 198