Skip to content

Instantly share code, notes, and snippets.

@Gkemon
Created September 5, 2022 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gkemon/e5830bb94ed4139964a3320541af8429 to your computer and use it in GitHub Desktop.
Save Gkemon/e5830bb94ed4139964a3320541af8429 to your computer and use it in GitHub Desktop.
SMS Utils for default SMS app
package com.emon.haziraKhata.sms;
import android.app.Activity;
import android.app.role.RoleManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.provider.Telephony;
import androidx.activity.ComponentActivity;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import com.emon.haziraKhata.listener.CommonCallback;
import com.emon.haziraKhata.log.AppLogger;
public class SMSUtils {
public static void setAsDefaultSMSApp(ComponentActivity activity, CommonCallback<Boolean> commonCallback) {
try {
ActivityResultLauncher<Intent> smsRoleResult = activity.registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == Activity.RESULT_OK) {
commonCallback.onSuccess(true);
}
});
final String myPackageName = activity.getPackageName();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
RoleManager roleManager = activity.getSystemService(RoleManager.class);
if (roleManager.isRoleAvailable(RoleManager.ROLE_SMS)) {
if (roleManager.isRoleHeld(RoleManager.ROLE_SMS)) {
commonCallback.onSuccess(true);
} else {
enableDefaultSMSApp(activity);
Intent intent = roleManager.createRequestRoleIntent(RoleManager.ROLE_SMS);
smsRoleResult.launch(intent);
}
}
} else {
if (!Telephony.Sms.getDefaultSmsPackage(activity).equals(myPackageName)) {
Intent intent =
new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);
intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME,
myPackageName);
activity.startActivity(intent);
smsRoleResult.launch(intent);
} else {
commonCallback.onSuccess(true);
}
}
} catch (Exception exception) {
commonCallback.onFailure(exception);
}
}
public static boolean isAppSetAsDefaultSMSApp(Context activity) {
try {
final String myPackageName = activity.getPackageName();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
RoleManager roleManager = activity.getSystemService(RoleManager.class);
if (roleManager.isRoleAvailable(RoleManager.ROLE_SMS)) {
return roleManager.isRoleHeld(RoleManager.ROLE_SMS);
}
return false;
} else {
return Telephony.Sms.getDefaultSmsPackage(activity).equals(myPackageName);
}
} catch (Exception exception) {
AppLogger.log(exception);
return false;
}
}
public static void disableAsDefaultSMSApp(Context context) {
try {
context.getPackageManager()
.setComponentEnabledSetting(
new ComponentName(context.getPackageName(), SMSReceiver.class.getCanonicalName()),
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP
);
} catch (Exception exception) {
AppLogger.log(exception);
}
}
public static void enableDefaultSMSApp(Context context) {
try {
context.getPackageManager()
.setComponentEnabledSetting(
new ComponentName(context, SMSReceiver.class),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP
);
} catch (Exception exception) {
AppLogger.log(exception);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment