summaryrefslogtreecommitdiffstats
path: root/java/com/android/dialer/shortcuts/CallContactActivity.java
blob: d2c9a760dfd4bbc5d251f2f2da297061bef5291f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.dialer.shortcuts;

import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.widget.Toast;
import com.android.dialer.callintent.nano.CallInitiationType;
import com.android.dialer.callintent.nano.CallSpecificAppData;
import com.android.dialer.common.LogUtil;
import com.android.dialer.interactions.PhoneNumberInteraction;
import com.android.dialer.interactions.PhoneNumberInteraction.InteractionErrorCode;
import com.android.dialer.util.TransactionSafeActivity;

/**
 * Invisible activity launched when a shortcut is selected by user. Calls a contact based on URI.
 */
public class CallContactActivity extends TransactionSafeActivity
    implements PhoneNumberInteraction.DisambigDialogDismissedListener,
        PhoneNumberInteraction.InteractionErrorListener,
        ActivityCompat.OnRequestPermissionsResultCallback {

  private static final String CONTACT_URI_KEY = "uri_key";

  private Uri contactUri;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if ("com.android.dialer.shortcuts.CALL_CONTACT".equals(getIntent().getAction())) {
      if (Shortcuts.areDynamicShortcutsEnabled(this)) {
        LogUtil.i("CallContactActivity.onCreate", "shortcut clicked");
        contactUri = getIntent().getData();
        makeCall();
      } else {
        LogUtil.i("CallContactActivity.onCreate", "dynamic shortcuts disabled");
        finish();
      }
    }
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    LogUtil.enterBlock("CallContactActivity.onDestroy");
  }

  /**
   * Attempt to make a call, finishing the activity if the required permissions are already granted.
   * If the required permissions are not already granted, the activity is not finished so that the
   * user can choose to grant or deny them.
   */
  private void makeCall() {
    CallSpecificAppData callSpecificAppData = new CallSpecificAppData();
    callSpecificAppData.callInitiationType = CallInitiationType.Type.LAUNCHER_SHORTCUT;
    PhoneNumberInteraction.startInteractionForPhoneCall(
        this, contactUri, false /* isVideoCall */, callSpecificAppData);
  }

  @Override
  public void onDisambigDialogDismissed() {
    finish();
  }

  @Override
  public void interactionError(@InteractionErrorCode int interactionErrorCode) {
    // Note: There is some subtlety to how contact lookup keys work that make it difficult to
    // distinguish the case of the contact missing from the case of the a contact not having a
    // number. For example, if a contact's phone number is deleted, subsequent lookups based on
    // lookup key will actually return no results because the phone number was part of the
    // lookup key. In this case, it would be inaccurate to say the contact can't be found though, so
    // in all cases we just say the contact can't be found or the contact doesn't have a number.
    switch (interactionErrorCode) {
      case InteractionErrorCode.CONTACT_NOT_FOUND:
      case InteractionErrorCode.CONTACT_HAS_NO_NUMBER:
        Toast.makeText(
                this,
                R.string.dialer_shortcut_contact_not_found_or_has_no_number,
                Toast.LENGTH_SHORT)
            .show();
        break;
      case InteractionErrorCode.USER_LEAVING_ACTIVITY:
      case InteractionErrorCode.OTHER_ERROR:
      default:
        // If the user is leaving the activity or the error code was "other" there's no useful
        // information to display but we still need to finish this invisible activity.
        break;
    }
    finish();
  }

  @Override
  public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putParcelable(CONTACT_URI_KEY, contactUri);
  }

  @Override
  public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    if (savedInstanceState == null) {
      return;
    }
    contactUri = savedInstanceState.getParcelable(CONTACT_URI_KEY);
  }

  @Override
  public void onRequestPermissionsResult(
      int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
      case PhoneNumberInteraction.REQUEST_READ_CONTACTS:
      case PhoneNumberInteraction.REQUEST_CALL_PHONE:
        {
          // If request is cancelled, the result arrays are empty.
          if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            makeCall();
          } else {
            Toast.makeText(this, R.string.dialer_shortcut_no_permissions, Toast.LENGTH_SHORT)
                .show();
            finish();
          }
          break;
        }
      default:
        throw new IllegalStateException("Unsupported request code: " + requestCode);
    }
  }
}