लाइव कार्ड

लाइव कार्ड, टाइमलाइन के मौजूदा सेक्शन में दिखते हैं और मौजूदा समय के हिसाब से जानकारी दिखाते हैं.

लाइव कार्ड ऐसे समय के लिए बेहतरीन हैं, जब उपयोगकर्ता सक्रिय रूप से किसी काम में ज़्यादा दिलचस्पी दिखाते हैं, लेकिन समय-समय पर Glass की जांच करना चाहते हैं. उदाहरण के लिए, हर मिनट में रन टाइम के दौरान अपना समय देखना या किसी संगीत प्लेयर को तब कंट्रोल करना, जब वे किसी गाने को स्किप करना या रोकना चाहते हों.

अगर आप Glass के लिए पहली बार डेवलप कर रहे हैं, तो पहले चल रही टास्क गाइड पढ़ें. हमारे दस्तावेज़ में, डिज़ाइन से जुड़े सबसे सही तरीकों का पालन करके लाइव कार्ड के साथ Glassware बनाने का तरीका बताया गया है.

वे कैसे काम करते हैं

लाइव कार्ड की मदद से कार्ड तब तक टाइमलाइन के मौजूदा सेक्शन में रहते हैं, जब तक वे काम के हैं. स्टैटिक कार्ड के उलट, लाइव कार्ड टाइमलाइन में नहीं बने रहते. साथ ही, काम पूरा हो जाने के बाद उपयोगकर्ता साफ़ तौर पर उन्हें हटा देते हैं.

उपयोगकर्ता आम तौर पर मुख्य मेन्यू में, बोलकर दिए जाने वाले निर्देश के ज़रिए लाइव कार्ड शुरू करते हैं. मुख्य मेन्यू में, कार्ड की बैकग्राउंड इमेज चालू होती है. इसके बाद, वे कार्ड पर टैप करके मेन्यू पर टैप कर सकते हैं. जैसे कि टाइमलाइन से आइटम को खारिज करना.

इनका इस्तेमाल कब करना चाहिए

लाइव कार्ड ऐसे चालू टास्क के लिए डिज़ाइन किए गए हैं जिन पर उपयोगकर्ता बार-बार जा सकते हैं. उदाहरण के लिए, डिसप्ले के चलने की स्थिति, नेविगेशन के दौरान ऐनिमेट किया हुआ मैप या संगीत प्लेयर.

लाइव कार्ड का एक और फ़ायदा यह है कि वे ऐसे यूआई के लिए भी सही होते हैं जिनमें उपयोगकर्ताओं के साथ रीयल-टाइम इंटरैक्शन और यूज़र इंटरफ़ेस (यूआई) में रीयल-टाइम अपडेट होने की ज़रूरत होती है.

लाइव कार्ड इस्तेमाल करने पर भी टाइमलाइन पर उपयोगकर्ता अनुभव का कंट्रोल होता है. इसलिए, लाइव कार्ड पर आगे या पीछे स्वाइप करने से, टाइमलाइन कार्ड पर ही नेविगेट करने लगती है. साथ ही, सिस्टम के काम करने के तरीके के आधार पर स्क्रीन चालू और बंद होती है. यह पांच सेकंड के बाद, बिना उपयोगकर्ता के इंटरैक्शन या सिर के ऊपर की ओर खिसकाए जाने के दौरान होती है.

हालांकि, लाइव कार्ड में ऐसी कई सुविधाओं का ऐक्सेस होता है जिन्हें इमर्सिव किया जा सकता है. उदाहरण के लिए, सेंसर या जीपीएस डेटा. इसकी मदद से, उपयोगकर्ताओं को शानदार अनुभव दिया जा सकता है. इसकी मदद से, उपयोगकर्ताओं को 'टाइमलाइन' से जुड़े अनुभव के साथ-साथ, मैसेज भेजने जैसे अन्य काम भी किए जा सकते हैं.

आर्किटेक्चर

लाइव कार्ड के लिए उन्हें लंबे समय तक इस्तेमाल करने की ज़रूरत होती है, ताकि वे पूरी अवधि तक उन पर नज़र रखे जा सकें. इसलिए, उन्हें बैकग्राउंड में मैनेज करें.

इसके बाद, लाइव कार्ड को पब्लिश और रेंडर किया जा सकता है. ऐसा, सेवा चालू होने के बाद या सेवा की निगरानी वाले अन्य इवेंट के लिए किया जा सकता है. कम समय (हर कुछ सेकंड में एक बार) या ज़्यादा फ़्रीक्वेंसी (ज़्यादा से ज़्यादा कितनी बार सिस्टम रीफ़्रेश हो सकता है) का इस्तेमाल करके, लाइव कार्ड को रेंडर किया जा सकता है.

जब लाइव कार्ड काम का न हो, तो उसे दिखाना बंद कर दें.

कम फ़्रीक्वेंसी वाला रेंडरिंग

कम फ़्रीक्वेंसी वाली रेंडरिंग, Android व्यू के एक छोटे सेट के लिए होती है. इससे डिसप्ले सिर्फ़ कुछ सेकंड में एक बार अपडेट हो सकता है.

