• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 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.android.commands.telecom;
18 
19 import android.content.ComponentName;
20 import android.content.Context;
21 import android.net.Uri;
22 import android.os.IUserManager;
23 import android.os.Process;
24 import android.os.RemoteException;
25 import android.os.ServiceManager;
26 import android.os.UserHandle;
27 import android.telecom.PhoneAccount;
28 import android.telecom.PhoneAccountHandle;
29 
30 import com.android.internal.os.BaseCommand;
31 import com.android.internal.telecom.ITelecomService;
32 
33 import java.io.PrintStream;
34 
35 public final class Telecom extends BaseCommand {
36 
37     /**
38      * Command-line entry point.
39      *
40      * @param args The command-line arguments
41      */
main(String[] args)42     public static void main(String[] args) {
43       (new Telecom()).run(args);
44     }
45 
46     private static final String COMMAND_SET_PHONE_ACCOUNT_ENABLED = "set-phone-account-enabled";
47     private static final String COMMAND_SET_PHONE_ACCOUNT_DISABLED = "set-phone-account-disabled";
48     private static final String COMMAND_REGISTER_PHONE_ACCOUNT = "register-phone-account";
49     private static final String COMMAND_REGISTER_SIM_PHONE_ACCOUNT = "register-sim-phone-account";
50     private static final String COMMAND_UNREGISTER_PHONE_ACCOUNT = "unregister-phone-account";
51     private static final String COMMAND_SET_DEFAULT_DIALER = "set-default-dialer";
52     private static final String COMMAND_GET_DEFAULT_DIALER = "get-default-dialer";
53     private static final String COMMAND_GET_SYSTEM_DIALER = "get-system-dialer";
54 
55     private ComponentName mComponent;
56     private String mAccountId;
57     private ITelecomService mTelecomService;
58     private IUserManager mUserManager;
59 
60     @Override
onShowUsage(PrintStream out)61     public void onShowUsage(PrintStream out) {
62         out.println(
63                 "usage: telecom [subcommand] [options]\n" +
64                 "usage: telecom set-phone-account-enabled <COMPONENT> <ID> <USER_SN>\n" +
65                 "usage: telecom set-phone-account-disabled <COMPONENT> <ID> <USER_SN>\n" +
66                 "usage: telecom register-phone-account <COMPONENT> <ID> <USER_SN> <LABEL>\n" +
67                 "usage: telecom register-sim-phone-account <COMPONENT> <ID> <USER_SN> <LABEL> <ADDRESS>\n" +
68                 "usage: telecom unregister-phone-account <COMPONENT> <ID> <USER_SN>\n" +
69                 "usage: telecom set-default-dialer <PACKAGE>\n" +
70                 "usage: telecom get-default-dialer\n" +
71                 "usage: telecom get-system-dialer\n" +
72                 "\n" +
73                 "telecom set-phone-account-enabled: Enables the given phone account, if it has \n" +
74                 " already been registered with Telecom.\n" +
75                 "\n" +
76                 "telecom set-phone-account-disabled: Disables the given phone account, if it \n" +
77                 " has already been registered with telecom.\n" +
78                 "\n" +
79                 "telecom set-default-dialer: Sets the default dialer to the given component. \n" +
80                 "\n" +
81                 "telecom get-default-dialer: Displays the current default dialer. \n" +
82                 "\n" +
83                 "telecom get-system-dialer: Displays the current system dialer. \n"
84                 );
85     }
86 
87     @Override
onRun()88     public void onRun() throws Exception {
89         mTelecomService = ITelecomService.Stub.asInterface(
90                 ServiceManager.getService(Context.TELECOM_SERVICE));
91         if (mTelecomService == null) {
92             showError("Error: Could not access the Telecom Manager. Is the system running?");
93             return;
94         }
95         mUserManager = IUserManager.Stub
96                 .asInterface(ServiceManager.getService(Context.USER_SERVICE));
97         if (mUserManager == null) {
98             showError("Error: Could not access the User Manager. Is the system running?");
99             return;
100         }
101 
102         String command = nextArgRequired();
103         switch (command) {
104             case COMMAND_SET_PHONE_ACCOUNT_ENABLED:
105                 runSetPhoneAccountEnabled(true);
106                 break;
107             case COMMAND_SET_PHONE_ACCOUNT_DISABLED:
108                 runSetPhoneAccountEnabled(false);
109                 break;
110             case COMMAND_REGISTER_PHONE_ACCOUNT:
111                 runRegisterPhoneAccount();
112                 break;
113             case COMMAND_REGISTER_SIM_PHONE_ACCOUNT:
114                 runRegisterSimPhoneAccount();
115                 break;
116             case COMMAND_UNREGISTER_PHONE_ACCOUNT:
117                 runUnregisterPhoneAccount();
118                 break;
119             case COMMAND_SET_DEFAULT_DIALER:
120                 runSetDefaultDialer();
121                 break;
122             case COMMAND_GET_DEFAULT_DIALER:
123                 runGetDefaultDialer();
124                 break;
125             case COMMAND_GET_SYSTEM_DIALER:
126                 runGetSystemDialer();
127                 break;
128             default:
129                 throw new IllegalArgumentException ("unknown command '" + command + "'");
130         }
131     }
132 
runSetPhoneAccountEnabled(boolean enabled)133     private void runSetPhoneAccountEnabled(boolean enabled) throws RemoteException {
134         final PhoneAccountHandle handle = getPhoneAccountHandleFromArgs();
135         final boolean success =  mTelecomService.enablePhoneAccount(handle, enabled);
136         if (success) {
137             System.out.println("Success - " + handle + (enabled ? " enabled." : " disabled."));
138         } else {
139             System.out.println("Error - is " + handle + " a valid PhoneAccount?");
140         }
141     }
142 
runRegisterPhoneAccount()143     private void runRegisterPhoneAccount() throws RemoteException {
144         final PhoneAccountHandle handle = getPhoneAccountHandleFromArgs();
145         final String label = nextArgRequired();
146         PhoneAccount account = PhoneAccount.builder(handle, label)
147                 .setCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER).build();
148         mTelecomService.registerPhoneAccount(account);
149         System.out.println("Success - " + handle + " registered.");
150     }
151 
runRegisterSimPhoneAccount()152     private void runRegisterSimPhoneAccount() throws RemoteException {
153         final PhoneAccountHandle handle = getPhoneAccountHandleFromArgs();
154         final String label = nextArgRequired();
155         final String address = nextArgRequired();
156         PhoneAccount account = PhoneAccount.builder(
157             handle, label)
158             .setAddress(Uri.parse(address))
159             .setSubscriptionAddress(Uri.parse(address))
160             .setCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER |
161                     PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)
162             .setShortDescription(label)
163             .addSupportedUriScheme(PhoneAccount.SCHEME_TEL)
164             .addSupportedUriScheme(PhoneAccount.SCHEME_VOICEMAIL)
165             .build();
166         mTelecomService.registerPhoneAccount(account);
167         System.out.println("Success - " + handle + " registered.");
168     }
169 
runUnregisterPhoneAccount()170     private void runUnregisterPhoneAccount() throws RemoteException {
171         final PhoneAccountHandle handle = getPhoneAccountHandleFromArgs();
172         mTelecomService.unregisterPhoneAccount(handle);
173         System.out.println("Success - " + handle + " unregistered.");
174     }
175 
runSetDefaultDialer()176     private void runSetDefaultDialer() throws RemoteException {
177         final String packageName = nextArgRequired();
178         final boolean success = mTelecomService.setDefaultDialer(packageName);
179         if (success) {
180             System.out.println("Success - " + packageName + " set as default dialer.");
181         } else {
182             System.out.println("Error - " + packageName + " is not an installed Dialer app, \n"
183                     + " or is already the default dialer.");
184         }
185     }
186 
runGetDefaultDialer()187     private void runGetDefaultDialer() throws RemoteException {
188         System.out.println(mTelecomService.getDefaultDialerPackage());
189     }
190 
runGetSystemDialer()191     private void runGetSystemDialer() throws RemoteException {
192         System.out.println(mTelecomService.getSystemDialerPackage());
193     }
194 
getPhoneAccountHandleFromArgs()195     private PhoneAccountHandle getPhoneAccountHandleFromArgs() throws RemoteException{
196         final ComponentName component = parseComponentName(nextArgRequired());
197         final String accountId = nextArgRequired();
198         final String userSnInStr = nextArgRequired();
199         UserHandle userHandle;
200         try {
201             final int userSn = Integer.parseInt(userSnInStr);
202             userHandle = UserHandle.of(mUserManager.getUserHandle(userSn));
203         } catch (NumberFormatException ex) {
204             throw new IllegalArgumentException ("Invalid user serial number " + userSnInStr);
205         }
206         return new PhoneAccountHandle(component, accountId, userHandle);
207     }
208 
parseComponentName(String component)209     private ComponentName parseComponentName(String component) {
210         ComponentName cn = ComponentName.unflattenFromString(component);
211         if (cn == null) {
212             throw new IllegalArgumentException ("Invalid component " + component);
213         }
214         return cn;
215     }
216 }