DetachedActivity.java
Go to the documentation of this file.
1 package com.intel.realsense.camera;
2 
3 import android.Manifest;
4 import android.content.Context;
5 import android.content.Intent;
6 import android.content.SharedPreferences;
7 import android.content.pm.PackageManager;
8 import android.os.Bundle;
9 import androidx.core.app.ActivityCompat;
10 import androidx.core.content.ContextCompat;
11 import androidx.appcompat.app.AppCompatActivity;
12 import android.util.Log;
13 import android.view.View;
14 import android.widget.Button;
15 import android.widget.TextView;
16 import android.widget.Toast;
17 
26 
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.Map;
30 
31 public class DetachedActivity extends AppCompatActivity {
32  private static final String TAG = "librs camera detached";
33  private static final int PLAYBACK_REQUEST_CODE = 1;
34  private static final String MINIMAL_D400_FW_VERSION = "5.10.0.0";
35 
37 
38  private Context mAppContext;
39  private RsContext mRsContext = new RsContext();
40 
41  private Map<ProductLine,String> mMinimalFirmwares = new HashMap<>();
42  private boolean mUpdating = false;
43  private boolean mDetached = false;
44 
45  @Override
46  protected void onCreate(Bundle savedInstanceState) {
47  super.onCreate(savedInstanceState);
48  setContentView(R.layout.activity_detached);
49 
50  mAppContext = getApplicationContext();
51 
53 
54  init();
55  }
56 
57  private void requestPermissionsIfNeeded() {
58  ArrayList<String> permissions = new ArrayList<>();
60  permissions.add(Manifest.permission.CAMERA);
61  }
62 
63  if (!isWritePermissionGranted()) {
64  permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
65  }
66  if (!permissions.isEmpty())
67  ActivityCompat.requestPermissions(this, permissions.toArray(new String[permissions.size()]), PermissionsUtils.PERMISSIONS_REQUEST_ALL);
68  }
69 
70  @Override
71  protected void onResume() {
72  super.onResume();
73 
75  RsContext.init(getApplicationContext());
78  }
79  }
80 
81  private boolean isCameraPermissionGranted() {
82  if (android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.O)
83  return true;
84  return ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED;
85  }
86 
87  private boolean isWritePermissionGranted() {
88  return ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
89  }
90 
91  private synchronized void init()
92  {
93  mPlaybackButton = findViewById(R.id.playbackButton);
94  mPlaybackButton.setOnClickListener(new View.OnClickListener() {
95  @Override
96  public void onClick(View view) {
97  mDetached = false;
98  finish();
99  Intent intent = new Intent(DetachedActivity.this, PlaybackActivity.class);
100  startActivityForResult(intent, PLAYBACK_REQUEST_CODE);
101  }
102  });
103 
104  runOnUiThread(new Runnable() {
105  @Override
106  public void run() {
107  String appVersion = BuildConfig.VERSION_NAME;
108  String lrsVersion = RsContext.getVersion();
109  TextView versions = findViewById(R.id.versionsText);
110  versions.setText("librealsense version: " + lrsVersion + "\ncamera app version: " + appVersion);
111  }
112  });
113 
114  mMinimalFirmwares.put(ProductLine.D400, MINIMAL_D400_FW_VERSION);
115  }
116 
117  private synchronized void validatedDevice(){
118  if(mUpdating)
119  return;
120  try(DeviceList dl = mRsContext.queryDevices()){
121  if(dl.getDeviceCount() == 0) {
122  init();
123 
124  // kill preview activity if device disconnected
125  if (mDetached) {
126  mDetached = false;
127 
128  Intent intent = new Intent(this, PreviewActivity.class);
129  intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
130  intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
131  intent.putExtra("keepalive", false);
132  startActivity(intent);
133  }
134 
135  return;
136  }
137 
138  try(Device d = dl.createDevice(0)){
139  if(d == null)
140  return;
141  if(d.is(Extension.UPDATE_DEVICE)){
143  fupd.setCancelable(false);
144  fupd.show(getFragmentManager(), null);
145  mUpdating = true;
146  }
147  else {
148  if (!validateFwVersion(d))
149  return;
150 
151  mDetached = false;
152 
153 
154  // launch preview activity and keep it alive
155  // the activity is single top instance, can be killed later the same instance
156  // to prevent issues with multiple instances
157  Intent intent = new Intent(this, PreviewActivity.class);
158  intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
159  intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
160  intent.putExtra("keepalive", true);
161  startActivity(intent);
162  }
163  }
164  } catch (Exception e){
165  Log.e(TAG, "error while validating device, error: " + e.getMessage());
166  }
167  }
168 
169  private boolean validateFwVersion(Device device){
170  final String currFw = device.getInfo(CameraInfo.FIRMWARE_VERSION).split("\n")[0];
171  final ProductLine pl = ProductLine.valueOf(device.getInfo(CameraInfo.PRODUCT_LINE));
172  if(mMinimalFirmwares.containsKey(pl)){
173  final String minimalFw = mMinimalFirmwares.get(pl);
174  if(!compareFwVersion(currFw, minimalFw)){
176  Bundle bundle = new Bundle();
177  bundle.putBoolean(getString(R.string.firmware_update_required), true);
178  fud.setArguments(bundle);
179  fud.show(getFragmentManager(), null);
180  return false;
181  }
182  }
183 
184  SharedPreferences sharedPref = getSharedPreferences(getString(R.string.app_settings), Context.MODE_PRIVATE);
185  boolean showUpdateMessage = sharedPref.getBoolean(getString(R.string.show_update_firmware), true);
186  if (!showUpdateMessage || !device.supportsInfo(CameraInfo.RECOMMENDED_FIRMWARE_VERSION))
187  return true;
188 
189  final String recommendedFw = device.getInfo(CameraInfo.RECOMMENDED_FIRMWARE_VERSION);
190  if(!compareFwVersion(currFw, recommendedFw)){
192  fud.show(getFragmentManager(), null);
193  return false;
194  }
195  return true;
196  }
197 
198  private boolean compareFwVersion(String currFw, String otherFw){
199  String[] sFw = currFw.split("\\.");
200  String[] sRecFw = otherFw.split("\\.");
201  for (int i = 0; i < sRecFw.length; i++) {
202  if (Integer.parseInt(sFw[i]) > Integer.parseInt(sRecFw[i]))
203  break;
204  if (Integer.parseInt(sFw[i]) < Integer.parseInt(sRecFw[i])) {
205  return false;
206  }
207  }
208  return true;
209  }
210 
211  public void onFwUpdateStatus(final boolean status){
212  mUpdating = false;
213 
214  runOnUiThread(new Runnable() {
215  @Override
216  public void run() {
217  String msg = status ? "firmware update done" : "firmware update failed";
218  Toast.makeText(DetachedActivity.this, msg, Toast.LENGTH_LONG).show();
219  }
220  });
221  }
222 
224  @Override
225  public void onDeviceAttach() {
226  validatedDevice();
227  }
228 
229  @Override
230  public void onDeviceDetach() {
231  mDetached = true;
232  validatedDevice();
233  }
234  };
235 }
String getInfo(CameraInfo info)
Definition: Device.java:25
::rosgraph_msgs::Log_< std::allocator< void > > Log
Definition: Log.h:88
boolean compareFwVersion(String currFw, String otherFw)
uvc_xu_option< int > super
Definition: l500-options.h:32
synchronized void setDevicesChangedCallback(DeviceListener listener)
Definition: RsContext.java:34
d
Definition: rmse.py:171
e
Definition: rmse.py:177
::std_msgs::String_< std::allocator< void > > String
Definition: String.h:47
static void init(Context context)
Definition: RsContext.java:9
status
Defines return codes that SDK interfaces use. Negative values indicate errors, a zero value indicates...
void onCreate(Bundle savedInstanceState)
def run(include_folder_path, addon_folder_path)
Definition: enums.py:46
def finish()
Definition: test.py:373
IMGUI_API bool Button(const char *label, const ImVec2 &size=ImVec2(0, 0))
Definition: imgui.cpp:5573
int i
auto device
Definition: pyrs_net.cpp:17
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:12