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;
44  mSelectedOptionIndex = -1;
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) {
83  mControlsParams = new ControlParams[numOfControls];
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 }
IMGUI_API bool RadioButton(const char *label, bool active)
Definition: imgui.cpp:7332
::rosgraph_msgs::Log_< std::allocator< void > > Log
Definition: Log.h:88
GLdouble s
::std_msgs::String_< std::allocator< void > > String
Definition: String.h:47
Dialog onCreateDialog(Bundle savedInstanceState)
Definition: getopt.h:41
devices
Definition: test-fg.py:9
GLboolean GLuint group
Definition: glext.h:5688
int i
GLdouble v


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