Skip to content

Commit

Permalink
controls' list made dynamic so that control will not appear if not re…
Browse files Browse the repository at this point in the history
…levant to device
  • Loading branch information
remibettan committed Jul 15, 2020
1 parent 06ed7fc commit 602956a
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,108 +7,105 @@
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.intel.realsense.librealsense.Device;
import com.intel.realsense.librealsense.DeviceList;
import com.intel.realsense.librealsense.Option;
import com.intel.realsense.librealsense.RsContext;
import com.intel.realsense.librealsense.Sensor;

import java.util.Arrays;
import java.util.List;

public class ControlsDialog extends DialogFragment {
// TODO - Ariel - Add more controls
private static final String TAG = "librs controls";
private Device mDevice;
private int numOfControls = 0;

private class ControlParams {
private String mName;
private String[] mValues;
private int mSelectedOptionIndex;
private Option mOption;

public ControlParams() {
mName = new String();
mValues = null;
mSelectedOptionIndex = -1;
mOption = null;
}
}

private ControlParams[] mControlsParams;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Activity activity = getActivity();

final Activity activity = getActivity();
LayoutInflater inflater = activity.getLayoutInflater();
View fragmentView = inflater.inflate(R.layout.controls_dialog, null);

// init device
try(RsContext ctx = new RsContext()){
try(DeviceList devices = ctx.queryDevices()) {
mDevice = devices.createDevice(0);
if (mDevice == null) {
Log.e(TAG, "No device could be found");
dismissAllowingStateLoss();
}
}
}

boolean isEmitterOptionSupported = false;

// retrieving options' current values
int indexOfCurrentEmitter = 0;
//int indexOfCurrentHwPreset = 0;
List<Sensor> sensors = mDevice.querySensors();
for (Sensor s : sensors) {
if (s.supports(Option.EMITTER_ENABLED)) {
indexOfCurrentEmitter = (int)s.getValue(Option.EMITTER_ENABLED);
isEmitterOptionSupported = true;
++numOfControls;
}
/*if (s.supports(Option.HARDWARE_PRESET)) {
indexOfCurrentHwPreset = (int)s.getValue(Option.HARDWARE_PRESET);
}*/
}

// EMITTER OPTION
final String emitterDescriptions[] = getOptionDescriptions(Option.EMITTER_ENABLED);
final RadioGroup emittersRadioGroup = fragmentView.findViewById(R.id.emitter_options_list);
// adding buttons to the group
for(int i = 0; i < emitterDescriptions.length; ++i) {
RadioButton button = new RadioButton(activity);
button.setId(i);
button.setText(emitterDescriptions[i]);
button.setTextColor(getResources().getColor(R.color.white));
button.setChecked(i == indexOfCurrentEmitter);
emittersRadioGroup.addView(button);
if (numOfControls > 0) {
mControlsParams = new ControlParams[numOfControls];
}

emittersRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
List<Sensor> sensors = mDevice.querySensors();
for (Sensor s : sensors) {
if (s.supports(Option.EMITTER_ENABLED)) {
s.setValue(Option.EMITTER_ENABLED, checkedId);
}
}
}
});
int controlIndex = 0;

//HW PRESETS OPTION
/*final String hwPresetsDescriptions[] = getOptionDescriptions(Option.HARDWARE_PRESET);
final RadioGroup hwPresetsRadioGroup = fragmentView.findViewById(R.id.hw_presets_radio_group);
// getting entries
final String hwPresetsEntries[] = this.getResources().getStringArray(R.array.controls_hw_presets_entries);
final int hwPresetsValues[] = this.getResources().getIntArray(R.array.controls_hw_presets_values);
// adding buttons to the group
for(int i = 0; i < hwPresetsEntries.length; ++i) {
RadioButton button = new RadioButton(activity);
button.setId(hwPresetsValues[i]);
button.setText(hwPresetsEntries[i]);
button.setTextColor(getResources().getColor(R.color.white));
button.setChecked(hwPresetsValues[i] == indexOfCurrentHwPreset);
hwPresetsRadioGroup.addView(button);
// EMITTER OPTION
if (isEmitterOptionSupported) {
//retrieve values
final String emitterDescriptions[] = getOptionDescriptions(Option.EMITTER_ENABLED);
// prepare params for control
ControlParams controlParams = new ControlParams();
controlParams.mName = "Projector";
controlParams.mValues = emitterDescriptions;
controlParams.mSelectedOptionIndex = indexOfCurrentEmitter;
controlParams.mOption = Option.EMITTER_ENABLED;

// add params to list
mControlsParams[controlIndex++] = controlParams;
}

hwPresetsRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
List<Sensor> sensors = mDevice.querySensors();
for (Sensor s : sensors) {
if (s.supports(Option.HARDWARE_PRESET)) {
s.setValue(Option.HARDWARE_PRESET, checkedId);
}
}
}
});*/

