File indexing completed on 2024-12-22 04:41:42
0001 /* 0002 * SPDX-FileCopyrightText: 2019 Erik Duisters <e.duisters1@gmail.com> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 package org.kde.kdeconnect.UserInterface; 0008 0009 import android.app.Activity; 0010 import android.app.role.RoleManager; 0011 import android.content.Intent; 0012 import android.os.Build; 0013 import android.os.Bundle; 0014 import android.provider.Telephony; 0015 0016 import androidx.annotation.Nullable; 0017 import androidx.core.app.ActivityCompat; 0018 0019 public class DefaultSmsAppAlertDialogFragment extends AlertDialogFragment { 0020 private static final String KEY_PERMISSIONS = "Permissions"; 0021 private static final String KEY_REQUEST_CODE = "RequestCode"; 0022 0023 private String[] permissions; 0024 private int requestCode; 0025 0026 @Override 0027 public void onCreate(@Nullable Bundle savedInstanceState) { 0028 super.onCreate(savedInstanceState); 0029 0030 Bundle args = getArguments(); 0031 0032 if (args == null) { 0033 return; 0034 } 0035 0036 permissions = args.getStringArray(KEY_PERMISSIONS); 0037 requestCode = args.getInt(KEY_REQUEST_CODE, 0); 0038 0039 setCallback(new Callback() { 0040 @Override 0041 public void onPositiveButtonClicked() { 0042 Activity host = requireActivity(); 0043 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { 0044 RoleManager roleManager = host.getSystemService(RoleManager.class); 0045 0046 if (roleManager.isRoleAvailable(RoleManager.ROLE_SMS)) { 0047 if (!roleManager.isRoleHeld(RoleManager.ROLE_SMS)) { 0048 Intent roleRequestIntent = roleManager.createRequestRoleIntent(RoleManager.ROLE_SMS); 0049 host.startActivityForResult(roleRequestIntent, requestCode); 0050 } 0051 } 0052 } else { 0053 Intent intent = new Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT); 0054 intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, getActivity().getPackageName()); 0055 host.startActivityForResult(intent, requestCode); 0056 } 0057 0058 ActivityCompat.requestPermissions(requireActivity(), permissions, requestCode); 0059 } 0060 }); 0061 } 0062 0063 public static class Builder extends AlertDialogFragment.AbstractBuilder<DefaultSmsAppAlertDialogFragment.Builder, DefaultSmsAppAlertDialogFragment> { 0064 0065 @Override 0066 public Builder getThis() { 0067 return this; 0068 } 0069 0070 public Builder setPermissions(String[] permissions) { 0071 args.putStringArray(KEY_PERMISSIONS, permissions); 0072 0073 return getThis(); 0074 } 0075 0076 public Builder setRequestCode(int requestCode) { 0077 args.putInt(KEY_REQUEST_CODE, requestCode); 0078 0079 return getThis(); 0080 } 0081 0082 @Override 0083 protected DefaultSmsAppAlertDialogFragment createFragment() { 0084 return new DefaultSmsAppAlertDialogFragment(); 0085 } 0086 } 0087 }