Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 39252724 authored by Yorke Lee's avatar Yorke Lee Committed by Natiq Ahmed
Browse files

Move ToneGenerator recreation/release to onStart/onStop

There is no reason to do the costly ToneGenerator initialization
inside onResume, which gets called during the Dialer -> InCallUI
transition. Move it to onStart/onStop so it only happens when Dialer
is actually in the background.

Measurement of time taken for InCallActivity start -> onResumed, 10 runs

Before: Average 541.9ms SD: 152.4ms
After: Average 350.5ms SD: 81.1ms

Bug: 18373617

Change-Id: I192e2bcc9fd4b7d423d804f333d8d32bff3f58c1
parent 33065b91
Loading
Loading
Loading
Loading
+29 −20
Original line number Diff line number Diff line
@@ -610,6 +610,28 @@ public class DialpadFragment extends AnalyticsFragment
        pound.setOnLongClickListener(this);
    }

    @Override
    public void onStart() {
        super.onStart();
        // if the mToneGenerator creation fails, just continue without it.  It is
        // a local audio signal, and is not as important as the dtmf tone itself.
        final long start = System.currentTimeMillis();
        synchronized (mToneGeneratorLock) {
            if (mToneGenerator == null) {
                try {
                    mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
                } catch (RuntimeException e) {
                    Log.w(TAG, "Exception caught while creating local tone generator: " + e);
                    mToneGenerator = null;
                }
            }
        }
        final long total = System.currentTimeMillis() - start;
        if (total > 50) {
            Log.i(TAG, "Time for ToneGenerator creation: " + total);
        }
    };

    @Override
    public void onResume() {
        super.onResume();
@@ -638,20 +660,6 @@ public class DialpadFragment extends AnalyticsFragment

        stopWatch.lap("hptc");

        // if the mToneGenerator creation fails, just continue without it.  It is
        // a local audio signal, and is not as important as the dtmf tone itself.
        synchronized (mToneGeneratorLock) {
            if (mToneGenerator == null) {
                try {
                    mToneGenerator = new ToneGenerator(DIAL_TONE_STREAM_TYPE, TONE_RELATIVE_VOLUME);
                } catch (RuntimeException e) {
                    Log.w(TAG, "Exception caught while creating local tone generator: " + e);
                    mToneGenerator = null;
                }
            }
        }
        stopWatch.lap("tg");

        mPressedDialpadKeys.clear();

        configureScreenFromIntent(getActivity());
@@ -735,12 +743,6 @@ public class DialpadFragment extends AnalyticsFragment
        stopTone();
        mPressedDialpadKeys.clear();

        synchronized (mToneGeneratorLock) {
            if (mToneGenerator != null) {
                mToneGenerator.release();
                mToneGenerator = null;
            }
        }
        // TODO: I wonder if we should not check if the AsyncTask that
        // lookup the last dialed number has completed.
        mLastNumberDialed = EMPTY_NUMBER;  // Since we are going to query again, free stale number.
@@ -752,6 +754,13 @@ public class DialpadFragment extends AnalyticsFragment
    public void onStop() {
        super.onStop();

        synchronized (mToneGeneratorLock) {
            if (mToneGenerator != null) {
                mToneGenerator.release();
                mToneGenerator = null;
            }
        }

        if (mClearDigitsOnStop) {
            mClearDigitsOnStop = false;
            clearDialpad();