ControlsDialog.java
Go to the documentation of this file.
1 package com.intel.realsense.camera;
2 
3 import android.app.Activity;
4 import android.app.AlertDialog;
5 import android.app.Dialog;
6 import android.app.DialogFragment;
7 import android.graphics.Color;
8 import android.graphics.drawable.ColorDrawable;
9 import android.os.Bundle;
10 import android.util.Log;
11 import android.view.Gravity;
12 import android.view.LayoutInflater;
13 import android.view.View;
14 import android.view.ViewGroup;
15 import android.widget.BaseAdapter;
16 import android.widget.ListView;
17 import android.widget.RadioButton;
18 import android.widget.RadioGroup;
19 import android.widget.TextView;
20 
26 
27 import java.util.List;
28 
29 public class ControlsDialog extends DialogFragment {
30  // TODO - Ariel - Add more controls
31  private static final String TAG = "librs controls";
32  private Device mDevice;
33  private int numOfControls = 0;
34 
35  private class ControlParams {
36  private String mName;
37  private String[] mValues;
38  private int mSelectedOptionIndex;
39  private Option mOption;
40 
41  public ControlParams() {
42  mName = new String();
43  mValues = null;
45  mOption = null;
46  }
47  }
48 
50 
51  @Override
52  public Dialog onCreateDialog(Bundle savedInstanceState) {
53 
54  final Activity activity = getActivity();
55  LayoutInflater inflater = activity.getLayoutInflater();
56  View fragmentView = inflater.inflate(R.layout.controls_dialog, null);
57 
58  // init device
59  try(RsContext ctx = new RsContext()){
60  try(DeviceList devices = ctx.queryDevices()) {
61  mDevice = devices.createDevice(0);
62  if (mDevice == null) {
63  Log.e(TAG, "No device could be found");
64  dismissAllowingStateLoss();
65  }
66  }
67  }
68 
69  boolean isEmitterOptionSupported = false;
70 
71  // retrieving options' current values
72  int indexOfCurrentEmitter = 0;
73  List<Sensor> sensors = mDevice.querySensors();
74  for (Sensor s : sensors) {
75  if (s.supports(Option.EMITTER_ENABLED)) {
76  indexOfCurrentEmitter = (int)s.getValue(Option.EMITTER_ENABLED);
77  isEmitterOptionSupported = true;
78  ++numOfControls;
79  }
80  }
81 
82  if (numOfControls > 0) {
84  }
85 
86  int controlIndex = 0;
87 
88  // EMITTER OPTION
89  if (isEmitterOptionSupported) {
90  //retrieve values
91  final String emitterDescriptions[] = getOptionDescriptions(Option.EMITTER_ENABLED);
92  // prepare params for control
93  ControlParams controlParams = new ControlParams();
94  controlParams.mName = "Projector";
95  controlParams.mValues = emitterDescriptions;
96  controlParams.mSelectedOptionIndex = indexOfCurrentEmitter;
97  controlParams.mOption = Option.EMITTER_ENABLED;
98 
99  // add params to list
100  mControlsParams[controlIndex++] = controlParams;
101  }
102 
103  //set controls list
104  ListView controlsList = fragmentView.findViewById(R.id.controls_list);
105  CustomAdapter customAdapter = new CustomAdapter();
106  controlsList.setAdapter(customAdapter);
107 
108  // CLOSE BUTTON
109  View closeButton = fragmentView.findViewById(R.id.controls_close_button);
110  closeButton.setOnClickListener(new View.OnClickListener() {
111  @Override
112  public void onClick(View v) {
113  dismissAllowingStateLoss();
114  }
115  });
116 
117  // BUILD DIALOG
118  AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
119  builder.setView(fragmentView);
120  AlertDialog rv = builder.create();
121  rv.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
122  rv.getWindow().setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP);
123  return rv;
124  }
125 
127  String[] optionDescriptions;
128  List<Sensor> sensors = mDevice.querySensors();
129  for (Sensor s : sensors) {
130  if (s.supports(option)) {
131  int minValue = (int) s.getMinRange(option);
132  int maxValue = (int) s.getMaxRange(option);
133  optionDescriptions = new String[maxValue - minValue + 1];
134  for (int i = minValue; i <= maxValue; ++i) {
135  optionDescriptions[i] = s.valueDescription(option, i);
136  }
137  return optionDescriptions;
138  }
139  }
140  return null;
141  }
142 
143  class CustomAdapter extends BaseAdapter {
144 
145  @Override
146  public int getCount() {
147  return numOfControls;
148  }
149 
150  @Override
151  public Object getItem(int position) {
152  return null;
153  }
154 
155  @Override
156  public long getItemId(int position) {
157  return 0;
158  }
159 
160  @Override
161  public View getView(int position, View convertView, ViewGroup parent) {
162  final Activity activity = getActivity();
163  convertView = activity.getLayoutInflater().inflate(R.layout.controls_dialog_row, null);
164 
165  // setting TextView
166  TextView control_name = convertView.findViewById(R.id.control_name);
167  control_name.setText(mControlsParams[position].mName);
168 
169  // setting RadioGroup
170  final RadioGroup control_options = convertView.findViewById(R.id.control_options_list);
171  // adding buttons to the group
172  for (int i = 0; i < mControlsParams[position].mValues.length; ++i) {
173  RadioButton button = new RadioButton(activity);
174  button.setId(i);
175  button.setText(mControlsParams[position].mValues[i]);
176  button.setTextColor(getResources().getColor(R.color.white));
177  button.setChecked(i == mControlsParams[position].mSelectedOptionIndex);
178  control_options.addView(button);
179  }
180 
181  final int positionFinal = position;
182  control_options.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
183  @Override
184  public void onCheckedChanged(RadioGroup group, int checkedId) {
185  List<Sensor> sensors = mDevice.querySensors();
186  for (Sensor s : sensors) {
187  if (s.supports(mControlsParams[positionFinal].mOption)) {
188  s.setValue(Option.EMITTER_ENABLED, checkedId);
189  }
190  }
191  }
192  });
193  return convertView;
194  }
195  }
196 }
com.intel.realsense.camera.ControlsDialog.mDevice
Device mDevice
Definition: ControlsDialog.java:32
com.intel.realsense.librealsense
Definition: ExampleInstrumentedTest.java:1
ImGui::RadioButton
IMGUI_API bool RadioButton(const char *label, bool active)
Definition: imgui.cpp:7335
com.intel.realsense
com.intel.realsense.camera.ControlsDialog.getOptionDescriptions
String[] getOptionDescriptions(Option option)
Definition: ControlsDialog.java:126
com.intel.realsense.camera.ControlsDialog.ControlParams.mValues
String[] mValues
Definition: ControlsDialog.java:37
com.intel.realsense.camera.ControlsDialog.ControlParams
Definition: ControlsDialog.java:35
devices
Definition: third-party/realdds/scripts/devices.py:1
com.intel.realsense.camera.ControlsDialog
Definition: ControlsDialog.java:29
com.intel.realsense.librealsense.Device
Definition: Device.java:6
com.intel.realsense.camera.ControlsDialog.ControlParams.mSelectedOptionIndex
int mSelectedOptionIndex
Definition: ControlsDialog.java:38
com.intel.realsense.librealsense.RsContext
Definition: RsContext.java:5
com.intel.realsense.librealsense.Option.EMITTER_ENABLED
EMITTER_ENABLED
Definition: Option.java:22
com.intel.realsense.camera.ControlsDialog.ControlParams.ControlParams
ControlParams()
Definition: ControlsDialog.java:41
rosgraph_msgs::Log
::rosgraph_msgs::Log_< std::allocator< void > > Log
Definition: Log.h:88
com.intel.realsense.camera.ControlsDialog.numOfControls
int numOfControls
Definition: ControlsDialog.java:33
com.intel.realsense.camera.ControlsDialog.ControlParams.mOption
Option mOption
Definition: ControlsDialog.java:39
i
int i
Definition: rs-pcl-color.cpp:54
com.intel.realsense.librealsense.Device.querySensors
List< Sensor > querySensors()
Definition: Device.java:17
com.intel.realsense.camera.ControlsDialog.ControlParams.mName
String mName
Definition: ControlsDialog.java:36
com.intel.realsense.camera.ControlsDialog.onCreateDialog
Dialog onCreateDialog(Bundle savedInstanceState)
Definition: ControlsDialog.java:52
com.intel
test-projection-from-recording.sensors
sensors
Definition: test-projection-from-recording.py:21
option
Definition: getopt.h:41
com.intel.realsense.librealsense.Option
Definition: Option.java:3
com.intel.realsense.camera.ControlsDialog.TAG
static final String TAG
Definition: ControlsDialog.java:31
std_msgs::String
::std_msgs::String_< std::allocator< void > > String
Definition: String.h:47
com
com.intel.realsense.librealsense.Sensor
Definition: Sensor.java:6
test-projection-from-recording.ctx
ctx
Definition: test-projection-from-recording.py:16
s
GLdouble s
Definition: glad/glad/glad.h:2441
com.intel.realsense.librealsense.DeviceList
Definition: DeviceList.java:5
com.intel.realsense.camera.ControlsDialog.mControlsParams
ControlParams[] mControlsParams
Definition: ControlsDialog.java:49


librealsense2
Author(s): LibRealSense ROS Team
autogenerated on Fri Aug 2 2024 08:30:01