TerminalActivity.java
Go to the documentation of this file.
1 package com.intel.realsense.camera;
2 
3 import android.content.Context;
4 import android.content.Intent;
5 import android.content.SharedPreferences;
6 import android.os.Bundle;
7 import androidx.appcompat.app.AppCompatActivity;
8 import android.util.Log;
9 import android.view.KeyEvent;
10 import android.view.View;
11 import android.widget.ArrayAdapter;
12 import android.widget.AutoCompleteTextView;
13 import android.widget.Button;
14 import android.widget.CompoundButton;
15 import android.widget.EditText;
16 import android.widget.Switch;
17 import android.widget.Toast;
18 
26 
27 import java.io.File;
28 
29 public class TerminalActivity extends AppCompatActivity {
30 
31  private static final String TAG = "librs camera terminal";
32  private static final int OPEN_FILE_REQUEST_CODE = 0;
33 
34  private Switch mAutoComplete;
35  private Switch mStreaming;
36 
39  private AutoCompleteTextView mInputText;
40  private EditText mOutputText;
41  private String mFilePath;
42 
44 
45  @Override
46  protected void onCreate(Bundle savedInstanceState) {
47  super.onCreate(savedInstanceState);
48  setContentView(R.layout.activity_terminal);
49 
50  mSendButton = findViewById(R.id.terminal_send_button);
51  mSendButton.setOnClickListener(new View.OnClickListener() {
52  @Override
53  public void onClick(View view) {
54  send();
55  }
56  });
57 
58  mClearButton = findViewById(R.id.terminal_clear_button);
59  mClearButton.setOnClickListener(new View.OnClickListener() {
60  @Override
61  public void onClick(View view) {
62  mInputText.setText("");
63  }
64  });
65  mInputText = findViewById(R.id.terminal_input_edit_text);
66  mInputText.setOnKeyListener(new View.OnKeyListener() {
67  public boolean onKey(View v, int keyCode, KeyEvent event) {
68  if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
69  (keyCode == KeyEvent.KEYCODE_ENTER)) {
70  send();
71  return true;
72  }
73  return false;
74  }
75  });
76  mOutputText = findViewById(R.id.terminal_output_edit_text);
77  mAutoComplete = findViewById(R.id.terminal_input_auto_complete);
78  mAutoComplete.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
79  @Override
80  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
81  SharedPreferences sharedPref = getSharedPreferences(getString(R.string.app_settings), Context.MODE_PRIVATE);
82  SharedPreferences.Editor editor = sharedPref.edit();
83  editor.putBoolean(getString(R.string.terminal_auto_complete), isChecked);
84  editor.commit();
86  }
87  });
88 
89  mStreaming = findViewById(R.id.terminal_stream);
90  mStreaming.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
91  @Override
92  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
93  if(isChecked)
95  else
96  stopStreaming();
97  }
98  });
99  init();
100  }
101 
102  private void stopStreaming() {
103  mStreamingThread.interrupt();
104  try {
105  mStreamingThread.join(1000);
106  } catch (InterruptedException e) {
107  Log.e(TAG, "stopStreaming, error: " + e.getMessage());
108  }
109  }
110 
111  @Override
112  protected void onPause() {
113  super.onPause();
114 
115  if(mStreaming.isChecked()){
116  stopStreaming();
117  }
118  }
119 
120  private void startStreaming() {
121  mStreamingThread = new Thread(new Runnable() {
122  @Override
123  public void run() {
124  try(Pipeline pipe = new Pipeline()){
125  pipe.start();
126  while(!mStreamingThread.isInterrupted()){
127  try(FrameSet frames = pipe.waitForFrames()){}
128  }
129  pipe.stop();
130  } catch (Exception e) {
131  mOutputText.setText("streaming error: " + e.getMessage());
132  }
133  }
134  });
135  mStreamingThread.start();
136  }
137 
138  private void send() {
139  RsContext mRsContext = new RsContext();
140  try(DeviceList devices = mRsContext.queryDevices()){
141  if(devices.getDeviceCount() == 0)
142  {
143  Log.e(TAG, "getDeviceCount returned zero");
144  Toast.makeText(this, "Connect a camera", Toast.LENGTH_LONG).show();
145  return;
146  }
147  try(Device device = devices.createDevice(0)){
149  String content = mInputText.getText().toString();
150  String res = dp.SendAndReceiveRawData(mFilePath, content);
151  mOutputText.setText("command: " + mInputText.getText() + "\n\n" + res);
152  mInputText.setText("");
153  }
154  }
155  catch(Exception e){
156  mOutputText.setText("Error: " + e.getMessage());
157  }
158  }
159  }
160 
161  @Override
162  protected void onResume() {
163  super.onResume();
164  }
165 
167  try{
168  if(mAutoComplete.isChecked()) {
169  String[] spArray = DebugProtocol.getCommands(mFilePath);
170  ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
171  android.R.layout.simple_dropdown_item_1line, spArray);
172  mInputText.setAdapter(adapter);
173  }else{
174  mInputText.setAdapter(null);
175  }
176  }catch(Exception e){
177  Log.e(TAG, e.getMessage());
178  mOutputText.setText("Error: " + e.getMessage());
179  }
180  }
181 
182  private void init(){
183  SharedPreferences sharedPref = getSharedPreferences(getString(R.string.app_settings), Context.MODE_PRIVATE);
184  mFilePath = sharedPref.getString(getString(R.string.terminal_commands_file), "");
185  mAutoComplete.setChecked(sharedPref.getBoolean(getString(R.string.terminal_auto_complete), false));
186 
187  if(mFilePath == null || !(new File(mFilePath).exists())){
188  Intent intent = new Intent(this, FileBrowserActivity.class);
189  intent.putExtra(getString(R.string.browse_folder), getString(R.string.realsense_folder) + File.separator + "hw");
190  startActivityForResult(intent, OPEN_FILE_REQUEST_CODE);
191  }
192  else
194  }
195 
196  @Override
197  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
198  super.onActivityResult(requestCode, resultCode, data);
199  if (requestCode == OPEN_FILE_REQUEST_CODE && resultCode == RESULT_OK) {
200  if (data != null) {
201  mFilePath = data.getStringExtra(getString(R.string.intent_extra_file_path));
202  SharedPreferences sharedPref = getSharedPreferences(getString(R.string.app_settings), Context.MODE_PRIVATE);
203  SharedPreferences.Editor editor = sharedPref.edit();
204  editor.putString(getString(R.string.terminal_commands_file), mFilePath);
205  editor.commit();
207  }
208  }
209  else{
210  Intent intent = new Intent();
211  setResult(RESULT_OK, intent);
212  finish();
213  }
214  }
215 }
void onCreate(Bundle savedInstanceState)
::rosgraph_msgs::Log_< std::allocator< void > > Log
Definition: Log.h:88
uvc_xu_option< int > super
Definition: l500-options.h:32
e
Definition: rmse.py:177
::std_msgs::String_< std::allocator< void > > String
Definition: String.h:47
devices
Definition: test-fg.py:9
void onActivityResult(int requestCode, int resultCode, Intent data)
def run(include_folder_path, addon_folder_path)
Definition: enums.py:46
def finish()
Definition: test.py:373
IMGUI_API bool Button(const char *label, const ImVec2 &size=ImVec2(0, 0))
Definition: imgui.cpp:5573
struct _cl_event * event
Definition: glext.h:2991
GLuint res
Definition: glext.h:8856
GLdouble v
auto device
Definition: pyrs_net.cpp:17
Definition: threads.c:40
Definition: parser.hpp:150
static String[] getCommands(String commands_file_path)


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:50:11