Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 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.telecomm; |
| 18 | |
Ihab Awad | 6050946 | 2014-06-14 16:43:08 -0700 | [diff] [blame] | 19 | import com.google.android.collect.Lists; |
| 20 | |
| 21 | import com.android.internal.telecomm.ITelecommService; |
| 22 | |
Yorke Lee | ceb96c9 | 2014-06-11 16:34:44 -0700 | [diff] [blame] | 23 | import android.content.ComponentName; |
Yorke Lee | ceb96c9 | 2014-06-11 16:34:44 -0700 | [diff] [blame] | 24 | import android.content.res.Resources; |
Ihab Awad | 6050946 | 2014-06-14 16:43:08 -0700 | [diff] [blame] | 25 | import android.net.Uri; |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 26 | import android.os.Handler; |
| 27 | import android.os.Message; |
| 28 | import android.os.ServiceManager; |
Ihab Awad | 6050946 | 2014-06-14 16:43:08 -0700 | [diff] [blame] | 29 | import android.telecomm.Subscription; |
Yorke Lee | ceb96c9 | 2014-06-11 16:34:44 -0700 | [diff] [blame] | 30 | import android.text.TextUtils; |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 31 | |
Ihab Awad | 6050946 | 2014-06-14 16:43:08 -0700 | [diff] [blame] | 32 | import java.util.List; |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Implementation of the ITelecomm interface. |
| 36 | */ |
| 37 | public 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 Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 43 | private static final int MSG_SHOW_CALL_SCREEN = 2; |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 44 | |
| 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 Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 60 | case MSG_SHOW_CALL_SCREEN: |
| 61 | showCallScreenInternal(msg.arg1 == 1); |
| 62 | break; |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | }; |
| 66 | |
Santos Cordon | 7da72ef | 2014-06-25 15:50:22 -0700 | [diff] [blame] | 67 | /** Private constructor; @see init() */ |
| 68 | private TelecommServiceImpl() { |
| 69 | publish(); |
| 70 | } |
| 71 | |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 72 | /** |
| 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 Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 87 | // |
| 88 | // Implementation of the ITelephony interface. |
| 89 | // |
| 90 | |
| 91 | @Override |
Santos Cordon | 7da72ef | 2014-06-25 15:50:22 -0700 | [diff] [blame] | 92 | 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 Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 109 | 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 Cordon | 7da72ef | 2014-06-25 15:50:22 -0700 | [diff] [blame] | 116 | @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 Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 128 | /** |
| 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 Cordon | f3671a6 | 2014-05-29 21:51:53 -0700 | [diff] [blame] | 146 | |
| 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 Lee | ceb96c9 | 2014-06-11 16:34:44 -0700 | [diff] [blame] | 155 | |
Ihab Awad | 6050946 | 2014-06-14 16:43:08 -0700 | [diff] [blame] | 156 | // 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 Cordon | 7da72ef | 2014-06-25 15:50:22 -0700 | [diff] [blame] | 201 | private void publish() { |
| 202 | Log.d(this, "publish: %s", this); |
| 203 | ServiceManager.addService(SERVICE_NAME, this); |
Ihab Awad | 6050946 | 2014-06-14 16:43:08 -0700 | [diff] [blame] | 204 | } |
Santos Cordon | b64c150 | 2014-05-21 21:21:49 -0700 | [diff] [blame] | 205 | } |