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