Ye Wen | e07acb9 | 2014-11-19 12:06:05 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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.ContentValues; |
| 20 | import android.content.Context; |
Ye Wen | e07acb9 | 2014-11-19 12:06:05 -0800 | [diff] [blame] | 21 | import android.os.Process; |
| 22 | import android.provider.Telephony; |
Ye Wen | 72f1355 | 2015-03-10 14:17:13 -0700 | [diff] [blame] | 23 | |
| 24 | import com.android.internal.telephony.SmsApplication; |
Ye Wen | e07acb9 | 2014-11-19 12:06:05 -0800 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * Helpers |
| 28 | */ |
| 29 | public class ProviderUtil { |
| 30 | |
| 31 | /** |
Ye Wen | 72f1355 | 2015-03-10 14:17:13 -0700 | [diff] [blame] | 32 | * Check if a caller of the provider has restricted access, |
| 33 | * i.e. being non-system, non-phone, non-default SMS app |
Ye Wen | e07acb9 | 2014-11-19 12:06:05 -0800 | [diff] [blame] | 34 | * |
Ye Wen | 72f1355 | 2015-03-10 14:17:13 -0700 | [diff] [blame] | 35 | * @param context the context to use |
| 36 | * @param packageName the caller package name |
| 37 | * @param uid the caller uid |
| 38 | * @return true if the caller is not system, or phone or default sms app, false otherwise |
Ye Wen | e07acb9 | 2014-11-19 12:06:05 -0800 | [diff] [blame] | 39 | */ |
Ye Wen | 72f1355 | 2015-03-10 14:17:13 -0700 | [diff] [blame] | 40 | public static boolean isAccessRestricted(Context context, String packageName, int uid) { |
| 41 | return (uid != Process.SYSTEM_UID |
| 42 | && uid != Process.PHONE_UID |
| 43 | && !SmsApplication.isDefaultSmsApplication(context, packageName)); |
Ye Wen | e07acb9 | 2014-11-19 12:06:05 -0800 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Whether should set CREATOR for an insertion |
| 48 | * |
| 49 | * @param values The content of the message |
| 50 | * @param uid The caller UID of the insertion |
| 51 | * @return true if we should set CREATOR, false otherwise |
| 52 | */ |
| 53 | public static boolean shouldSetCreator(ContentValues values, int uid) { |
| 54 | return (uid != Process.SYSTEM_UID && uid != Process.PHONE_UID) || |
| 55 | (!values.containsKey(Telephony.Sms.CREATOR) && |
| 56 | !values.containsKey(Telephony.Mms.CREATOR)); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Whether should remove CREATOR for an update |
| 61 | * |
| 62 | * @param values The content of the message |
| 63 | * @param uid The caller UID of the update |
| 64 | * @return true if we should remove CREATOR, false otherwise |
| 65 | */ |
| 66 | public static boolean shouldRemoveCreator(ContentValues values, int uid) { |
| 67 | return (uid != Process.SYSTEM_UID && uid != Process.PHONE_UID) && |
| 68 | (values.containsKey(Telephony.Sms.CREATOR) || |
| 69 | values.containsKey(Telephony.Mms.CREATOR)); |
| 70 | } |
| 71 | } |