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

Commit 51ada368 authored by Hugo Hudson's avatar Hugo Hudson
Browse files

Prepare playback immediately, new BackgroundTaskService.

- Immediately we start the CallDetailActivity with a voicemail, do the
  preparing of the media so we can see the duration.
- Use the duration text field to show "buffering..." until media source
  is prepared.
- Do the preparing in the background, this fixes a strict mode
  violation.
- If there's an error preparing, show an error message in that field.
- Add tests for both of the above cases.

BackgroundTaskService:
- Introduces new BackgroundTaskService, lightweight method for
  submitting AsyncTask objects.
- Introduces BackgroundTask interface and simple AbstractBackgroundTask
  abstract implementation.
- Adds BackgroundTaskService to ContactsApplication allowing Activity
  objects to get hold of a regular background task service.
- Adds a MockBackgroundTaskService for use with injecting for test, so
  that we can prevent or control processing of background tasks.

Other:
- Every time we resume the Activity, we were causing a new voicemail
  fragment to be created.  There was no bug tracking this, I just
  noticed it because of occasionally flaky tests.  Added a fix and test
  to catch it again next time.
- Fixes missing tear down method in PeopleActivityTest.

Bug: 5115133
Bug: 5059965
Bug: 5114261
Bug: 5113695
Change-Id: Ia2469229fa756da8b3977231fbf23a9d3fb379ce
parent cf27dd8f
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -1635,7 +1635,10 @@
    <string name="voicemail_initial_time">00:05</string>

    <!-- Message to show when there is an error playing back the voicemail. [CHAR LIMIT=40] -->
    <string name="voicemail_playback_error">Could not play voicemail</string>
    <string name="voicemail_playback_error">failed to play voicemail</string>

    <!-- Message to display before we have prepared the media player, i.e. before we know duration. [CHAR LIMIT=40] -->
    <string name="voicemail_buffering">buffering...</string>

    <!-- The header in the call log used to identify missed calls and voicemail that have not yet been consumed [CHAR LIMIT=10] -->
    <string name="call_log_new_header">New</string>
+20 −25
Original line number Diff line number Diff line
@@ -19,6 +19,9 @@ package com.android.contacts;
import com.android.contacts.calllog.CallDetailHistoryAdapter;
import com.android.contacts.calllog.CallTypeHelper;
import com.android.contacts.calllog.PhoneNumberHelper;
import com.android.contacts.util.AbstractBackgroundTask;
import com.android.contacts.util.BackgroundTask;
import com.android.contacts.util.BackgroundTaskService;
import com.android.contacts.voicemail.VoicemailPlaybackFragment;
import com.android.contacts.voicemail.VoicemailStatusHelper;
import com.android.contacts.voicemail.VoicemailStatusHelper.StatusMessage;
@@ -34,7 +37,6 @@ import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.CallLog;
import android.provider.CallLog.Calls;
@@ -87,6 +89,7 @@ public class CallDetailActivity extends ListActivity implements
    private ImageView mMainActionView;
    private ImageButton mMainActionPushLayerView;
    private ImageView mContactBackgroundView;
    private BackgroundTaskService mBackgroundTaskService;

    private String mNumber = null;
    private String mDefaultCountryIso;
@@ -144,6 +147,8 @@ public class CallDetailActivity extends ListActivity implements

        setContentView(R.layout.call_detail);

        mBackgroundTaskService = (BackgroundTaskService) getApplicationContext().getSystemService(
                BackgroundTaskService.BACKGROUND_TASK_SERVICE);
        mInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
        mResources = getResources();

