PresetsDialog.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.content.res.Resources;
8 import android.graphics.Color;
9 import android.graphics.drawable.ColorDrawable;
10 import android.os.Bundle;
11 import android.util.Log;
12 import android.view.Gravity;
13 import android.view.LayoutInflater;
14 import android.view.View;
15 import android.widget.RadioButton;
16 import android.widget.RadioGroup;
17 import android.widget.TextView;
18 
22 
23 import java.io.ByteArrayOutputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 
27 public class PresetsDialog extends DialogFragment {
28  private static final String TAG = "librs presets";
29  // mSelectedItem initialized with high number so that no preset would be checked when opening presets at the first time
30  private static int mSelectedItem = 500;
31  private String[] mPresets;
32  private Resources mResources;
33 
34  @Override
35  public Dialog onCreateDialog(Bundle savedInstanceState) {
36  final Activity activity = getActivity();
37  LayoutInflater inflater = activity.getLayoutInflater();
38  View fragmentView = inflater.inflate(R.layout.presets_dialog, null);
39 
40  TextView message = fragmentView.findViewById(R.id.presets_list_title);
41  mResources = getResources();
42 
43  try {
44  mPresets = mResources.getAssets().list("presets");
45  } catch (IOException e) {
46  message.setText("No presets found");
47  return null;
48  }
49 
50  if(mPresets.length == 0) {
51  message.setText("No presets found");
52  return null;
53  }
54  message.setText("Select a preset:");
55 
56  // setting RadioGroup
57  final RadioGroup presets_group = fragmentView.findViewById(R.id.presets_list_items);
58  // adding items to the list
59  for (int i = 0; i < mPresets.length; ++i) {
60  RadioButton button = new RadioButton(activity);
61  button.setId(i);
62  button.setText(mPresets[i].substring(0, mPresets[i].lastIndexOf('.'))); // text is w/o the file termination
63  button.setTextSize(16);
64  button.setTextColor(getResources().getColor(R.color.white));
65  button.setChecked(i == mSelectedItem);
66  presets_group.addView(button);
67  }
68 
69  final String[] finalPresets = mPresets;
70  presets_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
71  @Override
72  public void onCheckedChanged(RadioGroup group, int checkedId) {
73  mSelectedItem = checkedId;
74  }
75  });
76  // OK BUTTON
77  View okButton = fragmentView.findViewById(R.id.presets_ok_button);
78  okButton.setOnClickListener(new View.OnClickListener() {
79  @Override
80  public void onClick(View v) {
81  RsContext ctx = new RsContext();
82  try (DeviceList devices = ctx.queryDevices()) {
83  if (devices.getDeviceCount() == 0) {
84  Log.e(TAG, "failed to set preset, no device found");
85  dismiss();
86  }
87  try (Device device = devices.createDevice(0)) {
88  if (device == null || !device.isInAdvancedMode()) {
89  Log.e(TAG, "failed to set preset, device not in advanced mode");
90  dismiss();
91  }
92  final String item = finalPresets[mSelectedItem];
93  try {
94  InputStream is = mResources.getAssets().open("presets/" + item);
95  byte[] buffer = new byte[is.available()];
96  is.read(buffer);
97  ByteArrayOutputStream baos = new ByteArrayOutputStream();
98  baos.write(buffer);
99  baos.close();
100  is.close();
101  device.loadPresetFromJson(buffer);
102  } catch (IOException e) {
103  Log.e(TAG, "failed to set preset, failed to open preset file, error: " + e.getMessage());
104  } catch (Exception e) {
105  Log.e(TAG, "failed to set preset, error: " + e.getMessage());
106  } finally {
107  dismiss();
108  }
109  }
110  }
111  Log.i(TAG, "preset set to: " + finalPresets[mSelectedItem]);
112  dismissAllowingStateLoss();
113  }
114  });
115 
116  // Cancel BUTTON
117  View cancelButton = fragmentView.findViewById(R.id.presets_cancel_button);
118  cancelButton.setOnClickListener(new View.OnClickListener() {
119  @Override
120  public void onClick(View v) {
121  dismissAllowingStateLoss();
122  }
123  });
124 
125  // BUILD DIALOG
126  AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
127  builder.setView(fragmentView);
128  AlertDialog rv = builder.create();
129  rv.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
130  rv.getWindow().setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP);
131  return rv;
132  }
133 }
GLenum GLuint GLenum GLsizei const GLchar * message
IMGUI_API bool RadioButton(const char *label, bool active)
Definition: imgui.cpp:7332
::rosgraph_msgs::Log_< std::allocator< void > > Log
Definition: Log.h:88
e
Definition: rmse.py:177
GLenum GLfloat * buffer
::std_msgs::String_< std::allocator< void > > String
Definition: String.h:47
Dialog onCreateDialog(Bundle savedInstanceState)
devices
Definition: test-fg.py:9
GLboolean GLuint group
Definition: glext.h:5688
unsigned char byte
Definition: src/types.h:52
int i
GLdouble v
auto device
Definition: pyrs_net.cpp:17


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