Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 5bc6ae03 authored by Danny Baumann's avatar Danny Baumann Committed by Michael Bestas
Browse files

Allow calling contacts via specific phone accounts.

Change-Id: I237ab8d825e3234290fa3d371e087a68756c37b9
parent ec1d7d6c
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -19,4 +19,6 @@
    <!--Explanation with icon of Drawer in Contacts. Your medical information ie. Date of birth, Bloodtype, allergic details can be recorded for reference. -->
    <string name="menu_emergency_information_txt">Emergency information</string>

    <string name="call_via_template">Call via <xliff:g id="account">%1$s</xliff:g></string>

</resources>
 No newline at end of file
+21 −0
Original line number Diff line number Diff line
@@ -38,6 +38,7 @@ import com.android.contactsbind.FeedbackHelper;
import com.android.contactsbind.experiments.Flags;
import com.android.phone.common.PhoneConstants;

import java.util.ArrayList;
import java.util.List;

/**
@@ -188,6 +189,26 @@ public class CallUtil {
        }
    }

    /**
     * Returns a list of phone accounts that are able to call to numbers with the supplied scheme
     */
    public static List<PhoneAccount> getCallCapablePhoneAccounts(Context context, String scheme) {
        if (!PermissionsUtil.hasPermission(context,
                android.Manifest.permission.READ_PHONE_STATE)) {
            return null;
        }
        TelecomManager tm = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
        final ArrayList<PhoneAccount> accounts = new ArrayList<>();

        for (PhoneAccountHandle handle : tm.getCallCapablePhoneAccounts()) {
            final PhoneAccount account = tm.getPhoneAccount(handle);
            if (account != null && account.supportsUriScheme(scheme)) {
                accounts.add(account);
            }
        }
        return accounts;
    }

    /**
     * Determines if one of the call capable phone accounts defined supports calling with a subject
     * specified.
+30 −2
Original line number Diff line number Diff line
@@ -113,6 +113,7 @@ import com.android.contacts.activities.RequestPermissionsActivity;
import com.android.contacts.compat.CompatUtils;
import com.android.contacts.compat.EventCompat;
import com.android.contacts.compat.MultiWindowCompat;
import com.android.contacts.compat.PhoneNumberUtilsCompat;
import com.android.contacts.detail.ContactDisplayUtils;
import com.android.contacts.dialog.CallSubjectDialog;
import com.android.contacts.editor.ContactEditorFragment;
@@ -430,6 +431,7 @@ public class QuickContactActivity extends ContactsActivity {
        static final int COPY_TEXT = 0;
        static final int CLEAR_DEFAULT = 1;
        static final int SET_DEFAULT = 2;
        static final int CALL_VIA = 3;
    }

    private final OnCreateContextMenuListener mEntryContextMenuListener =
@@ -440,7 +442,13 @@ public class QuickContactActivity extends ContactsActivity {
                return;
            }
            final EntryContextMenuInfo info = (EntryContextMenuInfo) menuInfo;
            final String selectedMimeType = info.getMimeType();
            menu.setHeaderTitle(info.getCopyText());

            if (Phone.CONTENT_ITEM_TYPE.equals(selectedMimeType)) {
                addPhoneAccountsToMenu(QuickContactActivity.this, menu, info);
            }

            menu.add(ContextMenu.NONE, ContextMenuIds.COPY_TEXT,
                    ContextMenu.NONE, getString(R.string.copy_text));

@@ -449,8 +457,6 @@ public class QuickContactActivity extends ContactsActivity {
                return;
            }

            final String selectedMimeType = info.getMimeType();

            // Defaults to true will only enable the detail to be copied to the clipboard.
            boolean onlyOneOfMimeType = true;

@@ -470,6 +476,25 @@ public class QuickContactActivity extends ContactsActivity {
                        ContextMenu.NONE, getString(R.string.set_default));
            }
        }

        private void addPhoneAccountsToMenu(Context context,
                Menu menu, EntryContextMenuInfo info) {
            final String number = PhoneNumberUtilsCompat.normalizeNumber(info.getCopyText());
            final List<PhoneAccount> accounts = CallUtil.getCallCapablePhoneAccounts(context,
                    PhoneAccount.SCHEME_TEL);
            if (accounts == null || accounts.size() <= 1) {
                return;
            }

            for (PhoneAccount account : accounts) {
                final String text = context.getString(R.string.call_via_template,
                        account.getLabel());
                final Intent intent = CallUtil.getCallWithSubjectIntent(number,
                        account.getAccountHandle(), null);
                menu.add(ContextMenu.NONE, ContextMenuIds.CALL_VIA, ContextMenu.NONE, text)
                        .setIntent(intent);
            }
        }
    };

    @Override
@@ -497,6 +522,9 @@ public class QuickContactActivity extends ContactsActivity {
                        menuInfo.getId());
                this.startService(clearIntent);
                return true;
            case ContextMenuIds.CALL_VIA:
                startActivity(item.getIntent());
                return true;
            default:
                throw new IllegalArgumentException("Unknown menu option " + item.getItemId());
        }