Skip to content

Commit

Permalink
WIP - #57
Browse files Browse the repository at this point in the history
  • Loading branch information
Catfriend1 committed Sep 21, 2018
1 parent 0203aeb commit d40119e
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.provider.DocumentFile;
Expand Down Expand Up @@ -41,9 +44,13 @@

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import static android.support.v4.view.MarginLayoutParamsCompat.setMarginEnd;
Expand Down Expand Up @@ -97,6 +104,7 @@ public class FolderActivity extends SyncthingActivity
private TextView mFolderTypeDescriptionView;
private SwitchCompat mFolderFileWatcher;
private SwitchCompat mFolderPaused;
private ViewGroup mWifiSsidContainer;
private ViewGroup mDevicesContainer;
private TextView mPullOrderTypeView;
private TextView mPullOrderDescriptionView;
Expand Down Expand Up @@ -171,6 +179,7 @@ public void onCreate(Bundle savedInstanceState) {
mPullOrderDescriptionView = findViewById(R.id.pullOrderDescription);
mVersioningDescriptionView = findViewById(R.id.versioningDescription);
mVersioningTypeView = findViewById(R.id.versioningType);
mWifiSsidContainer = findViewById(R.id.wifiSsidContainer);
mDevicesContainer = findViewById(R.id.devicesContainer);
mEditIgnoreListTitle = findViewById(R.id.edit_ignore_list_title);
mEditIgnoreListContent = findViewById(R.id.edit_ignore_list_content);
Expand Down Expand Up @@ -416,8 +425,25 @@ private void updateViewsAndSetListeners() {
updateVersioningDescription();
mFolderFileWatcher.setChecked(mFolder.fsWatcherEnabled);
mFolderPaused.setChecked(mFolder.paused);
List<Device> devicesList = getApi().getDevices(false);

// Populate wifiSsidAvailList.
List<String> wifiSsidAvailList = new ArrayList<String>();
WifiConfiguration[] wifiNetworks = loadConfiguredNetworksSorted();
if (wifiNetworks != null) {
// Display without surrounding quotes.
wifiSsidAvailList = extractSsid(wifiNetworks, true);
}
mWifiSsidContainer.removeAllViews();
if (wifiSsidAvailList.isEmpty()) {
addEmptyWifiSsidListView();
} else {
for (String wifiSsid : wifiSsidAvailList) {
addWifiSsidViewAndSetListener(wifiSsid, getLayoutInflater());
}
}

// Populate devicesList.
List<Device> devicesList = getApi().getDevices(false);
mDevicesContainer.removeAllViews();
if (devicesList.isEmpty()) {
addEmptyDeviceListView();
Expand Down Expand Up @@ -628,6 +654,29 @@ private void initFolder() {
mFolder.versioning = new Folder.Versioning();
}

private void addEmptyWifiSsidListView() {
int height = (int) TypedValue.applyDimension(COMPLEX_UNIT_DIP, 48, getResources().getDisplayMetrics());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(WRAP_CONTENT, height);
int dividerInset = getResources().getDimensionPixelOffset(R.dimen.material_divider_inset);
int contentInset = getResources().getDimensionPixelOffset(R.dimen.abc_action_bar_content_inset_material);
setMarginStart(params, dividerInset);
setMarginEnd(params, contentInset);
TextView emptyView = new TextView(mWifiSsidContainer.getContext());
emptyView.setGravity(CENTER_VERTICAL);
emptyView.setText(R.string.devices_list_empty);
mWifiSsidContainer.addView(emptyView, params);
}

private void addWifiSsidViewAndSetListener(String wifiSsid, LayoutInflater inflater) {
inflater.inflate(R.layout.item_wifi_ssid_form, mWifiSsidContainer);
SwitchCompat wifiSsidView = (SwitchCompat) mWifiSsidContainer.getChildAt(mWifiSsidContainer.getChildCount()-1);
wifiSsidView.setOnCheckedChangeListener(null);
// ToDo wifiSsidView.setChecked(mFolder.getDevice(device.deviceID) != null);
wifiSsidView.setText(wifiSsid);
wifiSsidView.setTag(wifiSsid);
wifiSsidView.setOnCheckedChangeListener(mCheckedListener);
}

private void addEmptyDeviceListView() {
int height = (int) TypedValue.applyDimension(COMPLEX_UNIT_DIP, 48, getResources().getDisplayMetrics());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(WRAP_CONTENT, height);
Expand Down Expand Up @@ -825,4 +874,65 @@ private void setVersioningDescription(String type, String description) {
mVersioningTypeView.setText(type);
mVersioningDescriptionView.setText(description);
}

/**
* Removes any network that is no longer saved on the device. Otherwise it will never be
* removed from the allowed set by MultiSelectListPreference.
*/
private void filterRemovedNetworks(Set<String> selected, CharSequence[] all) {
HashSet<CharSequence> availableNetworks = new HashSet<>(Arrays.asList(all));
selected.retainAll(availableNetworks);
}

/**
* Converts WiFi configuration to a list representation, using the SSID.
*
* It can also remove surrounding quotes which indicate that the SSID is an UTF-8
* string and not a Hex-String, if the strings are intended to be displayed to the
* user, who will not expect the quotes.
*
* @param configs the objects to convert
* @param stripQuotes if to remove surrounding quotes
* @return the formatted SSID of the wifi configurations
*/
private List<String> extractSsid(WifiConfiguration[] configs, boolean stripQuotes) {
List<String> result = new ArrayList<String>();
for (int i = 0; i < configs.length; i++) {
// See #620: there may be null-SSIDs
String ssid = configs[i].SSID != null ? configs[i].SSID : "";
// WiFi SSIDs can either be UTF-8 (encapsulated in '"') or hex-strings
if (stripQuotes) {
result.add(ssid.replaceFirst("^\"", "").replaceFirst("\"$", ""));
} else {
result.add(ssid);
}
}
return result;
}

/**
* Load the configured WiFi networks, sort them by SSID.
*
* @return a sorted array of WifiConfiguration, or null, if data cannot be retrieved
*/
private WifiConfiguration[] loadConfiguredNetworksSorted() {
WifiManager wifiManager = (WifiManager)
getApplicationContext().getSystemService(Context.WIFI_SERVICE);
if (wifiManager != null) {
List<WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks();
// if WiFi is turned off, getConfiguredNetworks returns null on many devices
if (configuredNetworks != null) {
WifiConfiguration[] result = configuredNetworks.toArray(new WifiConfiguration[configuredNetworks.size()]);
Arrays.sort(result, (lhs, rhs) -> {
// See #620: There may be null-SSIDs
String l = lhs.SSID != null ? lhs.SSID : "";
String r = rhs.SSID != null ? rhs.SSID : "";
return l.compareToIgnoreCase(r);
});
return result;
}
}
// WiFi is turned off or device doesn't have WiFi
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,11 @@ private CharSequence[] extractSsid(WifiConfiguration[] configs, boolean stripQuo
// See #620: there may be null-SSIDs
String ssid = configs[i].SSID != null ? configs[i].SSID : "";
// WiFi SSIDs can either be UTF-8 (encapsulated in '"') or hex-strings
if (stripQuotes)
if (stripQuotes) {
result[i] = ssid.replaceFirst("^\"", "").replaceFirst("\"$", "");
else
} else {
result[i] = ssid;
}
}
return result;
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 23 additions & 9 deletions app/src/main/res/layout/fragment_folder.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,20 +54,34 @@
android:focusable="true"
android:hint="@string/directory"/>

<TextView
style="@style/Widget.Syncthing.TextView.Label.Details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_wifi_black_24dp"
android:drawableStart="@drawable/ic_wifi_black_24dp"
android:text="@string/wifi_networks" />

<LinearLayout
android:id="@+id/devicesContainer"
android:id="@+id/wifiSsidContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>

<TextView
style="@style/Widget.Syncthing.TextView.Label.Details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_device_hub_black_24dp_active"
android:drawableStart="@drawable/ic_device_hub_black_24dp_active"
android:text="@string/devices" />
<TextView
style="@style/Widget.Syncthing.TextView.Label.Details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/ic_device_hub_black_24dp_active"
android:drawableStart="@drawable/ic_device_hub_black_24dp_active"
android:text="@string/devices" />

<LinearLayout
android:id="@+id/devicesContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>

<LinearLayout
Expand Down Expand Up @@ -235,7 +249,7 @@
<EditText
android:id="@+id/edit_ignore_list_content"
style="@style/Widget.Syncthing.TextView.Label.Details"
android:inputType="textMultiLine"
android:inputType="textMultiLine|textNoSuggestions"
android:gravity="top|start"
android:hint="@string/ignore_patterns"
android:layout_width="match_parent"
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/res/layout/item_wifi_ssid_form.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.SwitchCompat xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="@style/Widget.Syncthing.TextView.Label.Details.WifiSsidList"
android:id="@+id/wifi_ssid_toggle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/material_divider_inset"
android:layout_marginStart="@dimen/material_divider_inset"
android:textAppearance="@style/TextAppearance.AppCompat.Caption"
tools:ignore="RtlHardcoded,RtlSymmetry" />
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ Please report any problems you encounter via Github.</string>
<!-- Setting title -->
<string name="folder_pause">Pause Folder</string>

<!-- Setting title -->
<string name="wifi_networks">WiFi networks</string>

<!-- Setting title -->
<string name="devices">Devices</string>

Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
<item name="android:paddingBottom">8dp</item>
</style>

<style name="Widget.Syncthing.TextView.Label.Details.WifiSsidList">
<item name="android:paddingLeft">4dp</item>
<item name="android:minHeight">48dp</item>
</style>

<style name="Widget.Syncthing.TextView.Label.Details.DeviceList">
<item name="android:paddingLeft">4dp</item>
<item name="android:minHeight">48dp</item>
Expand Down

0 comments on commit d40119e

Please sign in to comment.