1 /*
2  * Copyright (C) 2023 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.providers.telephony;
18 
19 import android.content.ContentProvider;
20 import android.content.ContentUris;
21 import android.content.ContentValues;
22 import android.database.Cursor;
23 import android.database.sqlite.SQLiteDatabase;
24 import android.database.sqlite.SQLiteQueryBuilder;
25 import android.net.Uri;
26 import android.provider.Telephony;
27 import android.util.Log;
28 
29 import java.util.Arrays;
30 
31 public class SatelliteProvider extends ContentProvider {
32     private static final String TAG = "SatelliteProvider";
33     private static final boolean VDBG = false; // STOPSHIP if true
34 
35     private SatelliteDatabaseHelper mDbHelper;
36 
37     @Override
onCreate()38     public boolean onCreate() {
39         Log.d(TAG, "onCreate");
40         mDbHelper = new SatelliteDatabaseHelper(getContext());
41         return true;
42     }
43 
44     @Override
query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)45     public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
46             String sortOrder) {
47         if (VDBG) {
48             Log.d(TAG, "query:"
49                     + " uri=" + uri
50                     + " values=" + Arrays.toString(projection)
51                     + " selection=" + selection
52                     + " selectionArgs=" + Arrays.toString(selectionArgs));
53         }
54         SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
55         qb.setTables(Telephony.SatelliteDatagrams.TABLE_NAME);
56 
57         SQLiteDatabase db = getReadableDatabase();
58         Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);
59         return c;
60     }
61 
62     @Override
getType(Uri uri)63     public String getType(Uri uri) {
64         return null;
65     }
66 
67     @Override
insert(Uri uri, ContentValues values)68     public Uri insert(Uri uri, ContentValues values) {
69         if (VDBG) {
70             Log.d(TAG, "insert:"
71                     + " uri=" + uri
72                     + " values=" + values);
73         }
74         long row = getWritableDatabase().insertOrThrow(Telephony.SatelliteDatagrams.TABLE_NAME,
75                 null, values);
76         if (row > 0) {
77             Uri newUri = ContentUris.withAppendedId(Telephony.SatelliteDatagrams.CONTENT_URI, row);
78             getContext().getContentResolver().notifyChange(newUri, null);
79             return newUri;
80         }
81         return null;
82     }
83 
84     @Override
delete(Uri uri, String selection, String[] selectionArgs)85     public int delete(Uri uri, String selection, String[] selectionArgs) {
86         if (VDBG) {
87             Log.d(TAG, "delete:"
88                     + " uri=" + uri
89                     + " selection={" + selection + "}"
90                     + " selection=" + selection
91                     + " selectionArgs=" + Arrays.toString(selectionArgs));
92         }
93         final int count = getWritableDatabase().delete(Telephony.SatelliteDatagrams.TABLE_NAME,
94                 selection, selectionArgs);
95         Log.d(TAG, "  delete.count=" + count);
96         return count;
97     }
98 
99     @Override
update(Uri uri, ContentValues values, String selection, String[] selectionArgs)100     public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
101        return 0;
102     }
103 
104     /**
105      * These methods can be overridden in a subclass for testing SatelliteProvider using an
106      * in-memory database.
107      */
getReadableDatabase()108     SQLiteDatabase getReadableDatabase() {
109         return mDbHelper.getReadableDatabase();
110     }
getWritableDatabase()111     SQLiteDatabase getWritableDatabase() {
112         return mDbHelper.getWritableDatabase();
113     }
114 }
115