Skip to content

Commit

Permalink
Merge pull request IntelRealSense#8489 from remibettan/android-preset…
Browse files Browse the repository at this point in the history
…s-and-controls-ui

Android - redesigning presets activity to dialog
  • Loading branch information
ev-mp authored Mar 24, 2021
2 parents 02e51de + b6a92c6 commit 011d869
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 110 deletions.
3 changes: 0 additions & 3 deletions wrappers/android/tools/camera/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@
android:name=".FileBrowserActivity"
android:label="@string/title_activity_file_browser"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".PresetsActivity"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".InfoActivity"
android:theme="@style/AppTheme.NoActionBar" />
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package com.intel.realsense.camera;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.res.Resources;
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.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.intel.realsense.librealsense.Device;
import com.intel.realsense.librealsense.DeviceList;
import com.intel.realsense.librealsense.RsContext;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class PresetsDialog extends DialogFragment {
private static final String TAG = "librs presets";
// mSelectedItem initialized with high number so that no preset would be checked when opening presets at the first time
private static int mSelectedItem = 500;
private String[] mPresets;
private Resources mResources;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Activity activity = getActivity();
LayoutInflater inflater = activity.getLayoutInflater();
View fragmentView = inflater.inflate(R.layout.presets_dialog, null);

TextView message = fragmentView.findViewById(R.id.presets_list_title);
mResources = getResources();

try {
mPresets = mResources.getAssets().list("presets");
} catch (IOException e) {
message.setText("No presets found");
return null;
}

if(mPresets.length == 0) {
message.setText("No presets found");
return null;
}
message.setText("Select a preset:");

// setting RadioGroup
final RadioGroup presets_group = fragmentView.findViewById(R.id.presets_list_items);
// adding items to the list
for (int i = 0; i < mPresets.length; ++i) {
RadioButton button = new RadioButton(activity);
button.setId(i);
button.setText(mPresets[i].substring(0, mPresets[i].lastIndexOf('.'))); // text is w/o the file termination
button.setTextSize(16);
button.setTextColor(getResources().getColor(R.color.white));
button.setChecked(i == mSelectedItem);
presets_group.addView(button);
}

final String[] finalPresets = mPresets;
presets_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
mSelectedItem = checkedId;
}
});
// OK BUTTON
View okButton = fragmentView.findViewById(R.id.presets_ok_button);
okButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
RsContext ctx = new RsContext();
try (DeviceList devices = ctx.queryDevices()) {
if (devices.getDeviceCount() == 0) {
Log.e(TAG, "failed to set preset, no device found");
dismiss();
}
try (Device device = devices.createDevice(0)) {
if (device == null || !device.isInAdvancedMode()) {
Log.e(TAG, "failed to set preset, device not in advanced mode");
dismiss();
}
final String item = finalPresets[mSelectedItem];
try {
InputStream is = mResources.getAssets().open("presets/" + item);
byte[] buffer = new byte[is.available()];
is.read(buffer);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(buffer);
baos.close();
is.close();
device.loadPresetFromJson(buffer);
} catch (IOException e) {
Log.e(TAG, "failed to set preset, failed to open preset file, error: " + e.getMessage());
} catch (Exception e) {
Log.e(TAG, "failed to set preset, error: " + e.getMessage());
} finally {
dismiss();
}
}
}
Log.i(TAG, "preset set to: " + finalPresets[mSelectedItem]);
dismissAllowingStateLoss();
}
});

// Cancel BUTTON
View cancelButton = fragmentView.findViewById(R.id.presets_cancel_button);
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismissAllowingStateLoss();
}
});

// BUILD DIALOG
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(fragmentView);
AlertDialog rv = builder.create();
rv.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
rv.getWindow().setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP);
return rv;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,9 @@ public void onItemClick(AdapterView<?> parent, final View view,
case INDEX_ADVANCE_MODE: device.toggleAdvancedMode(!device.isInAdvancedMode());
break;
case INDEX_PRESETS: {
Intent intent = new Intent(SettingsActivity.this, PresetsActivity.class);
startActivity(intent);
PresetsDialog cd = new PresetsDialog();
cd.setCancelable(true);
cd.show(getFragmentManager(), null);
break;
}
case INDEX_UPDATE: {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
android:textSize="14dp"
android:textColor="#ffffff"
android:backgroundTint="#000000"
android:text="controls"/>
android:text="Controls"/>

<Space
android:layout_width="0dp"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp"
android:background="@drawable/fragment_template">

<TextView
android:id="@+id/presets_list_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:text="Controls"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginBottom="5dp"/>

<RadioGroup
android:id="@+id/presets_list_items"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/presets_list_title"
android:layout_marginBottom="20dp"
tools:layout_height="150dip"
android:textSize="12sp">
<!-- items added within code -->
</RadioGroup>


<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/presets_list_items"
android:orientation="horizontal">
//OK button
<Button
android:id="@+id/presets_ok_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="OK"
android:textAllCaps="false"
android:textStyle="bold"/>
//Cancel button
<Button
android:id="@+id/presets_cancel_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="Cancel"
android:layout_alignParentEnd="true"
android:textAllCaps="false"
android:textStyle="bold"/>
</RelativeLayout>
</RelativeLayout>

0 comments on commit 011d869

Please sign in to comment.