• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
onCreate(Bundle saved)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(ResponderService.ACTION_INCOMING);
59     }
60 
startResponderService(String action)61     private void startResponderService(String action) {
62         Intent serviceIntent = new Intent(this, ResponderService.class);
63         serviceIntent.setAction(action);
64         startService(serviceIntent);
65     }
66 
67     @Override
onResume()68     protected void onResume() {
69         super.onResume();
70         LocalBroadcastManager.getInstance(this).registerReceiver(mReceiver,
71                 new IntentFilter(ACTION_NOTIFY));
72         mHistoryView.setText("");
73         startResponderService(ACTION_GET_CONVERSATION);
74     }
75 
76     @Override
onPause()77     protected void onPause() {
78         LocalBroadcastManager.getInstance(this).unregisterReceiver(mReceiver);
79         super.onPause();
80     }
81 
processMessage(Intent intent)82     private void processMessage(Intent intent) {
83         String text = intent.getStringExtra(EXTRA_MESSAGE);
84         if (!TextUtils.isEmpty(text)) {
85             mHistoryView.append("\n" + text);
86         }
87     }
88 
89     @Override
onCreateOptionsMenu(Menu menu)90     public boolean onCreateOptionsMenu(Menu menu) {
91         super.onCreateOptionsMenu(menu);
92         getMenuInflater().inflate(R.menu.main, menu);
93         return true;
94     }
95 
96     @Override
onOptionsItemSelected(MenuItem item)97     public boolean onOptionsItemSelected(MenuItem item) {
98         switch (item.getItemId()) {
99             case R.id.action_stop_service:
100                 stopService(new Intent(this, ResponderService.class));
101                 finish();
102                 break;
103         }
104         return true;
105     }
106 }
107