FirmwareUpdateProgressDialog.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.Context;
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.LayoutInflater;
13 import android.view.View;
14 import android.widget.ProgressBar;
15 
25 
26 import java.io.File;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.io.InputStream;
30 
31 public class FirmwareUpdateProgressDialog extends DialogFragment {
32  private static final String TAG = "librs fwupdate";
33 
36  @Override
37  public Dialog onCreateDialog(Bundle savedInstanceState) {
38  final Activity activity = getActivity();
39 
40  Bundle bundle = getArguments();
41  mFirmwareFilePath = bundle == null ? "" : bundle.getString(getString(R.string.firmware_update_file_path), "");
42 
43  LayoutInflater inflater = activity.getLayoutInflater();
44  View fragmentView = inflater.inflate(R.layout.firmware_update_progress, null);
45 
46  mProgressBar = fragmentView.findViewById(R.id.firmwareUpdateProgressBar);
47 
49  t.start();
50 
51  AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
52  builder.setView(fragmentView);
53  AlertDialog rv = builder.create();
54  rv.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
55  return rv;
56  }
57 
58  private int getFwImageId(Device device){
61  switch (pl){
62  case D400: return R.raw.fw_d4xx;
63  case SR300: return R.raw.fw_sr3xx;
64  case L500: return R.raw.fw_l5xx;
65  }
66  }
67  throw new RuntimeException("FW update is not supported for the connected device");
68  }
69 
70  private static byte[] readFwFile(InputStream in) throws IOException {
71  int length = in.available();
72  byte[] rv = new byte[length];
73  int len = in.read(rv,0, rv.length);
74  in.close();
75  return rv;
76  }
77 
78  private static InputStream getInputStream(String path) throws IOException {
79  File fwFile = new File(path);
80  return new FileInputStream(fwFile);
81  }
82 
83  private static InputStream getInputStream(Context context, int fwResId) throws IOException {
84  return context.getResources().openRawResource(fwResId);
85  }
86 
87  private Runnable mFirmwareUpdate = new Runnable() {
88  @Override
89  public void run() {
90  boolean done = false;
91  RsContext ctx = new RsContext();
92  boolean notify = false;
93  try(DeviceList dl = ctx.queryDevices()){
94  if(dl.getDeviceCount() == 0)
95  return;
96  try(Device d = dl.createDevice(0)){
97  if(d.is(Extension.UPDATE_DEVICE)) {
99  final byte[] bytes = readFwFile(getInputStream(getActivity(), getFwImageId(d)));
100  updateFirmware(fwud, bytes);
101  done = true;
102  notify = true;
103  } else if(d.is(Extension.UPDATABLE) && !mFirmwareFilePath.equals("")){
104  final byte[] bytes = readFwFile(getInputStream(mFirmwareFilePath));
105  Updatable fwud = d.as(Extension.UPDATABLE);
106  updateFirmware(fwud, bytes);//TODO: make it work
107  }
108  }
109  }catch (Exception e) {
110  Log.e(TAG, "firmware update failed, error: " + e.getMessage());
111  }finally {
112  if(notify)
113  ((DetachedActivity)getActivity()).onFwUpdateStatus(done);
114  dismissAllowingStateLoss();
115  }
116  }
117  };
118 
119  private void updateFirmware(UpdateDevice device, final byte[] bytes) {
120  device.update(bytes, new ProgressListener() {
121  @Override
122  public void onProgress(final float progress) {
123  getActivity().runOnUiThread(new Runnable() {
124  @Override
125  public void run() {
126  int perc = (int) (progress * 100);
127  mProgressBar.setProgress(perc);
128  }
129  });
130  }
131  });
132  Log.i(TAG, "Firmware update process finished successfully");
133  }
134 
135  private void updateFirmware(Updatable device, final byte[] bytes) {
136  device.updateUnsigned(bytes, new ProgressListener() {
137  @Override
138  public void onProgress(final float progress) {
139  getActivity().runOnUiThread(new Runnable() {
140  @Override
141  public void run() {
142  int perc = (int) (progress * 100);
143  mProgressBar.setProgress(perc);
144  }
145  });
146  }
147  });
148  Log.i(TAG, "Firmware update process finished successfully");
149  }
150 }
String getInfo(CameraInfo info)
Definition: Device.java:25
::rosgraph_msgs::Log_< std::allocator< void > > Log
Definition: Log.h:88
IMGUI_API void ProgressBar(float fraction, const ImVec2 &size_arg=ImVec2(-1, 0), const char *overlay=NULL)
Definition: imgui.cpp:7234
void updateFirmware(UpdateDevice device, final byte[] bytes)
GLsizei const GLchar *const * path
Definition: glext.h:4276
def progress(args)
Definition: log.py:43
d
Definition: rmse.py:171
e
Definition: rmse.py:177
::std_msgs::String_< std::allocator< void > > String
Definition: String.h:47
synchronized void updateUnsigned(byte[] image, ProgressListener listener)
Definition: Updatable.java:10
GLdouble t
GLenum GLsizei len
Definition: glext.h:3285
def run(include_folder_path, addon_folder_path)
Definition: enums.py:46
static int done
static InputStream getInputStream(Context context, int fwResId)
unsigned char byte
Definition: src/types.h:52
GLuint in
Definition: glext.h:8859
GLenum GLuint GLenum GLsizei length
auto device
Definition: pyrs_net.cpp:17
Definition: threads.c:40
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:14