SettingsActivity.java
Go to the documentation of this file.
1 package com.intel.realsense.camera;
2 
3 import android.app.ProgressDialog;
4 import android.content.Context;
5 import android.content.DialogInterface;
6 import android.content.Intent;
7 import android.content.SharedPreferences;
8 import android.os.AsyncTask;
9 import android.os.Bundle;
10 import androidx.core.app.ActivityCompat;
11 import androidx.core.content.ContextCompat;
12 import androidx.appcompat.app.AlertDialog;
13 import androidx.appcompat.app.AppCompatActivity;
14 import android.util.Log;
15 import android.util.Pair;
16 import android.view.View;
17 import android.widget.AdapterView;
18 import android.widget.ArrayAdapter;
19 import android.widget.ListView;
20 import android.widget.Toast;
21 
32 
33 import java.io.File;
34 import java.util.ArrayList;
35 import java.util.Collections;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.TreeMap;
40 
41 public class SettingsActivity extends AppCompatActivity {
42  private static final String TAG = "librs camera settings";
43 
44  AppCompatActivity mContext;
45 
46  private static final int OPEN_FILE_REQUEST_CODE = 0;
47  private static final int OPEN_FW_FILE_REQUEST_CODE = 1;
48 
49  private static final int INDEX_DEVICE_INFO = 0;
50  private static final int INDEX_ADVANCE_MODE = 1;
51  private static final int INDEX_PRESETS = 2;
52  private static final int INDEX_UPDATE = 3;
53  private static final int INDEX_UPDATE_UNSIGNED = 4;
54  private static final int INDEX_TERMINAL = 5;
55  private static final int INDEX_FW_LOG = 6;
56  private static final int INDEX_CREATE_FLASH_BACKUP = 7;
57 
58  private Device _device;
59 
60  private boolean areAdvancedFeaturesEnabled = false; // advanced features (fw logs, terminal etc.)
61 
62  @Override
63  protected void onCreate(Bundle savedInstanceState) {
64  super.onCreate(savedInstanceState);
65  setContentView(R.layout.activity_settings);
66  mContext = this;
67 
68  // Advanced features are enabled if xml files exists in the device.
69  String advancedFeaturesPath = FileUtilities.getExternalStorageDir(mContext) +
70  File.separator +
71  getString(R.string.realsense_folder) +
72  File.separator +
73  "hw";
74  areAdvancedFeaturesEnabled = !FileUtilities.isPathEmpty(advancedFeaturesPath);
75  }
76 
77  @Override
78  protected void onResume() {
79  super.onResume();
80 
81  int tries = 3;
82  for(int i = 0; i < tries; i++){
83  RsContext ctx = new RsContext();
84  try(DeviceList devices = ctx.queryDevices()) {
85  if (devices.getDeviceCount() == 0) {
86  Thread.sleep(500);
87  continue;
88  }
89  _device = devices.createDevice(0);
90  loadInfosList();
91  loadSettingsList(_device);
92  List<StreamProfileSelector> profilesList = createSettingList(_device);
93  RemoveUnsupportedProfiles(profilesList);
94  loadStreamList(_device, profilesList);
95  return;
96  } catch(Exception e){
97  Log.e(TAG, "failed to load settings, error: " + e.getMessage());
98  }
99  }
100  Log.e(TAG, "failed to load settings");
101  Toast.makeText(this, "Failed to load settings", Toast.LENGTH_LONG).show();
102  Intent intent = new Intent(this, DetachedActivity.class);
103  startActivity(intent);
104  finish();
105  }
106  @Override
107  protected void onPause() {
108  super.onPause();
109  if (_device != null)
110  _device.close();
111  }
112 
113  private void loadInfosList() {
114  final ListView listview = findViewById(R.id.info_list_view);
115  String appVersion = "Camera App Version: " + BuildConfig.VERSION_NAME;
116  String lrsVersion = "LibRealSense Version: " + RsContext.getVersion();
117 
118  final String[] info = { lrsVersion, appVersion};
119  final ArrayAdapter adapter = new ArrayAdapter<>(this, R.layout.files_list_view, info);
120  listview.setAdapter(adapter);
121  adapter.notifyDataSetChanged();
122  }
123 
124  private void loadSettingsList(final Device device){
125  final ListView listview = findViewById(R.id.settings_list_view);
126 
127  final Map<Integer,String> settingsMap = new TreeMap<>();
128  settingsMap.put(INDEX_DEVICE_INFO,"Device info");
129 
131  if (device.isInAdvancedMode()) {
132  settingsMap.put(INDEX_ADVANCE_MODE, "Disable advanced mode");
133  settingsMap.put(INDEX_PRESETS, "Presets");
134  }
135  else {
136  settingsMap.put(INDEX_ADVANCE_MODE, "Enable advanced mode");
137  }
138  }
139 
140  if(device.is(Extension.UPDATABLE)){
141  settingsMap.put(INDEX_UPDATE,"Firmware update");
142  try(Updatable fwud = device.as(Extension.UPDATABLE)){
143  if(fwud != null && fwud.supportsInfo(CameraInfo.CAMERA_LOCKED) && fwud.getInfo(CameraInfo.CAMERA_LOCKED).equals("NO"))
144  settingsMap.put(INDEX_UPDATE_UNSIGNED,"Firmware update (unsigned)");
145  }
146  }
147 
148  if (areAdvancedFeaturesEnabled) {
149  SharedPreferences sharedPref = getSharedPreferences(getString(R.string.app_settings), Context.MODE_PRIVATE);
150  boolean fw_logging_enabled = sharedPref.getBoolean(getString(R.string.fw_logging), false);
151  settingsMap.put(INDEX_FW_LOG, fw_logging_enabled ? "Stop FW logging" : "Start FW logging");
152 
153  settingsMap.put(INDEX_TERMINAL,"Terminal");
154  }
155 
156  settingsMap.put(INDEX_CREATE_FLASH_BACKUP, "Create FW backup");
157 
158  final String[] settings = new String[settingsMap.values().size()];
159  settingsMap.values().toArray(settings);
160  final ArrayAdapter adapter = new ArrayAdapter<>(this, R.layout.files_list_view, settings);
161  listview.setAdapter(adapter);
162 
163  listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
164 
165  @Override
166  public void onItemClick(AdapterView<?> parent, final View view,
167  int position, long id) {
168  Object[] keys = settingsMap.keySet().toArray();
169 
170  switch ((int)keys[position]){
171  case INDEX_DEVICE_INFO: {
172  Intent intent = new Intent(SettingsActivity.this, InfoActivity.class);
173  startActivity(intent);
174  break;
175  }
176  case INDEX_ADVANCE_MODE: device.toggleAdvancedMode(!device.isInAdvancedMode());
177  break;
178  case INDEX_PRESETS: {
179  PresetsDialog cd = new PresetsDialog();
180  cd.setCancelable(true);
181  cd.show(getFragmentManager(), null);
182  break;
183  }
184  case INDEX_UPDATE: {
186  Bundle bundle = new Bundle();
187  bundle.putBoolean(getString(R.string.firmware_update_request), true);
188  fud.setArguments(bundle);
189  fud.show(getFragmentManager(), "fw_update_dialog");
190  break;
191  }
192  case INDEX_UPDATE_UNSIGNED: {
193  Intent intent = new Intent(SettingsActivity.this, FileBrowserActivity.class);
194  intent.putExtra(getString(R.string.browse_folder), getString(R.string.realsense_folder) + File.separator + "firmware");
195  startActivityForResult(intent, OPEN_FILE_REQUEST_CODE);
196  break;
197  }
198  case INDEX_TERMINAL: {
199  Intent intent = new Intent(SettingsActivity.this, TerminalActivity.class);
200  startActivity(intent);
201  break;
202  }
203  case INDEX_FW_LOG: {
204  toggleFwLogging();
205  recreate();
206  break;
207  }
208  case INDEX_CREATE_FLASH_BACKUP: {
209  new FlashBackupTask(device, mContext).execute();
210  break;
211  }
212  default:
213  break;
214  }
215  }
216  });
217  }
218 
219  private class FlashBackupTask extends AsyncTask<Void, Void, Void> {
220 
221  private ProgressDialog mProgressDialog;
222  private Device mDevice;
223  String mBackupFileName = "fwdump.bin";
224  private Context mContext;
225 
226  public FlashBackupTask(Device mDevice, Context context) {
227  this.mDevice = mDevice;
228  this.mContext = context;
229  }
230 
231  @Override
232  protected Void doInBackground(Void... voids) {
233  try(final Updatable upd = mDevice.as(Extension.UPDATABLE)){
234  FileUtilities.saveFileToExternalDir(mContext, mBackupFileName, upd.createFlashBackup());
235  return null;
236  }
237  }
238 
239  @Override
240  protected void onPreExecute() {
241  super.onPreExecute();
242 
243  mProgressDialog = ProgressDialog.show(mContext, "Saving Firmware Backup", "Please wait, this can take a few minutes");
244  mProgressDialog.setCanceledOnTouchOutside(false);
245  mProgressDialog.setCancelable(false);
246  }
247 
248  @Override
249  protected void onPostExecute(Void aVoid) {
250  super.onPostExecute(aVoid);
251 
252  if (mProgressDialog != null) {
253  mProgressDialog.dismiss();
254  }
255 
256  runOnUiThread(new Runnable() {
257  @Override
258  public void run() {
259  new AlertDialog.Builder(mContext)
260  .setTitle("Firmware Backup Success")
261  .setMessage("Saved into: " + FileUtilities.getExternalStorageDir(mContext) + File.separator + mBackupFileName)
262  .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
263  @Override
264  public void onClick(DialogInterface dialog, int which) {
265  dialog.dismiss();
266  }
267  })
268  .show();
269 
270  }
271  });
272  }
273  }
274 
275 
276  private static String getDeviceConfig(String pid, StreamType streamType, int streamIndex){
277  return pid + "_" + streamType.name() + "_" + streamIndex;
278  }
279 
280  public static String getEnabledDeviceConfigString(String pid, StreamType streamType, int streamIndex){
281  return getDeviceConfig(pid, streamType, streamIndex) + "_enabled";
282  }
283 
284  public static String getIndexdDeviceConfigString(String pid, StreamType streamType, int streamIndex){
285  return getDeviceConfig(pid, streamType, streamIndex) + "_index";
286  }
287 
288  public static Map<Integer, List<StreamProfile>> createProfilesMap(Device device){
289  Map<Integer, List<StreamProfile>> rv = new HashMap<>();
290  List<Sensor> sensors = device.querySensors();
291  for (Sensor s : sensors){
292  List<StreamProfile> profiles = s.getStreamProfiles();
293  for (StreamProfile p : profiles){
294  Pair<StreamType, Integer> pair = new Pair<>(p.getType(), p.getIndex());
295  if(!rv.containsKey(pair.hashCode()))
296  rv.put(pair.hashCode(), new ArrayList<StreamProfile>());
297  rv.get(pair.hashCode()).add(p);
298  p.close();
299  }
300  }
301  return rv;
302  }
303 
304  private void loadStreamList(Device device, List<StreamProfileSelector> streamProfiles){
305  if(device == null || streamProfiles.size() == 0)
306  return;
307  if(!device.supportsInfo(CameraInfo.PRODUCT_ID))
308  throw new RuntimeException("try to config unknown device");
309 
310  StreamProfileSelector[] lines = streamProfiles.toArray(new StreamProfileSelector[streamProfiles.size()]);
311  final String pid = device.getInfo(CameraInfo.PRODUCT_ID);
312  final StreamProfileAdapter adapter = new StreamProfileAdapter(this, lines, new StreamProfileAdapter.Listener() {
313  @Override
314  public void onCheckedChanged(StreamProfileSelector holder) {
315  SharedPreferences sharedPref = getSharedPreferences(getString(R.string.app_settings), Context.MODE_PRIVATE);
316  SharedPreferences.Editor editor = sharedPref.edit();
317  StreamProfile p = holder.getProfile();
318  editor.putBoolean(getEnabledDeviceConfigString(pid, p.getType(), p.getIndex()), holder.isEnabled());
319  editor.putInt(getIndexdDeviceConfigString(pid, p.getType(), p.getIndex()), holder.getIndex());
320  editor.commit();
321  }
322  });
323 
324  ListView streamListView = findViewById(R.id.configuration_list_view);
325  streamListView.setAdapter(adapter);
326  adapter.notifyDataSetChanged();
327  }
328 
329  private List<StreamProfileSelector> createSettingList(final Device device){
330  Map<Integer, List<StreamProfile>> profilesMap = createProfilesMap(device);
331 
332  SharedPreferences sharedPref = getSharedPreferences(getString(R.string.app_settings), Context.MODE_PRIVATE);
333  if(!device.supportsInfo(CameraInfo.PRODUCT_ID))
334  throw new RuntimeException("try to config unknown device");
335  String pid = device.getInfo(CameraInfo.PRODUCT_ID);
336  List<StreamProfileSelector> lines = new ArrayList<>();
337  for(Map.Entry e : profilesMap.entrySet()){
338  List<StreamProfile> list = (List<StreamProfile>) e.getValue();
339  StreamProfile p = list.get(0);
340  boolean enabled = sharedPref.getBoolean(getEnabledDeviceConfigString(pid, p.getType(), p.getIndex()), false);
341  int index = sharedPref.getInt(getIndexdDeviceConfigString(pid, p.getType(), p.getIndex()), 0);
342  lines.add(new StreamProfileSelector(enabled, index, list));
343  }
344 
345  Collections.sort(lines);
346 
347  return lines;
348  }
349 
350  private void RemoveUnsupportedProfiles(List<StreamProfileSelector> streamProfiles){
351  StreamProfileSelector confidenceProfile = null;
352  for (StreamProfileSelector streamProfile : streamProfiles){
353  if (streamProfile.getProfile().getType() == StreamType.CONFIDENCE){
354  confidenceProfile = streamProfile;
355  break;
356  }
357  }
358 
359  // Confidence stream format is RAW8, and it is not supported for display.
360  // Its removal is necessary until format RAW8 display is enabled.
361  if (confidenceProfile != null)
362  streamProfiles.remove(confidenceProfile);
363  }
364 
365  void toggleFwLogging(){
366  SharedPreferences sharedPref = getSharedPreferences(getString(R.string.app_settings), Context.MODE_PRIVATE);
367  boolean fw_logging_enabled = sharedPref.getBoolean(getString(R.string.fw_logging), false);
368  String fw_logging_file_path = sharedPref.getString(getString(R.string.fw_logging_file_path), "");
369  if(fw_logging_file_path.equals("")){
370  Intent intent = new Intent(SettingsActivity.this, FileBrowserActivity.class);
371  intent.putExtra(getString(R.string.browse_folder), getString(R.string.realsense_folder) + File.separator + "hw");
372  startActivityForResult(intent, OPEN_FW_FILE_REQUEST_CODE);
373  return;
374  }
375 
376  SharedPreferences.Editor editor = sharedPref.edit();
377  editor.putBoolean(getString(R.string.fw_logging), !fw_logging_enabled);
378  editor.commit();
379  }
380 
381  @Override
382  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
383  super.onActivityResult(requestCode, resultCode, data);
384  if (resultCode != RESULT_OK || data == null)
385  return;
386  String filePath = data.getStringExtra(getString(R.string.intent_extra_file_path));
387  switch (requestCode){
388  case OPEN_FILE_REQUEST_CODE:{
390  Bundle bundle = new Bundle();
391  bundle.putString(getString(R.string.firmware_update_file_path), filePath);
392  fud.setArguments(bundle);
393  fud.setCancelable(false);
394  fud.show(getFragmentManager(), null);
395  break;
396  }
397  case OPEN_FW_FILE_REQUEST_CODE: {
398  SharedPreferences sharedPref = getSharedPreferences(getString(R.string.app_settings), Context.MODE_PRIVATE);
399  SharedPreferences.Editor editor = sharedPref.edit();
400  editor.putString(getString(R.string.fw_logging_file_path), filePath);
401  editor.commit();
402  toggleFwLogging();
403  break;
404  }
405  }
406  }
407 }
String getInfo(CameraInfo info)
Definition: Device.java:25
::rosgraph_msgs::Log_< std::allocator< void > > Log
Definition: Log.h:88
List< StreamProfileSelector > createSettingList(final Device device)
uvc_xu_option< int > super
Definition: l500-options.h:32
GLdouble s
void onActivityResult(int requestCode, int resultCode, Intent data)
GLfloat GLfloat p
Definition: glext.h:12687
boolean is(Extension extension)
Definition: Device.java:45
static String getIndexdDeviceConfigString(String pid, StreamType streamType, int streamIndex)
e
Definition: rmse.py:177
static Map< Integer, List< StreamProfile > > createProfilesMap(Device device)
void onCreate(Bundle savedInstanceState)
::std_msgs::String_< std::allocator< void > > String
Definition: String.h:47
static String getDeviceConfig(String pid, StreamType streamType, int streamIndex)
GLuint index
void RemoveUnsupportedProfiles(List< StreamProfileSelector > streamProfiles)
def info(name, value, persistent=False)
Definition: test.py:301
void loadStreamList(Device device, List< StreamProfileSelector > streamProfiles)
static void saveFileToExternalDir(Context context, final String fileName, byte[] data)
devices
Definition: test-fg.py:9
static String getExternalStorageDir(Context context)
def run(include_folder_path, addon_folder_path)
Definition: enums.py:46
def finish()
Definition: test.py:373
GLenum GLenum GLsizei const GLuint GLboolean enabled
void toggleAdvancedMode(boolean enable)
Definition: Device.java:29
static String getEnabledDeviceConfigString(String pid, StreamType streamType, int streamIndex)
static boolean isPathEmpty(String path)
int i
auto device
Definition: pyrs_net.cpp:17
Definition: threads.c:40
Definition: parser.hpp:150
boolean supportsInfo(CameraInfo info)
Definition: Device.java:21


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