Android Open Source - AndroidWearable-Samples Main Activity






From Project

Back to project page AndroidWearable-Samples.

License

The source code is released under:

Apache License

If you think the Android project AndroidWearable-Samples listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.example.android.wearable.datalayer;
// w w w. ja  v a 2 s .  c  o m
import static com.example.android.wearable.datalayer.DataLayerListenerService.LOGD;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.common.data.FreezableUtils;
import com.google.android.gms.wearable.Asset;
import com.google.android.gms.wearable.DataApi;
import com.google.android.gms.wearable.DataEvent;
import com.google.android.gms.wearable.DataEventBuffer;
import com.google.android.gms.wearable.DataMapItem;
import com.google.android.gms.wearable.MessageApi;
import com.google.android.gms.wearable.MessageEvent;
import com.google.android.gms.wearable.Node;
import com.google.android.gms.wearable.NodeApi;
import com.google.android.gms.wearable.Wearable;

import java.io.InputStream;
import java.util.List;

/**
 * Shows events and photo from the Wearable APIs.
 */
public class MainActivity extends Activity implements ConnectionCallbacks,
        OnConnectionFailedListener, DataApi.DataListener, MessageApi.MessageListener,
        NodeApi.NodeListener {

    private static final String TAG = "MainActivity";

    private GoogleApiClient mGoogleApiClient;
    private ListView mDataItemList;
    private TextView mIntroText;
    private DataItemAdapter mDataItemListAdapter;
    private View mLayout;
    private Handler mHandler;

    @Override
    public void onCreate(Bundle b) {
        super.onCreate(b);
        mHandler = new Handler();
        LOGD(TAG, "onCreate");
        setContentView(R.layout.main_activity);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        mDataItemList = (ListView) findViewById(R.id.dataItem_list);
        mIntroText = (TextView) findViewById(R.id.intro);
        mLayout = findViewById(R.id.layout);

        // Stores data events received by the local broadcaster.
        mDataItemListAdapter = new DataItemAdapter(this, android.R.layout.simple_list_item_1);
        mDataItemList.setAdapter(mDataItemListAdapter);

        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Wearable.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mGoogleApiClient.connect();
    }

    @Override
    protected void onPause() {
        super.onPause();
        Wearable.DataApi.removeListener(mGoogleApiClient, this);
        Wearable.MessageApi.removeListener(mGoogleApiClient, this);
        Wearable.NodeApi.removeListener(mGoogleApiClient, this);
        mGoogleApiClient.disconnect();
    }

    @Override
    public void onConnected(Bundle connectionHint) {
        LOGD(TAG, "onConnected(): Successfully connected to Google API client");
        Wearable.DataApi.addListener(mGoogleApiClient, this);
        Wearable.MessageApi.addListener(mGoogleApiClient, this);
        Wearable.NodeApi.addListener(mGoogleApiClient, this);
    }

    @Override
    public void onConnectionSuspended(int cause) {
        LOGD(TAG, "onConnectionSuspended(): Connection to Google API client was suspended");
    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        Log.e(TAG, "onConnectionFailed(): Failed to connect, with result: " + result);
    }

    private void generateEvent(final String title, final String text) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mIntroText.setVisibility(View.INVISIBLE);
                mDataItemListAdapter.add(new Event(title, text));
            }
        });
    }

    @Override
    public void onDataChanged(DataEventBuffer dataEvents) {
        LOGD(TAG, "onDataChanged(): " + dataEvents);

        final List<DataEvent> events = FreezableUtils.freezeIterable(dataEvents);
        dataEvents.close();
        for (DataEvent event : events) {
            if (event.getType() == DataEvent.TYPE_CHANGED) {
                String path = event.getDataItem().getUri().getPath();
                if (DataLayerListenerService.IMAGE_PATH.equals(path)) {
                    DataMapItem dataMapItem = DataMapItem.fromDataItem(event.getDataItem());
                    Asset photo = dataMapItem.getDataMap()
                            .getAsset(DataLayerListenerService.IMAGE_KEY);
                    final Bitmap bitmap = loadBitmapFromAsset(mGoogleApiClient, photo);
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            Log.d(TAG, "Setting background image..");
                            mLayout.setBackground(new BitmapDrawable(getResources(), bitmap));
                        }
                    });

                } else if (DataLayerListenerService.COUNT_PATH.equals(path)) {
                    LOGD(TAG, "Data Changed for COUNT_PATH");
                    generateEvent("DataItem Changed", event.getDataItem().toString());
                } else {
                    LOGD(TAG, "Unrecognized path: " + path);
                }

            } else if (event.getType() == DataEvent.TYPE_DELETED) {
                generateEvent("DataItem Deleted", event.getDataItem().toString());
            } else {
                generateEvent("Unknown data event type", "Type = " + event.getType());
            }
        }
    }

    /**
     * Extracts {@link android.graphics.Bitmap} data from the
     * {@link com.google.android.gms.wearable.Asset}
     */
    private Bitmap loadBitmapFromAsset(GoogleApiClient apiClient, Asset asset) {
        if (asset == null) {
            throw new IllegalArgumentException("Asset must be non-null");
        }

        InputStream assetInputStream = Wearable.DataApi.getFdForAsset(
                apiClient, asset).await().getInputStream();

        if (assetInputStream == null) {
            Log.w(TAG, "Requested an unknown Asset.");
            return null;
        }
        return BitmapFactory.decodeStream(assetInputStream);
    }

    @Override
    public void onMessageReceived(MessageEvent event) {
        LOGD(TAG, "onMessageReceived: " + event);
        generateEvent("Message", event.toString());
    }

    @Override
    public void onPeerConnected(Node node) {
        generateEvent("Node Connected", node.getId());
    }

    @Override
    public void onPeerDisconnected(Node node) {
        generateEvent("Node Disconnected", node.getId());
    }

    private static class DataItemAdapter extends ArrayAdapter<Event> {

        private final Context mContext;

        public DataItemAdapter(Context context, int unusedResource) {
            super(context, unusedResource);
            mContext = context;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(
                        Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(android.R.layout.two_line_list_item, null);
                convertView.setTag(holder);
                holder.text1 = (TextView) convertView.findViewById(android.R.id.text1);
                holder.text2 = (TextView) convertView.findViewById(android.R.id.text2);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            Event event = getItem(position);
            holder.text1.setText(event.title);
            holder.text2.setText(event.text);
            return convertView;
        }

        private class ViewHolder {

            TextView text1;
            TextView text2;
        }
    }

    private class Event {

        String title;
        String text;

        public Event(String title, String text) {
            this.title = title;
            this.text = text;
        }
    }
}




