public class

WearableControllerProvider

extends java.lang.Object

 java.lang.Object

↳androidx.wear.ambient.WearableControllerProvider

Gradle dependencies

compile group: 'androidx.wear', name: 'wear', version: '1.4.0-alpha01'

  • groupId: androidx.wear
  • artifactId: wear
  • version: 1.4.0-alpha01

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

Androidx artifact mapping:

androidx.wear:wear com.android.support:wear

Androidx class mapping:

androidx.wear.ambient.WearableControllerProvider android.support.wear.ambient.WearableControllerProvider

Overview

Provides a for ambient mode control.

Summary

Constructors
publicWearableControllerProvider()

Methods
public WearableActivityControllergetWearableController(Activity activity, androidx.wear.ambient.AmbientDelegate.AmbientCallback callback)

Retrieves a to use for ambient mode.

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

Constructors

public WearableControllerProvider()

Methods

public WearableActivityController getWearableController(Activity activity, androidx.wear.ambient.AmbientDelegate.AmbientCallback callback)

Retrieves a to use for ambient mode.

Parameters:

activity: The to be associated with the Controller.
callback: The androidx.wear.ambient.AmbientDelegate.AmbientCallback for the Controller.

Returns:

the platform-appropriate version of the .

Source

/*
 * Copyright (C) 2017 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.ambient;

import android.app.Activity;
import android.os.Bundle;

import androidx.annotation.RestrictTo;

import com.google.android.wearable.compat.WearableActivityController;

import java.lang.reflect.Method;

/**
 * Provides a {@link WearableActivityController} for ambient mode control.
 *
 */
@RestrictTo(RestrictTo.Scope.LIBRARY)
public class WearableControllerProvider {

    private static final String TAG = "WearableControllerProvider";

    private static volatile boolean sAmbientCallbacksVerifiedPresent;

    /**
     * Retrieves a {@link WearableActivityController} to use for ambient mode.
     *
     * @param activity The {@link Activity} to be associated with the Controller.
     * @param callback The {@link AmbientDelegate.AmbientCallback} for the Controller.
     * @return the platform-appropriate version of the {@link WearableActivityController}.
     */
    public WearableActivityController getWearableController(Activity activity,
            final AmbientDelegate.AmbientCallback callback) {
        SharedLibraryVersion.verifySharedLibraryPresent();

        // The AmbientCallback is an abstract class instead of an interface.
        WearableActivityController.AmbientCallback callbackBridge =
                new WearableActivityController.AmbientCallback() {
                    @Override
                    public void onEnterAmbient(Bundle ambientDetails) {
                        callback.onEnterAmbient(ambientDetails);
                    }

                    @Override
                    public void onUpdateAmbient() {
                        callback.onUpdateAmbient();
                    }

                    @Override
                    public void onExitAmbient() {
                        callback.onExitAmbient();
                    }

                    @Override
                    public void onInvalidateAmbientOffload() {
                        callback.onAmbientOffloadInvalidated();
                    }
                };

        verifyAmbientCallbacksPresent();

        return new WearableActivityController(TAG, activity, callbackBridge);
    }

    private static void verifyAmbientCallbacksPresent() {
        if (sAmbientCallbacksVerifiedPresent) {
            return;
        }
        try {
            Method method =
                    WearableActivityController.AmbientCallback.class.getDeclaredMethod(
                            "onEnterAmbient", Bundle.class);
            // Proguard is sneaky -- it will actually rewrite strings it finds in addition to
            // function names. Therefore add a "." prefix to the method name check to ensure the
            // function was not renamed by proguard.
            if (!".onEnterAmbient".equals("." + method.getName())) {
                throw new NoSuchMethodException();
            }
        } catch (NoSuchMethodException e) {
            throw new IllegalStateException(
                    "Could not find a required method for "
                            + "ambient support, likely due to proguard optimization. Please add "
                            + "com.google.android.wearable:wearable jar to the list of library jars"
                            + " for your project");
        }
        sAmbientCallbacksVerifiedPresent = true;
    }
}