यह आसान कॉन्टेंट वाले ऐसे लाइव कार्ड बनाने का आसान तरीका है जिसके लिए लगातार रेंडरिंग या बार-बार अपडेट करने की ज़रूरत नहीं होती.

हाई फ़्रीक्वेंसी रेंडरिंग

हाई फ़्रीक्वेंसी रेंडरिंग की मदद से, Android ग्राफ़िक फ़्रेमवर्क में उपलब्ध ज़्यादा विकल्पों का इस्तेमाल किया जा सकता है.

यह सिस्टम आपको लाइव कार्ड की बैकिंग के मुख्य हिस्से की जानकारी देता है, जिसे आप सीधे 2D व्यू और लेआउट या OpenGL के साथ मुश्किल 3D ग्राफ़िक का इस्तेमाल करके बनाते हैं.

 

कम फ़्रीक्वेंसी वाले लाइव कार्ड बनाना

कम फ़्रीक्वेंसी वाली रेंडरिंग के लिए, RemoteViews ऑब्जेक्ट से मिले यूज़र इंटरफ़ेस (यूआई) की ज़रूरत होती है जो Android लेआउट और व्यू के इन सबसेट के साथ काम करता है:

कम फ़्रीक्वेंसी वाली रेंडरिंग का इस्तेमाल तब करें, जब:

  • आपको सिर्फ़ पहले से सूची में बताए गए स्टैंडर्ड Android व्यू एपीआई की ज़रूरत होगी.
  • आपको बहुत कम अपडेट की ज़रूरत होगी (रीफ़्रेश करने के बीच कुछ सेकंड).

ध्यान रखें:

  • कार्ड को पब्लिश करने की टाइमलाइन पाने के लिए, लाइव कार्ड में हमेशा PendingIntent setAction() का एलान होना चाहिए.
  • प्रकाशित करने के बाद किसी कार्ड में बदलाव करने के लिए, setViews() को कार्ड पर फिर से पब्लिश करने से पहले अपडेट किए गए RemoteViews ऑब्जेक्ट के साथ कॉल करें.

