diff --git a/wrappers/android/librealsense/src/main/java/com/intel/realsense/librealsense/Options.java b/wrappers/android/librealsense/src/main/java/com/intel/realsense/librealsense/Options.java index 0f66686857..32b85d2b35 100644 --- a/wrappers/android/librealsense/src/main/java/com/intel/realsense/librealsense/Options.java +++ b/wrappers/android/librealsense/src/main/java/com/intel/realsense/librealsense/Options.java @@ -69,7 +69,7 @@ public String getDescription(Option option) { @Override public String valueDescription(Option option, float value) { - return null; + return nGetValueDescription(mHandle, option.value(), value); } private static native boolean nSupports(long handle, int option); diff --git a/wrappers/android/tools/camera/src/main/java/com/intel/realsense/camera/ControlsDialog.java b/wrappers/android/tools/camera/src/main/java/com/intel/realsense/camera/ControlsDialog.java index 2bfeb37a43..55bc2eb6f4 100644 --- a/wrappers/android/tools/camera/src/main/java/com/intel/realsense/camera/ControlsDialog.java +++ b/wrappers/android/tools/camera/src/main/java/com/intel/realsense/camera/ControlsDialog.java @@ -7,21 +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; -public class ControlsDialog extends DialogFragment { +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.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; + List sensors = mDevice.querySensors(); + for (Sensor s : sensors) { + if (s.supports(Option.EMITTER_ENABLED)) { + indexOfCurrentEmitter = (int)s.getValue(Option.EMITTER_ENABLED); + isEmitterOptionSupported = true; + ++numOfControls; + } + } + + if (numOfControls > 0) { + mControlsParams = new ControlParams[numOfControls]; + } + + int controlIndex = 0; + + // 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; + } + + //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 @@ -30,6 +114,7 @@ public void onClick(View v) { } }); + // BUILD DIALOG AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setView(fragmentView); AlertDialog rv = builder.create(); @@ -37,4 +122,75 @@ public void onClick(View v) { rv.getWindow().setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP); return rv; } + + private String[] getOptionDescriptions(Option option) { + String[] optionDescriptions; + List sensors = mDevice.querySensors(); + for (Sensor s : sensors) { + if (s.supports(option)) { + int minValue = (int) s.getMinRange(option); + int maxValue = (int) s.getMaxRange(option); + optionDescriptions = new String[maxValue - minValue + 1]; + for (int i = minValue; i <= maxValue; ++i) { + optionDescriptions[i] = s.valueDescription(option, i); + } + return optionDescriptions; + } + } + 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 sensors = mDevice.querySensors(); + for (Sensor s : sensors) { + if (s.supports(mControlsParams[positionFinal].mOption)) { + s.setValue(Option.EMITTER_ENABLED, checkedId); + } + } + } + }); + return convertView; + } + } } diff --git a/wrappers/android/tools/camera/src/main/java/com/intel/realsense/camera/PreviewActivity.java b/wrappers/android/tools/camera/src/main/java/com/intel/realsense/camera/PreviewActivity.java index 7caa494048..419d3585a6 100644 --- a/wrappers/android/tools/camera/src/main/java/com/intel/realsense/camera/PreviewActivity.java +++ b/wrappers/android/tools/camera/src/main/java/com/intel/realsense/camera/PreviewActivity.java @@ -261,68 +261,6 @@ protected void onPause() { pauseBackgroundTasks(); } - public void onRadioButtonClicked(View view) { - // TODO - Ariel - Move touch logic into controls fragment. Only Sensor's logic should stay here. - // TODO - Ariel - Add more controls - boolean checked = ((RadioButton) view).isChecked(); - - if(!checked) - return; - RsContext ctx = new RsContext(); - try(DeviceList devices = ctx.queryDevices()) { - try(Device device = devices.createDevice(0)){ - if(device == null) - return; - List sensors = device.querySensors(); - for(Sensor s : sensors){ - if(s.supports(Option.EMITTER_ENABLED)) { - switch(view.getId()) { - case R.id.radio_no_projector:{ - setOption(s, Option.EMITTER_ENABLED, 0); - break; - } - case R.id.radio_laser:{ - setOption(s, Option.EMITTER_ENABLED, 1); - break; - } - case R.id.radio_laser_auto:{ - setOption(s, Option.EMITTER_ENABLED, 2); - break; - } - case R.id.radio_led:{ - setOption(s, Option.EMITTER_ENABLED, 3); - break; - } - } - } - if(s.supports(Option.HARDWARE_PRESET)) { - switch(view.getId()) { - case R.id.radio_custom:{ - setOption(s, Option.HARDWARE_PRESET, 0); - break; - } - case R.id.radio_burst:{ - setOption(s, Option.HARDWARE_PRESET, 2); - break; - } - } - } - - } - } catch(Exception e){ - Log.e(TAG, "Failed to set controls: " + e.getMessage()); - Toast.makeText(this, "Failed to set controls", Toast.LENGTH_LONG).show(); - } - } - } - - private void setOption(Sensor s, Option option, int val) { - if(s.supports(option)) - s.setValue(option, val); - else - Toast.makeText(this, "This control is not supported by this device", Toast.LENGTH_LONG).show(); - } - private synchronized void resumeBackgroundTasks() { resumeFwLogger(); } diff --git a/wrappers/android/tools/camera/src/main/res/layout/controls_dialog.xml b/wrappers/android/tools/camera/src/main/res/layout/controls_dialog.xml index 8df5345233..22a14bd3b9 100644 --- a/wrappers/android/tools/camera/src/main/res/layout/controls_dialog.xml +++ b/wrappers/android/tools/camera/src/main/res/layout/controls_dialog.xml @@ -1,103 +1,43 @@ - - - - + - - - - - - + + + - - - - - -