1 /** 2 * Copyright (C) 2019 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 android.ext.services.notification; 17 18 import android.content.BroadcastReceiver; 19 import android.content.Context; 20 import android.content.Intent; 21 import android.content.IntentFilter; 22 import android.provider.Telephony; 23 import android.util.Log; 24 25 import androidx.annotation.Nullable; 26 27 /** 28 * A helper class for storing and retrieving the default SMS application. 29 */ 30 public class SmsHelper { 31 private static final String TAG = "SmsHelper"; 32 33 // TODO: user RoleManager instead of copying this constant 34 private static String ACTION_DEFAULT_SMS_PACKAGE_CHANGED_INTERNAL 35 = "android.provider.action.DEFAULT_SMS_PACKAGE_CHANGED_INTERNAL"; 36 37 private final Context mContext; 38 private String mDefaultSmsPackage; 39 private BroadcastReceiver mBroadcastReceiver; 40 SmsHelper(Context context)41 SmsHelper(Context context) { 42 mContext = context.getApplicationContext(); 43 } 44 initialize()45 void initialize() { 46 if (mBroadcastReceiver == null) { 47 mDefaultSmsPackage = Telephony.Sms.getDefaultSmsPackage(mContext); 48 mBroadcastReceiver = new BroadcastReceiver() { 49 @Override 50 public void onReceive(Context context, Intent intent) { 51 if (ACTION_DEFAULT_SMS_PACKAGE_CHANGED_INTERNAL.equals(intent.getAction())) { 52 mDefaultSmsPackage = Telephony.Sms.getDefaultSmsPackage(mContext); 53 } else { 54 Log.w(TAG, "Unknown broadcast received: " + intent.getAction()); 55 } 56 } 57 }; 58 mContext.registerReceiver( 59 mBroadcastReceiver, 60 new IntentFilter(ACTION_DEFAULT_SMS_PACKAGE_CHANGED_INTERNAL)); 61 } 62 } 63 destroy()64 void destroy() { 65 if (mBroadcastReceiver != null) { 66 mContext.unregisterReceiver(mBroadcastReceiver); 67 mBroadcastReceiver = null; 68 } 69 } 70 71 @Nullable getDefaultSmsPackage()72 public String getDefaultSmsPackage() { 73 return mDefaultSmsPackage; 74 } 75 } 76