blob: e3f8bcb98c26b8bb56d0e59a1cf8b2d616f0a9fd [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
Yorke Leeceb96c92014-06-11 16:34:44 -070019import android.content.ComponentName;
20import android.content.Context;
21import android.content.res.Resources;
Santos Cordonb64c1502014-05-21 21:21:49 -070022import android.os.Handler;
23import android.os.Message;
24import android.os.ServiceManager;
Yorke Leeceb96c92014-06-11 16:34:44 -070025import android.text.TextUtils;
Santos Cordonb64c1502014-05-21 21:21:49 -070026
27import com.android.internal.telecomm.ITelecommService;
28
29/**
30 * Implementation of the ITelecomm interface.
31 */
32public class TelecommServiceImpl extends ITelecommService.Stub {
33 private static final String TAG = TelecommServiceImpl.class.getSimpleName();
34
35 private static final String SERVICE_NAME = "telecomm";
36
37 private static final int MSG_SILENCE_RINGER = 1;
Santos Cordonf3671a62014-05-29 21:51:53 -070038 private static final int MSG_SHOW_CALL_SCREEN = 2;
Santos Cordonb64c1502014-05-21 21:21:49 -070039
40 /** The singleton instance. */
41 private static TelecommServiceImpl sInstance;
42
43 /**
44 * A handler that processes messages on the main thread in the phone process. Since many
45 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
46 * inbound binder threads to the main thread in the phone process.
47 */
48 private final Handler mHandler = new Handler() {
49 @Override
50 public void handleMessage(Message msg) {
51 switch (msg.what) {
52 case MSG_SILENCE_RINGER:
53 silenceRingerInternal();
54 break;
Santos Cordonf3671a62014-05-29 21:51:53 -070055 case MSG_SHOW_CALL_SCREEN:
56 showCallScreenInternal(msg.arg1 == 1);
57 break;
Santos Cordonb64c1502014-05-21 21:21:49 -070058 }
59 }
60 };
61
62 /**
63 * Initialize the singleton TelecommServiceImpl instance.
64 * This is only done once, at startup, from TelecommApp.onCreate().
65 */
66 static TelecommServiceImpl init() {
67 synchronized (TelecommServiceImpl.class) {
68 if (sInstance == null) {
69 sInstance = new TelecommServiceImpl();
70 } else {
71 Log.wtf(TAG, "init() called multiple times! sInstance %s", sInstance);
72 }
73 return sInstance;
74 }
75 }
76
77 /** Private constructor; @see init() */
78 private TelecommServiceImpl() {
79 publish();
80 }
81
82 private void publish() {
83 Log.d(this, "publish: %s", this);
84 ServiceManager.addService(SERVICE_NAME, this);
85 }
86
87 //
88 // Implementation of the ITelephony interface.
89 //
90
91 @Override
92 public void silenceRinger() {
93 Log.d(this, "silenceRinger");
94 // TODO: find a more appropriate permission to check here.
95 enforceModifyPermission();
96 mHandler.sendEmptyMessage(MSG_SILENCE_RINGER);
97 }
98
99 /**
100 * Internal implemenation of silenceRinger().
101 * This should only be called from the main thread of the Phone app.
102 * @see #silenceRinger
103 */
104 private void silenceRingerInternal() {
105 CallsManager.getInstance().getRinger().silence();
106 }
107
108 /**
109 * Make sure the caller has the MODIFY_PHONE_STATE permission.
110 *
111 * @throws SecurityException if the caller does not have the required permission
112 */
113 private void enforceModifyPermission() {
114 TelecommApp.getInstance().enforceCallingOrSelfPermission(
115 android.Manifest.permission.MODIFY_PHONE_STATE, null);
116 }
Santos Cordonf3671a62014-05-29 21:51:53 -0700117
118 @Override
119 public void showCallScreen(boolean showDialpad) {
120 mHandler.obtainMessage(MSG_SHOW_CALL_SCREEN, showDialpad ? 1 : 0, 0).sendToTarget();
121 }
122
123 private void showCallScreenInternal(boolean showDialpad) {
124 CallsManager.getInstance().getInCallController().bringToForeground(showDialpad);
125 }
Yorke Leeceb96c92014-06-11 16:34:44 -0700126
127 @Override
128 public ComponentName getSystemPhoneApplication() {
129 final Resources resources = TelecommApp.getInstance().getResources();
130 final String packageName = resources.getString(R.string.ui_default_package);
131 final String className = resources.getString(R.string.dialer_default_class);
132
133 if (TextUtils.isEmpty(packageName) || TextUtils.isEmpty(className)) {
134 return null;
135 }
136
137 return new ComponentName(packageName, className);
138 }
Santos Cordonb64c1502014-05-21 21:21:49 -0700139}