blob: ef26a336dc4f4c387a7276c4c0bea17fe06964f4 [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
67 /**
68 * Initialize the singleton TelecommServiceImpl instance.
69 * This is only done once, at startup, from TelecommApp.onCreate().
70 */
71 static TelecommServiceImpl init() {
72 synchronized (TelecommServiceImpl.class) {
73 if (sInstance == null) {
74 sInstance = new TelecommServiceImpl();
75 } else {
76 Log.wtf(TAG, "init() called multiple times! sInstance %s", sInstance);
77 }
78 return sInstance;
79 }
80 }
81
82 /** Private constructor; @see init() */
83 private TelecommServiceImpl() {
84 publish();
85 }
86
87 private void publish() {
88 Log.d(this, "publish: %s", this);
89 ServiceManager.addService(SERVICE_NAME, this);
90 }
91
92 //
93 // Implementation of the ITelephony interface.
94 //
95
96 @Override
97 public void silenceRinger() {
98 Log.d(this, "silenceRinger");
99 // TODO: find a more appropriate permission to check here.
100 enforceModifyPermission();
101 mHandler.sendEmptyMessage(MSG_SILENCE_RINGER);
102 }
103
104 /**
105 * Internal implemenation of silenceRinger().
106 * This should only be called from the main thread of the Phone app.
107 * @see #silenceRinger
108 */
109 private void silenceRingerInternal() {
110 CallsManager.getInstance().getRinger().silence();
111 }
112
113 /**
114 * Make sure the caller has the MODIFY_PHONE_STATE permission.
115 *
116 * @throws SecurityException if the caller does not have the required permission
117 */
118 private void enforceModifyPermission() {
119 TelecommApp.getInstance().enforceCallingOrSelfPermission(
120 android.Manifest.permission.MODIFY_PHONE_STATE, null);
121 }
Santos Cordonf3671a62014-05-29 21:51:53 -0700122
123 @Override
124 public void showCallScreen(boolean showDialpad) {
125 mHandler.obtainMessage(MSG_SHOW_CALL_SCREEN, showDialpad ? 1 : 0, 0).sendToTarget();
126 }
127
128 private void showCallScreenInternal(boolean showDialpad) {
129 CallsManager.getInstance().getInCallController().bringToForeground(showDialpad);
130 }
Yorke Leeceb96c92014-06-11 16:34:44 -0700131
132 @Override
133 public ComponentName getSystemPhoneApplication() {
134 final Resources resources = TelecommApp.getInstance().getResources();
135 final String packageName = resources.getString(R.string.ui_default_package);
136 final String className = resources.getString(R.string.dialer_default_class);
137
138 if (TextUtils.isEmpty(packageName) || TextUtils.isEmpty(className)) {
139 return null;
140 }
141
142 return new ComponentName(packageName, className);
143 }
Ihab Awad60509462014-06-14 16:43:08 -0700144
145 // TODO (STOPSHIP): Static list of Subscriptions for testing and UX work only.
146
147 private static final ComponentName sComponentName = new ComponentName(
148 "com.android.telecomm",
149 TelecommServiceImpl.class.getName()); // This field is a no-op
150
151 private static final List<Subscription> sSubscriptions = Lists.newArrayList(
152 new Subscription(
153 sComponentName,
154 "subscription0",
155 Uri.parse("tel:999-555-1212"),
156 R.string.test_subscription_0_label,
157 R.string.test_subscription_0_short_description,
158 R.drawable.q_mobile,
159 true,
160 true),
161 new Subscription(
162 sComponentName,
163 "subscription1",
164 Uri.parse("tel:333-111-2222"),
165 R.string.test_subscription_1_label,
166 R.string.test_subscription_1_short_description,
167 R.drawable.market_wireless,
168 true,
169 false),
170 new Subscription(
171 sComponentName,
172 "subscription2",
173 Uri.parse("mailto:two@example.com"),
174 R.string.test_subscription_2_label,
175 R.string.test_subscription_2_short_description,
176 R.drawable.talk_to_your_circles,
177 true,
178 false),
179 new Subscription(
180 sComponentName,
181 "subscription3",
182 Uri.parse("mailto:three@example.com"),
183 R.string.test_subscription_3_label,
184 R.string.test_subscription_3_short_description,
185 R.drawable.chat_with_others,
186 true,
187 false)
188 );
189
190
191
192 @Override
193 public List<Subscription> getSubscriptions() {
194 return sSubscriptions;
195 }
196
197 @Override
198 public void setEnabled(Subscription subscription, boolean enabled) {
199 // Enforce MODIFY_PHONE_STATE ?
200 // TODO
201 }
202
203 @Override
204 public void setSystemDefault(Subscription subscription) {
205 // Enforce MODIFY_PHONE_STATE ?
206 // TODO
207 }
Santos Cordonb64c1502014-05-21 21:21:49 -0700208}