1 /* 2 * Copyright (C) 2025 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.launcher3.util 18 19 import android.content.ContentProvider 20 import android.content.ContentValues 21 import android.content.Context 22 import android.database.Cursor 23 import android.net.Uri 24 import android.os.Bundle 25 26 /** Wrapper around [ContentProvider] which allows delegating all calls to an interface */ 27 abstract class ContentProviderProxy : ContentProvider() { 28 onCreatenull29 override fun onCreate() = true 30 31 override fun delete(uri: Uri, selection: String?, selectionArgs: Array<out String>?): Int = 32 checkGetProxy()?.delete(uri, selection, selectionArgs) ?: 0 33 34 /** Do not route this call through proxy as it doesn't generally require initializing objects */ 35 override fun getType(uri: Uri): String? = null 36 37 override fun insert(uri: Uri, values: ContentValues?): Uri? = 38 checkGetProxy()?.insert(uri, values) 39 40 override fun query( 41 uri: Uri, 42 projection: Array<out String>?, 43 selection: String?, 44 selectionArgs: Array<out String>?, 45 sortOrder: String?, 46 ): Cursor? = checkGetProxy()?.query(uri, projection, selection, selectionArgs, sortOrder) 47 48 override fun update( 49 uri: Uri, 50 values: ContentValues?, 51 selection: String?, 52 selectionArgs: Array<out String>?, 53 ): Int = checkGetProxy()?.update(uri, values, selection, selectionArgs) ?: 0 54 55 override fun call(method: String, arg: String?, extras: Bundle?): Bundle? = 56 checkGetProxy()?.call(method, arg, extras) 57 58 private fun checkGetProxy(): ProxyProvider? = context?.let { getProxy(it) } 59 getProxynull60 abstract fun getProxy(ctx: Context): ProxyProvider? 61 62 /** Interface for handling the actual content provider calls */ 63 interface ProxyProvider { 64 65 fun delete(uri: Uri, selection: String?, selectionArgs: Array<out String>?): Int = 0 66 67 fun insert(uri: Uri, values: ContentValues?): Uri? = null 68 69 fun query( 70 uri: Uri, 71 projection: Array<out String>?, 72 selection: String?, 73 selectionArgs: Array<out String>?, 74 sortOrder: String?, 75 ): Cursor? = null 76 77 fun update( 78 uri: Uri, 79 values: ContentValues?, 80 selection: String?, 81 selectionArgs: Array<out String>?, 82 ): Int = 0 83 84 fun call(method: String, arg: String?, extras: Bundle?): Bundle? = null 85 } 86 } 87