कम फ़्रीक्वेंसी वाले लाइव कार्ड बनाने के लिए:

  1. वह लेआउट या व्यू बनाएं जिसे रेंडर करना है. नीचे दिए गए उदाहरण में एक काल्पनिक बास्केटबॉल गेम का लेआउट दिखाया गया है:

     <TextView
         android:id="@+id/home_team_name_text_view"
         android:layout_width="249px"
         android:layout_height="wrap_content"
         android:layout_alignParentRight="true"
         android:gravity="center"
         android:textSize="40px" />
    
     <TextView
         android:id="@+id/away_team_name_text_view"
         android:layout_width="249px"
         android:layout_height="wrap_content"
         android:layout_alignParentLeft="true"
         android:gravity="center"
         android:textSize="40px" />
    
     <TextView
         android:id="@+id/away_score_text_view"
         android:layout_width="249px"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/away_team_name_text_view"
         android:layout_below="@+id/away_team_name_text_view"
         android:gravity="center"
         android:textSize="70px" />
    
     <TextView
         android:id="@+id/home_score_text_view"
         android:layout_width="249px"
         android:layout_height="wrap_content"
         android:layout_alignLeft="@+id/home_team_name_text_view"
         android:layout_below="@+id/home_team_name_text_view"
         android:gravity="center"
         android:textSize="70px" />
    
     <TextView
         android:id="@+id/footer_text"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentBottom="true"
         android:layout_alignParentLeft="true"
         android:layout_marginBottom="33px"
         android:textSize="26px" />
    

  2. एक ऐसी सेवा बनाएं जो लाइव कार्ड को मैनेज करती हो और आपके लेआउट या व्यू को रेंडर करती हो. इस उदाहरण में हर 30 सेकंड में, एक काल्पनिक बास्केटबॉल गेम का स्कोर अपडेट किया जाएगा.

    import java.util.Random;
    
    import com.google.android.glass.timeline.LiveCard;
    import com.google.android.glass.timeline.LiveCard.PublishMode;
    
    import android.app.PendingIntent;
    import android.app.Service;
    import android.content.Intent;
    import android.os.Handler;
    import android.os.IBinder;
    import android.widget.RemoteViews;
    
    public class LiveCardService extends Service {
    
        private static final String LIVE_CARD_TAG = "LiveCardDemo";
    
        private LiveCard mLiveCard;
        private RemoteViews mLiveCardView;
    
        private int homeScore, awayScore;
        private Random mPointsGenerator;
    
        private final Handler mHandler = new Handler();
        private final UpdateLiveCardRunnable mUpdateLiveCardRunnable =
            new UpdateLiveCardRunnable();
        private static final long DELAY_MILLIS = 30000;
    
        @Override
        public void onCreate() {
            super.onCreate();
            mPointsGenerator = new Random();
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            if (mLiveCard == null) {
    
                // Get an instance of a live card
                mLiveCard = new LiveCard(this, LIVE_CARD_TAG);
    
                // Inflate a layout into a remote view
                mLiveCardView = new RemoteViews(getPackageName(),
                        R.layout.main_layout);
    
                // Set up initial RemoteViews values
                homeScore = 0;
                awayScore = 0;
                mLiveCardView.setTextViewText(R.id.home_team_name_text_view,
                        getString(R.string.home_team));
                mLiveCardView.setTextViewText(R.id.away_team_name_text_view,
                        getString(R.string.away_team));
                mLiveCardView.setTextViewText(R.id.footer_text,
                        getString(R.string.game_quarter));
    
                // Set up the live card's action with a pending intent
                // to show a menu when tapped
                Intent menuIntent = new Intent(this, MenuActivity.class);
                menuIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                        Intent.FLAG_ACTIVITY_CLEAR_TASK);
                mLiveCard.setAction(PendingIntent.getActivity(
                        this, 0, menuIntent, 0));
    
                // Publish the live card
                mLiveCard.publish(PublishMode.REVEAL);
    
                // Queue the update text runnable
                mHandler.post(mUpdateLiveCardRunnable);
            }
            return START_STICKY;
        }
    
        @Override
        public void onDestroy() {
            if (mLiveCard != null && mLiveCard.isPublished()) {
                //Stop the handler from queuing more Runnable jobs
                mUpdateLiveCardRunnable.setStop(true);
    
                mLiveCard.unpublish();
                mLiveCard = null;
            }
            super.onDestroy();
        }
    
        /**
         * Runnable that updates live card contents
         */
        private class UpdateLiveCardRunnable implements Runnable{
    
            private boolean mIsStopped = false;
    
            /*
             * Updates the card with a fake score every 30 seconds as a demonstration.
             * You also probably want to display something useful in your live card.
             *
             * If you are executing a long running task to get data to update a
             * live card(e.g, making a web call), do this in another thread or
             * AsyncTask.
             */
            public void run(){
                if(!isStopped()){
                    // Generate fake points.
                    homeScore += mPointsGenerator.nextInt(3);
                    awayScore += mPointsGenerator.nextInt(3);
    
                    // Update the remote view with the new scores.
                    mLiveCardView.setTextViewText(R.id.home_score_text_view,
                            String.valueOf(homeScore));
                    mLiveCardView.setTextViewText(R.id.away_score_text_view,
                            String.valueOf(awayScore));
    
                    // Always call setViews() to update the live card's RemoteViews.
                    mLiveCard.setViews(mLiveCardView);
    
                    // Queue another score update in 30 seconds.
                    mHandler.postDelayed(mUpdateLiveCardRunnable, DELAY_MILLIS);
                }
            }
    
            public boolean isStopped() {
                return mIsStopped;
            }
    
            public void setStop(boolean isStopped) {
                this.mIsStopped = isStopped;
            }
        }
    
        @Override
        public IBinder onBind(Intent intent) {
          /*
           * If you need to set up interprocess communication
           * (activity to a service, for instance), return a binder object
           * so that the client can receive and modify data in this service.
           *
           * A typical use is to give a menu activity access to a binder object
           * if it is trying to change a setting that is managed by the live card
           * service. The menu activity in this sample does not require any
           * of these capabilities, so this just returns null.
           */
           return null;
        }
    }
    

हाई-फ़्रीक्वेंसी पर लाइव कार्ड बनाना

हाई फ़्रीक्वेंसी रेंडरिंग की मदद से, लाइव कार्ड के पीछे के सतह पर सीधे जाया जा सकता है.

हाई फ़्रीक्वेंसी रेंडरिंग का इस्तेमाल तब करें, जब:

  • आपको लाइव कार्ड को बार-बार अपडेट करना होगा.
  • रेंडर करने के लिए, आपको अलग-अलग तरह के एलिमेंट की ज़रूरत होती है. हाई फ़्रीक्वेंसी रेंडरिंग की मदद से, Android विज़ुअल और लेआउट का इस्तेमाल करके OpenGL ग्राफ़िक को मुश्किल बनाया जा सकता है.

ध्यान रखें:

  • आपको लाइव कार्ड के सरफ़ेस पर रेंडर करने के लिए, हमेशा बैकग्राउंड में चलने वाली सेवा बनानी चाहिए.
  • लाइव कार्ड में हमेशा PendingIntent setAction() के साथ एलान किया गया होना चाहिए.
  • दूसरे सभी मामलों के लिए, अगर आप OpenGL और DirectRenderingCallback को रेंडर कर रहे हैं, तो GLRenderer का इस्तेमाल करें.

Direct दिलचस्पी के लिए, कॉलबैक करने की सुविधा का इस्तेमाल करना

