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