• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.app.AppOpsManager;
20 import android.content.ContentProvider;
21 import android.content.ContentValues;
22 import android.content.UriMatcher;
23 import android.content.pm.PackageManager;
24 import android.database.Cursor;
25 import android.database.sqlite.SQLiteDatabase;
26 import android.database.sqlite.SQLiteOpenHelper;
27 import android.database.sqlite.SQLiteQueryBuilder;
28 import android.net.Uri;
29 import android.util.Log;
30 
31 /**
32  * This is the ContentProvider for the table sms_changes.
33  * This provider is applicable only for Android Auto builds as
34  * this table exists only in Android Auto environment.
35  *
36  * This provider does not notify of changes.
37  * Interested observers should instead listen to notification on sms table, instead.
38  */
39 public class SmsChangesProvider extends ContentProvider {
40     private final static String TAG = "SmsChangesProvider";
41 
42     private static final String TABLE_SMS_CHANGES = "sms_changes";
43 
44     // Db open helper for tables stored in CE(Credential Encrypted) storage.
45     private SQLiteOpenHelper mCeOpenHelper;
46 
47     @Override
getType(Uri url)48     public String getType(Uri url) {
49         return null;
50     }
51 
52     @Override
onCreate()53     public boolean onCreate() {
54         setAppOps(AppOpsManager.OP_READ_SMS, AppOpsManager.OP_WRITE_SMS);
55         mCeOpenHelper = MmsSmsDatabaseHelper.getInstanceForCe(getContext());
56         return true;
57     }
58 
59 
60     @Override
query(Uri url, String[] projectionIn, String selection, String[] selectionArgs, String sort)61     public Cursor query(Uri url, String[] projectionIn, String selection,
62             String[] selectionArgs, String sort) {
63         // return if FEATURE_AUTOMOTIVE is not set
64         if (!isAutoFeatureSet()) {
65             return null;
66         }
67 
68         // Only support one type of query
69         //  Caller sends content://mms-sms and other params
70         if (!isUrlSupported(url)) {
71             return null;
72         }
73 
74         SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
75         qb.setTables(TABLE_SMS_CHANGES);
76         SQLiteDatabase db = mCeOpenHelper.getReadableDatabase();
77         return qb.query(db, projectionIn, selection, selectionArgs,
78                 null /* groupBy */, null /* having */,
79                 null /* sortOrder */);
80     }
81 
82     @Override
insert(Uri url, ContentValues initialValues)83     public Uri insert(Uri url, ContentValues initialValues) {
84         return null;
85     }
86 
87     @Override
delete(Uri url, String where, String[] whereArgs)88     public int delete(Uri url, String where, String[] whereArgs) {
89         // return if FEATURE_AUTOMOTIVE is not set
90         if (!isAutoFeatureSet()) {
91             return 0;
92         }
93 
94         // only support deletion of all data from the table
95         if (!isUrlSupported(url)) return 0;
96 
97         return mCeOpenHelper.getWritableDatabase().delete(TABLE_SMS_CHANGES,
98                 null /* whereClause */, null /* whereArgs */);
99     }
100 
isUrlSupported(Uri url)101     private boolean isUrlSupported(Uri url) {
102         if (sURLMatcher.match(url) != SMSCHANGES_URL) {
103             Log.e(TAG, "Invalid or Unsupported request: " + url);
104             return false;
105         }
106         return true;
107     }
108 
109     @Override
update(Uri url, ContentValues values, String where, String[] whereArgs)110     public int update(Uri url, ContentValues values, String where, String[] whereArgs) {
111         return 0;
112     }
113 
isAutoFeatureSet()114     private boolean isAutoFeatureSet() {
115         return getContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE);
116     }
117 
118     private static final int SMSCHANGES_URL = 0;
119 
120     private static final UriMatcher sURLMatcher =
121             new UriMatcher(UriMatcher.NO_MATCH);
122 
123     static {
124         sURLMatcher.addURI("sms-changes", null, SMSCHANGES_URL);
125     }
126 }
127