Android के स्टैंडर्ड व्यू और ड्रॉइंग लॉजिक वाले लाइव कार्ड बनाने के लिए:

  1. एक क्लास बनाएं जो DirectRenderingCallback को लागू करती है. इन इंटरफ़ेस में कॉलबैक को लागू करके, लाइव कार्ड के सरफ़ेस लाइफ़साइकल के ज़रूरी इवेंट के दौरान कार्रवाइयां की जा सकती हैं.

    नीचे दिए गए उदाहरण में, समय-समय पर रेंडर करने के लिए बैकग्राउंड थ्रेड बनाया जाता है. हालांकि, बाहरी इवेंट (उदाहरण के लिए, सेंसर या जगह की जानकारी से जुड़े अपडेट) के जवाब में कार्ड को अपडेट किया जा सकता है.

    public class LiveCardRenderer implements DirectRenderingCallback {
    
        // About 30 FPS.
        private static final long FRAME_TIME_MILLIS = 33;
    
        private SurfaceHolder mHolder;
        private boolean mPaused;
        private RenderThread mRenderThread;
    
        @Override
        public void surfaceChanged(SurfaceHolder holder, int format,
                int width, int height) {
            // Update your views accordingly.
        }
    
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            mPaused = false;
            mHolder = holder;
            updateRendering();
        }
    
        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            mHolder = null;
            updateRendering();
        }
    
        @Override
        public void renderingPaused(SurfaceHolder holder, boolean paused) {
            mPaused = paused;
            updateRendering();
        }
    
        /**
         * Start or stop rendering according to the timeline state.
         */
        private void updateRendering() {
            boolean shouldRender = (mHolder != null) && !mPaused;
            boolean rendering = mRenderThread != null;
    
            if (shouldRender != rendering) {
                if (shouldRender) {
                    mRenderThread = new RenderThread();
                    mRenderThread.start();
                } else {
                    mRenderThread.quit();
                    mRenderThread = null;
                }
            }
        }
    
        /**
         * Draws the view in the SurfaceHolder's canvas.
         */
        private void draw() {
            Canvas canvas;
            try {
                canvas = mHolder.lockCanvas();
            } catch (Exception e) {
                return;
            }
            if (canvas != null) {
                // Draw on the canvas.
                mHolder.unlockCanvasAndPost(canvas);
            }
        }
    
        /**
         * Redraws in the background.
         */
        private class RenderThread extends Thread {
            private boolean mShouldRun;
    
            /**
             * Initializes the background rendering thread.
             */
            public RenderThread() {
                mShouldRun = true;
            }
    
            /**
             * Returns true if the rendering thread should continue to run.
             *
             * @return true if the rendering thread should continue to run
             */
            private synchronized boolean shouldRun() {
                return mShouldRun;
            }
    
            /**
             * Requests that the rendering thread exit at the next
             opportunity.
             */
            public synchronized void quit() {
                mShouldRun = false;
            }
    
            @Override
            public void run() {
                while (shouldRun()) {
                    draw();
                    SystemClock.sleep(FRAME_TIME_MILLIS);
                }
            }
        }
    }
    
  2. अपने DirectRenderingCallback के इंस्टेंस को LiveCard SurfaceHolder के कॉलबैक के तौर पर सेट करें. इससे लाइव कार्ड को यह पता चलता है कि रेंडर करने के लिए, किस लॉजिक का इस्तेमाल किया जाएगा.

    // Tag used to identify the LiveCard in debugging logs.
    private static final String LIVE_CARD_TAG = "my_card";
    
    // Cached instance of the LiveCard created by the publishCard() method.
    private LiveCard mLiveCard;
    
    private void publishCard(Context context) {
        if (mLiveCard == null) {
            mLiveCard = new LiveCard(this, LIVE_CARD_TAG);
    
            // Enable direct rendering.
            mLiveCard.setDirectRenderingEnabled(true);
            mLiveCard.getSurfaceHolder().addCallback(
                    new LiveCardRenderer());
    
            Intent intent = new Intent(context, MenuActivity.class);
            mLiveCard.setAction(PendingIntent.getActivity(context, 0,
                    intent, 0));
            mLiveCard.publish(LiveCard.PublishMode.SILENT);
        } else {
            // Card is already published.
            return;
        }
    }
    
    private void unpublishCard(Context context) {
        if (mLiveCard != null) {
            mLiveCard.unpublish();
            mLiveCard = null;
        }
    }
    

