Friday, 17 June 2016

Write a simple Hello Wearable program in Android Wearable ?




I hope you are stetted the all the Development set up as per my previous post ....


and make sure  about the latest Werable libraries 1.4  ..


MainActivity.java


package android.support.wearable.activity;

import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Bundle;
import android.os.Build.VERSION;
import android.support.annotation.CallSuper;
import com.google.android.wearable.compat.WearableActivityController;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.lang.reflect.Method;

@TargetApi(21)
public abstract class WearableActivity extends Activity {
    private final String TAG = WearableActivity.class.getSimpleName() + "[" + this.getClass().getSimpleName() + "]";
    private static final String WEARABLE_CONTROLLER_CLASS_NAME = "com.google.android.wearable.compat.WearableActivityController";
    private static volatile boolean sAmbientCallbacksVerifiedPresent;
    public static final String EXTRA_BURN_IN_PROTECTION = "com.google.android.wearable.compat.extra.BURN_IN_PROTECTION";
    public static final String EXTRA_LOWBIT_AMBIENT = "com.google.android.wearable.compat.extra.LOWBIT_AMBIENT";
    private WearableActivityController mWearableController;
    private boolean mSuperCalled;

    public WearableActivity() {
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.initAmbientSupport();
        if(this.mWearableController != null) {
            this.mWearableController.onCreate();
        }

    }

    protected void onResume() {
        super.onResume();
        if(this.mWearableController != null) {
            this.mWearableController.onResume();
        }

    }

    protected void onPause() {
        if(this.mWearableController != null) {
            this.mWearableController.onPause();
        }

        super.onPause();
    }

    protected void onStop() {
        if(this.mWearableController != null) {
            this.mWearableController.onStop();
        }

        super.onStop();
    }

    protected void onDestroy() {
        if(this.mWearableController != null) {
            this.mWearableController.onDestroy();
        }

        super.onDestroy();
    }

    public final void setAmbientEnabled() {
        if(this.mWearableController != null) {
            this.mWearableController.setAmbientEnabled();
        }

    }

    public final boolean isAmbient() {
        return this.mWearableController != null?this.mWearableController.isAmbient():false;
    }

    @CallSuper
    public void onEnterAmbient(Bundle ambientDetails) {
        this.mSuperCalled = true;
    }

    @CallSuper
    public void onUpdateAmbient() {
    }

    @CallSuper
    public void onExitAmbient() {
        this.mSuperCalled = true;
    }

    private void initAmbientSupport() {
        if(VERSION.SDK_INT > 21) {
            try {
                Class.forName("com.google.android.wearable.compat.WearableActivityController");
            } catch (ClassNotFoundException var2) {
                throw new IllegalStateException("Could not find wearable shared library classes. Please add <uses-library android:name=\"com.google.android.wearable\" android:required=\"false\" /> to the application manifest");
            }

            this.mWearableController = new WearableActivityController(this.TAG, this, new WearableActivity.AmbientCallback());
            verifyAmbientCallbacksPresent();
        }
    }

    private static void verifyAmbientCallbacksPresent() {
        if(!sAmbientCallbacksVerifiedPresent) {
            try {
                Method e = WearableActivity.AmbientCallback.class.getDeclaredMethod("onEnterAmbient", new Class[]{Bundle.class});
                if(!".onEnterAmbient".equals("." + e.getName())) {
                    throw new NoSuchMethodException();
                }
            } catch (NoSuchMethodException var1) {
                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;
        }
    }

    public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
        if(this.mWearableController != null) {
            this.mWearableController.dump(prefix, fd, writer, args);
        }

    }

    private class AmbientCallback extends com.google.android.wearable.compat.WearableActivityController.AmbientCallback {
        private AmbientCallback() {
        }

        public void onEnterAmbient(Bundle ambientDetails) {
            WearableActivity.this.mSuperCalled = false;
            WearableActivity.this.onEnterAmbient(ambientDetails);
            if(!WearableActivity.this.mSuperCalled) {
                throw new IllegalStateException("Activity " + WearableActivity.this.toString() + " did not call through to super.onEnterAmbient()");
            }
        }

        public void onExitAmbient() {
            WearableActivity.this.mSuperCalled = false;
            WearableActivity.this.onExitAmbient();
            if(!WearableActivity.this.mSuperCalled) {
                throw new IllegalStateException("Activity " + WearableActivity.this.toString() + " did not call through to super.onExitAmbient()");
            }
        }

        public void onUpdateAmbient() {
            WearableActivity.this.onUpdateAmbient();
        }
    }
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><android.support.wearable.view.BoxInsetLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:id="@+id/container"    tools:context="com.example.arajendra.wearaleapp.MainActivity"    tools:deviceIds="wear">

    <TextView        android:id="@+id/text"        app:layout_box="all"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:text="@string/hello_world" />

    <TextView        android:id="@+id/clock"        app:layout_box="all"        android:layout_gravity="bottom|start"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textColor="@android:color/white" />

</android.support.wearable.view.BoxInsetLayout>

No comments:

Post a Comment