blob: bf2543232f43f11d2248c35f076166f90d5a6391 [file] [log] [blame]
Santos Cordonb64c1502014-05-21 21:21:49 -07001/*
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
17package com.android.telecomm;
18
Ihab Awad60509462014-06-14 16:43:08 -070019import com.google.android.collect.Lists;
20
21import com.android.internal.telecomm.ITelecommService;
22
Yorke Leeceb96c92014-06-11 16:34:44 -070023import android.content.ComponentName;
Yorke Leeceb96c92014-06-11 16:34:44 -070024import android.content.res.Resources;
Ihab Awad60509462014-06-14 16:43:08 -070025import android.net.Uri;
Santos Cordonb64c1502014-05-21 21:21:49 -070026import android.os.Handler;
27import android.os.Message;
28import android.os.ServiceManager;
Ihab Awad60509462014-06-14 16:43:08 -070029import android.telecomm.Subscription;
Yorke Leeceb96c92014-06-11 16:34:44 -070030import android.text.TextUtils;
Santos Cordonb64c1502014-05-21 21:21:49 -070031
Ihab Awad60509462014-06-14 16:43:08 -070032import java.util.List;
Santos Cordonb64c1502014-05-21 21:21:49 -070033
34/**
35 * Implementation of the ITelecomm interface.
36 */
37public class TelecommServiceImpl extends ITelecommService.Stub {
38 private static final String TAG = TelecommServiceImpl.class.getSimpleName();
39
40 private static final String SERVICE_NAME = "telecomm";
41
42 private static final int MSG_SILENCE_RINGER = 1;
Santos Cordonf3671a62014-05-29 21:51:53 -070043 private static final int MSG_SHOW_CALL_SCREEN = 2;
Santos Cordonb64c1502014-05-21 21:21:49 -070044
45 /** The singleton instance. */
46 private static TelecommServiceImpl sInstance;
47
48 /**
49 * A handler that processes messages on the main thread in the phone process. Since many
50 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
51 * inbound binder threads to the main thread in the phone process.
52 */
53 private final Handler mHandler = new Handler() {
54 @Override
55 public void handleMessage(Message msg) {
56 switch (msg.what) {
57 case MSG_SILENCE_RINGER:
58 silenceRingerInternal();
59 break;
Santos Cordonf3671a62014-05-29 21:51:53 -070060 case MSG_SHOW_CALL_SCREEN:
61 showCallScreenInternal(msg.arg1 == 1);
62 break;
Santos Cordonb64c1502014-05-21 21:21:49 -070063 }
64 }
65 };
66
Santos Cordon7da72ef2014-06-25 15:50:22 -070067 /** Private constructor; @see init() */
68 private TelecommServiceImpl() {
69 publish();
70 }
71
Santos Cordonb64c1502014-05-21 21:21:49 -070072 /**
73 * Initialize the singleton TelecommServiceImpl instance.
74 * This is only done once, at startup, from TelecommApp.onCreate().
75 */
76 static TelecommServiceImpl init() {
77 synchronized (TelecommServiceImpl.class) {
78 if (sInstance == null) {
79 sInstance = new TelecommServiceImpl();
80 } else {
81 Log.wtf(TAG, "init() called multiple times! sInstance %s", sInstance);
82 }
83 return sInstance;
84 }
85 }
86
Santos Cordonb64c1502014-05-21 21:21:49 -070087 //
88 // Implementation of the ITelephony interface.
89 //
90
91 @Override
Santos Cordon7da72ef2014-06-25 15:50:22 -070092 public List<Subscription> getSubscriptions() {
93 return sSubscriptions;
94 }
95
96 @Override
97 public void setEnabled(Subscription subscription, boolean enabled) {
98 // Enforce MODIFY_PHONE_STATE ?
99 // TODO
100 }
101
102 @Override
103 public void setSystemDefault(Subscription subscription) {
104 // Enforce MODIFY_PHONE_STATE ?
105 // TODO
106 }
107
108 @Override
Santos Cordonb64c1502014-05-21 21:21:49 -0700109 public void silenceRinger() {
110 Log.d(this, "silenceRinger");
111 // TODO: find a more appropriate permission to check here.
112 enforceModifyPermission();
113 mHandler.sendEmptyMessage(MSG_SILENCE_RINGER);
114 }
115
Santos Cordon7da72ef2014-06-25 15:50:22 -0700116 @Override
117 public ComponentName getDefaultPhoneApp() {
118 Resources resources = TelecommApp.getInstance().getResources();
119 return new ComponentName(
120 resources.getString(R.string.ui_default_package),
121 resources.getString(R.string.dialer_default_class));
122 }
123
124 //
125 // Supporting methods for the ITelephony interface implementation.
126 //
127
Santos Cordonb64c1502014-05-21 21:21:49 -0700128 /**
129 * Internal implemenation of silenceRinger().
130 * This should only be called from the main thread of the Phone app.
131 * @see #silenceRinger
132 */
133 private void silenceRingerInternal() {
134 CallsManager.getInstance().getRinger().silence();
135 }
136
137 /**
138 * Make sure the caller has the MODIFY_PHONE_STATE permission.
139 *
140 * @throws SecurityException if the caller does not have the required permission
141 */
142 private void enforceModifyPermission() {
143 TelecommApp.getInstance().enforceCallingOrSelfPermission(
144 android.Manifest.permission.MODIFY_PHONE_STATE, null);
145 }
Santos Cordonf3671a62014-05-29 21:51:53 -0700146
147 @Override
148 public void showCallScreen(boolean showDialpad) {
149 mHandler.obtainMessage(MSG_SHOW_CALL_SCREEN, showDialpad ? 1 : 0, 0).sendToTarget();
150 }
151
152 private void showCallScreenInternal(boolean showDialpad) {
153 CallsManager.getInstance().getInCallController().bringToForeground(showDialpad);
154 }
Yorke Leeceb96c92014-06-11 16:34:44 -0700155
Ihab Awad60509462014-06-14 16:43:08 -0700156 // TODO (STOPSHIP): Static list of Subscriptions for testing and UX work only.
157
158 private static final ComponentName sComponentName = new ComponentName(
159 "com.android.telecomm",
160 TelecommServiceImpl.class.getName()); // This field is a no-op
161
162 private static final List<Subscription> sSubscriptions = Lists.newArrayList(
163 new Subscription(
164 sComponentName,
165 "subscription0",
166 Uri.parse("tel:999-555-1212"),
167 R.string.test_subscription_0_label,
168 R.string.test_subscription_0_short_description,
169 R.drawable.q_mobile,
170 true,
171 true),
172 new Subscription(
173 sComponentName,
174 "subscription1",
175 Uri.parse("tel:333-111-2222"),
176 R.string.test_subscription_1_label,
177 R.string.test_subscription_1_short_description,
178 R.drawable.market_wireless,
179 true,
180 false),
181 new Subscription(
182 sComponentName,
183 "subscription2",
184 Uri.parse("mailto:two@example.com"),
185 R.string.test_subscription_2_label,
186 R.string.test_subscription_2_short_description,
187 R.drawable.talk_to_your_circles,
188 true,
189 false),
190 new Subscription(
191 sComponentName,
192 "subscription3",
193 Uri.parse("mailto:three@example.com"),
194 R.string.test_subscription_3_label,
195 R.string.test_subscription_3_short_description,
196 R.drawable.chat_with_others,
197 true,
198 false)
199 );
200
Santos Cordon7da72ef2014-06-25 15:50:22 -0700201 private void publish() {
202 Log.d(this, "publish: %s", this);
203 ServiceManager.addService(SERVICE_NAME, this);
Ihab Awad60509462014-06-14 16:43:08 -0700204 }
Santos Cordonb64c1502014-05-21 21:21:49 -0700205}