OpenGL का इस्तेमाल करना

  1. एक ऐसी क्लास बनाएं जो GlRenderer को लागू करे. इस इंटरफ़ेस में कॉलबैक लागू करके, लाइव कार्ड के सरफ़ेस लाइफ़साइकल के ज़रूरी इवेंट के दौरान कार्रवाई की जा सकती है. यह उदाहरण एक रंगीन, घूमता हुआ घन बनाता है.

    import com.google.android.glass.timeline.GlRenderer;
    
    import android.opengl.GLES20;
    import android.opengl.Matrix;
    import android.os.SystemClock;
    
    import java.util.concurrent.TimeUnit;
    import javax.microedition.khronos.egl.EGLConfig;
    
    /**
     * Renders a 3D OpenGL Cube on a {@link LiveCard}.
     */
    public class CubeRenderer implements GlRenderer {
    
        /** Rotation increment per frame. */
        private static final float CUBE_ROTATION_INCREMENT = 0.6f;
    
        /** The refresh rate, in frames per second. */
        private static final int REFRESH_RATE_FPS = 60;
    
        /** The duration, in milliseconds, of one frame. */
        private static final float FRAME_TIME_MILLIS = TimeUnit.SECONDS.toMillis(1) / REFRESH_RATE_FPS;
    
        private final float[] mMVPMatrix;
        private final float[] mProjectionMatrix;
        private final float[] mViewMatrix;
        private final float[] mRotationMatrix;
        private final float[] mFinalMVPMatrix;
    
        private Cube mCube;
        private float mCubeRotation;
        private long mLastUpdateMillis;
    
        public CubeRenderer() {
            mMVPMatrix = new float[16];
            mProjectionMatrix = new float[16];
            mViewMatrix = new float[16];
            mRotationMatrix = new float[16];
            mFinalMVPMatrix = new float[16];
    
            // Set the fixed camera position (View matrix).
            Matrix.setLookAtM(mViewMatrix, 0, 0.0f, 0.0f, -4.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
        }
    
        @Override
        public void onSurfaceCreated(EGLConfig config) {
            // Set the background frame color
            GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
            GLES20.glClearDepthf(1.0f);
            GLES20.glEnable(GLES20.GL_DEPTH_TEST);
            GLES20.glDepthFunc(GLES20.GL_LEQUAL);
            mCube = new Cube();
        }
    
        @Override
        public void onSurfaceChanged(int width, int height) {
            float ratio = (float) width / height;
    
            GLES20.glViewport(0, 0, width, height);
            // This projection matrix is applied to object coordinates in the onDrawFrame() method.
            Matrix.frustumM(mProjectionMatrix, 0, -ratio, ratio, -1.0f, 1.0f, 3.0f, 7.0f);
            // modelView = projection x view
            Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);
        }
    
        @Override
        public void onDrawFrame() {
            GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
    
            // Apply the rotation.
            Matrix.setRotateM(mRotationMatrix, 0, mCubeRotation, 1.0f, 1.0f, 1.0f);
            // Combine the rotation matrix with the projection and camera view
            Matrix.multiplyMM(mFinalMVPMatrix, 0, mMVPMatrix, 0, mRotationMatrix, 0);
    
            // Draw cube.
            mCube.draw(mFinalMVPMatrix);
            updateCubeRotation();
        }
    
        /** Updates the cube rotation. */
        private void updateCubeRotation() {
            if (mLastUpdateMillis != 0) {
                float factor = (SystemClock.elapsedRealtime() - mLastUpdateMillis) / FRAME_TIME_MILLIS;
                mCubeRotation += CUBE_ROTATION_INCREMENT * factor;
            }
            mLastUpdateMillis = SystemClock.elapsedRealtime();
        }
    }
    
  2. एक ऐसी सेवा बनाएं जो लाइव कार्ड को मैनेज करती हो और CubeRenderer क्लास को लाइव कार्ड के रेंडरर के तौर पर सेट करती हो.

    import com.google.android.glass.timeline.LiveCard;
    import com.google.android.glass.timeline.LiveCard.PublishMode;
    
    import android.app.PendingIntent;
    import android.app.Service;
    import android.content.Intent;
    import android.os.IBinder;
    
    /**
     * Creates a {@link LiveCard} rendering a rotating 3D cube with OpenGL.
     */
    public class OpenGlService extends Service {
    
        private static final String LIVE_CARD_TAG = "opengl";
    
        private LiveCard mLiveCard;
    
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            if (mLiveCard == null) {
                mLiveCard = new LiveCard(this, LIVE_CARD_TAG);
                mLiveCard.setRenderer(new CubeRenderer());
                mLiveCard.setAction(
                        PendingIntent.getActivity(this, 0, new Intent(this, MenuActivity.class), 0));
                mLiveCard.publish(PublishMode.REVEAL);
            } else {
                mLiveCard.navigate();
            }
    
            return START_STICKY;
        }
    
        @Override
        public void onDestroy() {
            if (mLiveCard != null && mLiveCard.isPublished()) {
                mLiveCard.unpublish();
                mLiveCard = null;
            }
            super.onDestroy();
        }
    }
    
import android.opengl.GLES20;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

/**
 * Renders a 3D Cube using OpenGL ES 2.0.
 *
 * For more information on how to use OpenGL ES 2.0 on Android, see the
 * <a href="//developer.android.com/training/graphics/opengl/index.html">
 * Displaying Graphics with OpenGL ES</a> developer guide.
 */
public class Cube {

    /** Cube vertices */
    private static final float VERTICES[] = {
        -0.5f, -0.5f, -0.5f,
        0.5f, -0.5f, -0.5f,
        0.5f, 0.5f, -0.5f,
        -0.5f, 0.5f, -0.5f,
        -0.5f, -0.5f, 0.5f,
        0.5f, -0.5f, 0.5f,
        0.5f, 0.5f, 0.5f,
        -0.5f, 0.5f, 0.5f
    };

    /** Vertex colors. */
    private static final float COLORS[] = {
        0.0f, 1.0f, 1.0f, 1.0f,
        1.0f, 0.0f, 0.0f, 1.0f,
        1.0f, 1.0f, 0.0f, 1.0f,
        0.0f, 1.0f, 0.0f, 1.0f,
        0.0f, 0.0f, 1.0f, 1.0f,
        1.0f, 0.0f, 1.0f, 1.0f,
        1.0f, 1.0f, 1.0f, 1.0f,
        0.0f, 1.0f, 1.0f, 1.0f,
    };


