1db6da486217daa3418944cf93086a3bae549dad2Yorke Lee/*
2db6da486217daa3418944cf93086a3bae549dad2Yorke Lee * Copyright (C) 2014 The Android Open Source Project
3db6da486217daa3418944cf93086a3bae549dad2Yorke Lee *
4db6da486217daa3418944cf93086a3bae549dad2Yorke Lee * Licensed under the Apache License, Version 2.0 (the "License");
5db6da486217daa3418944cf93086a3bae549dad2Yorke Lee * you may not use this file except in compliance with the License.
6db6da486217daa3418944cf93086a3bae549dad2Yorke Lee * You may obtain a copy of the License at
7db6da486217daa3418944cf93086a3bae549dad2Yorke Lee *
8db6da486217daa3418944cf93086a3bae549dad2Yorke Lee *      http://www.apache.org/licenses/LICENSE-2.0
9db6da486217daa3418944cf93086a3bae549dad2Yorke Lee *
10db6da486217daa3418944cf93086a3bae549dad2Yorke Lee * Unless required by applicable law or agreed to in writing, software
11db6da486217daa3418944cf93086a3bae549dad2Yorke Lee * distributed under the License is distributed on an "AS IS" BASIS,
12db6da486217daa3418944cf93086a3bae549dad2Yorke Lee * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13db6da486217daa3418944cf93086a3bae549dad2Yorke Lee * See the License for the specific language governing permissions and
14db6da486217daa3418944cf93086a3bae549dad2Yorke Lee * limitations under the License.
15db6da486217daa3418944cf93086a3bae549dad2Yorke Lee */
16db6da486217daa3418944cf93086a3bae549dad2Yorke Lee
17db6da486217daa3418944cf93086a3bae549dad2Yorke Leepackage com.android.commands.telecom;
18db6da486217daa3418944cf93086a3bae549dad2Yorke Lee
19db6da486217daa3418944cf93086a3bae549dad2Yorke Leeimport android.content.ComponentName;
20db6da486217daa3418944cf93086a3bae549dad2Yorke Leeimport android.content.Context;
21d3586e174f6d14fd6079e4a2a2b7908b6e45c834Roshan Piusimport android.net.Uri;
22db6da486217daa3418944cf93086a3bae549dad2Yorke Leeimport android.os.RemoteException;
23db6da486217daa3418944cf93086a3bae549dad2Yorke Leeimport android.os.ServiceManager;
24b9381c790eef869ebd904c92bd019886600fc814Yorke Leeimport android.telecom.PhoneAccount;
25db6da486217daa3418944cf93086a3bae549dad2Yorke Leeimport android.telecom.PhoneAccountHandle;
26db6da486217daa3418944cf93086a3bae549dad2Yorke Leeimport android.text.TextUtils;
27db6da486217daa3418944cf93086a3bae549dad2Yorke Lee
28db6da486217daa3418944cf93086a3bae549dad2Yorke Leeimport com.android.internal.os.BaseCommand;
29db6da486217daa3418944cf93086a3bae549dad2Yorke Leeimport com.android.internal.telecom.ITelecomService;
30db6da486217daa3418944cf93086a3bae549dad2Yorke Lee
31db6da486217daa3418944cf93086a3bae549dad2Yorke Leeimport java.io.PrintStream;
32db6da486217daa3418944cf93086a3bae549dad2Yorke Lee
33db6da486217daa3418944cf93086a3bae549dad2Yorke Leepublic final class Telecom extends BaseCommand {
34db6da486217daa3418944cf93086a3bae549dad2Yorke Lee
35db6da486217daa3418944cf93086a3bae549dad2Yorke Lee    /**
36db6da486217daa3418944cf93086a3bae549dad2Yorke Lee     * Command-line entry point.
37db6da486217daa3418944cf93086a3bae549dad2Yorke Lee     *
38db6da486217daa3418944cf93086a3bae549dad2Yorke Lee     * @param args The command-line arguments
39db6da486217daa3418944cf93086a3bae549dad2Yorke Lee     */
40db6da486217daa3418944cf93086a3bae549dad2Yorke Lee    public static void main(String[] args) {
41db6da486217daa3418944cf93086a3bae549dad2Yorke Lee      (new Telecom()).run(args);
42db6da486217daa3418944cf93086a3bae549dad2Yorke Lee    }
43db6da486217daa3418944cf93086a3bae549dad2Yorke Lee
44b9381c790eef869ebd904c92bd019886600fc814Yorke Lee    private static final String COMMAND_SET_PHONE_ACCOUNT_ENABLED = "set-phone-account-enabled";
45b9381c790eef869ebd904c92bd019886600fc814Yorke Lee    private static final String COMMAND_SET_PHONE_ACCOUNT_DISABLED = "set-phone-account-disabled";
46b9381c790eef869ebd904c92bd019886600fc814Yorke Lee    private static final String COMMAND_REGISTER_PHONE_ACCOUNT = "register-phone-account";
47d3586e174f6d14fd6079e4a2a2b7908b6e45c834Roshan Pius    private static final String COMMAND_REGISTER_SIM_PHONE_ACCOUNT = "register-sim-phone-account";
48b9381c790eef869ebd904c92bd019886600fc814Yorke Lee    private static final String COMMAND_UNREGISTER_PHONE_ACCOUNT = "unregister-phone-account";
49db6da486217daa3418944cf93086a3bae549dad2Yorke Lee    private static final String COMMAND_SET_DEFAULT_DIALER = "set-default-dialer";
50db6da486217daa3418944cf93086a3bae549dad2Yorke Lee    private static final String COMMAND_GET_DEFAULT_DIALER = "get-default-dialer";
518cd95ee0d0d643d3500fbd0841b658053623902eYorke Lee    private static final String COMMAND_GET_SYSTEM_DIALER = "get-system-dialer";
52db6da486217daa3418944cf93086a3bae549dad2Yorke Lee
53db6da486217daa3418944cf93086a3bae549dad2Yorke Lee    private ComponentName mComponent;
54db6da486217daa3418944cf93086a3bae549dad2Yorke Lee    private String mAccountId;
55db6da486217daa3418944cf93086a3bae549dad2Yorke Lee    private ITelecomService mTelecomService;
56db6da486217daa3418944cf93086a3bae549dad2Yorke Lee
57db6da486217daa3418944cf93086a3bae549dad2Yorke Lee    @Override
58db6da486217daa3418944cf93086a3bae549dad2Yorke Lee    public void onShowUsage(PrintStream out) {
59db6da486217daa3418944cf93086a3bae549dad2Yorke Lee        out.println(
60db6da486217daa3418944cf93086a3bae549dad2Yorke Lee                "usage: telecom [subcommand] [options]\n" +
61db6da486217daa3418944cf93086a3bae549dad2Yorke Lee                "usage: telecom set-phone-account-enabled <COMPONENT> <ID>\n" +
62db6da486217daa3418944cf93086a3bae549dad2Yorke Lee                "usage: telecom set-phone-account-disabled <COMPONENT> <ID>\n" +
63b9381c790eef869ebd904c92bd019886600fc814Yorke Lee                "usage: telecom register-phone-account <COMPONENT> <ID> <LABEL>\n" +
64d3586e174f6d14fd6079e4a2a2b7908b6e45c834Roshan Pius                "usage: telecom register-sim-phone-account <COMPONENT> <ID> <LABEL> <ADDRESS>\n" +
65b9381c790eef869ebd904c92bd019886600fc814Yorke Lee                "usage: telecom unregister-phone-account <COMPONENT> <ID>\n" +
66db6da486217daa3418944cf93086a3bae549dad2Yorke Lee                "usage: telecom set-default-dialer <PACKAGE>\n" +
678cd95ee0d0d643d3500fbd0841b658053623902eYorke Lee                "usage: telecom get-default-dialer\n" +
688cd95ee0d0d643d3500fbd0841b658053623902eYorke Lee                "usage: telecom get-system-dialer\n" +
69db6da486217daa3418944cf93086a3bae549dad2Yorke Lee                "\n" +
70db6da486217daa3418944cf93086a3bae549dad2Yorke Lee                "telecom set-phone-account-enabled: Enables the given phone account, if it has \n" +
71db6da486217daa3418944cf93086a3bae549dad2Yorke Lee                " already been registered with Telecom.\n" +
72db6da486217daa3418944cf93086a3bae549dad2Yorke Lee                "\n" +
73db6da486217daa3418944cf93086a3bae549dad2Yorke Lee                "telecom set-phone-account-disabled: Disables the given phone account, if it \n" +
74db6da486217daa3418944cf93086a3bae549dad2Yorke Lee                " has already been registered with telecom.\n" +
75db6da486217daa3418944cf93086a3bae549dad2Yorke Lee                "\n" +
76adb53b35cc99e1e98bc4c640d534fa00d44ebe31Yorke Lee                "telecom set-default-dialer: Sets the default dialer to the given component. \n" +
77db6da486217daa3418944cf93086a3bae549dad2Yorke Lee                "\n" +
788cd95ee0d0d643d3500fbd0841b658053623902eYorke Lee                "telecom get-default-dialer: Displays the current default dialer. \n" +
798cd95ee0d0d643d3500fbd0841b658053623902eYorke Lee                "\n" +
808cd95ee0d0d643d3500fbd0841b658053623902eYorke Lee                "telecom get-system-dialer: Displays the current system dialer. \n"
81db6da486217daa3418944cf93086a3bae549dad2Yorke Lee                );
82db6da486217daa3418944cf93086a3bae549dad2Yorke Lee    }
83db6da486217daa3418944cf93086a3bae549dad2Yorke Lee
84db6da486217daa3418944cf93086a3bae549dad2Yorke Lee    @Override
85db6da486217daa3418944cf93086a3bae549dad2Yorke Lee    public void onRun() throws Exception {
86db6da486217daa3418944cf93086a3bae549dad2Yorke Lee        mTelecomService = ITelecomService.Stub.asInterface(
87db6da486217daa3418944cf93086a3bae549dad2Yorke Lee                ServiceManager.getService(Context.TELECOM_SERVICE));
88db6da486217daa3418944cf93086a3bae549dad2Yorke Lee        if (mTelecomService == null) {
89db6da486217daa3418944cf93086a3bae549dad2Yorke Lee            showError("Error: Could not access the Telecom Manager. Is the system running?");
90db6da486217daa3418944cf93086a3bae549dad2Yorke Lee            return;
91db6da486217daa3418944cf93086a3bae549dad2Yorke Lee        }
92db6da486217daa3418944cf93086a3bae549dad2Yorke Lee
93db6da486217daa3418944cf93086a3bae549dad2Yorke Lee        String command = nextArgRequired();
94db6da486217daa3418944cf93086a3bae549dad2Yorke Lee        switch (command) {
95b9381c790eef869ebd904c92bd019886600fc814Yorke Lee            case COMMAND_SET_PHONE_ACCOUNT_ENABLED:
96db6da486217daa3418944cf93086a3bae549dad2Yorke Lee                runSetPhoneAccountEnabled(true);
97db6da486217daa3418944cf93086a3bae549dad2Yorke Lee                break;
98b9381c790eef869ebd904c92bd019886600fc814Yorke Lee            case COMMAND_SET_PHONE_ACCOUNT_DISABLED:
99db6da486217daa3418944cf93086a3bae549dad2Yorke Lee                runSetPhoneAccountEnabled(false);
100db6da486217daa3418944cf93086a3bae549dad2Yorke Lee                break;
101b9381c790eef869ebd904c92bd019886600fc814Yorke Lee            case COMMAND_REGISTER_PHONE_ACCOUNT:
102b9381c790eef869ebd904c92bd019886600fc814Yorke Lee                runRegisterPhoneAccount();
103b9381c790eef869ebd904c92bd019886600fc814Yorke Lee                break;
104d3586e174f6d14fd6079e4a2a2b7908b6e45c834Roshan Pius            case COMMAND_REGISTER_SIM_PHONE_ACCOUNT:
105d3586e174f6d14fd6079e4a2a2b7908b6e45c834Roshan Pius                runRegisterSimPhoneAccount();
106d3586e174f6d14fd6079e4a2a2b7908b6e45c834Roshan Pius                break;
107b9381c790eef869ebd904c92bd019886600fc814Yorke Lee            case COMMAND_UNREGISTER_PHONE_ACCOUNT:
108b9381c790eef869ebd904c92bd019886600fc814Yorke Lee                runUnregisterPhoneAccount();
109b9381c790eef869ebd904c92bd019886600fc814Yorke Lee                break;
110db6da486217daa3418944cf93086a3bae549dad2Yorke Lee            case COMMAND_SET_DEFAULT_DIALER:
111db6da486217daa3418944cf93086a3bae549dad2Yorke Lee                runSetDefaultDialer();
112db6da486217daa3418944cf93086a3bae549dad2Yorke Lee                break;
113db6da486217daa3418944cf93086a3bae549dad2Yorke Lee            case COMMAND_GET_DEFAULT_DIALER:
114db6da486217daa3418944cf93086a3bae549dad2Yorke Lee                runGetDefaultDialer();
115db6da486217daa3418944cf93086a3bae549dad2Yorke Lee                break;
1168cd95ee0d0d643d3500fbd0841b658053623902eYorke Lee            case COMMAND_GET_SYSTEM_DIALER:
1178cd95ee0d0d643d3500fbd0841b658053623902eYorke Lee                runGetSystemDialer();
1188cd95ee0d0d643d3500fbd0841b658053623902eYorke Lee                break;
119db6da486217daa3418944cf93086a3bae549dad2Yorke Lee            default:
120db6da486217daa3418944cf93086a3bae549dad2Yorke Lee                throw new IllegalArgumentException ("unknown command '" + command + "'");
121db6da486217daa3418944cf93086a3bae549dad2Yorke Lee        }
122db6da486217daa3418944cf93086a3bae549dad2Yorke Lee    }
123db6da486217daa3418944cf93086a3bae549dad2Yorke Lee
124db6da486217daa3418944cf93086a3bae549dad2Yorke Lee    private void runSetPhoneAccountEnabled(boolean enabled) throws RemoteException {
125b9381c790eef869ebd904c92bd019886600fc814Yorke Lee        final PhoneAccountHandle handle = getPhoneAccountHandleFromArgs();
126db6da486217daa3418944cf93086a3bae549dad2Yorke Lee        final boolean success =  mTelecomService.enablePhoneAccount(handle, enabled);
127db6da486217daa3418944cf93086a3bae549dad2Yorke Lee        if (success) {
128db6da486217daa3418944cf93086a3bae549dad2Yorke Lee            System.out.println("Success - " + handle + (enabled ? " enabled." : " disabled."));
129db6da486217daa3418944cf93086a3bae549dad2Yorke Lee        } else {
130db6da486217daa3418944cf93086a3bae549dad2Yorke Lee            System.out.println("Error - is " + handle + " a valid PhoneAccount?");
131db6da486217daa3418944cf93086a3bae549dad2Yorke Lee        }
132db6da486217daa3418944cf93086a3bae549dad2Yorke Lee    }
133db6da486217daa3418944cf93086a3bae549dad2Yorke Lee
134b9381c790eef869ebd904c92bd019886600fc814Yorke Lee    private void runRegisterPhoneAccount() throws RemoteException {
135b9381c790eef869ebd904c92bd019886600fc814Yorke Lee        final PhoneAccountHandle handle = getPhoneAccountHandleFromArgs();
136b9381c790eef869ebd904c92bd019886600fc814Yorke Lee        final String label = nextArgRequired();
137b9381c790eef869ebd904c92bd019886600fc814Yorke Lee        PhoneAccount account = PhoneAccount.builder(handle, label)
138b9381c790eef869ebd904c92bd019886600fc814Yorke Lee                .setCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER).build();
139b9381c790eef869ebd904c92bd019886600fc814Yorke Lee        mTelecomService.registerPhoneAccount(account);
140b9381c790eef869ebd904c92bd019886600fc814Yorke Lee        System.out.println("Success - " + handle + " registered.");
141b9381c790eef869ebd904c92bd019886600fc814Yorke Lee    }
142b9381c790eef869ebd904c92bd019886600fc814Yorke Lee
143d3586e174f6d14fd6079e4a2a2b7908b6e45c834Roshan Pius    private void runRegisterSimPhoneAccount() throws RemoteException {
144d3586e174f6d14fd6079e4a2a2b7908b6e45c834Roshan Pius        final PhoneAccountHandle handle = getPhoneAccountHandleFromArgs();
145d3586e174f6d14fd6079e4a2a2b7908b6e45c834Roshan Pius        final String label = nextArgRequired();
146d3586e174f6d14fd6079e4a2a2b7908b6e45c834Roshan Pius        final String address = nextArgRequired();
147d3586e174f6d14fd6079e4a2a2b7908b6e45c834Roshan Pius        PhoneAccount account = PhoneAccount.builder(
148d3586e174f6d14fd6079e4a2a2b7908b6e45c834Roshan Pius            handle, label)
149d3586e174f6d14fd6079e4a2a2b7908b6e45c834Roshan Pius            .setAddress(Uri.parse(address))
150d3586e174f6d14fd6079e4a2a2b7908b6e45c834Roshan Pius            .setSubscriptionAddress(Uri.parse(address))
151d3586e174f6d14fd6079e4a2a2b7908b6e45c834Roshan Pius            .setCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER |
152d3586e174f6d14fd6079e4a2a2b7908b6e45c834Roshan Pius                    PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)
153d3586e174f6d14fd6079e4a2a2b7908b6e45c834Roshan Pius            .setShortDescription(label)
154d3586e174f6d14fd6079e4a2a2b7908b6e45c834Roshan Pius            .addSupportedUriScheme(PhoneAccount.SCHEME_TEL)
155d3586e174f6d14fd6079e4a2a2b7908b6e45c834Roshan Pius            .addSupportedUriScheme(PhoneAccount.SCHEME_VOICEMAIL)
156d3586e174f6d14fd6079e4a2a2b7908b6e45c834Roshan Pius            .build();
157d3586e174f6d14fd6079e4a2a2b7908b6e45c834Roshan Pius        mTelecomService.registerPhoneAccount(account);
158d3586e174f6d14fd6079e4a2a2b7908b6e45c834Roshan Pius        System.out.println("Success - " + handle + " registered.");
159d3586e174f6d14fd6079e4a2a2b7908b6e45c834Roshan Pius    }
160d3586e174f6d14fd6079e4a2a2b7908b6e45c834Roshan Pius
161b9381c790eef869ebd904c92bd019886600fc814Yorke Lee    private void runUnregisterPhoneAccount() throws RemoteException {
162b9381c790eef869ebd904c92bd019886600fc814Yorke Lee        final PhoneAccountHandle handle = getPhoneAccountHandleFromArgs();
163b9381c790eef869ebd904c92bd019886600fc814Yorke Lee        mTelecomService.unregisterPhoneAccount(handle);
164b9381c790eef869ebd904c92bd019886600fc814Yorke Lee        System.out.println("Success - " + handle + " unregistered.");
165b9381c790eef869ebd904c92bd019886600fc814Yorke Lee    }
166b9381c790eef869ebd904c92bd019886600fc814Yorke Lee
167db6da486217daa3418944cf93086a3bae549dad2Yorke Lee    private void runSetDefaultDialer() throws RemoteException {
168db6da486217daa3418944cf93086a3bae549dad2Yorke Lee        final String packageName = nextArgRequired();
169db6da486217daa3418944cf93086a3bae549dad2Yorke Lee        final boolean success = mTelecomService.setDefaultDialer(packageName);
170db6da486217daa3418944cf93086a3bae549dad2Yorke Lee        if (success) {
171db6da486217daa3418944cf93086a3bae549dad2Yorke Lee            System.out.println("Success - " + packageName + " set as default dialer.");
172db6da486217daa3418944cf93086a3bae549dad2Yorke Lee        } else {
173db6da486217daa3418944cf93086a3bae549dad2Yorke Lee            System.out.println("Error - " + packageName + " is not an installed Dialer app, \n"
174db6da486217daa3418944cf93086a3bae549dad2Yorke Lee                    + " or is already the default dialer.");
175db6da486217daa3418944cf93086a3bae549dad2Yorke Lee        }
176db6da486217daa3418944cf93086a3bae549dad2Yorke Lee    }
177db6da486217daa3418944cf93086a3bae549dad2Yorke Lee
178db6da486217daa3418944cf93086a3bae549dad2Yorke Lee    private void runGetDefaultDialer() throws RemoteException {
179db6da486217daa3418944cf93086a3bae549dad2Yorke Lee        System.out.println(mTelecomService.getDefaultDialerPackage());
180db6da486217daa3418944cf93086a3bae549dad2Yorke Lee    }
181db6da486217daa3418944cf93086a3bae549dad2Yorke Lee
1828cd95ee0d0d643d3500fbd0841b658053623902eYorke Lee    private void runGetSystemDialer() throws RemoteException {
1838cd95ee0d0d643d3500fbd0841b658053623902eYorke Lee        System.out.println(mTelecomService.getSystemDialerPackage());
1848cd95ee0d0d643d3500fbd0841b658053623902eYorke Lee    }
1858cd95ee0d0d643d3500fbd0841b658053623902eYorke Lee
186b9381c790eef869ebd904c92bd019886600fc814Yorke Lee    private PhoneAccountHandle getPhoneAccountHandleFromArgs() {
187b9381c790eef869ebd904c92bd019886600fc814Yorke Lee        final ComponentName component = parseComponentName(nextArgRequired());
188b9381c790eef869ebd904c92bd019886600fc814Yorke Lee        final String accountId = nextArgRequired();
189b9381c790eef869ebd904c92bd019886600fc814Yorke Lee        return new PhoneAccountHandle(component, accountId);
190b9381c790eef869ebd904c92bd019886600fc814Yorke Lee    }
191b9381c790eef869ebd904c92bd019886600fc814Yorke Lee
192db6da486217daa3418944cf93086a3bae549dad2Yorke Lee    private ComponentName parseComponentName(String component) {
193db6da486217daa3418944cf93086a3bae549dad2Yorke Lee        ComponentName cn = ComponentName.unflattenFromString(component);
194db6da486217daa3418944cf93086a3bae549dad2Yorke Lee        if (cn == null) {
195db6da486217daa3418944cf93086a3bae549dad2Yorke Lee            throw new IllegalArgumentException ("Invalid component " + component);
196db6da486217daa3418944cf93086a3bae549dad2Yorke Lee        }
197db6da486217daa3418944cf93086a3bae549dad2Yorke Lee        return cn;
198db6da486217daa3418944cf93086a3bae549dad2Yorke Lee    }
199db6da486217daa3418944cf93086a3bae549dad2Yorke Lee}