//set controls list
ListView controlsList = fragmentView.findViewById(R.id.controls_list);
CustomAdapter customAdapter = new CustomAdapter();
controlsList.setAdapter(customAdapter);

// CLOSE BUTTON
View closeButton = fragmentView.findViewById(R.id.controls_close_button);
closeButton.setOnClickListener(new View.OnClickListener() {
@Override
Expand All @@ -117,6 +114,7 @@ public void onClick(View v) {
}
});

// BUILD DIALOG
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(fragmentView);
AlertDialog rv = builder.create();
Expand All @@ -125,7 +123,7 @@ public void onClick(View v) {
return rv;
}

String[] getOptionDescriptions(Option option) {
private String[] getOptionDescriptions(Option option) {
String[] optionDescriptions;
List<Sensor> sensors = mDevice.querySensors();
for (Sensor s : sensors) {
Expand All @@ -142,4 +140,57 @@ String[] getOptionDescriptions(Option option) {
return null;
}

class CustomAdapter extends BaseAdapter {

@Override
public int getCount() {
return numOfControls;
}

@Override
public Object getItem(int position) {
return null;
}

@Override
public long getItemId(int position) {
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Activity activity = getActivity();
convertView = activity.getLayoutInflater().inflate(R.layout.controls_dialog_row, null);

// setting TextView
TextView control_name = convertView.findViewById(R.id.control_name);
control_name.setText(mControlsParams[position].mName);

// setting RadioGroup
final RadioGroup control_options = convertView.findViewById(R.id.control_options_list);
// adding buttons to the group
for (int i = 0; i < mControlsParams[position].mValues.length; ++i) {
RadioButton button = new RadioButton(activity);
button.setId(i);
button.setText(mControlsParams[position].mValues[i]);
button.setTextColor(getResources().getColor(R.color.white));
button.setChecked(i == mControlsParams[position].mSelectedOptionIndex);
control_options.addView(button);
}

final int positionFinal = position;
control_options.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
List<Sensor> sensors = mDevice.querySensors();
for (Sensor s : sensors) {
if (s.supports(mControlsParams[positionFinal].mOption)) {
s.setValue(Option.EMITTER_ENABLED, checkedId);
}
}
}
});
return convertView;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,30 @@
android:layout_height="wrap_content">

<TextView
android:id="@+id/controls_title"
android:id="@+id/controls_list_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Projector"
android:textColor="#ffffff"
android:text="Controls"
android:textSize="20dp"
android:textStyle="bold"
android:layout_marginBottom="5dp"/>

//Projector options
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/emitter_options_list"
<ListView
android:id="@+id/controls_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/controls_title"
android:orientation="vertical"
android:layout_marginBottom="20dp"
tools:layout_height="50dip">
//buttons added within code
</RadioGroup>
android:layout_below="@id/controls_list_title"
tools:layout_height="150dip">
<!-- items added within code -->
</ListView>



//Close button
<Button android:id="@+id/controls_close_button"
android:layout_below="@id/emitter_options_list"
<Button
android:id="@+id/controls_close_button"
android:layout_below="@id/controls_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dp"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:padding="20dp"
android:background="@drawable/fragment_template"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/control_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginBottom="5dp"/>

<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/control_name"
android:id="@+id/control_options_list"
android:layout_marginBottom="20dp"
tools:layout_height="150dip">
<!-- items added within code -->
</RadioGroup>

</RelativeLayout>

0 comments on commit 602956a

Please sign in to comment.