public class

SerializationHelper

extends java.lang.Object

 java.lang.Object

↳androidx.wear.ongoing.SerializationHelper

Gradle dependencies

compile group: 'androidx.wear', name: 'wear-ongoing', version: '1.0.0'

  • groupId: androidx.wear
  • artifactId: wear-ongoing
  • version: 1.0.0

Artifact androidx.wear:wear-ongoing:1.0.0 it located at Google repository (https://maven.google.com/)

Overview

Class used to manage Ongoing Activity information as part of a Bundle or Notification.

Summary

Methods
public static voidcopy(Bundle sourceBundle, Bundle destinationBundle)

Copies an Ongoing Activity information from a bundle to another, without deserializing and serializing (this needs to be done before accessing the source Bundle)

public static OngoingActivitycreate(Bundle bundle)

Deserializes the OngoingActivity from a Bundle.

public static OngoingActivitycreate(Notification notification)

Deserializes the OngoingActivity from a notification.

public static booleanhasOngoingActivity(Notification notification)

Checks if the given notification contains information of an ongoing activity.

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Methods

public static boolean hasOngoingActivity(Notification notification)

Checks if the given notification contains information of an ongoing activity.

public static OngoingActivity create(Notification notification)

Deserializes the OngoingActivity from a notification.

Parameters:

notification: the notification that may contain information about a Ongoing Activity.

Returns:

the data, or null of the notification doesn't contain Ongoing Activity data.

public static OngoingActivity create(Bundle bundle)

Deserializes the OngoingActivity from a Bundle.

Parameters:

bundle: the bundle that may contain information about a Ongoing Activity.

Returns:

the data, or null of the Bundle doesn't contain Ongoing Activity data.

public static void copy(Bundle sourceBundle, Bundle destinationBundle)

Copies an Ongoing Activity information from a bundle to another, without deserializing and serializing (this needs to be done before accessing the source Bundle)

Parameters:

sourceBundle: The bundle to get the Ongoing Activity data from
destinationBundle: The bundle to put the Ongoing Activity data into.

Source

/*
 * Copyright 2021 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 androidx.wear.ongoing;

import android.app.Notification;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.core.app.NotificationCompat;
import androidx.versionedparcelable.ParcelUtils;

/**
 * Class used to manage Ongoing Activity information as part of a Bundle or Notification.
 */
public class SerializationHelper {
    // Utility class, avoid instantiation
    private SerializationHelper() {
    }

    /**
     * Checks if the given notification contains information of an ongoing activity.
     */
    public static boolean hasOngoingActivity(@NonNull Notification notification) {
        return notification.extras.getBundle(EXTRA_ONGOING_ACTIVITY) != null;
    }

    /**
     * Deserializes the {@link OngoingActivity} from a notification.
     *
     * @param notification the notification that may contain information about a Ongoing
     *                     Activity.
     * @return the data, or null of the notification doesn't contain Ongoing Activity data.
     */
    @Nullable
    public static OngoingActivity create(@NonNull Notification notification) {
        return create(notification.extras);
    }

    /**
     * Deserializes the {@link OngoingActivity} from a Bundle.
     *
     * @param bundle the bundle that may contain information about a Ongoing Activity.
     * @return the data, or null of the Bundle doesn't contain Ongoing Activity data.
     */
    @Nullable
    public static OngoingActivity create(@NonNull Bundle bundle) {
        OngoingActivityData data = createInternal(bundle);
        return data == null ? null : new OngoingActivity(data);
    }

    /**
     * Copies an Ongoing Activity information from a bundle to another, without deserializing
     * and serializing (this needs to be done before accessing the source Bundle)
     *
     * @param sourceBundle The bundle to get the Ongoing Activity data from
     * @param destinationBundle The bundle to put the Ongoing Activity data into.
     */
    public static void copy(@NonNull Bundle sourceBundle, @NonNull Bundle destinationBundle) {
        destinationBundle.putBundle(EXTRA_ONGOING_ACTIVITY,
                new Bundle(sourceBundle.getBundle(EXTRA_ONGOING_ACTIVITY)));
    }

    /**
     * Add the information from the given OngoingActivityData into the notification builder.
     * @hide
     */
    @RestrictTo(RestrictTo.Scope.LIBRARY)
    @NonNull
    static NotificationCompat.Builder extend(@NonNull NotificationCompat.Builder builder,
            @NonNull OngoingActivityData data) {
        ParcelUtils.putVersionedParcelable(builder.getExtras(), EXTRA_ONGOING_ACTIVITY,
                data);
        return builder;
    }

    /**
     * Add the information from the given OngoingActivityData into the notification builder
     * and build the notification.
     * @hide
     */
    @RestrictTo(RestrictTo.Scope.LIBRARY)
    @NonNull
    static Notification extendAndBuild(@NonNull NotificationCompat.Builder builder,
            @NonNull OngoingActivityData data) {
        Notification notification = extend(builder, data).build();
        // TODO(http://b/169394642): Undo this if/when the bug is fixed.
        notification.extras.putBundle(
                EXTRA_ONGOING_ACTIVITY,
                builder.getExtras().getBundle(EXTRA_ONGOING_ACTIVITY)
        );
        return notification;
    }

    /**
     * Deserialize a OngoingActivityData from a notification.
     * @hide
     */
    @RestrictTo(RestrictTo.Scope.LIBRARY)
    @Nullable
    static OngoingActivityData createInternal(@NonNull Notification notification) {
        return createInternal(notification.extras);
    }

    /**
     * Deserialize a OngoingActivityData from a bundle.
     * @hide
     */
    @RestrictTo(RestrictTo.Scope.LIBRARY)
    @Nullable
    static OngoingActivityData createInternal(@NonNull Bundle bundle) {
        return ParcelUtils.getVersionedParcelable(bundle, EXTRA_ONGOING_ACTIVITY);
    }

    /** Notification action extra which contains ongoing activity extensions */
    private static final String EXTRA_ONGOING_ACTIVITY =
            "android.wearable.ongoingactivities.EXTENSIONS";
}