blob: 9793fda1bb1644c04b1f4dacdeca571b632e5224 [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
Ihab Awad60509462014-06-14 16:43:08 -0700132 // TODO (STOPSHIP): Static list of Subscriptions for testing and UX work only.
133
134 private static final ComponentName sComponentName = new ComponentName(
135 "com.android.telecomm",
136 TelecommServiceImpl.class.getName()); // This field is a no-op
137
138 private static final List<Subscription> sSubscriptions = Lists.newArrayList(
139 new Subscription(
140 sComponentName,
141 "subscription0",
142 Uri.parse("tel:999-555-1212"),
143 R.string.test_subscription_0_label,
144 R.string.test_subscription_0_short_description,
145 R.drawable.q_mobile,
146 true,
147 true),
148 new Subscription(
149 sComponentName,
150 "subscription1",
151 Uri.parse("tel:333-111-2222"),
152 R.string.test_subscription_1_label,
153 R.string.test_subscription_1_short_description,
154 R.drawable.market_wireless,
155 true,
156 false),
157 new Subscription(
158 sComponentName,
159 "subscription2",
160 Uri.parse("mailto:two@example.com"),
161 R.string.test_subscription_2_label,
162 R.string.test_subscription_2_short_description,
163 R.drawable.talk_to_your_circles,
164 true,
165 false),
166 new Subscription(
167 sComponentName,
168 "subscription3",
169 Uri.parse("mailto:three@example.com"),
170 R.string.test_subscription_3_label,
171 R.string.test_subscription_3_short_description,
172 R.drawable.chat_with_others,
173 true,
174 false)
175 );
176
177
178
179 @Override
180 public List<Subscription> getSubscriptions() {
181 return sSubscriptions;
182 }
183
184 @Override
185 public void setEnabled(Subscription subscription, boolean enabled) {
186 // Enforce MODIFY_PHONE_STATE ?
187 // TODO
188 }
189
190 @Override
191 public void setSystemDefault(Subscription subscription) {
192 // Enforce MODIFY_PHONE_STATE ?
193 // TODO
194 }
Santos Cordonb64c1502014-05-21 21:21:49 -0700195}