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.providers.telephony; 17 18 import static android.provider.Telephony.RcsColumns.TRANSACTION_FAILED; 19 20 import static android.telephony.ims.RcsQueryContinuationToken.QUERY_CONTINUATION_TOKEN; 21 import static com.android.providers.telephony.RcsProvider.TAG; 22 23 import android.content.ContentValues; 24 import android.database.Cursor; 25 import android.database.sqlite.SQLiteDatabase; 26 import android.net.Uri; 27 import android.os.Bundle; 28 import android.os.Parcelable; 29 import android.telephony.ims.RcsQueryContinuationToken; 30 import android.text.TextUtils; 31 import android.util.Log; 32 33 class RcsProviderUtil { 34 /** 35 * The value returned when database insertion fails, 36 * @see SQLiteDatabase#insert(String, String, ContentValues) 37 */ 38 static final int INSERTION_FAILED = -1; 39 appendLimit(StringBuilder stringBuilder, int limit)40 static void appendLimit(StringBuilder stringBuilder, int limit) { 41 if (limit > 0) { 42 stringBuilder.append(" LIMIT ").append(limit); 43 } 44 } 45 createContinuationTokenBundle(Cursor cursor, Parcelable continuationToken, String tokenKey)46 static void createContinuationTokenBundle(Cursor cursor, Parcelable continuationToken, 47 String tokenKey) { 48 if (cursor.getCount() > 0) { 49 Bundle bundle = new Bundle(); 50 bundle.putParcelable(tokenKey, continuationToken); 51 cursor.setExtras(bundle); 52 } 53 } 54 55 /** 56 * This method gets a token with raw query, performs the query, increments the token's offset 57 * and returns it as a cursor extra 58 */ performContinuationQuery(SQLiteDatabase db, RcsQueryContinuationToken continuationToken)59 static Cursor performContinuationQuery(SQLiteDatabase db, 60 RcsQueryContinuationToken continuationToken) { 61 String rawQuery = continuationToken.getRawQuery(); 62 int offset = continuationToken.getOffset(); 63 64 if (offset <= 0 || TextUtils.isEmpty(rawQuery)) { 65 Log.e(TAG, "RcsProviderUtil: Invalid continuation token"); 66 return null; 67 } 68 69 String continuationQuery = rawQuery + " OFFSET " + offset; 70 71 Cursor cursor = db.rawQuery(continuationQuery, null); 72 if (cursor.getCount() > 0) { 73 continuationToken.incrementOffset(); 74 Bundle bundle = new Bundle(); 75 bundle.putParcelable(QUERY_CONTINUATION_TOKEN, continuationToken); 76 cursor.setExtras(bundle); 77 } 78 79 return cursor; 80 } 81 buildUriWithRowIdAppended(Uri prefix, long rowId)82 static Uri buildUriWithRowIdAppended(Uri prefix, long rowId) { 83 if (rowId == TRANSACTION_FAILED) { 84 return null; 85 } 86 return Uri.withAppendedPath(prefix, Long.toString(rowId)); 87 } 88 returnUriAsIsIfSuccessful(Uri uri, long rowId)89 static Uri returnUriAsIsIfSuccessful(Uri uri, long rowId) { 90 if (rowId == TRANSACTION_FAILED) { 91 return null; 92 } 93 return uri; 94 } 95 } 96