1 /* 2 * Copyright 2024 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 * https://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.devicediagnostics 18 19 import android.content.ContentProvider 20 import android.content.ContentValues 21 import android.content.Context 22 import android.database.AbstractCursor 23 import android.database.Cursor 24 import android.net.Uri 25 import android.os.IBinder 26 import android.os.ITradeInMode 27 import android.os.ServiceManager 28 import android.util.Slog 29 import com.android.tradeinmode.flags.Flags.enableTradeInMode 30 31 private const val TAG = "TradeInMode" 32 33 class TradeInModeTestingContentProvider : ContentProvider() { onCreatenull34 override fun onCreate(): Boolean { 35 return true 36 } 37 querynull38 override fun query( 39 uri: Uri, 40 projection: Array<out String>?, 41 selection: String?, 42 selectionArgs: Array<out String>?, 43 sortOrder: String?, 44 ): Cursor? { 45 return TestingCursor(context!!, selection) 46 } 47 getTypenull48 override fun getType(uri: Uri): String? { 49 Slog.d(TAG, "Not implemented") 50 return null 51 } 52 insertnull53 override fun insert(uri: Uri, values: ContentValues?): Uri? { 54 Slog.d(TAG, "Not implemented") 55 return null 56 } 57 deletenull58 override fun delete(uri: Uri, selection: String?, selectionArgs: Array<out String>?): Int { 59 Slog.d(TAG, "Not implemented") 60 return 0 61 } 62 updatenull63 override fun update( 64 uri: Uri, 65 values: ContentValues?, 66 selection: String?, 67 selectionArgs: Array<out String>?, 68 ): Int { 69 Slog.d(TAG, "Not implemented") 70 return 0 71 } 72 73 class TestingCursor(val context: Context, val selection: String?) : AbstractCursor() { getCountnull74 override fun getCount(): Int { 75 return 1 76 } 77 getColumnNamesnull78 override fun getColumnNames(): Array<String> { 79 return arrayOf("Status") 80 } 81 getStringnull82 override fun getString(column: Int): String { 83 if (!enableTradeInMode()) { 84 throw Exception("Trade-in mode flag not enabled") 85 } 86 87 var b: IBinder = ServiceManager.getServiceOrThrow("tradeinmode") 88 var service: ITradeInMode = ITradeInMode.Stub.asInterface(b) 89 if (selection == "start") { 90 service.startTesting() 91 } else if (selection == "wipe") { 92 service.scheduleWipeForTesting() 93 } else if (selection == "stop") { 94 service.stopTesting() 95 } else if (selection == "status") { 96 if (service.isTesting()) { 97 return "testing" 98 } 99 return "none" 100 } else { 101 throw Exception("Invalid selection: " + selection) 102 } 103 return "ok" 104 } 105 getShortnull106 override fun getShort(column: Int): Short { 107 TODO("Not implemented") 108 } 109 getIntnull110 override fun getInt(column: Int): Int { 111 TODO("Not implemented") 112 } 113 getLongnull114 override fun getLong(column: Int): Long { 115 TODO("Not implemented") 116 } 117 getFloatnull118 override fun getFloat(column: Int): Float { 119 TODO("Not implemented") 120 } 121 getDoublenull122 override fun getDouble(column: Int): Double { 123 TODO("Not implemented") 124 } 125 isNullnull126 override fun isNull(column: Int): Boolean { 127 TODO("Not implemented") 128 } 129 } 130 } 131