Add PoC code for DeviceListManagement

This commit is contained in:
Denis-Cosmin Nutiu 2020-12-06 19:15:43 +02:00
parent 9b10461725
commit 058d91d0a9
8 changed files with 234 additions and 9 deletions

View file

@ -20,5 +20,9 @@ public class DeviceListPreference extends ListPreference {
public DeviceListPreference(Context context) {
super(context);
}
// TODO: Create custom dialog, override onClick.
@Override
protected void onClick() {
new DeviceManagementDialog(getContext()).show(this);
}
}

View file

@ -0,0 +1,111 @@
package dev.nuculabs.nucuhub.ui.settings;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import androidx.preference.ListPreference;
import dev.nuculabs.nucuhub.R;
import java.util.HashSet;
public class DeviceManagementDialog extends Dialog {
private SharedPreferences sharedPreferences;
private ListPreference preference = null;
private ArrayAdapter<String> adapter;
public DeviceManagementDialog(@NonNull Context context) {
super(context, R.style.ThemeOverlay_AppCompat_ActionBar);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_device_management_dialog);
getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
setupToolbar();
sharedPreferences = getContext().getSharedPreferences("settings", Activity.MODE_PRIVATE);
final ListView deviceListView = findViewById(R.id.settings_device_dialog_list);
adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1);
deviceListView.setAdapter(adapter);
// dummy code to add simple items.
final CharSequence [] preferenceCharSeq = preference.getEntries();
for (CharSequence item : preferenceCharSeq) {
adapter.add(item.toString());
}
Button testConnectionButton = findViewById(R.id.settings_device_test_connection_button);
testConnectionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adapter.add("new object.");
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// back button click
if (item.getItemId() == android.R.id.home) {
saveChangesAndDismiss();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
saveChangesAndDismiss();
super.onBackPressed();
}
public void show(@NonNull ListPreference preference) {
this.preference = preference;
super.show();
}
public void saveChangesAndDismiss() {
updatePreferenceEntryValues();
dismiss();
}
private void updatePreferenceEntryValues() {
// dummy code to update the entries.
int itemsLength = adapter.getCount();
CharSequence[] entries = new CharSequence[itemsLength];
HashSet<String> entriesSet = new HashSet<>();
for (int i = 0; i < itemsLength; i++) {
entries[i] = adapter.getItem(i);
entriesSet.add(entries[i].toString());
}
preference.setEntries(entries);
SharedPreferences.Editor spe = sharedPreferences.edit();
spe.putStringSet("current_device_list", entriesSet);
}
private void setupToolbar() {
Toolbar toolbar = findViewById(R.id.settings_device_dialog_toolbar);
toolbar.setTitle(R.string.device_management_title);
toolbar.setNavigationIcon(R.drawable.ic_baseline_arrow_gray_24);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveChangesAndDismiss();
}
});
}
}

View file

@ -1,21 +1,41 @@
package dev.nuculabs.nucuhub.ui.settings;
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.preference.ListPreference;
import androidx.preference.PreferenceFragmentCompat;
import dev.nuculabs.nucuhub.R;
import java.util.Set;
public class SettingsFragment extends PreferenceFragmentCompat {
private SharedPreferences sharedPreferences;
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
// To get preferences use this in main activity:
// SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
setPreferencesFromResource(R.xml.root_preferences, rootKey);
ListPreference devicesList = findPreference("device_list");
updateDeviceListItems();
}
private void updateDeviceListItems() {
// dummy code to update device list entries
SharedPreferences sp = getContext().getSharedPreferences("settings", Activity.MODE_PRIVATE);
SharedPreferences.Editor spe = sp.edit();
DeviceListPreference devicesList = findPreference("device_list");
Set<String> savedEntries = sp.getStringSet("current_device_list", null);
if (savedEntries != null) {
CharSequence[] items = new CharSequence[savedEntries.size()];
int index = 0;
for (String item : savedEntries) {
items[index] = item;
index += 1;
}
devicesList.setEntries(items);
}
// TODO: Override dialog in order to add a search devices button.
//
// set default value
}
}

View file

@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#C5C5C5"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M20,11H7.83l5.59,-5.59L12,4l-8,8 8,8 1.41,-1.41L7.83,13H20v-2z"/>
</vector>

View file

@ -0,0 +1,78 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="wrap_content" android:orientation="vertical"
>
<com.google.android.material.appbar.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/settings_device_dialog_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
tools:visibility="visible">
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<TextView
android:text="@string/settings_connect_new_device"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Large" android:paddingTop="10dp"
android:paddingLeft="10dp" android:paddingRight="10dp"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp" android:paddingRight="10dp">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="match_parent" android:id="@+id/settings_device_input_device">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/settings_device_add_new_device_input_hint"/>
</com.google.android.material.textfield.TextInputLayout>
<TextView
android:text="$$Connection status$$"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/text_connection_status" android:gravity="center"/>
<Button
android:text="@string/settings_device_test_connection_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/settings_device_test_connection_button"
tools:text="Test Connection"/>
<Button
android:text="@string/settings_device_add_device_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/settings_device_add_device_button"
android:enabled="false"/>
</LinearLayout>
<View android:layout_height="1dp"
android:layout_width="fill_parent"
android:background="#800B001B"
/>
<TextView
android:text="@string/settings_device_saved_devices"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Large" android:paddingTop="10dp"
android:paddingLeft="10dp" android:paddingRight="10dp"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:paddingLeft="20dp" android:paddingRight="20dp"
android:paddingTop="10dp" android:paddingBottom="10dp">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent" android:id="@+id/settings_device_dialog_list"/>
</LinearLayout>
</LinearLayout>

View file

@ -33,6 +33,12 @@
<!-- Device Preferences -->
<string name="device_selection_title">Your current device</string>
<string name="device_management_title">Device Management</string>
<string name="settings_connect_new_device">Connect new device</string>
<string name="settings_device_test_connection_btn">Test Connection</string>
<string name="settings_device_add_new_device_input_hint">http://hostname:port</string>
<string name="settings_device_add_device_btn">Add Device</string>
<string name="settings_device_saved_devices">Saved Devices</string>
<!-- Messages Preferences -->

View file

@ -41,8 +41,9 @@
<dev.nuculabs.nucuhub.ui.settings.DeviceListPreference
app:key="device_list"
app:title="@string/device_selection_title"
app:entries="@array/device_list_array"
app:entryValues="@array/device_list_array"
app:entries="@array/reply_entries"
app:entryValues="@array/reply_values"
app:defaultValue="reply"
app:useSimpleSummaryProvider="true"/>
</PreferenceCategory>