1/*
2 * Copyright (C) 2017 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.dialer.postcall;
18
19import android.Manifest.permission;
20import android.content.Context;
21import android.content.Intent;
22import android.content.pm.PackageManager;
23import android.net.Uri;
24import android.os.Bundle;
25import android.provider.Settings;
26import android.support.annotation.NonNull;
27import android.support.annotation.Nullable;
28import android.support.v7.app.AppCompatActivity;
29import android.telephony.SmsManager;
30import com.android.dialer.common.Assert;
31import com.android.dialer.common.LogUtil;
32import com.android.dialer.enrichedcall.EnrichedCallComponent;
33import com.android.dialer.enrichedcall.EnrichedCallManager;
34import com.android.dialer.util.PermissionsUtil;
35import com.android.dialer.widget.DialerToolbar;
36import com.android.dialer.widget.MessageFragment;
37
38/** Activity used to send post call messages after a phone call. */
39public class PostCallActivity extends AppCompatActivity implements MessageFragment.Listener {
40
41  public static final String KEY_PHONE_NUMBER = "phone_number";
42  public static final String KEY_MESSAGE = "message";
43  public static final String KEY_RCS_POST_CALL = "rcs_post_call";
44  private static final int REQUEST_CODE_SEND_SMS = 1;
45
46  private boolean useRcs;
47
48  public static Intent newIntent(
49      @NonNull Context context, @NonNull String number, boolean isRcsPostCall) {
50    Intent intent = new Intent(Assert.isNotNull(context), PostCallActivity.class);
51    intent.putExtra(KEY_PHONE_NUMBER, Assert.isNotNull(number));
52    intent.putExtra(KEY_RCS_POST_CALL, isRcsPostCall);
53    return intent;
54  }
55
56  @Override
57  protected void onCreate(@Nullable Bundle bundle) {
58    super.onCreate(bundle);
59    setContentView(R.layout.post_call_activity);
60
61    ((DialerToolbar) findViewById(R.id.toolbar)).setTitle(R.string.post_call_message);
62    useRcs = getIntent().getBooleanExtra(KEY_RCS_POST_CALL, false);
63    LogUtil.i("PostCallActivity.onCreate", "useRcs: %b", useRcs);
64
65    int postCallCharLimit =
66        useRcs
67            ? getResources().getInteger(R.integer.post_call_char_limit)
68            : MessageFragment.NO_CHAR_LIMIT;
69    String[] messages =
70        new String[] {
71          getString(R.string.post_call_message_1),
72          getString(R.string.post_call_message_2),
73          getString(R.string.post_call_message_3)
74        };
75    MessageFragment fragment =
76        MessageFragment.builder()
77            .setCharLimit(postCallCharLimit)
78            .showSendIcon()
79            .setMessages(messages)
80            .build();
81    getSupportFragmentManager()
82        .beginTransaction()
83        .replace(R.id.message_container, fragment)
84        .commit();
85  }
86
87  @Override
88  public void onMessageFragmentSendMessage(@NonNull String message) {
89    String number = Assert.isNotNull(getIntent().getStringExtra(KEY_PHONE_NUMBER));
90    getIntent().putExtra(KEY_MESSAGE, message);
91
92    if (useRcs) {
93      LogUtil.i("PostCallActivity.onMessageFragmentSendMessage", "sending post call Rcs.");
94      getEnrichedCallManager().sendPostCallNote(number, message);
95      PostCall.onMessageSent(this, number);
96      finish();
97    } else if (PermissionsUtil.hasPermission(this, permission.SEND_SMS)) {
98      LogUtil.i("PostCallActivity.sendMessage", "Sending post call SMS.");
99      SmsManager smsManager = SmsManager.getDefault();
100      smsManager.sendMultipartTextMessage(
101          number, null, smsManager.divideMessage(message), null, null);
102      PostCall.onMessageSent(this, number);
103      finish();
104    } else if (PermissionsUtil.isFirstRequest(this, permission.SEND_SMS)
105        || shouldShowRequestPermissionRationale(permission.SEND_SMS)) {
106      LogUtil.i("PostCallActivity.sendMessage", "Requesting SMS_SEND permission.");
107      requestPermissions(new String[] {permission.SEND_SMS}, REQUEST_CODE_SEND_SMS);
108    } else {
109      LogUtil.i(
110          "PostCallActivity.sendMessage", "Permission permanently denied, sending to settings.");
111      Intent intent = new Intent(Intent.ACTION_VIEW);
112      intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
113      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
114      intent.setData(Uri.parse("package:" + this.getPackageName()));
115      startActivity(intent);
116    }
117  }
118
119  @Override
120  public void onMessageFragmentAfterTextChange(String message) {}
121
122  @Override
123  public void onRequestPermissionsResult(
124      int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
125    if (permissions.length > 0 && permissions[0].equals(permission.SEND_SMS)) {
126      PermissionsUtil.permissionRequested(this, permissions[0]);
127    }
128    if (requestCode == REQUEST_CODE_SEND_SMS
129        && grantResults.length > 0
130        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
131      onMessageFragmentSendMessage(getIntent().getStringExtra(KEY_MESSAGE));
132    }
133  }
134
135  @NonNull
136  private EnrichedCallManager getEnrichedCallManager() {
137    return EnrichedCallComponent.get(this).getEnrichedCallManager();
138  }
139}
140