• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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.server.telecom;
18 
19 // TODO: Needed for move to system service: import com.android.internal.R;
20 import com.android.internal.os.SomeArgs;
21 import com.android.internal.telephony.PhoneConstants;
22 import com.android.internal.telephony.SmsApplication;
23 
24 import android.content.ComponentName;
25 import android.content.Context;
26 import android.content.Intent;
27 import android.content.SharedPreferences;
28 import android.content.res.Resources;
29 import android.net.Uri;
30 import android.os.Handler;
31 import android.os.Looper;
32 import android.os.Message;
33 import android.telecom.Response;
34 import android.telephony.SubscriptionManager;
35 import android.telephony.TelephonyManager;
36 import android.widget.Toast;
37 
38 import java.util.ArrayList;
39 import java.util.List;
40 
41 /**
42  * Helper class to manage the "Respond via Message" feature for incoming calls.
43  */
44 public class RespondViaSmsManager extends CallsManagerListenerBase {
45     private static final int MSG_SHOW_SENT_TOAST = 2;
46 
47     private final CallsManager mCallsManager;
48     private final TelecomSystem.SyncRoot mLock;
49 
50     private final Handler mHandler = new Handler(Looper.getMainLooper()) {
51         @Override
52         public void handleMessage(Message msg) {
53             switch (msg.what) {
54                 case MSG_SHOW_SENT_TOAST: {
55                     SomeArgs args = (SomeArgs) msg.obj;
56                     try {
57                         String toastMessage = (String) args.arg1;
58                         Context context = (Context) args.arg2;
59                         showMessageSentToast(toastMessage, context);
60                     } finally {
61                         args.recycle();
62                     }
63                     break;
64                 }
65             }
66         }
67     };
68 
RespondViaSmsManager(CallsManager callsManager, TelecomSystem.SyncRoot lock)69     public RespondViaSmsManager(CallsManager callsManager, TelecomSystem.SyncRoot lock) {
70         mCallsManager = callsManager;
71         mLock = lock;
72     }
73 
74     /**
75      * Read the (customizable) canned responses from SharedPreferences,
76      * or from defaults if the user has never actually brought up
77      * the Settings UI.
78      *
79      * The interface of this method is asynchronous since it does disk I/O.
80      *
81      * @param response An object to receive an async reply, which will be called from
82      *                 the main thread.
83      * @param context The context.
84      */
loadCannedTextMessages(final Response<Void, List<String>> response, final Context context)85     public void loadCannedTextMessages(final Response<Void, List<String>> response,
86             final Context context) {
87         new Thread() {
88             @Override
89             public void run() {
90                 Log.d(RespondViaSmsManager.this, "loadCannedResponses() starting");
91 
92                 // This function guarantees that QuickResponses will be in our
93                 // SharedPreferences with the proper values considering there may be
94                 // old QuickResponses in Telephony pre L.
95                 QuickResponseUtils.maybeMigrateLegacyQuickResponses(context);
96 
97                 final SharedPreferences prefs = context.getSharedPreferences(
98                         QuickResponseUtils.SHARED_PREFERENCES_NAME,
99                         Context.MODE_PRIVATE | Context.MODE_MULTI_PROCESS);
100                 final Resources res = context.getResources();
101 
102                 final ArrayList<String> textMessages = new ArrayList<>(
103                         QuickResponseUtils.NUM_CANNED_RESPONSES);
104 
105                 // Note the default values here must agree with the corresponding
106                 // android:defaultValue attributes in respond_via_sms_settings.xml.
107                 textMessages.add(0, prefs.getString(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_1,
108                         res.getString(R.string.respond_via_sms_canned_response_1)));
109                 textMessages.add(1, prefs.getString(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_2,
110                         res.getString(R.string.respond_via_sms_canned_response_2)));
111                 textMessages.add(2, prefs.getString(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_3,
112                         res.getString(R.string.respond_via_sms_canned_response_3)));
113                 textMessages.add(3, prefs.getString(QuickResponseUtils.KEY_CANNED_RESPONSE_PREF_4,
114                         res.getString(R.string.respond_via_sms_canned_response_4)));
115 
116                 Log.d(RespondViaSmsManager.this,
117                         "loadCannedResponses() completed, found responses: %s",
118                         textMessages.toString());
119 
120                 synchronized (mLock) {
121                     response.onResult(null, textMessages);
122                 }
123             }
124         }.start();
125     }
126 
127     @Override
onIncomingCallRejected(Call call, boolean rejectWithMessage, String textMessage)128     public void onIncomingCallRejected(Call call, boolean rejectWithMessage, String textMessage) {
129         if (rejectWithMessage && call.getHandle() != null) {
130             int subId = mCallsManager.getPhoneAccountRegistrar().getSubscriptionIdForPhoneAccount(
131                     call.getTargetPhoneAccount());
132             rejectCallWithMessage(call.getContext(), call.getHandle().getSchemeSpecificPart(),
133                     textMessage, subId);
134         }
135     }
136 
showMessageSentToast(final String phoneNumber, final Context context)137     private void showMessageSentToast(final String phoneNumber, final Context context) {
138         // ...and show a brief confirmation to the user (since
139         // otherwise it's hard to be sure that anything actually
140         // happened.)
141         final Resources res = context.getResources();
142         final String formatString = res.getString(
143                 R.string.respond_via_sms_confirmation_format);
144         final String confirmationMsg = String.format(formatString, phoneNumber);
145         Toast.makeText(context, confirmationMsg,
146                 Toast.LENGTH_LONG).show();
147 
148         // TODO: If the device is locked, this toast won't actually ever
149         // be visible!  (That's because we're about to dismiss the call
150         // screen, which means that the device will return to the
151         // keyguard.  But toasts aren't visible on top of the keyguard.)
152         // Possible fixes:
153         // (1) Is it possible to allow a specific Toast to be visible
154         //     on top of the keyguard?
155         // (2) Artificially delay the dismissCallScreen() call by 3
156         //     seconds to allow the toast to be seen?
157         // (3) Don't use a toast at all; instead use a transient state
158         //     of the InCallScreen (perhaps via the InCallUiState
159         //     progressIndication feature), and have that state be
160         //     visible for 3 seconds before calling dismissCallScreen().
161     }
162 
163     /**
164      * Reject the call with the specified message. If message is null this call is ignored.
165      */
rejectCallWithMessage(Context context, String phoneNumber, String textMessage, int subId)166     private void rejectCallWithMessage(Context context, String phoneNumber, String textMessage,
167             int subId) {
168         if (textMessage != null) {
169             final ComponentName component =
170                     SmsApplication.getDefaultRespondViaMessageApplication(context,
171                             true /*updateIfNeeded*/);
172             if (component != null) {
173                 // Build and send the intent
174                 final Uri uri = Uri.fromParts(Constants.SCHEME_SMSTO, phoneNumber, null);
175                 final Intent intent = new Intent(TelephonyManager.ACTION_RESPOND_VIA_MESSAGE, uri);
176                 intent.putExtra(Intent.EXTRA_TEXT, textMessage);
177                 if (SubscriptionManager.isValidSubscriptionId(subId)) {
178                     intent.putExtra(PhoneConstants.SUBSCRIPTION_KEY, subId);
179                 }
180 
181                 SomeArgs args = SomeArgs.obtain();
182                 args.arg1 = phoneNumber;
183                 args.arg2 = context;
184                 mHandler.obtainMessage(MSG_SHOW_SENT_TOAST, args).sendToTarget();
185                 intent.setComponent(component);
186                 context.startService(intent);
187             }
188         }
189     }
190 }
191