1 /* 2 * Copyright (C) 2019 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 package com.android.internal.telephony.ims; 17 18 import static android.provider.Telephony.RcsColumns.RcsIncomingMessageColumns.INCOMING_MESSAGE_URI; 19 import static android.provider.Telephony.RcsColumns.RcsOutgoingMessageColumns.OUTGOING_MESSAGE_URI; 20 21 import android.content.ContentResolver; 22 import android.content.ContentValues; 23 import android.database.Cursor; 24 import android.net.Uri; 25 import android.os.RemoteException; 26 import android.text.TextUtils; 27 28 /** 29 * Utility functions for {@link RcsMessageStoreController} 30 * 31 * @hide 32 */ 33 public class RcsMessageStoreUtil { 34 private ContentResolver mContentResolver; 35 RcsMessageStoreUtil(ContentResolver contentResolver)36 RcsMessageStoreUtil(ContentResolver contentResolver) { 37 mContentResolver = contentResolver; 38 } 39 getIntValueFromTableRow(Uri tableUri, String valueColumn, String idColumn, int idValue)40 int getIntValueFromTableRow(Uri tableUri, String valueColumn, String idColumn, 41 int idValue) throws RemoteException { 42 try (Cursor cursor = getValueFromTableRow(tableUri, valueColumn, idColumn, idValue)) { 43 if (cursor != null && cursor.moveToFirst()) { 44 return cursor.getInt(cursor.getColumnIndex(valueColumn)); 45 } else { 46 throw new RemoteException("The row with (" + idColumn + " = " + idValue 47 + ") could not be found in " + tableUri); 48 } 49 } 50 } 51 getLongValueFromTableRow(Uri tableUri, String valueColumn, String idColumn, int idValue)52 long getLongValueFromTableRow(Uri tableUri, String valueColumn, String idColumn, 53 int idValue) throws RemoteException { 54 try (Cursor cursor = getValueFromTableRow(tableUri, valueColumn, idColumn, idValue)) { 55 if (cursor != null && cursor.moveToFirst()) { 56 return cursor.getLong(cursor.getColumnIndex(valueColumn)); 57 } else { 58 throw new RemoteException("The row with (" + idColumn + " = " + idValue 59 + ") could not be found in " + tableUri); 60 } 61 } 62 } 63 getDoubleValueFromTableRow(Uri tableUri, String valueColumn, String idColumn, int idValue)64 double getDoubleValueFromTableRow(Uri tableUri, String valueColumn, String idColumn, 65 int idValue) throws RemoteException { 66 try (Cursor cursor = getValueFromTableRow(tableUri, valueColumn, idColumn, idValue)) { 67 if (cursor != null && cursor.moveToFirst()) { 68 return cursor.getDouble(cursor.getColumnIndex(valueColumn)); 69 } else { 70 throw new RemoteException("The row with (" + idColumn + " = " + idValue 71 + ") could not be found in " + tableUri); 72 } 73 } 74 } 75 getStringValueFromTableRow(Uri tableUri, String valueColumn, String idColumn, int idValue)76 String getStringValueFromTableRow(Uri tableUri, String valueColumn, String idColumn, 77 int idValue) throws RemoteException { 78 try (Cursor cursor = getValueFromTableRow(tableUri, valueColumn, idColumn, idValue)) { 79 if (cursor != null && cursor.moveToFirst()) { 80 return cursor.getString(cursor.getColumnIndex(valueColumn)); 81 } else { 82 throw new RemoteException("The row with (" + idColumn + " = " + idValue 83 + ") could not be found in " + tableUri); 84 } 85 } 86 } 87 getUriValueFromTableRow(Uri tableUri, String valueColumn, String idColumn, int idValue)88 Uri getUriValueFromTableRow(Uri tableUri, String valueColumn, String idColumn, 89 int idValue) throws RemoteException { 90 try (Cursor cursor = getValueFromTableRow(tableUri, valueColumn, idColumn, idValue)) { 91 if (cursor != null && cursor.moveToFirst()) { 92 String uriAsString = cursor.getString(cursor.getColumnIndex(valueColumn)); 93 94 if (!TextUtils.isEmpty(uriAsString)) { 95 return Uri.parse(uriAsString); 96 } 97 return null; 98 } else { 99 throw new RemoteException("The row with (" + idColumn + " = " + idValue 100 + ") could not be found in " + tableUri); 101 } 102 } 103 } 104 updateValueOfProviderUri(Uri uri, String valueColumn, int value, String errorMessage)105 void updateValueOfProviderUri(Uri uri, String valueColumn, int value, String errorMessage) 106 throws RemoteException { 107 ContentValues contentValues = new ContentValues(1); 108 contentValues.put(valueColumn, value); 109 performUpdate(uri, contentValues, errorMessage); 110 } 111 updateValueOfProviderUri(Uri uri, String valueColumn, double value, String errorMessage)112 void updateValueOfProviderUri(Uri uri, String valueColumn, double value, String errorMessage) 113 throws RemoteException { 114 ContentValues contentValues = new ContentValues(1); 115 contentValues.put(valueColumn, value); 116 performUpdate(uri, contentValues, errorMessage); 117 } 118 updateValueOfProviderUri(Uri uri, String valueColumn, long value, String errorMessage)119 void updateValueOfProviderUri(Uri uri, String valueColumn, long value, String errorMessage) 120 throws RemoteException { 121 ContentValues contentValues = new ContentValues(1); 122 contentValues.put(valueColumn, value); 123 performUpdate(uri, contentValues, errorMessage); 124 } 125 updateValueOfProviderUri(Uri uri, String valueColumn, String value, String errorMessage)126 void updateValueOfProviderUri(Uri uri, String valueColumn, String value, String errorMessage) 127 throws RemoteException { 128 ContentValues contentValues = new ContentValues(1); 129 contentValues.put(valueColumn, value); 130 performUpdate(uri, contentValues, errorMessage); 131 } 132 updateValueOfProviderUri(Uri uri, String valueColumn, Uri value, String errorMessage)133 void updateValueOfProviderUri(Uri uri, String valueColumn, Uri value, String errorMessage) 134 throws RemoteException { 135 ContentValues contentValues = new ContentValues(1); 136 contentValues.put(valueColumn, value == null ? null : value.toString()); 137 performUpdate(uri, contentValues, errorMessage); 138 } 139 performUpdate(Uri uri, ContentValues contentValues, String errorMessage)140 private void performUpdate(Uri uri, ContentValues contentValues, String errorMessage) 141 throws RemoteException { 142 int updateCount = mContentResolver.update(uri, contentValues, null, null); 143 144 // TODO - convert remote exceptions to return values. 145 if (updateCount <= 0) { 146 throw new RemoteException(errorMessage); 147 } 148 } 149 getValueFromTableRow(Uri tableUri, String valueColumn, String idColumn, int idValue)150 private Cursor getValueFromTableRow(Uri tableUri, String valueColumn, String idColumn, 151 int idValue) { 152 return mContentResolver.query(tableUri, new String[]{valueColumn}, idColumn + "=?", 153 new String[]{Integer.toString(idValue)}, null); 154 } 155 getMessageTableUri(boolean isIncoming)156 static Uri getMessageTableUri(boolean isIncoming) { 157 return isIncoming ? INCOMING_MESSAGE_URI : OUTGOING_MESSAGE_URI; 158 } 159 160 161 } 162