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 static android.provider.Telephony.Sms.Intents.ACTION_DEFAULT_SMS_PACKAGE_CHANGED_INTERNAL; 19 20 import android.annotation.Nullable; 21 import android.content.BroadcastReceiver; 22 import android.content.ComponentName; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.IntentFilter; 26 import android.util.Log; 27 28 import com.android.internal.telephony.SmsApplication; 29 30 /** 31 * A helper class for storing and retrieving the default SMS application. 32 */ 33 public class SmsHelper { 34 private static final String TAG = "SmsHelper"; 35 36 private final Context mContext; 37 private ComponentName mDefaultSmsApplication; 38 private BroadcastReceiver mBroadcastReceiver; 39 SmsHelper(Context context)40 SmsHelper(Context context) { 41 mContext = context.getApplicationContext(); 42 } 43 initialize()44 void initialize() { 45 if (mBroadcastReceiver == null) { 46 mDefaultSmsApplication = SmsApplication.getDefaultSmsApplication(mContext, false); 47 mBroadcastReceiver = new BroadcastReceiver() { 48 @Override 49 public void onReceive(Context context, Intent intent) { 50 if (ACTION_DEFAULT_SMS_PACKAGE_CHANGED_INTERNAL.equals(intent.getAction())) { 51 mDefaultSmsApplication = 52 SmsApplication.getDefaultSmsApplication(mContext, false); 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 getDefaultSmsApplication()72 public ComponentName getDefaultSmsApplication() { 73 return mDefaultSmsApplication; 74 } 75 } 76