PreviewActivity.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.graphics.Color;
7 import android.graphics.Rect;
8 import android.os.Bundle;
9 import com.google.android.material.floatingactionbutton.FloatingActionButton;
10 import androidx.appcompat.app.AppCompatActivity;
11 import android.util.Log;
12 import android.util.Pair;
13 import android.view.View;
14 import android.view.ViewGroup;
15 import android.view.WindowManager;
16 import android.widget.RelativeLayout;
17 import android.widget.TextView;
18 import android.widget.Toast;
19 
28 
29 import java.util.HashMap;
30 import java.util.Map;
31 
32 public class PreviewActivity extends AppCompatActivity {
33  private static final String TAG = "librs camera preview";
34 
36  private FloatingActionButton mStartRecordFab;
37  private TextView mPlaybackButton;
38  private TextView mSettingsButton;
39  private TextView mStatisticsButton;
40  private TextView m3dButton;
41  private TextView mControlsButton;
42 
43  private TextView mStatsView;
44  private Map<Integer, TextView> mLabels;
45 
48 
50  private boolean mFwLogsRunning = false;
51 
52  private boolean statsToggle = false;
53  private boolean mShow3D = false;
54 
55  boolean keepalive = true;
56 
57  public synchronized void toggleStats(){
58  statsToggle = !statsToggle;
59  if(statsToggle){
60  mGLSurfaceView.setVisibility(View.GONE);
61  mStatsView.setVisibility(View.VISIBLE);
62  mStatisticsButton.setText("Preview");
63  }
64  else {
65  mGLSurfaceView.setVisibility(View.VISIBLE);
66  mStatsView.setVisibility(View.GONE);
67  mStatisticsButton.setText("Statistics");
68  }
69  }
70 
71  @Override
72  protected void onCreate(Bundle savedInstanceState) {
73  super.onCreate(savedInstanceState);
74  setContentView(R.layout.activity_preview);
75 
76  keepalive = true;
77 
78  Intent intent = this.getIntent();
79 
80  if (intent != null && intent.getExtras() != null ) {
81  keepalive = intent.getExtras().getBoolean("keepalive");
82  }
83 
84  getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
85 
86  mGLSurfaceView = findViewById(R.id.glSurfaceView);
87  mStatsView = findViewById(R.id.streaming_stats_text);
88  mStartRecordFab = findViewById(R.id.start_record_fab);
89  mPlaybackButton = findViewById(R.id.preview_playback_button);
90  mSettingsButton = findViewById(R.id.preview_settings_button);
91  mStatisticsButton = findViewById(R.id.preview_stats_button);
92  m3dButton = findViewById(R.id.preview_3d_button);
93  mControlsButton = findViewById(R.id.controls_button);
94 
95  mStartRecordFab.setOnClickListener(new View.OnClickListener() {
96  @Override
97  public void onClick(View view) {
98  Intent intent = new Intent(PreviewActivity.this, RecordingActivity.class);
99  startActivity(intent);
100  finish();
101  }
102  });
103  mPlaybackButton.setOnClickListener(new View.OnClickListener() {
104  @Override
105  public void onClick(View view) {
106  Intent intent = new Intent(PreviewActivity.this, PlaybackActivity.class);
107  startActivity(intent);
108  }
109  });
110  mSettingsButton.setOnClickListener(new View.OnClickListener() {
111  @Override
112  public void onClick(View view) {
113  Intent intent = new Intent(PreviewActivity.this, SettingsActivity.class);
114  startActivity(intent);
115  }
116  });
117  m3dButton.setOnClickListener(new View.OnClickListener() {
118  @Override
119  public void onClick(View view) {
120  mGLSurfaceView.setVisibility(View.GONE);
121  mGLSurfaceView.clear();
122  clearLables();
123  mShow3D = !mShow3D;
124  m3dButton.setTextColor(mShow3D ? Color.YELLOW : Color.WHITE);
125  mGLSurfaceView.setVisibility(View.VISIBLE);
126  SharedPreferences sharedPref = getSharedPreferences(getString(R.string.app_settings), Context.MODE_PRIVATE);
127  SharedPreferences.Editor editor = sharedPref.edit();
128  editor.putBoolean(getString(R.string.show_3d), mShow3D);
129  editor.commit();
130  }
131  });
132  mControlsButton.setOnClickListener(new View.OnClickListener() {
133  @Override
134  public void onClick(View view) {
135  ControlsDialog cd = new ControlsDialog();
136  cd.setCancelable(true);
137  cd.show(getFragmentManager(), null);
138  }
139  });
140  mStatisticsButton.setOnClickListener(new View.OnClickListener() {
141  @Override
142  public void onClick(View view) {
143  toggleStats();
144  }
145  });
146  SharedPreferences sharedPref = getSharedPreferences(getString(R.string.app_settings), Context.MODE_PRIVATE);
147  mShow3D = sharedPref.getBoolean(getString(R.string.show_3d), false);
148  m3dButton.setTextColor(mShow3D ? Color.YELLOW : Color.WHITE);
149  }
150 
151  @Override
152  protected void onDestroy() {
153  super.onDestroy();
154  mGLSurfaceView.close();
155  }
156 
157  private synchronized Map<Integer, TextView> createLabels(Map<Integer, Pair<String, Rect>> rects){
158  if(rects == null)
159  return null;
160  mLabels = new HashMap<>();
161 
162  final RelativeLayout rl = findViewById(R.id.labels_layout);
163  for(Map.Entry<Integer, Pair<String, Rect>> e : rects.entrySet()){
164  TextView tv = new TextView(getApplicationContext());
165  ViewGroup.LayoutParams lp = new RelativeLayout.LayoutParams(
166  ViewGroup.LayoutParams.WRAP_CONTENT,
167  ViewGroup.LayoutParams.WRAP_CONTENT);
168  tv.setLayoutParams(lp);
169  tv.setTextColor(Color.parseColor("#ffffff"));
170  tv.setTextSize(14);
171  rl.addView(tv);
172  mLabels.put(e.getKey(), tv);
173  }
174  return mLabels;
175  }
176 
177  private void printLables(final Map<Integer, Pair<String, Rect>> rects){
178  if(rects == null)
179  return;
180  final Map<Integer, String> lables = new HashMap<>();
181  if(mLabels == null)
182  mLabels = createLabels(rects);
183  for(Map.Entry<Integer, Pair<String, Rect>> e : rects.entrySet()){
184  lables.put(e.getKey(), e.getValue().first);
185  }
186 
187  runOnUiThread(new Runnable() {
188  @Override
189  public void run() {
190  for(Map.Entry<Integer,TextView> e : mLabels.entrySet()){
191  Integer uid = e.getKey();
192  if(rects.get(uid) == null)
193  continue;
194  Rect r = rects.get(uid).second;
195  TextView tv = e.getValue();
196  tv.setX(r.left);
197  tv.setY(r.top);
198  tv.setText(lables.get(uid));
199  }
200  }
201  });
202  }
203 
204  private void clearLables(){
205  if(mLabels != null){
206  for(Map.Entry<Integer, TextView> label : mLabels.entrySet())
207  label.getValue().setVisibility(View.GONE);
208  mLabels = null;
209  }
210  }
211 
212  @Override
213  protected void onResume() {
214  super.onResume();
215 
216  if(keepalive == false)
217  {
218  return;
219  }
220 
221  mStreamingStats = new StreamingStats();
222  mStreamer = new Streamer(this, true, new Streamer.Listener() {
223  @Override
224  public void config(Config config) {
225 
226  }
227 
228  @Override
229  public void onFrameset(final FrameSet frameSet) {
230  mStreamingStats.onFrameset(frameSet);
231  if(statsToggle){
232  clearLables();
233  runOnUiThread(new Runnable() {
234  @Override
235  public void run() {
236  mStatsView.setText(mStreamingStats.prettyPrint());
237  }
238  });
239  }
240  else{
241  mGLSurfaceView.showPointcloud(mShow3D);
242  mGLSurfaceView.upload(frameSet);
243  Map<Integer, Pair<String, Rect>> rects = mGLSurfaceView.getRectangles();
244  printLables(rects);
245  }
246  }
247  });
248 
249  try {
250  mGLSurfaceView.clear();
251  mStreamer.start();
252  }
253  catch (Exception e){
254  if(mStreamer != null)
255  mStreamer.stop();
256  Log.e(TAG, e.getMessage());
257  Toast.makeText(this, "Failed to set streaming configuration ", Toast.LENGTH_LONG).show();
258  Intent intent = new Intent(PreviewActivity.this, SettingsActivity.class);
259  startActivity(intent);
260  }
261 
263  }
264 
265  @Override
266  protected void onPause() {
267  super.onPause();
268 
269  clearLables();
270  if(mStreamer != null)
271  mStreamer.stop();
272  if(mGLSurfaceView != null)
273  mGLSurfaceView.clear();
274 
276  }
277 
278  private synchronized void resumeBackgroundTasks() {
279  resumeFwLogger();
280  }
281 
282  private synchronized void pauseBackgroundTasks() {
283  pauseFwLogger();
284  }
285 
286  private synchronized void resumeFwLogger() {
287  if (!mFwLogsRunning)
288  {
289  SharedPreferences sharedPref = getSharedPreferences(getString(R.string.app_settings), Context.MODE_PRIVATE);
290  boolean fw_logging_enabled = sharedPref.getBoolean(getString(R.string.fw_logging), false);
291  String fw_logging_file_path = sharedPref.getString(getString(R.string.fw_logging_file_path), "");
292 
293  if (fw_logging_enabled) {
294  mFwLogsThread = new FwLogsThread();
295  if(!fw_logging_file_path.equals("")){
296  mFwLogsThread.init(fw_logging_file_path);
297  }
298  mFwLogsThread.start();
299  mFwLogsRunning = true;
300  }
301  }
302  }
303 
304  private synchronized void pauseFwLogger(){
305  SharedPreferences sharedPref = getSharedPreferences(getString(R.string.app_settings), Context.MODE_PRIVATE);
306  boolean fw_logging_enabled = sharedPref.getBoolean(getString(R.string.fw_logging), false);
307  if (fw_logging_enabled) {
308  mFwLogsThread.stopLogging();
309  }
310  if(mFwLogsThread != null && mFwLogsThread.isAlive()) {
311  mFwLogsThread.interrupt();
312  }
313  mFwLogsRunning = false;
314  }
315 
316  @Override
317  protected void onNewIntent(Intent intent)
318  {
319  super.onNewIntent(intent);
320 
321  if (intent != null && intent.getExtras() != null ) {
322  keepalive = intent.getExtras().getBoolean("keepalive");
323  }
324 
325  // destroy activity if requested
326  if(keepalive == false)
327  {
328  PreviewActivity.this.finish();
329  }
330  }
331 
332 }
::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
void printLables(final Map< Integer, Pair< String, Rect >> rects)
Map< Integer, Pair< String, Rect > > getRectangles()
GLdouble GLdouble r
def run(include_folder_path, addon_folder_path)
Definition: enums.py:46
def finish()
Definition: test.py:373
GLuint GLsizei const GLchar * label
void onCreate(Bundle savedInstanceState)
synchronized Map< Integer, TextView > createLabels(Map< Integer, Pair< String, Rect >> rects)
void init(String fw_logging_file_path)


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