1 /*
2  * Copyright (C) 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  *      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.tv.settings.connectivity;
17 
18 
19 import android.content.ContentProvider;
20 import android.content.ContentValues;
21 import android.content.Context;
22 import android.content.SharedPreferences;
23 import android.database.Cursor;
24 import android.database.MatrixCursor;
25 import android.net.Uri;
26 import android.util.Log;
27 
28 /**
29  * A {@link ContentProvider} that provides information for detecting network changes to the
30  * current network for re-authenticating SmartHome access.
31  * This ContentProvider should only be accessed by the Launcherx Process, and no other apps should
32  * be able to invoke it (as determined by permission).
33  */
34 public class NetworkChangeContentProvider extends ContentProvider {
35     private static final String TAG = "NetworkChangeContentProvider";
36     private static final int DEFAULT_COUNT = 0;
37 
38     private SharedPreferences mSharedPreferences;
39 
40     @Override
onCreate()41     public boolean onCreate() {
42         if (getContext() != null) {
43             mSharedPreferences = getContext().getSharedPreferences(
44                     NetworkChangeDetectionConfigs.SHARED_PREFERENCES_NAME,
45                     Context.MODE_PRIVATE);
46         }
47         return true;
48     }
49 
50     @Override
query( Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)51     public Cursor query(
52             Uri uri,
53             String[] projection,
54             String selection,
55             String[] selectionArgs,
56             String sortOrder) {
57         Log.d(TAG, "Querying with URI " + uri);
58         String path = uri.getPath();
59 
60         if (!path.endsWith("/" + NetworkChangeDetectionConfigs.PREFERENCE_KEY)) {
61             throw new IllegalArgumentException("Unknown URI: " + uri);
62         } else {
63             int totalCount = mSharedPreferences.getInt(NetworkChangeDetectionConfigs.PREFERENCE_KEY,
64                     DEFAULT_COUNT);
65             MatrixCursor cursor = new MatrixCursor(
66                     new String[]{NetworkChangeDetectionConfigs.PREFERENCE_KEY});
67             cursor.addRow(new Object[]{totalCount});
68             cursor.setNotificationUri(getContext().getContentResolver(),
69                     NetworkChangeDetectionConfigs.CONTENT_URI);
70             return cursor;
71         }
72     }
73 
74     @Override
getType(Uri uri)75     public String getType(Uri uri) {
76         return null;
77     }
78 
79     @Override
insert(Uri uri, ContentValues contentValues)80     public Uri insert(Uri uri, ContentValues contentValues) {
81         throw new UnsupportedOperationException("Read-only access supported.");
82     }
83 
84     @Override
delete(Uri uri, String s, String[] strings)85     public int delete(Uri uri, String s, String[] strings) {
86         throw new UnsupportedOperationException("Read-only access supported.");
87     }
88 
89     @Override
update(Uri uri, ContentValues contentValues, String s, String[] strings)90     public int update(Uri uri, ContentValues contentValues, String s, String[] strings) {
91         throw new UnsupportedOperationException("Read-only access supported.");
92     }
93 }