@@ -164,13 +169,13 @@ public class CallDetailActivity extends ListActivity implements
        mContactPhotoManager = ContactPhotoManager.getInstance(this);
        getListView().setOnItemClickListener(this);
        configureActionBar();
        optionallyHandleVoicemail();
    }

    @Override
    public void onResume() {
        super.onResume();
        updateData(getCallLogEntryUris());
        optionallyHandleVoicemail();
    }

    /**
@@ -210,15 +215,14 @@ public class CallDetailActivity extends ListActivity implements
    }

    private void markVoicemailAsRead(final Uri voicemailUri) {
        new AsyncTask<Void, Void, Void>() {
        mBackgroundTaskService.submit(new AbstractBackgroundTask() {
            @Override
            protected Void doInBackground(Void... params) {
            public void doInBackground() {
                ContentValues values = new ContentValues();
                values.put(Voicemails.IS_READ, true);
                getContentResolver().update(voicemailUri, values, null, null);
                return null;
            }
        }.execute();
        });
    }

    /**
@@ -662,12 +666,16 @@ public class CallDetailActivity extends ListActivity implements
            }
            callIds.append(ContentUris.parseId(callUri));
        }
        runInBackgroundThenFinishActivity(new Runnable() {
        mBackgroundTaskService.submit(new BackgroundTask() {
            @Override
            public void run() {
            public void doInBackground() {
                getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
                        Calls._ID + " IN (" + callIds + ")", null);
            }
            @Override
            public void onPostExecute() {
                finish();
            }
        });
    }
    public void onMenuEditNumberBeforeCall(MenuItem menuItem) {
@@ -680,29 +688,16 @@ public class CallDetailActivity extends ListActivity implements

    public void onMenuTrashVoicemail(MenuItem menuItem) {
        final Uri voicemailUri = getVoicemailUri();
        runInBackgroundThenFinishActivity(new Runnable() {
        mBackgroundTaskService.submit(new BackgroundTask() {
            @Override
            public void run() {
            public void doInBackground() {
                getContentResolver().delete(voicemailUri, null, null);
            }
        });
    }

    /**
     * Run a task in the background, and then finish this activity when the task is done.
     */
    private void runInBackgroundThenFinishActivity(final Runnable runnable) {
        new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(Void... params) {
                runnable.run();
                return null;
            }
            @Override
            protected void onPostExecute(Void result) {
            public void onPostExecute() {
                finish();
            }
        }.execute();
        });
    }

    private void configureActionBar() {
+9 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.contacts;

import com.android.contacts.model.AccountTypeManager;
import com.android.contacts.test.InjectedServices;
import com.android.contacts.util.BackgroundTaskService;
import com.google.common.annotations.VisibleForTesting;

import android.app.Application;
@@ -33,6 +34,7 @@ public final class ContactsApplication extends Application {
    private static InjectedServices sInjectedServices;
    private AccountTypeManager mAccountTypeManager;
    private ContactPhotoManager mContactPhotoManager;
    private BackgroundTaskService mBackgroundTaskService;

    /**
     * Overrides the system services with mocks for testing.
@@ -93,6 +95,13 @@ public final class ContactsApplication extends Application {
            return mContactPhotoManager;
        }

        if (BackgroundTaskService.BACKGROUND_TASK_SERVICE.equals(name)) {
            if (mBackgroundTaskService == null) {
                mBackgroundTaskService = BackgroundTaskService.createBackgroundTaskService();
            }
            return mBackgroundTaskService;
        }

        return super.getSystemService(name);
    }

+29 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2011 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.contacts.util;

import com.android.contacts.util.BackgroundTask;

/**
 * Base class you can use if you only want to override the {@link #doInBackground()} method.
 */
public abstract class AbstractBackgroundTask implements BackgroundTask {
    @Override
    public void onPostExecute() {
        // No action necessary.
    }
}
+31 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2011 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.contacts.util;

/**
 * Simple interface to improve the testability of code using AsyncTasks.
 * <p>
 * Provides a trivial replacement for no-arg versions of AsyncTask clients.  We may extend this
 * to add more functionality as we require.
 * <p>
 * The same memory-visibility guarantees are made here as are made for AsyncTask objects, namely
 * that fields set in {@link #doInBackground()} are visible to {@link #onPostExecute()}.
 */
public interface BackgroundTask {
    public void doInBackground();
    public void onPostExecute();
}
Loading