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.messagingservice;
18 
19 import android.app.Fragment;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.ServiceConnection;
24 import android.content.SharedPreferences;
25 import android.os.Bundle;
26 import android.os.IBinder;
27 import android.os.Message;
28 import android.os.Messenger;
29 import android.os.RemoteException;
30 import android.text.method.ScrollingMovementMethod;
31 import android.util.Log;
32 import android.view.LayoutInflater;
33 import android.view.View;
34 import android.view.ViewGroup;
35 import android.widget.Button;
36 import android.widget.TextView;
37 
38 /**
39  * The main fragment that shows the buttons and the text view containing the log.
40  */
41 public class MessagingFragment extends Fragment implements View.OnClickListener {
42 
43     private static final String TAG = MessagingFragment.class.getSimpleName();
44 
45     private Button mSendSingleConversation;
46     private Button mSendTwoConversations;
47     private Button mSendConversationWithThreeMessages;
48     private TextView mDataPortView;
49     private Button mClearLogButton;
50 
51     private Messenger mService;
52     private boolean mBound;
53 
54     private final ServiceConnection mConnection = new ServiceConnection() {
55         @Override
56         public void onServiceConnected(ComponentName componentName, IBinder service) {
57             mService = new Messenger(service);
58             mBound = true;
59             setButtonsState(true);
60         }
61 
62         @Override
63         public void onServiceDisconnected(ComponentName componentName) {
64             mService = null;
65             mBound = false;
66             setButtonsState(false);
67         }
68     };
69 
70     private final SharedPreferences.OnSharedPreferenceChangeListener listener =
71             new SharedPreferences.OnSharedPreferenceChangeListener() {
72         @Override
73         public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
74             if (MessageLogger.LOG_KEY.equals(key)) {
75                 mDataPortView.setText(MessageLogger.getAllMessages(getActivity()));
76             }
77         }
78     };
79 
MessagingFragment()80     public MessagingFragment() {
81     }
82 
83     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)84     public View onCreateView(LayoutInflater inflater, ViewGroup container,
85                              Bundle savedInstanceState) {
86         View rootView = inflater.inflate(R.layout.fragment_message_me, container, false);
87 
88         mSendSingleConversation = (Button) rootView.findViewById(R.id.send_1_conversation);
89         mSendSingleConversation.setOnClickListener(this);
90 
91         mSendTwoConversations = (Button) rootView.findViewById(R.id.send_2_conversations);
92         mSendTwoConversations.setOnClickListener(this);
93 
94         mSendConversationWithThreeMessages =
95                 (Button) rootView.findViewById(R.id.send_1_conversation_3_messages);
96         mSendConversationWithThreeMessages.setOnClickListener(this);
97 
98         mDataPortView = (TextView) rootView.findViewById(R.id.data_port);
99         mDataPortView.setMovementMethod(new ScrollingMovementMethod());
100 
101         mClearLogButton = (Button) rootView.findViewById(R.id.clear);
102         mClearLogButton.setOnClickListener(this);
103 
104         setButtonsState(false);
105 
106         return rootView;
107     }
108 
109     @Override
onClick(View view)110     public void onClick(View view) {
111         if (view == mSendSingleConversation) {
112             sendMsg(1, 1);
113         } else if (view == mSendTwoConversations) {
114             sendMsg(2, 1);
115         } else if (view == mSendConversationWithThreeMessages) {
116             sendMsg(1, 3);
117         } else if (view == mClearLogButton) {
118             MessageLogger.clear(getActivity());
119             mDataPortView.setText(MessageLogger.getAllMessages(getActivity()));
120         }
121     }
122 
123     @Override
onStart()124     public void onStart() {
125         super.onStart();
126         getActivity().bindService(new Intent(getActivity(), MessagingService.class), mConnection,
127                 Context.BIND_AUTO_CREATE);
128     }
129 
130     @Override
onPause()131     public void onPause() {
132         super.onPause();
133         MessageLogger.getPrefs(getActivity()).unregisterOnSharedPreferenceChangeListener(listener);
134     }
135 
136     @Override
onResume()137     public void onResume() {
138         super.onResume();
139         mDataPortView.setText(MessageLogger.getAllMessages(getActivity()));
140         MessageLogger.getPrefs(getActivity()).registerOnSharedPreferenceChangeListener(listener);
141     }
142 
143     @Override
onStop()144     public void onStop() {
145         super.onStop();
146         if (mBound) {
147             getActivity().unbindService(mConnection);
148             mBound = false;
149         }
150     }
151 
sendMsg(int howManyConversations, int messagesPerConversation)152     private void sendMsg(int howManyConversations, int messagesPerConversation) {
153         if (mBound) {
154             Message msg = Message.obtain(null, MessagingService.MSG_SEND_NOTIFICATION,
155                     howManyConversations, messagesPerConversation);
156             try {
157                 mService.send(msg);
158             } catch (RemoteException e) {
159                 Log.e(TAG, "Error sending a message", e);
160                 MessageLogger.logMessage(getActivity(), "Error occurred while sending a message.");
161             }
162         }
163     }
164 
setButtonsState(boolean enable)165     private void setButtonsState(boolean enable) {
166         mSendSingleConversation.setEnabled(enable);
167         mSendTwoConversations.setEnabled(enable);
168         mSendConversationWithThreeMessages.setEnabled(enable);
169     }
170 }
171