1 /*
2  * Copyright (C) 2013 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.example.android.smssample;
18 
19 import android.content.Context;
20 import android.content.Intent;
21 import android.os.Build;
22 import android.os.Build.VERSION_CODES;
23 import android.provider.Telephony;
24 import android.provider.Telephony.Sms.Intents;
25 
26 public class Utils {
27 
28     /**
29      * Check if the device runs Android 4.3 (JB MR2) or higher.
30      */
hasJellyBeanMR2()31     public static boolean hasJellyBeanMR2() {
32         return Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR2;
33     }
34 
35     /**
36      * Check if the device runs Android 4.4 (KitKat) or higher.
37      */
hasKitKat()38     public static boolean hasKitKat() {
39         return Build.VERSION.SDK_INT >= VERSION_CODES.KITKAT;
40     }
41 
42     /**
43      * Check if your app is the default system SMS app.
44      * @param context The Context
45      * @return True if it is default, False otherwise. Pre-KitKat will always return True.
46      */
isDefaultSmsApp(Context context)47     public static boolean isDefaultSmsApp(Context context) {
48         if (hasKitKat()) {
49             return context.getPackageName().equals(Telephony.Sms.getDefaultSmsPackage(context));
50         }
51 
52         return true;
53     }
54 
55     /**
56      * Trigger the intent to open the system dialog that asks the user to change the default
57      * SMS app.
58      * @param context The Context
59      */
setDefaultSmsApp(Context context)60     public static void setDefaultSmsApp(Context context) {
61         // This is a new intent which only exists on KitKat
62         if (hasKitKat()) {
63             Intent intent = new Intent(Intents.ACTION_CHANGE_DEFAULT);
64             intent.putExtra(Intents.EXTRA_PACKAGE_NAME, context.getPackageName());
65             context.startActivity(intent);
66         }
67     }
68 }
69