Initial commit
[fonbot.git] / src / ro / ieval / fonbot / FonBotLocalActivity.java
1 package ro.ieval.fonbot;
2
3 import static ro.ieval.fonbot.Utils.toNonNull;
4 import static ro.ieval.fonbot.Address.Protocol.LOCAL;
5
6 import org.eclipse.jdt.annotation.Nullable;
7
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;
19
20 /*
21 * Copyright © 2013 Marius Gavrilescu
22 *
23 * This file is part of FonBot.
24 *
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.
29 *
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.
34 *
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/>.
37 */
38
39 /**
40 * Activity that lets users send commands from inside the app.
41 *
42 * @author Marius Gavrilescu <marius@ieval.ro>
43 */
44 public final class FonBotLocalActivity extends Activity {
45 /**
46 * BroadcastReceiver that receives command responses.
47 *
48 * @author Marius Gavrilescu <marius@ieval.ro>
49 */
50 private final class ReplyReceiver extends BroadcastReceiver {
51
52 @Override
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");
57 }
58 }
59
60 /**
61 * The extra name for the command response.
62 *
63 * @see #RESPONSE_RECEIVED_ACTION
64 */
65 public static final String EXTRA_RESPONSE = "response";
66 /**
67 * The action for sending back responses to commands.
68 */
69 public static final String RESPONSE_RECEIVED_ACTION="ro.ieval.fonbot.LOCAL_RESPONSE";
70 /**
71 * Intent filter that catches {@link #RESPONSE_RECEIVED_ACTION}
72 */
73 private static final IntentFilter INTENT_FILTER = new IntentFilter(RESPONSE_RECEIVED_ACTION);
74 /**
75 * Reply address for local commands
76 */
77 private static final Address LOCAL_ADDRESS=new Address(toNonNull(LOCAL), null);
78 /**
79 * The one instance of {@link ReplyReceiver}
80 */
81 private final ReplyReceiver replyReceiver = new ReplyReceiver();
82 /**
83 * The TextView that contains command responses
84 */
85 private TextView consoleTextView;
86
87 @Override
88 protected void onCreate(final @Nullable Bundle savedInstanceState) {
89 super.onCreate(savedInstanceState);
90 setContentView(R.layout.local);
91
92 consoleTextView = (TextView) findViewById(R.id.consoleTextView);
93 final EditText commandEditText = (EditText) findViewById(R.id.argEditText);
94 commandEditText.setOnEditorActionListener(new OnEditorActionListener() {
95 @Override
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));
106 return true;
107 }
108 return false;
109 }
110 });
111 }
112
113 @Override
114 protected void onResume() {
115 super.onResume();
116 this.registerReceiver(replyReceiver, INTENT_FILTER);
117 }
118
119 @Override
120 protected void onPause() {
121 super.onPause();
122 this.unregisterReceiver(replyReceiver);
123 }
124 }
This page took 0.023465 seconds and 4 git commands to generate.