• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.server.telecom.testapps;
18 
19 import android.app.Activity;
20 import android.net.Uri;
21 import android.os.Bundle;
22 import android.telecom.ConnectionRequest;
23 import android.telecom.PhoneAccountHandle;
24 import android.telecom.TelecomManager;
25 import android.telecom.VideoProfile;
26 import android.util.Log;
27 import android.view.View;
28 import android.view.WindowManager;
29 import android.widget.Button;
30 import android.widget.CheckBox;
31 import android.widget.EditText;
32 import android.widget.ListView;
33 import android.widget.RadioButton;
34 import android.widget.Toast;
35 
36 import com.android.server.telecom.testapps.R;
37 
38 import java.util.Objects;
39 
40 /**
41  * Provides a sample third-party calling app UX which implements the self managed connection service
42  * APIs.
43  */
44 public class SelfManagedCallingActivity extends Activity {
45     private static final String TAG = "SelfMgCallActivity";
46     private SelfManagedCallList mCallList = SelfManagedCallList.getInstance();
47     private CheckBox mCheckIfPermittedBeforeCalling;
48     private Button mPlaceOutgoingCallButton;
49     private Button mPlaceIncomingCallButton;
50     private RadioButton mUseAcct1Button;
51     private RadioButton mUseAcct2Button;
52     private RadioButton mVideoCallButton;
53     private RadioButton mAudioCallButton;
54     private EditText mNumber;
55     private ListView mListView;
56     private SelfManagedCallListAdapter mListAdapter;
57 
58     private SelfManagedCallList.Listener mCallListListener = new SelfManagedCallList.Listener() {
59         @Override
60         public void onCreateIncomingConnectionFailed(ConnectionRequest request) {
61             Log.i(TAG, "onCreateIncomingConnectionFailed " + request);
62             Toast.makeText(SelfManagedCallingActivity.this,
63                     R.string.incomingCallNotPermittedCS , Toast.LENGTH_SHORT).show();
64         };
65 
66         @Override
67         public void onCreateOutgoingConnectionFailed(ConnectionRequest request) {
68             Log.i(TAG, "onCreateOutgoingConnectionFailed " + request);
69             Toast.makeText(SelfManagedCallingActivity.this,
70                     R.string.outgoingCallNotPermittedCS , Toast.LENGTH_SHORT).show();
71         };
72 
73         @Override
74         public void onConnectionListChanged() {
75             Log.i(TAG, "onConnectionListChanged");
76             mListAdapter.updateConnections();
77         };
78     };
79 
80     @Override
onCreate(Bundle savedInstanceState)81     public void onCreate(Bundle savedInstanceState) {
82         super.onCreate(savedInstanceState);
83         int flags =
84                 WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
85                         | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
86                         | WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES;
87 
88         getWindow().addFlags(flags);
89         setContentView(R.layout.self_managed_sample_main);
90         mCheckIfPermittedBeforeCalling = (CheckBox) findViewById(
91                 R.id.checkIfPermittedBeforeCalling);
92         mPlaceOutgoingCallButton = (Button) findViewById(R.id.placeOutgoingCallButton);
93         mPlaceOutgoingCallButton.setOnClickListener(new View.OnClickListener() {
94             @Override
95             public void onClick(View v) {
96                 placeOutgoingCall();
97             }
98         });
99         mPlaceIncomingCallButton = (Button) findViewById(R.id.placeIncomingCallButton);
100         mPlaceIncomingCallButton.setOnClickListener(new View.OnClickListener() {
101             @Override
102             public void onClick(View v) {
103                 placeIncomingCall();
104             }
105         });
106 
107         mUseAcct1Button = (RadioButton) findViewById(R.id.useAcct1Button);
108         mUseAcct2Button = (RadioButton) findViewById(R.id.useAcct2Button);
109         mVideoCallButton = (RadioButton) findViewById(R.id.videoCallButton);
110         mAudioCallButton = (RadioButton) findViewById(R.id.audioCallButton);
111         mNumber = (EditText) findViewById(R.id.phoneNumber);
112         mListView = (ListView) findViewById(R.id.callList);
113         mCallList.setListener(mCallListListener);
114         mCallList.registerPhoneAccounts(this);
115         mListAdapter = new SelfManagedCallListAdapter(getLayoutInflater(),
116                 mCallList.getConnections());
117         mListView.setAdapter(mListAdapter);
118         Log.i(TAG, "onCreate - mCallList id " + Objects.hashCode(mCallList));
119     }
120 
getSelectedPhoneAccountHandle()121     private PhoneAccountHandle getSelectedPhoneAccountHandle() {
122         if (mUseAcct1Button.isChecked()) {
123             return mCallList.getPhoneAccountHandle(SelfManagedCallList.SELF_MANAGED_ACCOUNT_1);
124         } else if (mUseAcct2Button.isChecked()) {
125             return mCallList.getPhoneAccountHandle(SelfManagedCallList.SELF_MANAGED_ACCOUNT_2);
126         }
127         return null;
128     }
129 
placeOutgoingCall()130     private void placeOutgoingCall() {
131         TelecomManager tm = TelecomManager.from(this);
132         PhoneAccountHandle phoneAccountHandle = getSelectedPhoneAccountHandle();
133 
134         if (mCheckIfPermittedBeforeCalling.isChecked()) {
135             if (!tm.isOutgoingCallPermitted(phoneAccountHandle)) {
136                 Toast.makeText(this, R.string.outgoingCallNotPermitted , Toast.LENGTH_SHORT).show();
137                 return;
138             }
139         }
140 
141         Bundle extras = new Bundle();
142         extras.putParcelable(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE,
143                 getSelectedPhoneAccountHandle());
144         if (mVideoCallButton.isChecked()) {
145             extras.putInt(TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE,
146                     VideoProfile.STATE_BIDIRECTIONAL);
147         }
148         tm.placeCall(Uri.parse(mNumber.getText().toString()), extras);
149     }
150 
placeIncomingCall()151     private void placeIncomingCall() {
152         TelecomManager tm = TelecomManager.from(this);
153         PhoneAccountHandle phoneAccountHandle = getSelectedPhoneAccountHandle();
154 
155         if (mCheckIfPermittedBeforeCalling.isChecked()) {
156             if (!tm.isIncomingCallPermitted(phoneAccountHandle)) {
157                 Toast.makeText(this, R.string.incomingCallNotPermitted , Toast.LENGTH_SHORT).show();
158                 return;
159             }
160         }
161 
162         Bundle extras = new Bundle();
163         extras.putParcelable(TelecomManager.EXTRA_INCOMING_CALL_ADDRESS,
164                 Uri.parse(mNumber.getText().toString()));
165         if (mVideoCallButton.isChecked()) {
166             extras.putInt(TelecomManager.EXTRA_INCOMING_VIDEO_STATE,
167                     VideoProfile.STATE_BIDIRECTIONAL);
168         }
169         tm.addNewIncomingCall(getSelectedPhoneAccountHandle(), extras);
170     }
171 }