178a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad/*
278a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad * Copyright (C) 2013 The Android Open Source Project
378a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad *
478a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad * Licensed under the Apache License, Version 2.0 (the "License");
578a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad * you may not use this file except in compliance with the License.
678a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad * You may obtain a copy of the License at
778a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad *
878a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad *      http://www.apache.org/licenses/LICENSE-2.0
978a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad *
1078a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad * Unless required by applicable law or agreed to in writing, software
1178a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad * distributed under the License is distributed on an "AS IS" BASIS,
1278a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1378a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad * See the License for the specific language governing permissions and
1478a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad * limitations under the License.
1578a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad */
1678a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad
1778a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awadpackage com.android.server.telecom.components;
1878a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad
1978a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awadimport com.android.server.telecom.CallIntentProcessor;
208d5d9ddc66b55b6906364ab3c0e244dab4d58f13Ihab Awadimport com.android.server.telecom.TelecomSystem;
2178a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad
2278a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awadimport android.app.Activity;
23b3feab5a6ad30161726cf2decf4c0b06a78f5d41Yorke Leeimport android.content.Context;
2478a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awadimport android.content.Intent;
2578a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awadimport android.os.Bundle;
269a7302fadf0724b9ac9487996d1a0f48e4ea1e11Hall Liuimport android.os.PowerManager;
27b3feab5a6ad30161726cf2decf4c0b06a78f5d41Yorke Leeimport android.os.UserHandle;
28b3feab5a6ad30161726cf2decf4c0b06a78f5d41Yorke Leeimport android.os.UserManager;
29a3eccfee788c3ac3c831a443b085b141b39bb63dBrad Ebingerimport android.telecom.Log;
3078a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awadimport android.telecom.TelecomManager;
3178a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad
3278a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad// TODO: Needed for move to system service: import com.android.internal.R;
3378a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad
3478a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad/**
3578a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad * Activity that handles system CALL actions and forwards them to {@link CallIntentProcessor}.
3678a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad * Handles all three CALL action types: CALL, CALL_PRIVILEGED, and CALL_EMERGENCY.
3778a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad *
3878a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad * Pre-L, the only way apps were were allowed to make outgoing emergency calls was the
3978a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad * ACTION_CALL_PRIVILEGED action (which requires the system only CALL_PRIVILEGED permission).
4078a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad *
4178a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad * In L, any app that has the CALL_PRIVILEGED permission can continue to make outgoing emergency
4278a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad * calls via ACTION_CALL_PRIVILEGED.
4378a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad *
4478a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad * In addition, the default dialer (identified via
4578a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad * {@link TelecomManager#getDefaultPhoneApp()} will also be granted the ability to
4678a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad * make emergency outgoing calls using the CALL action. In order to do this, it must call
4778a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad * startActivityForResult on the CALL intent to allow its package name to be passed to
488d5d9ddc66b55b6906364ab3c0e244dab4d58f13Ihab Awad * {@link UserCallActivity}. Calling startActivity will continue to work on all non-emergency
498d5d9ddc66b55b6906364ab3c0e244dab4d58f13Ihab Awad * numbers just like it did pre-L.
5078a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad */
518d5d9ddc66b55b6906364ab3c0e244dab4d58f13Ihab Awadpublic class UserCallActivity extends Activity implements TelecomSystem.Component {
5278a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad
5378a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad    @Override
5478a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad    protected void onCreate(Bundle bundle) {
5578a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad        super.onCreate(bundle);
569a7302fadf0724b9ac9487996d1a0f48e4ea1e11Hall Liu        PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
579a7302fadf0724b9ac9487996d1a0f48e4ea1e11Hall Liu        PowerManager.WakeLock wakelock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
589a7302fadf0724b9ac9487996d1a0f48e4ea1e11Hall Liu                "UserCallActivity");
599a7302fadf0724b9ac9487996d1a0f48e4ea1e11Hall Liu        wakelock.acquire();
609a7302fadf0724b9ac9487996d1a0f48e4ea1e11Hall Liu
6172930a80679d461ad694b6555b0749d5fc4e31a8Brad Ebinger        Log.startSession("UCA.oC");
6272930a80679d461ad694b6555b0749d5fc4e31a8Brad Ebinger        try {
6372930a80679d461ad694b6555b0749d5fc4e31a8Brad Ebinger            // TODO: Figure out if there is something to restore from bundle.
6472930a80679d461ad694b6555b0749d5fc4e31a8Brad Ebinger            // See OutgoingCallBroadcaster in services/Telephony for more.
6572930a80679d461ad694b6555b0749d5fc4e31a8Brad Ebinger            Intent intent = getIntent();
6672930a80679d461ad694b6555b0749d5fc4e31a8Brad Ebinger            verifyCallAction(intent);
6772930a80679d461ad694b6555b0749d5fc4e31a8Brad Ebinger            final UserManager userManager = (UserManager) getSystemService(Context.USER_SERVICE);
6872930a80679d461ad694b6555b0749d5fc4e31a8Brad Ebinger            final UserHandle userHandle = new UserHandle(userManager.getUserHandle());
6972930a80679d461ad694b6555b0749d5fc4e31a8Brad Ebinger            // Once control flow has passed to this activity, it is no longer guaranteed that we can
7072930a80679d461ad694b6555b0749d5fc4e31a8Brad Ebinger            // accurately determine whether the calling package has the CALL_PHONE runtime permission.
7172930a80679d461ad694b6555b0749d5fc4e31a8Brad Ebinger            // At this point in time we trust that the ActivityManager has already performed this
7272930a80679d461ad694b6555b0749d5fc4e31a8Brad Ebinger            // validation before starting this activity.
73d307d6d02a5b3d8fb63e2af0c9ab5ae3ca16b201Tyler Gunn            // Create a new instance of intent to avoid modifying the
74d307d6d02a5b3d8fb63e2af0c9ab5ae3ca16b201Tyler Gunn            // ActivityThread.ActivityClientRecord#intent directly.
75d307d6d02a5b3d8fb63e2af0c9ab5ae3ca16b201Tyler Gunn            // Modifying directly may be a potential risk when relaunching this activity.
76d307d6d02a5b3d8fb63e2af0c9ab5ae3ca16b201Tyler Gunn            new UserCallIntentProcessor(this, userHandle).processIntent(new Intent(intent),
7772930a80679d461ad694b6555b0749d5fc4e31a8Brad Ebinger                    getCallingPackage(), true /* hasCallAppOp*/);
7872930a80679d461ad694b6555b0749d5fc4e31a8Brad Ebinger        } finally {
7972930a80679d461ad694b6555b0749d5fc4e31a8Brad Ebinger            Log.endSession();
809a7302fadf0724b9ac9487996d1a0f48e4ea1e11Hall Liu            wakelock.release();
8172930a80679d461ad694b6555b0749d5fc4e31a8Brad Ebinger        }
829a7302fadf0724b9ac9487996d1a0f48e4ea1e11Hall Liu        Log.i(this, "onCreate done");
8378a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad        finish();
8478a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad    }
8578a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad
8678a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad    private void verifyCallAction(Intent intent) {
8778a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad        if (getClass().getName().equals(intent.getComponent().getClassName())) {
8878a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad            // If we were launched directly from the CallActivity, not one of its more privileged
8978a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad            // aliases, then make sure that only the non-privileged actions are allowed.
9078a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad            if (!Intent.ACTION_CALL.equals(intent.getAction())) {
9178a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad                Log.w(this, "Attempt to deliver non-CALL action; forcing to CALL");
9278a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad                intent.setAction(Intent.ACTION_CALL);
9378a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad            }
9478a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad        }
9578a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad    }
968d5d9ddc66b55b6906364ab3c0e244dab4d58f13Ihab Awad
978d5d9ddc66b55b6906364ab3c0e244dab4d58f13Ihab Awad    @Override
988d5d9ddc66b55b6906364ab3c0e244dab4d58f13Ihab Awad    public TelecomSystem getTelecomSystem() {
998d5d9ddc66b55b6906364ab3c0e244dab4d58f13Ihab Awad        return TelecomSystem.getInstance();
1008d5d9ddc66b55b6906364ab3c0e244dab4d58f13Ihab Awad    }
10178a5e6b9c1595c81f72d7a822617cb78db224e48Ihab Awad}
102