]> iEval git - fonbot.git/blame - src/ro/ieval/fonbot/FonBotLocalActivity.java
Add request ID
[fonbot.git] / src / ro / ieval / fonbot / FonBotLocalActivity.java
CommitLineData
8dfb76c9
MG
1package ro.ieval.fonbot;
2
3import static ro.ieval.fonbot.Utils.toNonNull;
4import static ro.ieval.fonbot.Address.Protocol.LOCAL;
5
6import org.eclipse.jdt.annotation.Nullable;
7
8import android.app.Activity;
9import android.content.BroadcastReceiver;
10import android.content.Context;
11import android.content.Intent;
12import android.content.IntentFilter;
13import android.os.Bundle;
14import android.view.KeyEvent;
15import android.view.inputmethod.EditorInfo;
16import android.widget.EditText;
17import android.widget.TextView;
18import 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 */
44public 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.041293 seconds and 4 git commands to generate.