Java Source Code List

com.example.android.google.wearable.app.GridExampleActivity.java
com.example.android.google.wearable.app.MainActivity.java
com.example.android.google.wearable.watchviewstub.MainActivity.java
com.example.android.support.wearable.notifications.ActionsPreset.java
com.example.android.support.wearable.notifications.ActionsPresets.java
com.example.android.support.wearable.notifications.AnimatedNotificationDisplayActivity.java
com.example.android.support.wearable.notifications.BackgroundPickers.java
com.example.android.support.wearable.notifications.BasicNotificationDisplayActivity.java
com.example.android.support.wearable.notifications.MainActivity.java
com.example.android.support.wearable.notifications.MainActivity.java
com.example.android.support.wearable.notifications.NamedPreset.java
com.example.android.support.wearable.notifications.NotificationIntentReceiver.java
com.example.android.support.wearable.notifications.NotificationPreset.java
com.example.android.support.wearable.notifications.NotificationPreset.java
com.example.android.support.wearable.notifications.NotificationPresets.java
com.example.android.support.wearable.notifications.NotificationPresets.java
com.example.android.support.wearable.notifications.NotificationUtil.java
com.example.android.support.wearable.notifications.PriorityPreset.java
com.example.android.support.wearable.notifications.PriorityPresets.java
com.example.android.support.wearable.notifications.WearableListItemLayout.java
com.example.android.wearable.agendadata.CalendarQueryService.java
com.example.android.wearable.agendadata.Constants.java
com.example.android.wearable.agendadata.Constants.java
com.example.android.wearable.agendadata.DeleteService.java
com.example.android.wearable.agendadata.HomeListenerService.java
com.example.android.wearable.agendadata.MainActivity.java
com.example.android.wearable.datalayer.DataLayerListenerService.java
com.example.android.wearable.datalayer.MainActivity.java
com.example.android.wearable.datalayer.MainActivity.java
com.example.android.wearable.delayedconfirmation.MainActivity.java
com.example.android.wearable.delayedconfirmation.MainActivity.java
com.example.android.wearable.delayedconfirmation.WearableMessageListenerService.java
com.example.android.wearable.elizachat.ElizaResponder.java
com.example.android.wearable.elizachat.MainActivity.java
com.example.android.wearable.elizachat.ResponderService.java
com.example.android.wearable.embeddedapp.PhoneActivity.java
com.example.android.wearable.embeddedapp.WearableActivity.java
com.example.android.wearable.findphone.DisconnectListenerService.java
com.example.android.wearable.findphone.FindPhoneActivity.java
com.example.android.wearable.findphone.FindPhoneService.java
com.example.android.wearable.findphone.SoundAlarmListenerService.java
com.example.android.wearable.flashlight.MainActivity.java
com.example.android.wearable.flashlight.PartyLightView.java
com.example.android.wearable.geofencing.CheckInAndDeleteDataItemsService.java
com.example.android.wearable.geofencing.Constants.java
com.example.android.wearable.geofencing.Constants.java
com.example.android.wearable.geofencing.GeofenceTransitionsIntentService.java
com.example.android.wearable.geofencing.HomeListenerService.java
com.example.android.wearable.geofencing.MainActivity.java
com.example.android.wearable.geofencing.SimpleGeofenceStore.java
com.example.android.wearable.geofencing.SimpleGeofence.java
com.example.android.wearable.gridviewpager.MainActivity.java
com.example.android.wearable.gridviewpager.SampleGridPagerAdapter.java
com.example.android.wearable.jumpingjack.MainActivity.java
com.example.android.wearable.jumpingjack.PagerAdapter.java
com.example.android.wearable.jumpingjack.Utils.java
com.example.android.wearable.jumpingjack.fragments.CounterFragment.java
com.example.android.wearable.jumpingjack.fragments.SettingsFragment.java
com.example.android.wearable.quiz.Constants.java
com.example.android.wearable.quiz.Constants.java
com.example.android.wearable.quiz.DeleteQuestionService.java
com.example.android.wearable.quiz.JsonUtils.java
com.example.android.wearable.quiz.MainActivity.java
com.example.android.wearable.quiz.QuizListenerService.java
com.example.android.wearable.quiz.QuizReportActionService.java
com.example.android.wearable.quiz.UpdateQuestionService.java
com.example.android.wearable.recipeassistant.AssetUtils.java
com.example.android.wearable.recipeassistant.Constants.java
com.example.android.wearable.recipeassistant.MainActivity.java
com.example.android.wearable.recipeassistant.RecipeActivity.java
com.example.android.wearable.recipeassistant.RecipeListAdapter.java
com.example.android.wearable.recipeassistant.RecipeService.java
com.example.android.wearable.recipeassistant.Recipe.java
com.example.android.wearable.synchronizednotifications.DismissListener.java
com.example.android.wearable.synchronizednotifications.NotificationUpdateService.java
com.example.android.wearable.synchronizednotifications.PhoneActivity.java
com.example.android.wearable.synchronizednotifications.WearableActivity.java
com.example.android.wearable.synchronizednotifications.common.Constants.java
com.example.android.wearable.timer.SetTimerActivity.java
com.example.android.wearable.timer.TimerNotificationService.java
com.example.android.wearable.timer.util.Constants.java
com.example.android.wearable.timer.util.TimerFormat.java
com.example.android.wearable.timer.util.TimerObj.java