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 | |
| 19 | import android.os.Handler; |
| 20 | import android.os.Message; |
| 21 | import android.os.ServiceManager; |
| 22 | |
| 23 | import com.android.internal.telecomm.ITelecommService; |
| 24 | |
| 25 | /** |
| 26 | * Implementation of the ITelecomm interface. |
| 27 | */ |
| 28 | public class TelecommServiceImpl extends ITelecommService.Stub { |
| 29 | private static final String TAG = TelecommServiceImpl.class.getSimpleName(); |
| 30 | |
| 31 | private static final String SERVICE_NAME = "telecomm"; |
| 32 | |
| 33 | private static final int MSG_SILENCE_RINGER = 1; |
| 34 | |
| 35 | /** The singleton instance. */ |
| 36 | private static TelecommServiceImpl sInstance; |
| 37 | |
| 38 | /** |
| 39 | * A handler that processes messages on the main thread in the phone process. Since many |
| 40 | * of the Phone calls are not thread safe this is needed to shuttle the requests from the |
| 41 | * inbound binder threads to the main thread in the phone process. |
| 42 | */ |
| 43 | private final Handler mHandler = new Handler() { |
| 44 | @Override |
| 45 | public void handleMessage(Message msg) { |
| 46 | switch (msg.what) { |
| 47 | case MSG_SILENCE_RINGER: |
| 48 | silenceRingerInternal(); |
| 49 | break; |
| 50 | } |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | /** |
| 55 | * Initialize the singleton TelecommServiceImpl instance. |
| 56 | * This is only done once, at startup, from TelecommApp.onCreate(). |
| 57 | */ |
| 58 | static TelecommServiceImpl init() { |
| 59 | synchronized (TelecommServiceImpl.class) { |
| 60 | if (sInstance == null) { |
| 61 | sInstance = new TelecommServiceImpl(); |
| 62 | } else { |
| 63 | Log.wtf(TAG, "init() called multiple times! sInstance %s", sInstance); |
| 64 | } |
| 65 | return sInstance; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** Private constructor; @see init() */ |
| 70 | private TelecommServiceImpl() { |
| 71 | publish(); |
| 72 | } |
| 73 | |
| 74 | private void publish() { |
| 75 | Log.d(this, "publish: %s", this); |
| 76 | ServiceManager.addService(SERVICE_NAME, this); |
| 77 | } |
| 78 | |
| 79 | // |
| 80 | // Implementation of the ITelephony interface. |
| 81 | // |
| 82 | |
| 83 | @Override |
| 84 | public void silenceRinger() { |
| 85 | Log.d(this, "silenceRinger"); |
| 86 | // TODO: find a more appropriate permission to check here. |
| 87 | enforceModifyPermission(); |
| 88 | mHandler.sendEmptyMessage(MSG_SILENCE_RINGER); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Internal implemenation of silenceRinger(). |
| 93 | * This should only be called from the main thread of the Phone app. |
| 94 | * @see #silenceRinger |
| 95 | */ |
| 96 | private void silenceRingerInternal() { |
| 97 | CallsManager.getInstance().getRinger().silence(); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Make sure the caller has the MODIFY_PHONE_STATE permission. |
| 102 | * |
| 103 | * @throws SecurityException if the caller does not have the required permission |
| 104 | */ |
| 105 | private void enforceModifyPermission() { |
| 106 | TelecommApp.getInstance().enforceCallingOrSelfPermission( |
| 107 | android.Manifest.permission.MODIFY_PHONE_STATE, null); |
| 108 | } |
| 109 | } |