    /** Order to draw vertices as triangles. */
    private static final byte INDICES[] = {
        0, 1, 3, 3, 1, 2, // Front face.
        0, 1, 4, 4, 5, 1, // Bottom face.
        1, 2, 5, 5, 6, 2, // Right face.
        2, 3, 6, 6, 7, 3, // Top face.
        3, 7, 4, 4, 3, 0, // Left face.
        4, 5, 7, 7, 6, 5, // Rear face.
    };

    /** Number of coordinates per vertex in {@link VERTICES}. */
    private static final int COORDS_PER_VERTEX = 3;

    /** Number of values per colors in {@link COLORS}. */
    private static final int VALUES_PER_COLOR = 4;

    /** Vertex size in bytes. */
    private final int VERTEX_STRIDE = COORDS_PER_VERTEX * 4;

    /** Color size in bytes. */
    private final int COLOR_STRIDE = VALUES_PER_COLOR * 4;

    /** Shader code for the vertex. */
    private static final String VERTEX_SHADER_CODE =
            "uniform mat4 uMVPMatrix;" +
            "attribute vec4 vPosition;" +
            "attribute vec4 vColor;" +
            "varying vec4 _vColor;" +
            "void main() {" +
            "  _vColor = vColor;" +
            "  gl_Position = uMVPMatrix * vPosition;" +
            "}";

    /** Shader code for the fragment. */
    private static final String FRAGMENT_SHADER_CODE =
            "precision mediump float;" +
            "varying vec4 _vColor;" +
            "void main() {" +
            "  gl_FragColor = _vColor;" +
            "}";


    private final FloatBuffer mVertexBuffer;
    private final FloatBuffer mColorBuffer;
    private final ByteBuffer mIndexBuffer;
    private final int mProgram;
    private final int mPositionHandle;
    private final int mColorHandle;
    private final int mMVPMatrixHandle;

    public Cube() {
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(VERTICES.length * 4);

        byteBuffer.order(ByteOrder.nativeOrder());
        mVertexBuffer = byteBuffer.asFloatBuffer();
        mVertexBuffer.put(VERTICES);
        mVertexBuffer.position(0);

        byteBuffer = ByteBuffer.allocateDirect(COLORS.length * 4);
        byteBuffer.order(ByteOrder.nativeOrder());
        mColorBuffer = byteBuffer.asFloatBuffer();
        mColorBuffer.put(COLORS);
        mColorBuffer.position(0);

        mIndexBuffer = ByteBuffer.allocateDirect(INDICES.length);
        mIndexBuffer.put(INDICES);
        mIndexBuffer.position(0);

        mProgram = GLES20.glCreateProgram();
        GLES20.glAttachShader(mProgram, loadShader(GLES20.GL_VERTEX_SHADER, VERTEX_SHADER_CODE));
        GLES20.glAttachShader(
                mProgram, loadShader(GLES20.GL_FRAGMENT_SHADER, FRAGMENT_SHADER_CODE));
        GLES20.glLinkProgram(mProgram);

        mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
        mColorHandle = GLES20.glGetAttribLocation(mProgram, "vColor");
        mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
    }

    /**
     * Encapsulates the OpenGL ES instructions for drawing this shape.
     *
     * @param mvpMatrix The Model View Project matrix in which to draw this shape
     */
    public void draw(float[] mvpMatrix) {
        // Add program to OpenGL environment.
        GLES20.glUseProgram(mProgram);

        // Prepare the cube coordinate data.
        GLES20.glEnableVertexAttribArray(mPositionHandle);
        GLES20.glVertexAttribPointer(
                mPositionHandle, 3, GLES20.GL_FLOAT, false, VERTEX_STRIDE, mVertexBuffer);

        // Prepare the cube color data.
        GLES20.glEnableVertexAttribArray(mColorHandle);
        GLES20.glVertexAttribPointer(
                mColorHandle, 4, GLES20.GL_FLOAT, false, COLOR_STRIDE, mColorBuffer);

        // Apply the projection and view transformation.
        GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);

        // Draw the cube.
        GLES20.glDrawElements(
                GLES20.GL_TRIANGLES, INDICES.length, GLES20.GL_UNSIGNED_BYTE, mIndexBuffer);

        // Disable vertex arrays.
        GLES20.glDisableVertexAttribArray(mPositionHandle);
        GLES20.glDisableVertexAttribArray(mColorHandle);
    }

    /** Loads the provided shader in the program. */
    private static int loadShader(int type, String shaderCode){
        int shader = GLES20.glCreateShader(type);

        GLES20.glShaderSource(shader, shaderCode);
        GLES20.glCompileShader(shader);

        return shader;
    }
}

लाइव कार्ड पर फ़ोकस करने के लिए

जब LiveCard.publish() से लाइव कार्ड पब्लिश किया जाता है, तो आपको यह एक पैरामीटर पास करना होता है. इससे यह कंट्रोल किया जा सकता है कि फ़ोकस तुरंत चालू हो या नहीं.

