Home | History | Annotate | Download | only in elizachat
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.wearable.elizachat;
     18 
     19 import android.app.Activity;
     20 import android.content.BroadcastReceiver;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.content.IntentFilter;
     24 import android.os.Bundle;
     25 import android.support.v4.content.LocalBroadcastManager;
     26 import android.text.TextUtils;
     27 import android.view.Menu;
     28 import android.view.MenuItem;
     29 import android.widget.TextView;
     30 
     31 public class MainActivity extends Activity {
     32 
     33     @SuppressWarnings("unused")
     34     private static final String TAG = "MainActivity";
     35 
     36     public static final String EXTRA_MESSAGE = "message";
     37 
     38     public static final String ACTION_NOTIFY = "com.example.android.wearable.elizachat.NOTIFY";
     39 
     40     public static final String ACTION_GET_CONVERSATION
     41             = "com.example.android.wearable.elizachat.CONVERSATION";
     42 
     43     private BroadcastReceiver mReceiver;
     44 
     45     private TextView mHistoryView;
     46 
     47     @Override
     48     protected void onCreate(Bundle saved) {
     49         super.onCreate(saved);
     50         setContentView(R.layout.main);
     51         mReceiver = new BroadcastReceiver() {
     52             @Override
     53             public void onReceive(Context context, Intent intent) {
     54                 processMessage(intent);
     55             }
     56         };
     57         mHistoryView = (TextView) findViewById(R.id.history);
     58         startResponderService();
     59     }
     60 
     61     private void startResponderService() {
     62         Intent serviceIntent = new Intent(ResponderService.ACTION_INCOMING);
     63         startService(serviceIntent);
     64     }
     65 
     66     @Override
     67     protected void onResume() {
     68         super.onResume();
     69         LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver,
     70                 new IntentFilter(ACTION_NOTIFY));
     71         mHistoryView.setText("");
     72         Intent serviceIntent = new Intent(ACTION_GET_CONVERSATION);
     73         startService(serviceIntent);
     74 
     75     }
     76 
     77     @Override
     78     protected void onPause() {
     79         LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
     80         super.onPause();
     81     }
     82 
     83     private void processMessage(Intent intent) {
     84         String text = intent.getStringExtra(EXTRA_MESSAGE);
     85         if (!TextUtils.isEmpty(text)) {
     86             mHistoryView.append("\n" + text);
     87         }
     88     }
     89 
     90     @Override
     91     public boolean onCreateOptionsMenu(Menu menu) {
     92         super.onCreateOptionsMenu(menu);
     93         getMenuInflater().inflate(R.menu.main, menu);
     94         return true;
     95     }
     96 
     97     @Override
     98     public boolean onOptionsItemSelected(MenuItem item) {
     99         switch (item.getItemId()) {
    100             case R.id.action_stop_service:
    101                 stopService(new Intent(this, ResponderService.class));
    102                 finish();
    103                 break;
    104         }
    105         return true;
    106     }
    107 }
    108