1 package com.android.cellbroadcastreceiver;
2 
3 import android.content.ContentProvider;
4 import android.content.ContentValues;
5 import android.content.SharedPreferences;
6 import android.database.Cursor;
7 import android.database.sqlite.SQLiteDatabase;
8 import android.database.sqlite.SQLiteOpenHelper;
9 import android.database.sqlite.SQLiteQueryBuilder;
10 import android.net.Uri;
11 import android.os.Bundle;
12 import android.preference.PreferenceManager;
13 import android.provider.Telephony;
14 import android.provider.Telephony.CellBroadcasts;
15 import android.util.Log;
16 import java.util.Arrays;
17 import java.util.List;
18 
19 /**
20  * Very limited subset of function which is only used to surfaces data.
21  */
22 public class LegacyCellBroadcastContentProvider extends ContentProvider {
23     // shared preference under developer settings
24     private static final String ENABLE_ALERT_MASTER_PREF = "enable_alerts_master_toggle";
25 
26     private static final String TAG = LegacyCellBroadcastContentProvider.class.getSimpleName();
27     /** A list of preference supported by legacy app **/
28     private static final List<String> PREF_KEYS = Arrays.asList(
29             CellBroadcasts.Preference.ENABLE_CMAS_AMBER_PREF,
30             CellBroadcasts.Preference.ENABLE_AREA_UPDATE_INFO_PREF,
31             CellBroadcasts.Preference.ENABLE_TEST_ALERT_PREF,
32             CellBroadcasts.Preference.ENABLE_STATE_LOCAL_TEST_PREF,
33             CellBroadcasts.Preference.ENABLE_PUBLIC_SAFETY_PREF,
34             CellBroadcasts.Preference.ENABLE_CMAS_SEVERE_THREAT_PREF,
35             CellBroadcasts.Preference.ENABLE_CMAS_EXTREME_THREAT_PREF,
36             CellBroadcasts.Preference.ENABLE_CMAS_PRESIDENTIAL_PREF,
37             CellBroadcasts.Preference.ENABLE_EMERGENCY_PERF,
38             CellBroadcasts.Preference.ENABLE_ALERT_VIBRATION_PREF,
39             CellBroadcasts.Preference.ENABLE_CMAS_IN_SECOND_LANGUAGE_PREF,
40             ENABLE_ALERT_MASTER_PREF
41     );
42 
43     /** The database for this content provider. */
44     private SQLiteOpenHelper mOpenHelper;
45 
46     @Override
onCreate()47     public boolean onCreate() {
48         Log.d(TAG, "onCreate");
49         mOpenHelper = new CellBroadcastDatabaseHelper(getContext(), true);
50         return true;
51     }
52 
53     @Override
query(Uri uri, String[] projectionIn, String selection, String[] selectionArgs, String sortOrder)54     public Cursor query(Uri uri, String[] projectionIn, String selection,
55             String[] selectionArgs, String sortOrder) {
56         Log.d(TAG, "query:"
57                 + " uri=" + uri
58                 + " values=" + Arrays.toString(projectionIn)
59                 + " selection=" + selection
60                 + " selectionArgs=" + Arrays.toString(selectionArgs));
61 
62         SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
63         qb.setTables(CellBroadcastDatabaseHelper.TABLE_NAME);
64         SQLiteDatabase db = mOpenHelper.getReadableDatabase();
65         Cursor c = qb.query(db, projectionIn, selection, selectionArgs, null, null, sortOrder);
66         Log.d(TAG, "query from legacy cellbroadcast, returned " + c.getCount() + " messages");
67         return c;
68     }
69 
70     @Override
call(String method, String name, Bundle args)71     public Bundle call(String method, String name, Bundle args) {
72         Log.d(TAG, "call:"
73                 + " method=" + method
74                 + " name=" + name
75                 + " args=" + args);
76         SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getContext());
77         if (Telephony.CellBroadcasts.CALL_METHOD_GET_PREFERENCE.equals(method)) {
78             if (PREF_KEYS.contains(name)) {
79                 // if preference value does not exists, return null.
80                 if (sp != null && sp.contains(name)) {
81                     Bundle result = new Bundle();
82                     result.putBoolean(name, sp.getBoolean(name, true));
83                     Log.d(TAG, "migrate sharedpreference: " + name + " val: " + result.get(name));
84                     return result;
85                 }
86             } else {
87                 Log.e(TAG, "unsupported preference name" + name);
88             }
89         } else {
90             Log.e(TAG, "unsuppprted call method: " + method);
91         }
92         return null;
93     }
94 
95     @Override
getType(Uri uri)96     public String getType(Uri uri) {
97         Log.d(TAG, "getType");
98         return null;
99     }
100 
101     @Override
insert(Uri uri, ContentValues values)102     public Uri insert(Uri uri, ContentValues values) {
103         throw new UnsupportedOperationException("insert not supported");
104     }
105 
106     @Override
delete(Uri uri, String selection, String[] selectionArgs)107     public int delete(Uri uri, String selection, String[] selectionArgs) {
108         throw new UnsupportedOperationException("delete not supported");
109     }
110 
111     @Override
update(Uri uri, ContentValues values, String selection, String[] selectionArgs)112     public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
113         throw new UnsupportedOperationException("update not supported");
114     }
115 }