1 package ro
.ieval
.fonbot
;
3 import static ro
.ieval
.fonbot
.Utils
.toNonNull
;
4 import static ro
.ieval
.fonbot
.Address
.Protocol
.LOCAL
;
6 import org
.eclipse
.jdt
.annotation
.Nullable
;
8 import android
.app
.Activity
;
9 import android
.content
.BroadcastReceiver
;
10 import android
.content
.Context
;
11 import android
.content
.Intent
;
12 import android
.content
.IntentFilter
;
13 import android
.os
.Bundle
;
14 import android
.view
.KeyEvent
;
15 import android
.view
.inputmethod
.EditorInfo
;
16 import android
.widget
.EditText
;
17 import android
.widget
.TextView
;
18 import android
.widget
.TextView
.OnEditorActionListener
;
21 * Copyright © 2013 Marius Gavrilescu
23 * This file is part of FonBot.
25 * FonBot is free software: you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation, either version 3 of the License, or
28 * (at your option) any later version.
30 * FonBot is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License
36 * along with FonBot. If not, see <http://www.gnu.org/licenses/>.
40 * Activity that lets users send commands from inside the app.
42 * @author Marius Gavrilescu <marius@ieval.ro>
44 public final class FonBotLocalActivity
extends Activity
{
46 * BroadcastReceiver that receives command responses.
48 * @author Marius Gavrilescu <marius@ieval.ro>
50 private final class ReplyReceiver
extends BroadcastReceiver
{
53 public void onReceive(final @Nullable Context context
, final @Nullable Intent intent
) {
54 if (context
== null || intent
== null) return;
55 consoleTextView
.setText(consoleTextView
.getText() +
56 intent
.getStringExtra(EXTRA_RESPONSE
) + "\n");
61 * The extra name for the command response.
63 * @see #RESPONSE_RECEIVED_ACTION
65 public static final String EXTRA_RESPONSE
= "response";
67 * The action for sending back responses to commands.
69 public static final String RESPONSE_RECEIVED_ACTION
="ro.ieval.fonbot.LOCAL_RESPONSE";
71 * Intent filter that catches {@link #RESPONSE_RECEIVED_ACTION}
73 private static final IntentFilter INTENT_FILTER
= new IntentFilter(RESPONSE_RECEIVED_ACTION
);
75 * Reply address for local commands
77 private static final Address LOCAL_ADDRESS
=new Address(toNonNull(LOCAL
), null);
79 * The one instance of {@link ReplyReceiver}
81 private final ReplyReceiver replyReceiver
= new ReplyReceiver();
83 * The TextView that contains command responses
85 private TextView consoleTextView
;
88 protected void onCreate(final @Nullable Bundle savedInstanceState
) {
89 super.onCreate(savedInstanceState
);
90 setContentView(R
.layout
.local
);
92 consoleTextView
= (TextView
) findViewById(R
.id
.consoleTextView
);
93 final EditText commandEditText
= (EditText
) findViewById(R
.id
.argEditText
);
94 commandEditText
.setOnEditorActionListener(new OnEditorActionListener() {
96 public boolean onEditorAction(final @Nullable TextView v
, final int actionId
, final @Nullable KeyEvent event
) {
97 if(actionId
==EditorInfo
.IME_ACTION_SEND
||
98 actionId
==EditorInfo
.IME_NULL
){
99 final String
[] commandWithArgs
= Utils
.shellwords(toNonNull(commandEditText
.getText().toString()));
100 final String command
=commandWithArgs
[0];
101 final String
[] args
=new String
[commandWithArgs
.length
-1];
102 System
.arraycopy(commandWithArgs
, 1, args
, 0, args
.length
);
103 commandEditText
.setText("");
104 Utils
.processCommand(FonBotLocalActivity
.this, toNonNull(command
), args
,
105 toNonNull(LOCAL_ADDRESS
));
114 protected void onResume() {
116 this.registerReceiver(replyReceiver
, INTENT_FILTER
);
120 protected void onPause() {
122 this.unregisterReceiver(replyReceiver
);