टाइमलाइन प्रकाशित करने के तुरंत बाद कार्ड पर जाने के लिए, LiveCard.PublishMode.REVEAL का इस्तेमाल करें. बिना आवाज़ के कार्ड को पब्लिश करने और कार्ड पर उपयोगकर्ताओं को ले जाने के लिए, LiveCard.PublishMode.SILENT का इस्तेमाल करें.

इसके अलावा, LiveCard.navigate() कार्ड से आप प्रकाशित होने के बाद सीधे उस पर जा सकते हैं. उदाहरण के लिए, अगर उपयोगकर्ता मुख्य वॉइस मेन्यू से आपका लाइव कार्ड शुरू करने की कोशिश करते हैं, तो लाइव कार्ड पर जाने का तरीका है.

मेन्यू बनाना और दिखाना

लाइव कार्ड अपना मेन्यू सिस्टम नहीं दिखा सकते, इसलिए आपको लाइव कार्ड के लिए मेन्यू दिखाने के लिए एक गतिविधि बनानी होगी.

इसके बाद मेन्यू की गतिविधि में ऐसे आइटम हो सकते हैं जो लाइव कार्ड को रोकने, इमर्सिव या ऐसी कोई अन्य कार्रवाई करने के लिए ज़रूरी हैं जिन्हें आपको पूरा करना है. आप मेन्यू सेटिंग के रूप में सिस्टम सेटिंग गतिविधियां भी जोड़ सकते हैं, जैसे कि वॉल्यूम नियंत्रण. ज़्यादा जानकारी के लिए, सेटिंग शुरू करना देखें.

मेन्यू के रिसॉर्स बनाना

मेन्यू के रिसॉर्स, Android प्लैटफ़ॉर्म पर बनाने के जैसे ही होते हैं, लेकिन Glass के लिए इन दिशा-निर्देशों का पालन करें:

  • हर मेन्यू आइटम के लिए, 50 × 50 पिक्सल वाला मेन्यू आइटम आइकॉन दें. मेन्यू आइकॉन सफ़ेद रंग में पारदर्शी बैकग्राउंड पर होना चाहिए. उदाहरण के लिए या अपने उपयोग के लिए उन्हें डाउनलोड करने के लिए Glass मेन्यू आइटम आइकॉन देखें.
  • किसी ऐसे छोटे नाम का इस्तेमाल करें जो कार्रवाई के बारे में बताता हो और जो शीर्षक के केस में हो. इंपेरिटिव क्रिया अच्छी तरह से काम करती है (उदाहरण के लिए, शेयर करें या सभी को जवाब दें).
  • Glass, मेनू आइटम के बिना लाइव कार्ड प्रदर्शित नहीं करता है. कम से कम, एक स्टॉप मेन्यू आइटम दें, ताकि उपयोगकर्ता टाइमलाइन से लाइव कार्ड हटा सकें.
  • CheckBox विजेट काम नहीं करता.

    <menu xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:id="@+id/menu_item_1"
            android:title="@string/Menu_Item_1"       <!-- must have "Stop" menu item -->
            android:icon="@drawable/menu_item_1_icon" />   <!-- white on transparent icon -->
    </menu>
    

मेन्यू के कॉलबैक हैंडल के लिए, गतिविधि बनाना

आपको मेन्यू की ऐसी गतिविधि तय करनी होगी जिसे आपका लाइव कार्ड तब ट्रिगर करेगा, जब उपयोगकर्ता उस पर टैप करेंगे.

मेन्यू गतिविधि में मेन्यू बनाने, दिखाने, और खारिज करने के लिए, कॉलबैक के इन तरीकों Activity को बदलें:

  1. onCreateOptionsMenu() एक्सएमएल मेन्यू रिसॉर्स को इनफ़्लेट करता है.
  2. जब किसी गतिविधि पर फ़ोकस हो, तो onAttachedToWindow() मेन्यू दिखाता है.
  3. onPrepareOptionsMenu() ज़रूरी होने पर मेन्यू आइटम दिखाता या छिपाता है. उदाहरण के लिए, उपयोगकर्ता क्या कर रहे हैं, इनके आधार पर आप अलग-अलग आइटम दिखा सकते हैं. उदाहरण के लिए, कुछ प्रासंगिक डेटा के आधार पर आप अलग-अलग आइटम दिखा सकते हैं.
  4. onOptionsItemSelected() उपयोगकर्ताओं को चुनता है.
  5. onOptionsMenuClosed() गतिविधि को पूरा करने के लिए, ताकि यह लाइव कार्ड पर दिखाई न दे.

आपको यहां पर गतिविधि पूरी करनी होगी, ताकि यह किसी मेन्यू या किसी मेन्यू को बंद करने या स्वाइप करके बंद हो जाए.

/**
 * Activity showing the options menu.
 */
public class MenuActivity extends Activity {

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        openOptionsMenu();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.stopwatch, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection.
        switch (item.getItemId()) {
            case R.id.stop:
                stopService(new Intent(this, StopwatchService.class));
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onOptionsMenuClosed(Menu menu) {
        // Nothing else to do, closing the activity.
        finish();
    }
}

मेन्यू की गतिविधि को पारदर्शी बनाना

कांच की शैली का इस्तेमाल करने के लिए, मेन्यू की गतिविधि को पारदर्शी बनाएं, ताकि मेन्यू के नीचे लाइव कार्ड दिखता रहे:

  1. एक res/values/styles.xml फ़ाइल बनाएं और ऐसी स्टाइल की जानकारी दें जो गतिविधि के बैकग्राउंड को पारदर्शी बनाए.

    <resources>
        <style name="MenuTheme" parent="@android:style/Theme.DeviceDefault">
            <item name="android:windowBackground">@android:color/transparent</item>
            <item name="android:colorBackgroundCacheHint">@null</item>
            <item name="android:windowIsTranslucent">true</item>
            <item name="android:windowAnimationStyle">@null</item>
        </style>
    </resources>
    
  2. अपनी AndroidManifest.xml फ़ाइल में, मेन्यू गतिविधि के लिए थीम असाइन करें:

    <?xml version="1.0" encoding="utf-8"?>
        <manifest ... >
          ...
            <application ... >
                ...
                <activity
                    android:name=".MenuActivity"
                    android:theme="@style/MenuTheme"
                    ...>
                </activity>
            </application>
    
        </manifest>
    

मेन्यू दिखाना

कार्ड का इस्तेमाल करने के लिए, setAction() का इस्तेमाल करके PendingIntent दें. जब कोई उपयोगकर्ता कार्ड पर टैप करता है, तो मेन्यू की गतिविधि शुरू करने के लिए, 'मंज़ूरी बाकी है' इंटेंट का इस्तेमाल किया जाता है:

Intent menuIntent = new Intent(this, MenuActivity.class);
mLiveCard.setAction(PendingIntent.getActivity(this, 0, menuIntent, 0));
mLiveCard.publish(LiveCard.PublishMode.REVEAL); // or SILENT

बोलकर दिए जाने वाले काम के निर्देशों का इस्तेमाल करना

  1. बताएं कि आपका MenuActivity बोलकर दिए जाने वाले काम के निर्देश काम करता है:

    // Initialize your LiveCard as usual.
    mLiveCard.setVoiceActionEnabled(true);
    mLiveCard.publish(LiveCard.PublishMode.REVEAL); // or SILENT
    
  2. वॉइस फ़्लो के ज़रिए शुरू करने के लिए अपने MenuActivity में बदलाव करें:

    /**
     * Activity showing the options menu.
     */
    public class MenuActivity extends Activity {
    
        private boolean mFromLiveCardVoice;
        private boolean mIsFinishing;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mFromLiveCardVoice =
                    getIntent().getBooleanExtra(LiveCard.EXTRA_FROM_LIVECARD_VOICE, false);
            if (mFromLiveCardVoice) {
                // When activated by voice from a live card, enable voice commands. The menu
                // will automatically "jump" ahead to the items (skipping the guard phrase
                // that was already said at the live card).
                getWindow().requestFeature(WindowUtils.FEATURE_VOICE_COMMANDS);
            }
        }
    
        @Override
        public void onAttachedToWindow() {
            super.onAttachedToWindow();
            if (!mFromLiveCardVoice) {
                openOptionsMenu();
            }
        }
    
        @Override
        public boolean onCreatePanelMenu(int featureId, Menu menu) {
            if (isMyMenu(featureId)) {
                getMenuInflater().inflate(R.menu.stopwatch, menu);
                return true;
            }
            return super.onCreatePanelMenu(featureId, menu);
        }
    
        @Override
        public boolean onPreparePanel(int featureId, View view, Menu menu) {
            if (isMyMenu(featureId)) {
                // Don't reopen menu once we are finishing. This is necessary
                // since voice menus reopen themselves while in focus.
                return !mIsFinishing;
            }
            return super.onPreparePanel(featureId, view, menu);
        }
    
        @Override
        public boolean onMenuItemSelected(int featureId, MenuItem item) {
            if (isMyMenu(featureId)) {
                // Handle item selection.
                switch (item.getItemId()) {
                    case R.id.stop_this:
                        stopService(new Intent(this, StopwatchService.class));
                        return true;
                }
            }
            return super.onMenuItemSelected(featureId, item);
        }
    
        @Override
        public void onPanelClosed(int featureId, Menu menu) {
            super.onPanelClosed(featureId, menu);
            if (isMyMenu(featureId)) {
                // When the menu panel closes, either an item is selected from the menu or the
                // menu is dismissed by swiping down. Either way, we end the activity.
                isFinishing = true;
                finish();
            }
        }
    
        /**
         * Returns {@code true} when the {@code featureId} belongs to the options menu or voice
         * menu that are controlled by this menu activity.
         */
        private boolean isMyMenu(int featureId) {
            return featureId == Window.FEATURE_OPTIONS_PANEL ||
                   featureId == WindowUtils.FEATURE_VOICE_COMMANDS;
        }
    }
    

ज़्यादा जानकारी के लिए, बोलकर दिए जाने वाले निर्देश देखें.

मेन्यू के रंग-रूप और व्यवहार में बदलाव करने के लिए, हेल्पर के कुछ तरीके उपलब्ध हैं. ज़्यादा जानकारी के लिए, MenuUtils देखें.